When DeviceStorage is not available, display only one "input type=file multiple", in which the use must select all the archive files. It's quicker and easier than selecting each type of file individually. Fixes #28

Also implemented reading all the kind of files of an archive : titles_search.idx and coordinate files (both unused for now)
This commit is contained in:
mossroy 2013-07-27 11:51:12 +02:00
parent 96e4e6bbdf
commit 21a97278b2
5 changed files with 105 additions and 56 deletions

View File

@ -80,16 +80,8 @@
<div id="configuration">
<h2>Configuration</h2>
<div id="openLocalFiles" style="display: none;">
<br /> Please select the file titles.idx :<br />
<input type="file" id="titleFile" class="btn" /><br />
Please select the files wikipedia_*.dat from the same dump :<br />
<input type="file" id="dataFiles" multiple class="btn" /><br />
Please select the file math.idx from the same dump:<br />
<input type="file" id="mathIndexFile" class="btn" /><br />
Please select the file math.dat from the same dump:<br />
<input type="file" id="mathDataFile" class="btn" /><br />
Please select the file metadata.txt from the same dump:<br />
<input type="file" id="metadataFile" class="btn" /><br />
<br /> Please select all the files inside the archive (wikipedia_*.dat, metadata.txt etc)<br />
<input type="file" id="archiveFiles" multiple class="btn" /><br />
</div>
<div id="scanningForArchives" style="display: none;">
<br /> Scanning your sdcard for archives... Please wait <img src="img/spinner.gif" />

View File

