mirror of
https://github.com/kiwix/kiwix-js.git
synced 2025-09-22 03:52:21 -04:00
Fix the way we look for an article with a specific title name.
Instead of taking the first result of the search on normalized names, we browse the results and take the one that exactly matches the title name we look for. Fixes #32 A few improvements also come in this commit, to externalize the maximum number of results displayed when searching, and to fix some bugs on the small dump
This commit is contained in:
parent
44f6f8c508
commit
e141a769db
30
js/app.js
30
js/app.js
@ -10,6 +10,9 @@ define(function(require) {
|
||||
var evopediaTitle = require('title');
|
||||
var evopediaArchive = require('archive');
|
||||
var util = require('util');
|
||||
|
||||
// Maximum number of titles to display in a search
|
||||
var MAX_SEARCH_RESULT_SIZE = 50;
|
||||
|
||||
|
||||
var localArchive = null;
|
||||
@ -210,7 +213,7 @@ define(function(require) {
|
||||
$('#configuration').hide();
|
||||
$('#articleContent').empty();
|
||||
if (localArchive.titleFile) {
|
||||
localArchive.findTitlesWithPrefix(prefix.trim(), populateListOfTitles);
|
||||
localArchive.findTitlesWithPrefix(prefix.trim(), MAX_SEARCH_RESULT_SIZE, populateListOfTitles);
|
||||
} else {
|
||||
alert("Title file not set");
|
||||
}
|
||||
@ -284,15 +287,13 @@ define(function(require) {
|
||||
* @param {type} title
|
||||
*/
|
||||
function readArticle(title) {
|
||||
if ($.isArray(title)) {
|
||||
title = title[0];
|
||||
if (title.fileNr === 255) {
|
||||
localArchive.resolveRedirect(title, readArticle);
|
||||
return;
|
||||
}
|
||||
if (title.fileNr === 255) {
|
||||
localArchive.resolveRedirect(title, readArticle);
|
||||
}
|
||||
else {
|
||||
localArchive.readArticle(title, displayArticleInForm);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the the given HTML article in the web page,
|
||||
@ -314,6 +315,9 @@ define(function(require) {
|
||||
$('#articleContent').find('a').each(function() {
|
||||
// Store current link's url
|
||||
var url = $(this).attr("href");
|
||||
if (url === null || url === undefined) {
|
||||
return;
|
||||
}
|
||||
var lowerCaseUrl = url.toLowerCase();
|
||||
var cssClass = $(this).attr("class");
|
||||
|
||||
@ -396,8 +400,16 @@ define(function(require) {
|
||||
function goToArticle(titleName) {
|
||||
$("#articleName").html(titleName);
|
||||
$("#readingArticle").show();
|
||||
$("#articleContent").html("");
|
||||
localArchive.getTitleByName(titleName, readArticle);
|
||||
localArchive.getTitleByName(titleName, function(title) {
|
||||
if (title == null) {
|
||||
$("#readingArticle").hide();
|
||||
alert("Article with title " + titleName + " not found in the archive");
|
||||
}
|
||||
else {
|
||||
$("#articleContent").html("");
|
||||
readArticle(title);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
@ -14,6 +14,9 @@ define(function(require) {
|
||||
|
||||
// Size of chunks read in the dump files : 128 KB
|
||||
var CHUNK_SIZE = 131072;
|
||||
// The maximum number of titles that can have the same name after normalizing
|
||||
// This is used by the algorithm that searches for a specific article by its name
|
||||
var MAX_TITLES_WITH_SAME_NORMALIZED_NAME = 30;
|
||||
|
||||
/**
|
||||
* LocalArchive class : defines a wikipedia dump on the filesystem
|
||||
@ -244,8 +247,8 @@ define(function(require) {
|
||||
|
||||
/**
|
||||
* This function is recursively called after each asynchronous read, so that
|
||||
* to find the closest index in titleFile to the given prefix When found,
|
||||
* call the callbackFunction with the index
|
||||
* to find the closest index in titleFile to the given prefix
|
||||
* When found, call the callbackFunction with the index
|
||||
*
|
||||
* @param reader
|
||||
* @param normalizedPrefix
|
||||
@ -307,15 +310,6 @@ define(function(require) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Look for a title in the title file at the given offset, and call the callbackFunction with this Title
|
||||
* @param titleOffset
|
||||
* @param callbackFunction
|
||||
*/
|
||||
LocalArchive.prototype.getTitleAtOffset = function(titleOffset, callbackFunction) {
|
||||
this.getTitlesStartingAtOffset(titleOffset, 1, callbackFunction);
|
||||
};
|
||||
|
||||
/**
|
||||
* Read the titles in the title file starting at the given offset (maximum titleCount), and call the callbackFunction with this list of Title instances
|
||||
* @param titleOffset
|
||||
@ -367,6 +361,7 @@ define(function(require) {
|
||||
|
||||
/**
|
||||
* Look for a title by its name, and call the callbackFunction with this Title
|
||||
* If the title is not found, the callbackFunction is called with parameter null
|
||||
* @param titleName
|
||||
* @param callbackFunction
|
||||
*/
|
||||
@ -380,7 +375,20 @@ define(function(require) {
|
||||
var currentLocalArchiveInstance = this;
|
||||
var normalizedTitleName = normalize_string.normalizeString(titleName);
|
||||
this.recursivePrefixSearch(reader, normalizedTitleName, 0, titleFileSize, function(titleOffset) {
|
||||
currentLocalArchiveInstance.getTitleAtOffset(titleOffset, callbackFunction);
|
||||
currentLocalArchiveInstance.getTitlesStartingAtOffset(titleOffset, MAX_TITLES_WITH_SAME_NORMALIZED_NAME, function(titleList) {
|
||||
if (titleList !== null && titleList.length>0) {
|
||||
for (var i=0; i<titleList.length; i++) {
|
||||
var title = titleList[i];
|
||||
if (title.name === titleName) {
|
||||
// The title has been found
|
||||
callbackFunction(title);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// The title has not been found
|
||||
callbackFunction(null);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@ -393,11 +401,12 @@ define(function(require) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Find the 50 titles that start with the given prefix, and call the callbackFunction with this list of Titles
|
||||
* Find titles that start with the given prefix, and call the callbackFunction with this list of Titles
|
||||
* @param prefix
|
||||
* @param maxSize Maximum number of titles to read
|
||||
* @param callbackFunction
|
||||
*/
|
||||
LocalArchive.prototype.findTitlesWithPrefix = function(prefix, callbackFunction) {
|
||||
LocalArchive.prototype.findTitlesWithPrefix = function(prefix, maxSize, callbackFunction) {
|
||||
var titleFileSize = this.titleFile.size;
|
||||
if (prefix) {
|
||||
prefix = normalize_string.normalizeString(prefix);
|
||||
@ -411,7 +420,7 @@ define(function(require) {
|
||||
var currentLocalArchiveInstance = this;
|
||||
var normalizedPrefix = normalize_string.normalizeString(prefix);
|
||||
this.recursivePrefixSearch(reader, normalizedPrefix, 0, titleFileSize, function(titleOffset) {
|
||||
currentLocalArchiveInstance.getTitlesStartingAtOffset(titleOffset, 50, function(titleList) {
|
||||
currentLocalArchiveInstance.getTitlesStartingAtOffset(titleOffset, maxSize, function(titleList) {
|
||||
// Keep only the titles with names starting with the prefix
|
||||
var i = 0;
|
||||
for (i = 0; i < titleList.length; i++) {
|
||||
|
@ -34,7 +34,7 @@ define(function(require) {
|
||||
equal(titleList.length, 4, "4 titles found, as requested");
|
||||
var indexAbraham = -1;
|
||||
for (var i = 0; i < titleList.length; i++) {
|
||||
if (titleList[i] && titleList[i].name == "Abraham") {
|
||||
if (titleList[i] && titleList[i].name === "Abraham") {
|
||||
indexAbraham = i;
|
||||
}
|
||||
}
|
||||
@ -70,7 +70,7 @@ define(function(require) {
|
||||
equal(titleList.length, 4, "4 titles should be found");
|
||||
start();
|
||||
};
|
||||
localArchive.findTitlesWithPrefix("Am", callbackFunction);
|
||||
localArchive.findTitlesWithPrefix("Am", 10, callbackFunction);
|
||||
});
|
||||
|
||||
// Create a title instance for the Article 'Abraham'
|
||||
@ -84,20 +84,20 @@ define(function(require) {
|
||||
titleAbraham.titleOffset = 57;
|
||||
|
||||
asyncTest("check getTitleByName with accents : Diego Velázquez", function() {
|
||||
var callbackFunction = function(titleList) {
|
||||
ok(titleList && titleList.length == 1, "One title found");
|
||||
equal(titleList[0].name, "Diego_Velázquez", "Name of the title is correct");
|
||||
var callbackFunction = function(title) {
|
||||
ok(title !== null, "Title found");
|
||||
equal(title.name, "Diego_Velázquez", "Name of the title is correct");
|
||||
start();
|
||||
};
|
||||
localArchive.getTitleByName("Diego Velázquez", callbackFunction);
|
||||
localArchive.getTitleByName("Diego_Velázquez", callbackFunction);
|
||||
});
|
||||
asyncTest("check getTitleByName with quote : Hundred Years' War", function() {
|
||||
var callbackFunction = function(titleList) {
|
||||
ok(titleList && titleList.length == 1, "One title found");
|
||||
equal(titleList[0].name, "Hundred_Years'_War", "Name of the title is correct");
|
||||
var callbackFunction = function(title) {
|
||||
ok(title !== null, "Title found");
|
||||
equal(title.name, "Hundred_Years'_War", "Name of the title is correct");
|
||||
start();
|
||||
};
|
||||
localArchive.getTitleByName("Hundred Years' War", callbackFunction);
|
||||
localArchive.getTitleByName("Hundred_Years'_War", callbackFunction);
|
||||
});
|
||||
|
||||
test("check parseTitleFromId", function() {
|
||||
@ -128,13 +128,20 @@ define(function(require) {
|
||||
ok(htmlArticle.match("</div>[ \t]$"), "</div> at the end");
|
||||
start();
|
||||
};
|
||||
var callbackTitleList = function(titleList) {
|
||||
ok(titleList && titleList.length == 1, "One title found");
|
||||
var title = titleList[0];
|
||||
var callbackTitleFound = function(title) {
|
||||
ok(title !== null, "Title found");
|
||||
equal(title.name, "AIDS", "Name of the title is correct");
|
||||
localArchive.readArticle(title, callbackArticleRead);
|
||||
};
|
||||
localArchive.getTitleByName("AIDS", callbackTitleList);
|
||||
localArchive.getTitleByName("AIDS", callbackTitleFound);
|
||||
});
|
||||
|
||||
asyncTest("check getTitleByName with a title name that does not exist in the archive", function() {
|
||||
var callbackTitleFound = function(title) {
|
||||
ok(title === null, "No title found because it does not exist in the archive");
|
||||
start();
|
||||
};
|
||||
localArchive.getTitleByName("abcdef", callbackTitleFound);
|
||||
});
|
||||
|
||||
asyncTest("check loading a math image", function() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user