1 "<script language=\"PHP\">\n"
2 "require_once \"search-config.php\";\n"
4 "function end_form($value)\n"
7 " global $translator;\n"
8 " if ($config['DISABLE_INDEX'] == false)\n"
10 " echo \" <input type=\\\"text\\\" id=\\\"MSearchField\\\" name=\\\"query\\\" value=\\\"$value\\\" size=\\\"20\\\" accesskey=\\\"S\\\" onfocus=\\\"searchBox.OnSearchFieldFocus(true)\\\" onblur=\\\"searchBox.OnSearchFieldFocus(false)\\\"/>\\n </form>\\n </div><div class=\\\"right\\\"></div>\\n </div>\\n </li>\\n </ul>\\n </div>\\n</div>\\n\";\n"
12 " if ($config['GENERATE_TREEVIEW'])\n"
14 " echo $translator['split_bar'];\n"
18 "function end_page()\n"
20 " //global $config;\n"
21 " //global $translator;\n"
22 " //if ($config['GENERATE_TREEVIEW'])\n"
24 " // echo \"</div>\\n<div id=\\\"nav-path\\\" class=\\\"navpath\\\">\\n <ul>\\n <li class=\\\"footer\\\">\";\n"
25 " // echo $translator['logo'];\n"
26 " // echo \"</li>\\n </ul>\\n</div>\";\n"
28 " echo \"</body></html>\";\n"
31 "function search_results()\n"
33 " global $translator;\n"
34 " return $translator['search_results_title'];\n"
37 "function matches_text($num)\n"
39 " global $translator;\n"
40 " $string = $translator['search_results'][($num>2)?2:$num];\n"
41 " // The eval is used so that translator strings can contain $num.\n"
42 " eval(\"\\$result = \\\"$string\\\";\");\n"
46 "function report_matches()\n"
48 " global $translator;\n"
49 " return $translator['search_matches'];\n"
52 "function readInt($file)\n"
54 " $b1 = ord(fgetc($file)); $b2 = ord(fgetc($file));\n"
55 " $b3 = ord(fgetc($file)); $b4 = ord(fgetc($file));\n"
56 " return ($b1<<24)|($b2<<16)|($b3<<8)|$b4;\n"
59 "function readString($file)\n"
62 " while (ord($c=fgetc($file))) $result.=$c;\n"
66 "function readHeader($file)\n"
68 " $header =fgetc($file); $header.=fgetc($file);\n"
69 " $header.=fgetc($file); $header.=fgetc($file);\n"
73 "function computeIndex($word)\n"
75 " // Simple hashing that allows for substring search\n"
76 " if (strlen($word)<2) return -1;\n"
77 " // high char of the index\n"
78 " $hi = ord($word{0});\n"
79 " if ($hi==0) return -1;\n"
80 " // low char of the index\n"
81 " $lo = ord($word{1});\n"
82 " if ($lo==0) return -1;\n"
84 " return $hi*256+$lo;\n"
87 "function search($file,$word,&$statsList)\n"
89 " $index = computeIndex($word);\n"
90 " if ($index!=-1) // found a valid index\n"
92 " fseek($file,$index*4+4); // 4 bytes per entry, skip header\n"
93 " $index = readInt($file);\n"
94 " if ($index) // found words matching the hash key\n"
96 " $start=sizeof($statsList);\n"
98 " fseek($file,$index);\n"
99 " $w = readString($file);\n"
102 " $statIdx = readInt($file);\n"
103 " if ($word==substr($w,0,strlen($word)))\n"
104 " { // found word that matches (as substring)\n"
105 " $statsList[$count++]=array(\n"
106 " \"word\"=>$word,\n"
108 " \"index\"=>$statIdx,\n"
109 " \"full\"=>strlen($w)==strlen($word),\n"
110 " \"docs\"=>array()\n"
113 " $w = readString($file);\n"
118 " for ($count=$start;$count<sizeof($statsList);$count++)\n"
120 " $statInfo = &$statsList[$count];\n"
121 " $multiplier = 1;\n"
122 " // whole word matches have a double weight\n"
123 " if ($statInfo[\"full\"]) $multiplier=2;\n"
124 " fseek($file,$statInfo[\"index\"]); \n"
125 " $numDocs = readInt($file);\n"
126 " $docInfo = array();\n"
127 " // read docs info + occurrence frequency of the word\n"
128 " for ($i=0;$i<$numDocs;$i++)\n"
130 " $idx=readInt($file); \n"
131 " $freq=readInt($file); \n"
132 " $docInfo[$i]=array(\"idx\" => $idx,\n"
133 " \"freq\" => $freq>>1,\n"
134 " \"rank\" => 0.0,\n"
135 " \"hi\" => $freq&1\n"
137 " if ($freq&1) // word occurs in high priority doc\n"
140 " $totalFreqHi+=$freq*$multiplier;\n"
142 " else // word occurs in low priority doc\n"
144 " $totalFreqLo+=$freq*$multiplier;\n"
147 " // read name and url info for the doc\n"
148 " for ($i=0;$i<$numDocs;$i++)\n"
150 " fseek($file,$docInfo[$i][\"idx\"]);\n"
151 " $docInfo[$i][\"name\"]=readString($file);\n"
152 " $docInfo[$i][\"url\"]=readString($file);\n"
154 " $statInfo[\"docs\"]=$docInfo;\n"
156 " $totalFreq=($totalHi+1)*$totalFreqLo + $totalFreqHi;\n"
157 " for ($count=$start;$count<sizeof($statsList);$count++)\n"
159 " $statInfo = &$statsList[$count];\n"
160 " $multiplier = 1;\n"
161 " // whole word matches have a double weight\n"
162 " if ($statInfo[\"full\"]) $multiplier=2;\n"
163 " for ($i=0;$i<sizeof($statInfo[\"docs\"]);$i++)\n"
165 " $docInfo = &$statInfo[\"docs\"];\n"
166 " // compute frequency rank of the word in each doc\n"
167 " $freq=$docInfo[$i][\"freq\"];\n"
168 " if ($docInfo[$i][\"hi\"])\n"
170 " $statInfo[\"docs\"][$i][\"rank\"]=\n"
171 " (float)($freq*$multiplier+$totalFreqLo)/$totalFreq;\n"
175 " $statInfo[\"docs\"][$i][\"rank\"]=\n"
176 " (float)($freq*$multiplier)/$totalFreq;\n"
182 " return $statsList;\n"
185 "function combine_results($results,&$docs)\n"
187 " foreach ($results as $wordInfo)\n"
189 " $docsList = &$wordInfo[\"docs\"];\n"
190 " foreach ($docsList as $di)\n"
192 " $key=$di[\"url\"];\n"
193 " $rank=$di[\"rank\"];\n"
194 " if (isset($docs[$key]))\n"
196 " $docs[$key][\"rank\"]+=$rank;\n"
200 " $docs[$key] = array(\"url\"=>$key,\n"
201 " \"name\"=>$di[\"name\"],\n"
205 " $docs[$key][\"words\"][] = array(\n"
206 " \"word\"=>$wordInfo[\"word\"],\n"
207 " \"match\"=>$wordInfo[\"match\"],\n"
208 " \"freq\"=>$di[\"freq\"]\n"
215 "function filter_results($docs,&$requiredWords,&$forbiddenWords)\n"
217 " $filteredDocs=array();\n"
218 " while (list ($key, $val) = each ($docs)) \n"
220 " $words = &$docs[$key][\"words\"];\n"
221 " $copy=1; // copy entry by default\n"
222 " if (sizeof($requiredWords)>0)\n"
224 " foreach ($requiredWords as $reqWord)\n"
227 " foreach ($words as $wordInfo)\n"
229 " $found = $wordInfo[\"word\"]==$reqWord;\n"
230 " if ($found) break;\n"
234 " $copy=0; // document contains none of the required words\n"
239 " if (sizeof($forbiddenWords)>0)\n"
241 " foreach ($words as $wordInfo)\n"
243 " if (in_array($wordInfo[\"word\"],$forbiddenWords))\n"
245 " $copy=0; // document contains a forbidden word\n"
250 " if ($copy) $filteredDocs[$key]=$docs[$key];\n"
252 " return $filteredDocs;\n"
255 "function compare_rank($a,$b)\n"
257 " if ($a[\"rank\"] == $b[\"rank\"]) \n"
261 " return ($a[\"rank\"]>$b[\"rank\"]) ? -1 : 1; \n"
264 "function sort_results($docs,&$sorted)\n"
266 " $sorted = $docs;\n"
267 " usort($sorted,\"compare_rank\");\n"
271 "function report_results(&$docs)\n"
273 " echo \"<div class=\\\"header\\\">\";\n"
274 " echo \" <div class=\\\"headertitle\\\">\\n\";\n"
275 " echo \" <h1>\".search_results().\"</h1>\\n\";\n"
276 " echo \" </div>\\n\";\n"
277 " echo \"</div>\\n\";\n"
278 " echo \"<div class=\\\"searchresults\\\">\\n\";\n"
279 " echo \"<table cellspacing=\\\"2\\\">\\n\";\n"
280 " $numDocs = sizeof($docs);\n"
281 " if ($numDocs==0)\n"
283 " echo \" <tr>\\n\";\n"
284 " echo \" <td colspan=\\\"2\\\">\".matches_text(0).\"</td>\\n\";\n"
285 " echo \" </tr>\\n\";\n"
289 " echo \" <tr>\\n\";\n"
290 " echo \" <td colspan=\\\"2\\\">\".matches_text($numDocs);\n"
292 " echo \" </td>\\n\";\n"
293 " echo \" </tr>\\n\";\n"
295 " foreach ($docs as $doc)\n"
297 " echo \" <tr>\\n\";\n"
298 " echo \" <td align=\\\"right\\\">$num.</td>\";\n"
299 " echo \"<td><a class=\\\"el\\\" href=\\\"\".$doc[\"url\"].\"\\\">\".$doc[\"name\"].\"</a></td>\\n\";\n"
300 " echo \" <tr>\\n\";\n"
301 " echo \" <td></td><td class=\\\"tiny\\\">\".report_matches().\" \";\n"
302 " foreach ($doc[\"words\"] as $wordInfo)\n"
304 " $word = $wordInfo[\"word\"];\n"
305 " $matchRight = substr($wordInfo[\"match\"],strlen($word));\n"
306 " echo \"<b>$word</b>$matchRight(\".$wordInfo[\"freq\"].\") \";\n"
308 " echo \" </td>\\n\";\n"
309 " echo \" </tr>\\n\";\n"
313 " echo \"</table>\\n\";\n"
314 " echo \"</div>\\n\";\n"
317 "function run_query($query)\n"
319 " if(strcmp('4.1.0', phpversion()) > 0) \n"
321 " die(\"Error: PHP version 4.1.0 or above required!\");\n"
323 " if (!($file=fopen(\"search/search.idx\",\"rb\"))) \n"
325 " die(\"Error: Search index file could NOT be opened!\");\n"
327 " if (readHeader($file)!=\"DOXS\")\n"
329 " die(\"Error: Header of index file is invalid!\");\n"
331 " $results = array();\n"
332 " $requiredWords = array();\n"
333 " $forbiddenWords = array();\n"
334 " $foundWords = array();\n"
335 " $word=strtok($query,\" \");\n"
336 " while ($word) // for each word in the search query\n"
338 " if (($word{0}=='+')) { $word=substr($word,1); $requiredWords[]=$word; }\n"
339 " if (($word{0}=='-')) { $word=substr($word,1); $forbiddenWords[]=$word; }\n"
340 " if (!in_array($word,$foundWords))\n"
342 " $foundWords[]=$word;\n"
343 " search($file,strtolower($word),$results);\n"
345 " $word=strtok(\" \");\n"
348 " $docs = array();\n"
349 " combine_results($results,$docs);\n"
350 " // filter out documents with forbidden word or that do not contain\n"
351 " // required words\n"
352 " $filteredDocs = filter_results($docs,$requiredWords,$forbiddenWords);\n"
353 " // sort the results based on rank\n"
354 " $sorted = array();\n"
355 " sort_results($filteredDocs,$sorted);\n"
362 " if (array_key_exists(\"query\", $_GET))\n"
364 " $query=$_GET[\"query\"];\n"
366 " $sorted = run_query($query);\n"
367 " // Now output the HTML stuff...\n"
368 " // End the HTML form\n"
369 " end_form(preg_replace(\"/[^a-zA-Z0-9\\-\\_\\.]/i\", \" \", $query ));\n"
370 " // report results to the user\n"
371 " report_results($sorted);\n"