4 This plugins provides the internal page PowerSearch, which allows
5 to search in page contents and/or titles (or for author names, if any),
6 it tries to guess how good the database match matches the requested
7 search strings and orders results.
8 The top 10 results are printed more verbosely.
12 define("EWIKI_PAGE_POWERSEARCH", "PowerSearch");
13 $ewiki_plugins["page"][EWIKI_PAGE_POWERSEARCH] = "ewiki_page_powersearch";
14 $ewiki_plugins["action"]["search"] = "ewiki_action_powersearch";
17 function ewiki_action_powersearch(&$id, &$data, &$action) {
18 $o = ewiki_make_title(EWIKI_PAGE_POWERSEARCH, EWIKI_PAGE_POWERSEARCH, 2);
19 $o.= ewiki_powersearch($id);
24 function ewiki_page_powersearch($id, &$data, $action) {
27 ($where = preg_replace('/[^a-z]/', '', @$_REQUEST["where"]))
28 or ($where = "content");
30 $o = ewiki_make_title($id, $id, 2);
33 $o .= '<div class="search-form">
34 <form name="powersearch" action="' . ewiki_script("", $id) . '" method="GET">
35 <input type="hidden" name="id" value="'.$id.'">
36 <input type="text" id="q" name="q" size="30">
37 in <select name="where"><option value="content">page texts</option><option value="id">titles</option><option value="author">author names</option></select>
39 <input type="submit" value=" S E A R C H ">
41 <script type="text/javascript"><!--
42 document.powersearch.q.focus();
48 $o .= ewiki_powersearch($q, $where);
56 function ewiki_powersearch($q, $where='content'){
57 $q = ewiki_lowercase(preg_replace('/\s*[\000-\040]+\s*/', ' ', $q));
63 foreach (explode(" ", $q) as $search) {
69 $result = ewiki_db::SEARCH($where, $search);
71 while ($row = $result->get()) {
72 if (($row["flags"] & EWIKI_DB_F_TYPE) == EWIKI_DB_F_TEXT) {
75 $content = strtolower($row[$where]);
78 #-- have a closer look
79 $len1 = strlen($content) + 1;
81 if (!isset($scored[$id])) {
84 $scored[$id] += 800 * (strlen($search) / $len1);
85 $scored[$id] += 65 * (count(explode($search, $content)) - 2);
87 while (($p = strpos($content, $search, $p+1)) !== false) {
88 $scored[$id] += 80 * (1 - $p / $len1);
101 foreach ($scored as $id => $score) {
103 #-- refetch page for top 10 entries (still cached by OS or DB)
104 $row = ($n < 10) ? ewiki_db::GET($id) : NULL;
106 #-- check access rights in protected mode
107 if (EWIKI_PROTECTED_MODE && !ewiki_auth($id, $row, "view", $ring=false, $force=0)) {
108 if (EWIKI_PROTECTED_MODE_HIDING) {
111 $row["content"] = ewiki_t("FORBIDDEN");
116 $o .= '<div class="search-result '.($oe^=1?"odd":"even").'">'
117 . '<a href="' . ewiki_script("", $id) . '">' . $id . "</a> "
118 #<off># . "<small><small>(#$score)</small></small>"
121 #-- top 10 results are printed more verbosely
125 preg_match_all('/([_-\w]+)/', $row["content"], $uu);
126 $text = htmlentities(substr(implode(" ", $uu[1]), 0, 200));
127 $o .= "<br />\n<small>$text\n"
128 . "<br />" . strftime(ewiki_t("LASTCHANGED"), $row["lastmodified"])
129 . "<br /><br /></small>\n";