@ -99,10 +99,11 @@ define(function(require) {
function setLocalArchiveFromArchiveList() {
var archiveDirectory = $('#archiveList').val();
localArchive = new evopedia.LocalArchive();
localArchive.readTitleFileFromStorage(storage, archiveDirectory);
localArchive.readTitleFilesFromStorage(storage, archiveDirectory);
localArchive.readDataFilesFromStorage(storage, archiveDirectory, 0);
localArchive.readMathFilesFromStorage(storage, archiveDirectory);
localArchive.readMetadataFileFromStorage(storage, archiveDirectory);
localArchive.readCoordinateFilesFromStorage(storage, archiveDirectory, 0);
}
/**
@ -110,28 +111,15 @@ define(function(require) {
*/
function displayFileSelect() {
$('#openLocalFiles').show();
$('#dataFiles').on('change', setLocalArchiveFromFileSelect);
$('#titleFile').on('change', setLocalArchiveFromFileSelect);
$('#mathIndexFile').on('change', setLocalArchiveFromFileSelect);
$('#mathDataFile').on('change', setLocalArchiveFromFileSelect);
$('#metadataFile').on('change', setLocalArchiveFromFileSelect);
$('#archiveFiles').on('change', setLocalArchiveFromFileSelect);
}
/**
* Sets the localArchive from the File selects populated by user
*/
function setLocalArchiveFromFileSelect() {
var dataFiles = document.getElementById('dataFiles').files;
var titleFile = document.getElementById('titleFile').files[0];
var mathIndexFile = document.getElementById('mathIndexFile').files[0];
var mathDataFile = document.getElementById('mathDataFile').files[0];
var metadataFile = document.getElementById('metadataFile').files[0];
localArchive = new evopedia.LocalArchive();
localArchive.dataFiles = dataFiles;
localArchive.titleFile = titleFile;
localArchive.mathIndexFile = mathIndexFile;
localArchive.mathDataFile = mathDataFile;
localArchive.readMetadataFile(metadataFile);
localArchive.initializeFromArchiveFiles(document.getElementById('archiveFiles').files);
}
/**

View File

@ -44,7 +44,7 @@ define(function(require) {
var bits, h1, h2, h3, h4, i = 0;
var enc = "";
for (i = 0; i < byteArray.length; ) {
for (var i = 0; i < byteArray.length; ) {
bits = byteArray[i++] << 16;
bits |= byteArray[i++] << 8;
bits |= byteArray[i++];
@ -65,26 +65,27 @@ define(function(require) {
/**
* LocalArchive class : defines a wikipedia dump on the filesystem
* It's still minimal for now. TODO : complete implementation to handle maths and coordinates
*/
function LocalArchive() {
this.dataFiles = new Array();
this.coordinateFiles = new Array();
this.titleFile = null;
this.mathIndexFile = null;
this.mathDataFile = null;
this.date = null;
this.language = null;
this.titleSearchFile = null;
};
/**
* Read the title File in the given directory, and assign it to the
* Read the title Files in the given directory, and assign them to the
* current LocalArchive
*
* @param storage
* @param directory
*/
LocalArchive.prototype.readTitleFileFromStorage = function(storage, directory) {
LocalArchive.prototype.readTitleFilesFromStorage = function(storage, directory) {
var currentLocalArchiveInstance = this;
var filerequest = storage.get(directory + '/titles.idx');
filerequest.onsuccess = function() {
@ -93,6 +94,13 @@ define(function(require) {
filerequest.onerror = function(event) {
alert("error reading title file in directory " + directory + " : " + event.target.error.name);
};
var filerequestSearch = storage.get(directory + '/titles_search.idx');
filerequestSearch.onsuccess = function() {
currentLocalArchiveInstance.titleSearchFile = filerequest.result;
};
filerequest.onerror = function(event) {
// Do nothing : this file is not mandatory in an archive
};
};
/**
@ -128,6 +136,39 @@ define(function(require) {
};
};
/**
* Read the coordinate Files in the given directory (starting at given index), and
* assign them to the current LocalArchive
*
* @param storage
* @param directory
* @param index
*/
LocalArchive.prototype.readCoordinateFilesFromStorage = function(storage, directory, index) {
var currentLocalArchiveInstance = this;
var prefixedFileNumber = "";
if (index < 10) {
prefixedFileNumber = "0" + index;
} else {
prefixedFileNumber = index;
}
var filerequest = storage.get(directory + '/coordinates_' + prefixedFileNumber
+ '.idx');
filerequest.onsuccess = function() {
currentLocalArchiveInstance.coordinateFiles[index] = filerequest.result;
currentLocalArchiveInstance.readCoordinateFilesFromStorage(storage, directory,
index + 1);
};
filerequest.onerror = function(event) {
// TODO there must be a better to way to detect a FileNotFound
if (event.target.error.name != "NotFoundError") {
alert("error reading data file " + index + " in directory "
+ directory + " : " + event.target.error.name);
}
};
};
/**
* Read the metadata.txt file in the given directory, and store its content
* in the current instance
@ -164,6 +205,53 @@ define(function(require) {
};
reader.readAsText(file);
};
/**
* Initialize the localArchive from given archive files
* @param {type} archiveFiles
* @returns {undefined}
*/
LocalArchive.prototype.initializeFromArchiveFiles = function(archiveFiles) {
var dataFileRegex = /^wikipedia_(\d\d).dat$/;
var coordinateFileRegex = /^coordinates_(\d\d).idx$/;
this.dataFiles = new Array();
this.coordinateFiles = new Array();
for (var i=0; i<archiveFiles.length; i++) {
var file = archiveFiles[i];
if (file) {
if (file.name === "metadata.txt") {
this.readMetadataFile(file);
}
else if (file.name === "titles.idx") {
this.titleFile = file;
}
else if (file.name === "titles_search.idx") {
this.titleSearchFile = file;
}
else if (file.name === "math.idx") {
this.mathIndexFile = file;
}
else if (file.name === "math.dat") {
this.mathDataFile = file;
}
else {
var coordinateFileNr = coordinateFileRegex.exec(file.name);
if (coordinateFileNr && coordinateFileNr.length > 0) {
var intFileNr = 1 * coordinateFileNr[1];
this.coordinateFiles[intFileNr] = file;
}
else {
var dataFileNr = dataFileRegex.exec(file.name);
if (dataFileNr && dataFileNr.length > 0) {
var intFileNr = 1 * dataFileNr[1];
this.dataFiles[intFileNr] = file;
}
}
}
}
}
};
/**
* Read the math files (math.idx and math.dat) in the given directory, and assign it to the

View File

@ -18,17 +18,8 @@
</head>
<body>
Please select the file titles.idx from wikipedia_small_2010-08-14 (included with source code) :<br />
<input type="file" id="titleFile" />
<br />Please select the file wikipedia_00.dat from the same dump :<br />
<input type="file" id="dataFiles" multiple />
<br /> Please select the file math.idx from the same dump:<br />
<input type="file" id="mathIndexFile" />
<br /> Please select the file math.dat from the same dump:<br />
<input type="file" id="mathDataFile" />
<br /> Please select the file metadata.txt from the same dump:<br />
<input type="file" id="metadataFile" /><br />
<br />
Please select all the files inside the archive wikipedia_small_2010-08-14 (included with source code) : wikipedia_*.dat, metadata.txt etc<br />
<input type="file" id="archiveFiles" multiple class="btn" /><br />
<input type="button" id="runTests" value="Run tests" />
<div id="qunit"></div>
</body>

View File

@ -17,25 +17,15 @@ define(function(require) {
equal("test", "test", "QUnit is properly configured");
});
test("check title and data files are set", function() {
var titleFile = document.getElementById('titleFile').files[0];
var dataFiles = document.getElementById('dataFiles').files;
ok(titleFile && titleFile.size > 0, "Title file set and not empty");
ok(dataFiles && dataFiles[0] && dataFiles[0].size > 0, "First data file set and not empty");
var mathIndexFile = document.getElementById('mathIndexFile').files[0];
var mathDataFile = document.getElementById('mathDataFile').files[0];
ok(mathIndexFile && mathIndexFile.size > 0, "Math index file set and not empty");
ok(mathDataFile && mathDataFile.size > 0, "Math data file set and not empty");
test("check archive files are selected", function() {
var archiveFiles = document.getElementById('archiveFiles').files;
ok(archiveFiles && archiveFiles[0] && archiveFiles[0].size > 0, "First archive file set and not empty");
ok(archiveFiles.length >= 5, "At least 5 files are selected");
});
// Create a localArchive from selected files, in order to run the following tests
var localArchive = new evopedia.LocalArchive();
localArchive.titleFile = document.getElementById('titleFile').files[0];
localArchive.dataFiles = document.getElementById('dataFiles').files;
localArchive.mathIndexFile = document.getElementById('mathIndexFile').files[0];
localArchive.mathDataFile = document.getElementById('mathDataFile').files[0];
var metadataFile = document.getElementById('metadataFile').files[0];
localArchive.readMetadataFile(metadataFile);
localArchive.initializeFromArchiveFiles(document.getElementById('archiveFiles').files);
module("evopedia");
asyncTest("check getTitlesStartingAtOffset 0", function() {