PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/axis2c-src-1.6.0/docs/api/html/search.php

#
PHP | 385 lines | 344 code | 19 blank | 22 comment | 42 complexity | 9014260ffa5756145b1e3265f9688f2e MD5 | raw file
Possible License(s): Apache-2.0
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  3. <title>Search</title>
  4. <link href="doxygen.css" rel="stylesheet" type="text/css">
  5. <link href="tabs.css" rel="stylesheet" type="text/css">
  6. </head><body>
  7. <!-- Generated by Doxygen 1.5.3 -->
  8. <div class="tabs">
  9. <ul>
  10. <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
  11. <li><a href="modules.html"><span>Modules</span></a></li>
  12. <li><a href="annotated.html"><span>Classes</span></a></li>
  13. <li><a href="files.html"><span>Files</span></a></li>
  14. <li><a href="dirs.html"><span>Directories</span></a></li>
  15. <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
  16. <li><a href="examples.html"><span>Examples</span></a></li>
  17. <li>
  18. <form action="search.php" method="get">
  19. <table cellspacing="0" cellpadding="0" border="0">
  20. <tr>
  21. <td><label>&nbsp;<u>S</u>earch&nbsp;for&nbsp;</label></td>
  22. <?php
  23. function search_results()
  24. {
  25. return "Search Results";
  26. }
  27. function matches_text($num)
  28. {
  29. if ($num==0)
  30. {
  31. return "Sorry, no documents matching your query.";
  32. }
  33. else if ($num==1)
  34. {
  35. return "Found <b>1</b> document matching your query.";
  36. }
  37. else // $num>1
  38. {
  39. return "Found <b>$num</b> documents matching your query. Showing best matches first.";
  40. }
  41. }
  42. function report_matches()
  43. {
  44. return "Matches: ";
  45. }
  46. function end_form($value)
  47. {
  48. echo " <td><input type=\"text\" name=\"query\" value=\"$value\" size=\"20\" accesskey=\"s\"/></td>\n </tr>\n </table>\n </form>\n </li>\n </ul>\n</div>\n";
  49. }
  50. function readInt($file)
  51. {
  52. $b1 = ord(fgetc($file)); $b2 = ord(fgetc($file));
  53. $b3 = ord(fgetc($file)); $b4 = ord(fgetc($file));
  54. return ($b1<<24)|($b2<<16)|($b3<<8)|$b4;
  55. }
  56. function readString($file)
  57. {
  58. $result="";
  59. while (ord($c=fgetc($file))) $result.=$c;
  60. return $result;
  61. }
  62. function readHeader($file)
  63. {
  64. $header =fgetc($file); $header.=fgetc($file);
  65. $header.=fgetc($file); $header.=fgetc($file);
  66. return $header;
  67. }
  68. function computeIndex($word)
  69. {
  70. // Fast string hashing
  71. //$lword = strtolower($word);
  72. //$l = strlen($lword);
  73. //for ($i=0;$i<$l;$i++)
  74. //{
  75. // $c = ord($lword{$i});
  76. // $v = (($v & 0xfc00) ^ ($v << 6) ^ $c) & 0xffff;
  77. //}
  78. //return $v;
  79. // Simple hashing that allows for substring search
  80. if (strlen($word)<2) return -1;
  81. // high char of the index
  82. $hi = ord($word{0});
  83. if ($hi==0) return -1;
  84. // low char of the index
  85. $lo = ord($word{1});
  86. if ($lo==0) return -1;
  87. // return index
  88. return $hi*256+$lo;
  89. }
  90. function search($file,$word,&$statsList)
  91. {
  92. $index = computeIndex($word);
  93. if ($index!=-1) // found a valid index
  94. {
  95. fseek($file,$index*4+4); // 4 bytes per entry, skip header
  96. $index = readInt($file);
  97. if ($index) // found words matching the hash key
  98. {
  99. $start=sizeof($statsList);
  100. $count=$start;
  101. fseek($file,$index);
  102. $w = readString($file);
  103. while ($w)
  104. {
  105. $statIdx = readInt($file);
  106. if ($word==substr($w,0,strlen($word)))
  107. { // found word that matches (as substring)
  108. $statsList[$count++]=array(
  109. "word"=>$word,
  110. "match"=>$w,
  111. "index"=>$statIdx,
  112. "full"=>strlen($w)==strlen($word),
  113. "docs"=>array()
  114. );
  115. }
  116. $w = readString($file);
  117. }
  118. $totalHi=0;
  119. $totalFreqHi=0;
  120. $totalFreqLo=0;
  121. for ($count=$start;$count<sizeof($statsList);$count++)
  122. {
  123. $statInfo = &$statsList[$count];
  124. $multiplier = 1;
  125. // whole word matches have a double weight
  126. if ($statInfo["full"]) $multiplier=2;
  127. fseek($file,$statInfo["index"]);
  128. $numDocs = readInt($file);
  129. $docInfo = array();
  130. // read docs info + occurrence frequency of the word
  131. for ($i=0;$i<$numDocs;$i++)
  132. {
  133. $idx=readInt($file);
  134. $freq=readInt($file);
  135. $docInfo[$i]=array("idx" => $idx,
  136. "freq" => $freq>>1,
  137. "rank" => 0.0,
  138. "hi" => $freq&1
  139. );
  140. if ($freq&1) // word occurs in high priority doc
  141. {
  142. $totalHi++;
  143. $totalFreqHi+=$freq*$multiplier;
  144. }
  145. else // word occurs in low priority doc
  146. {
  147. $totalFreqLo+=$freq*$multiplier;
  148. }
  149. }
  150. // read name and url info for the doc
  151. for ($i=0;$i<$numDocs;$i++)
  152. {
  153. fseek($file,$docInfo[$i]["idx"]);
  154. $docInfo[$i]["name"]=readString($file);
  155. $docInfo[$i]["url"]=readString($file);
  156. }
  157. $statInfo["docs"]=$docInfo;
  158. }
  159. $totalFreq=($totalHi+1)*$totalFreqLo + $totalFreqHi;
  160. for ($count=$start;$count<sizeof($statsList);$count++)
  161. {
  162. $statInfo = &$statsList[$count];
  163. $multiplier = 1;
  164. // whole word matches have a double weight
  165. if ($statInfo["full"]) $multiplier=2;
  166. for ($i=0;$i<sizeof($statInfo["docs"]);$i++)
  167. {
  168. $docInfo = &$statInfo["docs"];
  169. // compute frequency rank of the word in each doc
  170. $freq=$docInfo[$i]["freq"];
  171. if ($docInfo[$i]["hi"])
  172. {
  173. $statInfo["docs"][$i]["rank"]=
  174. (float)($freq*$multiplier+$totalFreqLo)/$totalFreq;
  175. }
  176. else
  177. {
  178. $statInfo["docs"][$i]["rank"]=
  179. (float)($freq*$multiplier)/$totalFreq;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. return $statsList;
  186. }
  187. function combine_results($results,&$docs)
  188. {
  189. foreach ($results as $wordInfo)
  190. {
  191. $docsList = &$wordInfo["docs"];
  192. foreach ($docsList as $di)
  193. {
  194. $key=$di["url"];
  195. $rank=$di["rank"];
  196. if (in_array($key, array_keys($docs)))
  197. {
  198. $docs[$key]["rank"]+=$rank;
  199. }
  200. else
  201. {
  202. $docs[$key] = array("url"=>$key,
  203. "name"=>$di["name"],
  204. "rank"=>$rank
  205. );
  206. }
  207. $docs[$key]["words"][] = array(
  208. "word"=>$wordInfo["word"],
  209. "match"=>$wordInfo["match"],
  210. "freq"=>$di["freq"]
  211. );
  212. }
  213. }
  214. return $docs;
  215. }
  216. function filter_results($docs,&$requiredWords,&$forbiddenWords)
  217. {
  218. $filteredDocs=array();
  219. while (list ($key, $val) = each ($docs))
  220. {
  221. $words = &$docs[$key]["words"];
  222. $copy=1; // copy entry by default
  223. if (sizeof($requiredWords)>0)
  224. {
  225. foreach ($requiredWords as $reqWord)
  226. {
  227. $found=0;
  228. foreach ($words as $wordInfo)
  229. {
  230. $found = $wordInfo["word"]==$reqWord;
  231. if ($found) break;
  232. }
  233. if (!$found)
  234. {
  235. $copy=0; // document contains none of the required words
  236. break;
  237. }
  238. }
  239. }
  240. if (sizeof($forbiddenWords)>0)
  241. {
  242. foreach ($words as $wordInfo)
  243. {
  244. if (in_array($wordInfo["word"],$forbiddenWords))
  245. {
  246. $copy=0; // document contains a forbidden word
  247. break;
  248. }
  249. }
  250. }
  251. if ($copy) $filteredDocs[$key]=$docs[$key];
  252. }
  253. return $filteredDocs;
  254. }
  255. function compare_rank($a,$b)
  256. {
  257. if ($a["rank"] == $b["rank"])
  258. {
  259. return 0;
  260. }
  261. return ($a["rank"]>$b["rank"]) ? -1 : 1;
  262. }
  263. function sort_results($docs,&$sorted)
  264. {
  265. $sorted = $docs;
  266. usort($sorted,"compare_rank");
  267. return $sorted;
  268. }
  269. function report_results(&$docs)
  270. {
  271. echo "<table cellspacing=\"2\">\n";
  272. echo " <tr>\n";
  273. echo " <td colspan=\"2\"><h2>".search_results()."</h2></td>\n";
  274. echo " </tr>\n";
  275. $numDocs = sizeof($docs);
  276. if ($numDocs==0)
  277. {
  278. echo " <tr>\n";
  279. echo " <td colspan=\"2\">".matches_text(0)."</td>\n";
  280. echo " </tr>\n";
  281. }
  282. else
  283. {
  284. echo " <tr>\n";
  285. echo " <td colspan=\"2\">".matches_text($numDocs);
  286. echo "\n";
  287. echo " </td>\n";
  288. echo " </tr>\n";
  289. $num=1;
  290. foreach ($docs as $doc)
  291. {
  292. echo " <tr>\n";
  293. echo " <td align=\"right\">$num.</td>";
  294. echo "<td><a class=\"el\" href=\"".$doc["url"]."\">".$doc["name"]."</a></td>\n";
  295. echo " <tr>\n";
  296. echo " <td></td><td class=\"tiny\">".report_matches()." ";
  297. foreach ($doc["words"] as $wordInfo)
  298. {
  299. $word = $wordInfo["word"];
  300. $matchRight = substr($wordInfo["match"],strlen($word));
  301. echo "<b>$word</b>$matchRight(".$wordInfo["freq"].") ";
  302. }
  303. echo " </td>\n";
  304. echo " </tr>\n";
  305. $num++;
  306. }
  307. }
  308. echo "</table>\n";
  309. }
  310. function main()
  311. {
  312. if(strcmp('4.1.0', phpversion()) > 0)
  313. {
  314. die("Error: PHP version 4.1.0 or above required!");
  315. }
  316. if (!($file=fopen("search.idx","rb")))
  317. {
  318. die("Error: Search index file could NOT be opened!");
  319. }
  320. if (readHeader($file)!="DOXS")
  321. {
  322. die("Error: Header of index file is invalid!");
  323. }
  324. $query="";
  325. if (array_key_exists("query", $_GET))
  326. {
  327. $query=$_GET["query"];
  328. }
  329. end_form($query);
  330. echo "&nbsp;\n<div class=\"searchresults\">\n";
  331. $results = array();
  332. $requiredWords = array();
  333. $forbiddenWords = array();
  334. $foundWords = array();
  335. $word=strtok($query," ");
  336. while ($word) // for each word in the search query
  337. {
  338. if (($word{0}=='+')) { $word=substr($word,1); $requiredWords[]=$word; }
  339. if (($word{0}=='-')) { $word=substr($word,1); $forbiddenWords[]=$word; }
  340. if (!in_array($word,$foundWords))
  341. {
  342. $foundWords[]=$word;
  343. search($file,strtolower($word),$results);
  344. }
  345. $word=strtok(" ");
  346. }
  347. $docs = array();
  348. combine_results($results,$docs);
  349. // filter out documents with forbidden word or that do not contain
  350. // required words
  351. $filteredDocs = filter_results($docs,$requiredWords,$forbiddenWords);
  352. // sort the results based on rank
  353. $sorted = array();
  354. sort_results($filteredDocs,$sorted);
  355. // report results to the user
  356. report_results($sorted);
  357. echo "</div>\n";
  358. fclose($file);
  359. }
  360. main();
  361. ?>
  362. <hr size="1"><address style="text-align: right;"><small>Generated on Fri Apr 17 11:49:48 2009 for Axis2/C by&nbsp;
  363. <a href="http://www.doxygen.org/index.html">
  364. <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address>
  365. </body>
  366. </html>