From b4950d45709eb8bd325aee54d82e48b0bc03b47b Mon Sep 17 00:00:00 2001 From: peter-x Date: Thu, 7 Jan 2016 16:07:39 +0100 Subject: [PATCH] Fix for finding some files in ZIM. The namespace has to be taken into account when titles are compared. --- tests/tests.js | 19 +++++++++---------- www/js/app.js | 9 ++++----- www/js/lib/zimArchive.js | 15 ++++++++++++--- www/js/lib/zimDirEntry.js | 2 +- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/tests/tests.js b/tests/tests.js index 9b8b1b35..8685e938 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -19,8 +19,8 @@ * You should have received a copy of the GNU General Public License * along with Evopedia (file LICENSE-GPLv3.txt). If not, see */ -define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geometry'], - function($, evopediaTitle, evopediaArchive, zimArchive, zimDirEntry, util, geometry) { +define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geometry', 'utf8'], + function($, evopediaTitle, evopediaArchive, zimArchive, zimDirEntry, util, geometry, utf8) { var localEvopediaArchive; var localZimArchive; @@ -172,7 +172,7 @@ define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geom equal(title._name, "Diego_Velázquez", "Name of the title is correct"); start(); }; - localEvopediaArchive.getTitleByName("Diego_Velázquez").then(callbackFunction); + localEvopediaArchive.getTitleByName("A/Diego_Velázquez").then(callbackFunction); }); asyncTest("check getTitleByName with quote : Hundred Years' War", function() { expect(2); @@ -181,7 +181,7 @@ define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geom equal(title._name, "Hundred_Years'_War", "Name of the title is correct"); start(); }; - localEvopediaArchive.getTitleByName("Hundred_Years'_War").then(callbackFunction); + localEvopediaArchive.getTitleByName("A/Hundred_Years'_War").then(callbackFunction); }); test("check parseTitleFromId", function() { @@ -219,7 +219,7 @@ define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geom equal(title._name, "AIDS", "Name of the title is correct"); localEvopediaArchive.readArticle(title, callbackArticleRead); }; - localEvopediaArchive.getTitleByName("AIDS").then(callbackTitleFound); + localEvopediaArchive.getTitleByName("A/AIDS").then(callbackTitleFound); }); asyncTest("check getTitleByName with a title name that does not exist in the archive", function() { @@ -228,7 +228,7 @@ define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geom ok(title === null, "No title found because it does not exist in the archive"); start(); }; - localEvopediaArchive.getTitleByName("abcdef").then(callbackTitleFound); + localEvopediaArchive.getTitleByName("A/abcdef").then(callbackTitleFound); }); asyncTest("check loading a math image", function() { @@ -505,7 +505,7 @@ define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geom }); asyncTest("article '(The Night Time Is) The Right Time' correctly redirects to 'Night Time Is the Right Time'", function() { expect(6); - localZimArchive.getTitleByName("(The_Night_Time_Is)_The_Right_Time.html").then(function(title) { + localZimArchive.getTitleByName("A/(The_Night_Time_Is)_The_Right_Time.html").then(function(title) { ok(title !== null, "Title found"); ok(title.isRedirect(), "Title is a redirect."); equal(title.name(), "(The Night Time Is) The Right Time", "Correct redirect title name."); @@ -519,10 +519,9 @@ define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geom }); asyncTest("Image 'm/RayCharles_AManAndHisSoul.jpg' can be loaded", function() { expect(4); - localZimArchive.getTitleByName("m/RayCharles_AManAndHisSoul.jpg").then(function(title) { - console.log(title); + localZimArchive.getTitleByName("I/m/RayCharles_AManAndHisSoul.jpg").then(function(title) { ok(title !== null, "Title found"); - equal(title.url, "m/RayCharles_AManAndHisSoul.jpg", "URL is correct."); + equal(title.url, "I/m/RayCharles_AManAndHisSoul.jpg", "URL is correct."); localZimArchive.readBinaryFile(title, function(title, data) { equal(data.length, 4951, "Data length is correct."); var beginning = new Uint8Array([255, 216, 255, 224, 0, 16, 74, 70, diff --git a/www/js/app.js b/www/js/app.js index 0c98d84f..cdb58789 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -769,9 +769,10 @@ define(['jquery', 'abstractBackend', 'util', 'cookies','geometry','osabstraction var titleName = event.data.titleName; var messagePort = event.ports[0]; var readFile = function(title) { - console.log("Found title."); - if (title.isRedirect()) { - console.log("Following redirect..."); + if (title === null) { + console.error("Title " + titleName + " not found in archive."); + messagePort.postMessage({'action': 'giveContent', 'titleName' : titleName, 'content': ''}); + } else if (title.isRedirect()) { selectedArchive.resolveRedirect(title, readFile); } else { console.log("Reading binary file..."); @@ -781,9 +782,7 @@ define(['jquery', 'abstractBackend', 'util', 'cookies','geometry','osabstraction }); } } - console.log("Fetching title " + titleName); selectedArchive.getTitleByName(titleName).then(readFile).fail(function() { - console.log("could not find title:" + arguments); messagePort.postMessage({'action': 'giveContent', 'titleName' : titleName, 'content': new UInt8Array()}); }); } diff --git a/www/js/lib/zimArchive.js b/www/js/lib/zimArchive.js index 46e0e5a4..ae1f2b0e 100644 --- a/www/js/lib/zimArchive.js +++ b/www/js/lib/zimArchive.js @@ -121,6 +121,10 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'], return that._file.dirEntryByTitleIndex(i).then(function(dirEntry) { if (dirEntry.title == "") return -1; // ZIM sorts empty titles (assets) to the end + else if (dirEntry.namespace < "A") + return 1; + else if (dirEntry.namespace > "A") + return -1; return prefix < dirEntry.title ? -1 : 1; }); }, true).then(function(firstIndex) { @@ -206,17 +210,22 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'], var that = this; return util.binarySearch(0, this._file.articleCount, function(i) { return that._file.dirEntryByUrlIndex(i).then(function(dirEntry) { - if (titleName < dirEntry.url) + var url = dirEntry.namespace + "/" + dirEntry.url; + if (titleName < url) return -1; - else if (titleName > dirEntry.url) + else if (titleName > url) return 1; else return 0; }); }).then(function(index) { + if (index === null) return null; return that._file.dirEntryByUrlIndex(index); }).then(function(dirEntry) { - return that._dirEntryToTitleObject(dirEntry); + if (dirEntry === null) + return null; + else + return that._dirEntryToTitleObject(dirEntry); }); }; diff --git a/www/js/lib/zimDirEntry.js b/www/js/lib/zimDirEntry.js index 955446e1..e385e80e 100644 --- a/www/js/lib/zimDirEntry.js +++ b/www/js/lib/zimDirEntry.js @@ -54,7 +54,7 @@ define([], function() { this.redirectTarget = dirEntryData.redirectTarget; this.cluster = dirEntryData.cluster; this.blob = dirEntryData.blob; - this.url = dirEntryData.url; + this.url = dirEntryData.namespace + '/' + dirEntryData.url; this.title = dirEntryData.title; };