Fix for UBSan build
[platform/upstream/doxygen.git] / src / search_functions_php.h
1 "<script language=\"PHP\">\n"
2 "require_once \"search-config.php\";\n"
3 "\n"
4 "function end_form($value)\n"
5 "{\n"
6 "  global $config;\n"
7 "  global $translator;\n"
8 "  if ($config['DISABLE_INDEX'] == false)\n"
9 "  {\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"
11 "  }\n"
12 "  if ($config['GENERATE_TREEVIEW'])\n"
13 "  {\n"
14 "    echo $translator['split_bar'];\n"
15 "  }\n"
16 "}\n"
17 "\n"
18 "function end_page()\n"
19 "{\n"
20 "  //global $config;\n"
21 "  //global $translator;\n"
22 "  //if ($config['GENERATE_TREEVIEW'])\n"
23 "  //{\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"
27 "  //}\n"
28 "  echo \"</body></html>\";\n"
29 "}\n"
30 "\n"
31 "function search_results()\n"
32 "{\n"
33 "  global $translator;\n"
34 "  return $translator['search_results_title'];\n"
35 "}\n"
36 "\n"
37 "function matches_text($num)\n"
38 "{\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"
43 "  return $result;\n"
44 "}\n"
45 "\n"
46 "function report_matches()\n"
47 "{\n"
48 "  global $translator;\n"
49 "  return $translator['search_matches'];\n"
50 "}\n"
51 "\n"
52 "function readInt($file)\n"
53 "{\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"
57 "}\n"
58 "\n"
59 "function readString($file)\n"
60 "{\n"
61 "  $result=\"\";\n"
62 "  while (ord($c=fgetc($file))) $result.=$c;\n"
63 "  return $result;\n"
64 "}\n"
65 "\n"
66 "function readHeader($file)\n"
67 "{\n"
68 "  $header =fgetc($file); $header.=fgetc($file);\n"
69 "  $header.=fgetc($file); $header.=fgetc($file);\n"
70 "  return $header;\n"
71 "}\n"
72 "\n"
73 "function computeIndex($word)\n"
74 "{\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"
83 "  // return index\n"
84 "  return $hi*256+$lo;\n"
85 "}\n"
86 "\n"
87 "function search($file,$word,&$statsList)\n"
88 "{\n"
89 "  $index = computeIndex($word);\n"
90 "  if ($index!=-1) // found a valid index\n"
91 "  {\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"
95 "    {\n"
96 "      $start=sizeof($statsList);\n"
97 "      $count=$start;\n"
98 "      fseek($file,$index);\n"
99 "      $w = readString($file);\n"
100 "      while ($w)\n"
101 "      {\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"
107 "              \"match\"=>$w,\n"
108 "              \"index\"=>$statIdx,\n"
109 "              \"full\"=>strlen($w)==strlen($word),\n"
110 "              \"docs\"=>array()\n"
111 "              );\n"
112 "        }\n"
113 "        $w = readString($file);\n"
114 "      }\n"
115 "      $totalHi=0;\n"
116 "      $totalFreqHi=0;\n"
117 "      $totalFreqLo=0;\n"
118 "      for ($count=$start;$count<sizeof($statsList);$count++)\n"
119 "      {\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"
129 "        {\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"
136 "                            );\n"
137 "          if ($freq&1) // word occurs in high priority doc\n"
138 "          {\n"
139 "            $totalHi++;\n"
140 "            $totalFreqHi+=$freq*$multiplier;\n"
141 "          }\n"
142 "          else // word occurs in low priority doc\n"
143 "          {\n"
144 "            $totalFreqLo+=$freq*$multiplier;\n"
145 "          }\n"
146 "        }\n"
147 "        // read name and url info for the doc\n"
148 "        for ($i=0;$i<$numDocs;$i++)\n"
149 "        {\n"
150 "          fseek($file,$docInfo[$i][\"idx\"]);\n"
151 "          $docInfo[$i][\"name\"]=readString($file);\n"
152 "          $docInfo[$i][\"url\"]=readString($file);\n"
153 "        }\n"
154 "        $statInfo[\"docs\"]=$docInfo;\n"
155 "      }\n"
156 "      $totalFreq=($totalHi+1)*$totalFreqLo + $totalFreqHi;\n"
157 "      for ($count=$start;$count<sizeof($statsList);$count++)\n"
158 "      {\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"
164 "        {\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"
169 "          {\n"
170 "            $statInfo[\"docs\"][$i][\"rank\"]=\n"
171 "              (float)($freq*$multiplier+$totalFreqLo)/$totalFreq;\n"
172 "          }\n"
173 "          else\n"
174 "          {\n"
175 "            $statInfo[\"docs\"][$i][\"rank\"]=\n"
176 "              (float)($freq*$multiplier)/$totalFreq;\n"
177 "          }\n"
178 "        }\n"
179 "      }\n"
180 "    }\n"
181 "  }\n"
182 "  return $statsList;\n"
183 "}\n"
184 "\n"
185 "function combine_results($results,&$docs)\n"
186 "{\n"
187 "  foreach ($results as $wordInfo)\n"
188 "  {\n"
189 "    $docsList = &$wordInfo[\"docs\"];\n"
190 "    foreach ($docsList as $di)\n"
191 "    {\n"
192 "      $key=$di[\"url\"];\n"
193 "      $rank=$di[\"rank\"];\n"
194 "      if (isset($docs[$key]))\n"
195 "      {\n"
196 "        $docs[$key][\"rank\"]+=$rank;\n"
197 "      }\n"
198 "      else\n"
199 "      {\n"
200 "        $docs[$key] = array(\"url\"=>$key,\n"
201 "            \"name\"=>$di[\"name\"],\n"
202 "            \"rank\"=>$rank\n"
203 "            );\n"
204 "      }\n"
205 "      $docs[$key][\"words\"][] = array(\n"
206 "               \"word\"=>$wordInfo[\"word\"],\n"
207 "               \"match\"=>$wordInfo[\"match\"],\n"
208 "               \"freq\"=>$di[\"freq\"]\n"
209 "               );\n"
210 "    }\n"
211 "  }\n"
212 "  return $docs;\n"
213 "}\n"
214 "\n"
215 "function filter_results($docs,&$requiredWords,&$forbiddenWords)\n"
216 "{\n"
217 "  $filteredDocs=array();\n"
218 "  while (list ($key, $val) = each ($docs)) \n"
219 "  {\n"
220 "    $words = &$docs[$key][\"words\"];\n"
221 "    $copy=1; // copy entry by default\n"
222 "    if (sizeof($requiredWords)>0)\n"
223 "    {\n"
224 "      foreach ($requiredWords as $reqWord)\n"
225 "      {\n"
226 "        $found=0;\n"
227 "        foreach ($words as $wordInfo)\n"
228 "        { \n"
229 "          $found = $wordInfo[\"word\"]==$reqWord;\n"
230 "          if ($found) break;\n"
231 "        }\n"
232 "        if (!$found) \n"
233 "        {\n"
234 "          $copy=0; // document contains none of the required words\n"
235 "          break;\n"
236 "        }\n"
237 "      }\n"
238 "    }\n"
239 "    if (sizeof($forbiddenWords)>0)\n"
240 "    {\n"
241 "      foreach ($words as $wordInfo)\n"
242 "      {\n"
243 "        if (in_array($wordInfo[\"word\"],$forbiddenWords))\n"
244 "        {\n"
245 "          $copy=0; // document contains a forbidden word\n"
246 "          break;\n"
247 "        }\n"
248 "      }\n"
249 "    }\n"
250 "    if ($copy) $filteredDocs[$key]=$docs[$key];\n"
251 "  }\n"
252 "  return $filteredDocs;\n"
253 "}\n"
254 "\n"
255 "function compare_rank($a,$b)\n"
256 "{\n"
257 "  if ($a[\"rank\"] == $b[\"rank\"]) \n"
258 "  {\n"
259 "    return 0;\n"
260 "  }\n"
261 "  return ($a[\"rank\"]>$b[\"rank\"]) ? -1 : 1; \n"
262 "}\n"
263 "\n"
264 "function sort_results($docs,&$sorted)\n"
265 "{\n"
266 "  $sorted = $docs;\n"
267 "  usort($sorted,\"compare_rank\");\n"
268 "  return $sorted;\n"
269 "}\n"
270 "\n"
271 "function report_results(&$docs)\n"
272 "{\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"
282 "  {\n"
283 "    echo \"  <tr>\\n\";\n"
284 "    echo \"    <td colspan=\\\"2\\\">\".matches_text(0).\"</td>\\n\";\n"
285 "    echo \"  </tr>\\n\";\n"
286 "  }\n"
287 "  else\n"
288 "  {\n"
289 "    echo \"  <tr>\\n\";\n"
290 "    echo \"    <td colspan=\\\"2\\\">\".matches_text($numDocs);\n"
291 "    echo \"\\n\";\n"
292 "    echo \"    </td>\\n\";\n"
293 "    echo \"  </tr>\\n\";\n"
294 "    $num=1;\n"
295 "    foreach ($docs as $doc)\n"
296 "    {\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"
303 "      {\n"
304 "        $word = $wordInfo[\"word\"];\n"
305 "        $matchRight = substr($wordInfo[\"match\"],strlen($word));\n"
306 "        echo \"<b>$word</b>$matchRight(\".$wordInfo[\"freq\"].\") \";\n"
307 "      }\n"
308 "      echo \"    </td>\\n\";\n"
309 "      echo \"  </tr>\\n\";\n"
310 "      $num++;\n"
311 "    }\n"
312 "  }\n"
313 "  echo \"</table>\\n\";\n"
314 "  echo \"</div>\\n\";\n"
315 "}\n"
316 "\n"
317 "function run_query($query)\n"
318 "{\n"
319 "  if(strcmp('4.1.0', phpversion()) > 0) \n"
320 "  {\n"
321 "    die(\"Error: PHP version 4.1.0 or above required!\");\n"
322 "  }\n"
323 "  if (!($file=fopen(\"search/search.idx\",\"rb\"))) \n"
324 "  {\n"
325 "    die(\"Error: Search index file could NOT be opened!\");\n"
326 "  }\n"
327 "  if (readHeader($file)!=\"DOXS\")\n"
328 "  {\n"
329 "    die(\"Error: Header of index file is invalid!\");\n"
330 "  }\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"
337 "  {\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"
341 "    {\n"
342 "      $foundWords[]=$word;\n"
343 "      search($file,strtolower($word),$results);\n"
344 "    }\n"
345 "    $word=strtok(\" \");\n"
346 "  }\n"
347 "  fclose($file);\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"
356 "  return $sorted;\n"
357 "}\n"
358 "\n"
359 "function main()\n"
360 "{\n"
361 "  $query = \"\";\n"
362 "  if (array_key_exists(\"query\", $_GET))\n"
363 "  {\n"
364 "    $query=$_GET[\"query\"];\n"
365 "  }\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"
372 "  end_page();\n"
373 "}\n"
374 "</script>\n"