]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SearchHighlight.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / SearchHighlight.php
1 <?php
2
3 /*
4  * Copyright 2004,2007 $ThePhpWikiProgrammingTeam
5  *
6  * This file is NOT part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 require_once 'lib/TextSearchQuery.php';
24 require_once 'lib/PageList.php';
25
26 /**
27  * When someone is referred from a search engine like Google, Yahoo
28  * or our own fulltextsearch, the terms they search for are highlighted.
29  * See http://wordpress.org/about/shots/1.2/plugins.png
30  *
31  * This plugin is normally just used to print a header through an action page.
32  * The highlighting is done through InlineParser automatically if ENABLE_SEARCHHIGHLIGHT is enabled.
33  * If hits = 1, then the list of found terms is also printed.
34  */
35 class WikiPlugin_SearchHighlight
36     extends WikiPlugin
37 {
38     function getDescription()
39     {
40         return _("Hilight referred search terms.");
41     }
42
43     function getDefaultArguments()
44     {
45         // s, engine and engine_url are picked from the request
46         return array('noheader' => false, //don't print the header
47             'hits' => false, //print the list of lines with lines terms additionally
48             's' => false,
49             'case_exact' => false, //not yet supported
50             'regex' => false, //not yet supported
51         );
52     }
53
54     /**
55      * @param WikiDB $dbi
56      * @param string $argstr
57      * @param WikiRequest $request
58      * @param string $basepage
59      * @return mixed
60      */
61     function run($dbi, $argstr, &$request, $basepage)
62     {
63         $args = $this->getArgs($argstr, $request);
64         if (empty($args['s']) and isset($request->_searchhighlight)) {
65             $args['s'] = $request->_searchhighlight['query'];
66         }
67         if (empty($args['s'])) {
68             return HTML();
69         }
70         extract($args);
71         $html = HTML();
72         if (!$noheader and isset($request->_searchhighlight)) {
73             $engine = $request->_searchhighlight['engine'];
74             $html->pushContent(HTML::div(array('class' => 'search-context'),
75                 fmt("%s: Found %s through %s",
76                     $basepage,
77                     $request->_searchhighlight['query'],
78                     $engine)));
79         }
80         if ($hits) {
81             $query = new TextSearchQuery($s, $case_exact, $regex);
82             $hilight_re = $query->getHighlightRegexp();
83             $page = $request->getPage();
84             $html->pushContent($this->showhits($page, $hilight_re));
85         }
86         return $html;
87     }
88
89     function showhits($page, $hilight_re)
90     {
91         $current = $page->getCurrentRevision();
92         $matches = preg_grep("/$hilight_re/i", $current->getContent());
93         $html = HTML::dl();
94         foreach ($matches as $line) {
95             $line = $this->highlight_line($line, $hilight_re);
96             $html->pushContent(HTML::dd(array('class' => 'search-context'),
97                 HTML::small($line)));
98         }
99         return $html;
100     }
101
102     function highlight_line($line, $hilight_re)
103     {
104         $html = HTML();
105         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
106             $line = substr($line, strlen($m[0]));
107             // prematch + match
108             $html->pushContent($m[1], HTML::strong(array('class' => 'search-term'), $m[2]));
109         }
110         $html->pushContent($line); // postmatch
111         return $html;
112     }
113 }
114
115 // Local Variables:
116 // mode: php
117 // tab-width: 8
118 // c-basic-offset: 4
119 // c-hanging-comment-ender-p: nil
120 // indent-tabs-mode: nil
121 // End: