changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / filter / search_highlight.php
1 <?php
2
3 /*
4    CSS-highlights the terms used as search patterns. This is done
5    by evaluating the REFERRER and using the QUERY_STRINGs "q="
6    parameter (which is used by Google and ewikis` PowerSearch).
7
8    Using this plugin costs you nearly nothing (not slower), because
9    there most often isn't a "?q=" from a search engine in the referer
10    url.
11
12    Modified 20040811 by Jochen Schuh
13      
14    Missing feature:
15      Different search words are now marked different so they could 
16      (but must not) styled individually.
17      
18    CSS styling examples:
19    - I prefer to highlight only in the contents of the page, not in the
20      menu or the list of modules or the footer. This is done with:
21           em {
22             font-style:normal;
23           }
24    - To style all of the searchwords the same you can use this rule. Using
25      only this rule would be enough to style all searchwords. But also if
26      you want to style individually such a styling rule is heavily
27      recommended as fallback to style a possible 10. or 42. searchword:
28           .text-body em.highlight {
29             border-top: solid;
30             border-bottom: solid;
31             border-width: 1px;
32             padding-top: 1px;
33             padding-bottom: 1px;
34             border-color: #000000;
35             background-color: #EEEEEE;
36           }
37    - The first few searchwords _can_ easily styled different with:
38           .text-body em.searchword-0 {
39             border-color: #0066FF;
40             background-color: #E8F4FF;
41           }
42           .text-body em.searchword-1 {
43             border-color: #FF6600;
44             background-color: #FFF4E8;
45           }
46 */
47
48
49 $ewiki_plugins["page_final"][] = "ewiki_search_highlight";
50
51
52 function ewiki_search_highlight(&$o, &$id, &$data, &$action) {
53
54    $ref = $_SERVER["HTTP_REFERER"];
55    if (strpos($ref, "q=") || strpos($ref, "search=")) {
56
57       #-- get ?q=...
58       $uu = $ref;
59       $uu = substr($uu, strpos($uu, "?"));
60       parse_str($uu, $q);
61       if (($q = $q["q"]) || ($q = $q["search"])) {
62
63          #-- get words out of it
64          $q = preg_replace('/[^-_\d'.EWIKI_CHARS_L.EWIKI_CHARS_U.']+/', " ", $q);
65          $q = array_unique(explode(" ", $q));
66
67          #-- walk through words
68          foreach ($q as $key => $word) {
69
70             if (empty($word)) {
71                continue;
72             }
73
74             while ($l = strpos(strtolower($o), strtolower($word), $l)) {
75
76                #-- check for html-tags
77                $t0 = strpos($o, "<", $l);
78                $t1 = strpos($o, ">", $l);
79                $found = substr($o, $l, strlen($word));
80                if ((!$t0) || ($t0 < $t1)) {
81
82                   $repl = '<em class="highlight marker searchword-' . $key . '">' . $found . '</em>';
83                   $o = substr($o, 0, $l)
84                      . $repl
85                      . substr($o, $l + strlen($word));
86
87                   $l += strlen($repl);
88                }
89
90                $l++;   // advance strpos
91             }
92
93          } // foreach(word)
94
95       }
96
97    } // if(q)
98
99 } // func
100
101 ?>