1 |
dpavlin |
391 |
/** |
2 |
|
|
search.js searchs an XML index to HTML files. |
3 |
|
|
|
4 |
|
|
A part of the jsfind project (http://projects.elucidsoft.net/jsfind) |
5 |
|
|
Copyright (C) 2003 Shawn Garbett |
6 |
|
|
|
7 |
|
|
This program is free software; you can redistribute it and/or |
8 |
|
|
modify it under the terms of the GNU General Public License |
9 |
|
|
as published by the Free Software Foundation; either version 2 |
10 |
|
|
of the License, or (at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
|
|
along with this program; if not, write to the Free Software |
19 |
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
20 |
|
|
|
21 |
|
|
Contact Info: |
22 |
|
|
Shawn Garbett <Shawn@eLucidSoft.net> |
23 |
|
|
http://www.elucidsoft.net |
24 |
|
|
4037 General Bate Drive |
25 |
|
|
Nashville, TN 37204 |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
// Constants |
29 |
|
|
var conversion = new String |
30 |
|
|
("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"); |
31 |
|
|
|
32 |
|
|
// State variables |
33 |
|
|
var query_left = ""; |
34 |
|
|
var search_err = ""; |
35 |
|
|
var results = null; |
36 |
|
|
var index_path = ""; |
37 |
|
|
|
38 |
|
|
var watchdog_id = 0; |
39 |
|
|
var watchdog_callback = null; |
40 |
|
|
|
41 |
|
|
// Object to hold search results |
42 |
|
|
function Result(title, link, freq) |
43 |
|
|
{ |
44 |
|
|
this.title=title; |
45 |
|
|
this.link=link; |
46 |
|
|
this.frequency=Number(freq); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
// Function to merge (intersect) two result sets |
50 |
|
|
function intersect_results(data) |
51 |
|
|
{ |
52 |
|
|
// If there are no stored results, then these are the results |
53 |
|
|
if(!results) |
54 |
|
|
{ |
55 |
|
|
results = data; |
56 |
|
|
return; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
var output=new Array(); |
60 |
|
|
|
61 |
|
|
// There are existing results, to do an intersect... |
62 |
|
|
for(var i=0; i<results.length; i++) |
63 |
|
|
{ |
64 |
|
|
for(var j=0; j<data.length; j++) |
65 |
|
|
{ |
66 |
|
|
if(data[j].title == results[i].title) |
67 |
|
|
{ |
68 |
|
|
results[i].frequency += data[j].frequency; |
69 |
|
|
output.push(results[i]); |
70 |
|
|
break; |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
results = output; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
/* |
79 |
|
|
From David Flanagan's, _Javascript:_The Definitive_Guide_, pg. 294-5, |
80 |
|
|
published by O'Reilly, 4th edition, 2002 |
81 |
|
|
*/ |
82 |
|
|
function debug(msg) |
83 |
|
|
{ |
84 |
dpavlin |
396 |
// return; // Disable debugging |
85 |
dpavlin |
391 |
|
86 |
dpavlin |
464 |
var debug_div = element_id('debug'); |
87 |
dpavlin |
391 |
|
88 |
dpavlin |
464 |
if (debug_div) debug_div.innerHTML += msg+"<br/>\n"; |
89 |
dpavlin |
391 |
} |
90 |
|
|
|
91 |
|
|
// |
92 |
|
|
|
93 |
|
|
// Convert a number into a base 62 alphanumeric number string |
94 |
|
|
function convert(num) |
95 |
|
|
{ |
96 |
|
|
var base = conversion.length; |
97 |
|
|
var pow = 1; |
98 |
|
|
var pos = 0; |
99 |
|
|
var out = ""; |
100 |
|
|
|
101 |
|
|
if(num == 0) |
102 |
|
|
{ |
103 |
|
|
return "0"; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
while (num > 0) |
107 |
|
|
{ |
108 |
|
|
pos = num % base; |
109 |
|
|
out = conversion.charAt(pos) + out; |
110 |
|
|
num = Math.floor(num/base); |
111 |
|
|
pow *= base; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
return out; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
function watchdog() |
118 |
|
|
{ |
119 |
|
|
debug ("TIMEOUT!"); |
120 |
|
|
watchdog_callback(new Array()); |
121 |
|
|
} |
122 |
|
|
|
123 |
dpavlin |
469 |
var xmldoc; |
124 |
|
|
|
125 |
dpavlin |
391 |
// This function loads the XML document from the specified URL, and when |
126 |
|
|
// it is fully loaded, passes that document and the url to the specified |
127 |
|
|
// handler function. This function works with any XML document |
128 |
|
|
function loadXML(url, handler, data, result_handler) |
129 |
|
|
{ |
130 |
|
|
debug("loadXML("+url+","+data+")"); |
131 |
|
|
|
132 |
|
|
// Timeout operation in 10 seconds |
133 |
|
|
watchdog_callback = result_handler; |
134 |
|
|
watchdog_id=setTimeout("watchdog()", 20000); |
135 |
|
|
|
136 |
|
|
debug("setTimeout = "+watchdog_id); |
137 |
|
|
|
138 |
|
|
try |
139 |
|
|
{ |
140 |
|
|
// Use the standard DOM Level 2 technique, if it is supported |
141 |
|
|
if (document.implementation && document.implementation.createDocument) |
142 |
|
|
{ |
143 |
|
|
// Create a new Document object |
144 |
dpavlin |
450 |
xmldoc = document.implementation.createDocument("", "", null); |
145 |
dpavlin |
391 |
|
146 |
|
|
// Specify what should happen when it finishes loading |
147 |
|
|
xmldoc.onload = function() { handler(xmldoc, url, data, result_handler); } |
148 |
|
|
|
149 |
|
|
//xmldoc.onerror = docError; |
150 |
|
|
//xmldoc.addEventListener("load",docError,false); |
151 |
|
|
|
152 |
|
|
// And tell it what URL to load |
153 |
|
|
xmldoc.load(url); |
154 |
|
|
} |
155 |
|
|
// Otherwise use Microsoft's proprietary API for Internet Explorer |
156 |
|
|
// Something about not following standards once again |
157 |
|
|
else if (window.ActiveXObject) |
158 |
|
|
{ |
159 |
dpavlin |
450 |
xmldoc = new ActiveXObject("Microsoft.XMLDOM"); // Create doc. |
160 |
|
|
if (! xmldoc) xmldoc = new ActiveXObject("MSXML2.DOMDocument"); // Create doc. |
161 |
dpavlin |
391 |
// Specify onload |
162 |
|
|
xmldoc.onreadystatechange = function() |
163 |
|
|
{ |
164 |
|
|
if (xmldoc.readyState == 4) handler(xmldoc, url, data, result_handler); |
165 |
|
|
} |
166 |
|
|
xmldoc.load(url); // Start loading! |
167 |
|
|
} |
168 |
|
|
} |
169 |
|
|
catch(ex) |
170 |
|
|
{ |
171 |
|
|
clearTimeout(watchdog_id); |
172 |
|
|
debug("clearTimeout = "+watchdog_id); |
173 |
|
|
debug ("CAUGHT EXCEPTION!"); |
174 |
|
|
result_handler(new Array()); |
175 |
|
|
return false; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
return true; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
function loadData(xmldoc, url, pos, result_handler) |
182 |
|
|
{ |
183 |
|
|
clearTimeout(watchdog_id); |
184 |
|
|
debug("clearTimeout = "+watchdog_id); |
185 |
|
|
|
186 |
|
|
debug ("loadData("+url+","+pos+")"); |
187 |
|
|
|
188 |
|
|
var data = new Array(); |
189 |
|
|
|
190 |
|
|
// Get all entries |
191 |
|
|
var entries = xmldoc.getElementsByTagName("e"); |
192 |
|
|
|
193 |
|
|
if(entries.length > pos) |
194 |
|
|
{ |
195 |
|
|
// Get the links associated with this query |
196 |
|
|
var links = entries[pos].getElementsByTagName("l"); |
197 |
|
|
|
198 |
|
|
// Dynamically append results to output |
199 |
|
|
for(var i=0; i<links.length; i++) |
200 |
|
|
{ |
201 |
|
|
data.push(new Result(links[i].getAttribute("t"), |
202 |
|
|
links[i].firstChild.data, |
203 |
|
|
links[i].getAttribute("f"))); |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
intersect_results(data); |
207 |
|
|
|
208 |
|
|
if(query_left.length > 0) |
209 |
|
|
{ |
210 |
|
|
doSearch(index_path, query_left, result_handler); |
211 |
|
|
} |
212 |
|
|
else |
213 |
|
|
{ |
214 |
|
|
results.sort(sortResults); |
215 |
|
|
result_handler(results); |
216 |
|
|
} |
217 |
|
|
} |
218 |
|
|
else |
219 |
|
|
{ |
220 |
|
|
debug("INTERNAL ERROR, Inconsistent index"); |
221 |
|
|
search_err="INTERNAL ERROR, Inconsistent index"; |
222 |
|
|
} |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
function sortResults(a, b) |
226 |
|
|
{ |
227 |
|
|
return a.frequency - b.frequency; |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
function traverseTree(xmldoc, url, query, result_handler) |
231 |
|
|
{ |
232 |
|
|
clearTimeout(watchdog_id); |
233 |
|
|
debug("clearTimeout = "+watchdog_id); |
234 |
|
|
|
235 |
|
|
debug("traverseTree("+xmldoc+","+url+","+query+")"); |
236 |
|
|
|
237 |
|
|
var keys = xmldoc.getElementsByTagName("k"); |
238 |
|
|
var i; |
239 |
|
|
|
240 |
|
|
for(i = 0; i < keys.length; i++) |
241 |
|
|
{ |
242 |
|
|
var key = keys[i].firstChild.data; |
243 |
|
|
debug("traverseTree: key="+key+" query="+query); |
244 |
|
|
if(key != '' && key != null) |
245 |
|
|
{ |
246 |
|
|
// Case where current key is greater than query, descend |
247 |
|
|
if(key > query) |
248 |
|
|
{ |
249 |
|
|
if(key != '' && key != null) |
250 |
|
|
{ |
251 |
|
|
if(!loadXML(url.replace(".xml","/"+convert(i)+".xml"), |
252 |
|
|
traverseTree,query,result_handler)) |
253 |
|
|
{ |
254 |
|
|
debug("Unable to locate key "+query); |
255 |
|
|
result_handler(new Array()); |
256 |
|
|
} |
257 |
|
|
// make sure of garbage collection |
258 |
|
|
xmldoc=null; |
259 |
|
|
return; |
260 |
|
|
} |
261 |
|
|
} |
262 |
|
|
// Found it! |
263 |
|
|
else if(key==query) |
264 |
|
|
{ |
265 |
|
|
if(!loadXML(url.replace(/(\w+\.xml)/, "_$1"), |
266 |
|
|
loadData, i, result_handler)) |
267 |
|
|
{ |
268 |
|
|
debug("ERROR: Unable to locate data "+query); |
269 |
|
|
result_handler(new Array()); |
270 |
|
|
} |
271 |
|
|
// make sure of garbage collection |
272 |
|
|
xmldoc=null; |
273 |
|
|
return; |
274 |
|
|
} |
275 |
|
|
} |
276 |
|
|
} |
277 |
|
|
// Look past the end... |
278 |
|
|
if(keys.length == 0 || !loadXML(url.replace(".xml","/"+convert(i)+".xml"), |
279 |
|
|
traverseTree,query,result_handler)) |
280 |
|
|
{ |
281 |
|
|
debug("Unable to locate key "+query); |
282 |
|
|
result_handler(new Array()); |
283 |
|
|
} |
284 |
|
|
// make sure of garbage collection |
285 |
|
|
xmldoc=null; |
286 |
|
|
return; |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
function doSearch(index_name,query, result_func) |
290 |
|
|
{ |
291 |
|
|
//alert("doSearch("+index_name+","+query+")"); |
292 |
|
|
var pos=query.search(/[\s\+]/); |
293 |
|
|
if (index_name) index_path = index_name+'/'; |
294 |
|
|
|
295 |
|
|
if(pos < 0) |
296 |
|
|
{ |
297 |
|
|
query_left = ""; |
298 |
|
|
} |
299 |
|
|
else |
300 |
|
|
{ |
301 |
|
|
query_left = query.slice(pos+1); |
302 |
|
|
query = query.slice(0,pos); |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
if(!loadXML(index_path+"0.xml", traverseTree, query.toLowerCase(), result_func)) |
306 |
|
|
{ |
307 |
|
|
debug("ERROR: Couldn't find main index 0.xml"); |
308 |
|
|
search_err = "INTERNAL ERROR: Unable to load main index 0.xml"; |
309 |
|
|
} |
310 |
|
|
} |