-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebidx.js
235 lines (177 loc) · 5.43 KB
/
webidx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
window.webidx = {};
webidx = window.webidx;
webidx.search = async function (params) {
if (!webidx.sql) {
//
// initialise sql.js
//
webidx.sql = await window.initSqlJs({locateFile: file => `https://sql.js.org/dist/${file}`});
}
if (webidx.hasOwnProperty('db')) {
webidx.displayResults(webidx.query(params.query), params);
} else {
webidx.loadDB(params);
}
};
webidx.loadDB = function (params) {
var xhr = new XMLHttpRequest();
xhr.open('GET', params.dbfile);
xhr.timeout = params.timeout ?? 5000;
xhr.responseType = 'arraybuffer';
xhr.ontimeout = function() {
if (params.hasOwnProperty('errorCallback')) {
params.errorCallback('Unable to load index, please refresh the page.');
}
};
xhr.onload = function() {
webidx.initializeDB(this.response);
webidx.displayResults(webidx.query(params.query), params);
};
xhr.send();
};
webidx.initializeDB = function (arrayBuffer) {
webidx.db = new webidx.sql.Database(window.pako.inflate(new Uint8Array(arrayBuffer)));
//
// prepare statements
//
webidx.wordQuery = webidx.db.prepare("SELECT `id` FROM `words` WHERE (`word`=:word)");
webidx.idxQuery = webidx.db.prepare("SELECT `page_id` FROM `index` WHERE (`word`=:word)");
webidx.pageQuery = webidx.db.prepare("SELECT `url`,`title` FROM `pages` WHERE (`id`=:id)");
};
webidx.getWordID = function (word) {
webidx.wordQuery.bind([word]);
webidx.wordQuery.step();
var word_id = webidx.wordQuery.get().shift();
webidx.wordQuery.reset();
return word_id;
};
webidx.getPagesHavingWord = function (word_id) {
var pages = [];
webidx.idxQuery.bind([word_id]);
while (webidx.idxQuery.step()) {
pages.push(webidx.idxQuery.get().shift());
}
webidx.idxQuery.reset();
return pages;
};
webidx.getPage = function (page_id) {
webidx.pageQuery.bind([page_id]);
webidx.pageQuery.step();
var page = webidx.pageQuery.getAsObject();
webidx.pageQuery.reset();
return page;
};
webidx.query = function (query) {
//
// split the search term into words
//
var words = query.toLowerCase().split(" ");
//
// this array maps page ID to rank
//
var pageRank = [];
//
// iterate over each word
//
while (words.length > 0) {
var word = words.shift();
var invert = false;
if (0 == word.indexOf("-")) {
invert = true;
word = word.substring(1);
}
var word_id = webidx.getWordID(word);
//
// if the word isn't present, ignore it
//
if (word_id) {
var pages = webidx.getPagesHavingWord(word_id);
pages.forEach(function (page_id) {
if (invert) {
if (pageRank[page_id]) {
pageRank[page_id] -= 65535;
} else {
pageRank[page_id] = -65535;
}
} else {
if (pageRank[page_id]) {
pageRank[page_id]++;
} else {
pageRank[page_id] = 1;
}
}
});
}
}
//
// transform the results into a format that can be sorted
//
var sortedPages = [];
pageRank.forEach(function (rank, page_id) {
if (rank > 0) {
sortedPages.push({rank: rank, page_id: page_id});
}
})
//
// sort the results in descending rank order
//
sortedPages.sort(function(a, b) {
return b.rank - a.rank;
});
//
// this will be populated with the actual pages
//
var pages = [];
//
// get page data for each result
//
sortedPages.forEach(function(result) {
pages.push(webidx.getPage(result.page_id));
});
return pages;
};
webidx.regExpQuote = function (str) {
return str.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&');
};
webidx.displayResults = function (pages, params) {
var callback = params.resultCallback ?? webidx.displayDialog;
callback(pages, params);
};
webidx.displayDialog = function (pages, params) {
var dialog = document.createElement('dialog');
dialog.classList.add('webidx-results-dialog')
dialog.appendChild(document.createElement('h2')).appendChild(document.createTextNode('Search Results'));
if (pages.length < 1) {
dialog.appendChild(document.createElement('p')).appendChild(document.createTextNode('Nothing found.'));
} else {
var ul = dialog.appendChild(document.createElement('ul'));
pages.forEach(function(page) {
var titleText = page.title;
if (params.titleSuffix) {
titleText = titleText.replace(new RegExp(webidx.regExpQuote(params.titleSuffix)+'$'), '');
}
if (params.titlePrefix) {
titleText = titleText.replace(new RegExp('^' + webidx.regExpQuote(params.titleSuffix)), '');
}
var li = ul.appendChild(document.createElement('li'));
var a = li.appendChild(document.createElement('a'));
a.setAttribute('href', page.url);
a.appendChild(document.createTextNode(titleText));
li.appendChild(document.createElement('br'));
var span = li.appendChild(document.createElement('span'));
span.classList.add('webidx-page-url');
span.appendChild(document.createTextNode(page.url));
});
}
var form = dialog.appendChild(document.createElement('form'));
form.setAttribute('method', 'dialog');
var button = form.appendChild(document.createElement('button'));
button.setAttribute('autofocus', true);
button.appendChild(document.createTextNode('Close'));
document.body.appendChild(dialog);
dialog.addEventListener('close', function() {
dialog.parentNode.removeChild(dialog);
});
dialog.showModal();
dialog.scrollTop = 0;
};