]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SearchHighlight.php
Remove history
[SourceForge/phpwiki.git] / lib / plugin / SearchHighlight.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
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
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 getName() {
39         return _("SearchHighlight");
40     }
41
42     function getDescription() {
43         return _("Hilight referred search terms.");
44     }
45
46     function getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision$");
49     }
50
51     function getDefaultArguments() {
52         // s, engine and engine_url are picked from the request
53         return array('noheader' => false,    //don't print the header
54                      'hits'     => false,    //print the list of lines with lines terms additionally
55                      's'        => false,
56                      'case_exact' => false,  //not yet supported
57                      'regex'    => false,    //not yet supported
58                      );
59     }
60
61     function run($dbi, $argstr, &$request, $basepage) {
62         $args = $this->getArgs($argstr, $request);
63         if (empty($args['s']) and isset($request->_searchhighlight)) {
64             $args['s'] = $request->_searchhighlight['query'];
65         }
66         if (empty($args['s']))
67             return '';
68         extract($args);
69         $html = HTML();
70         if (!$noheader and isset($request->_searchhighlight)) {
71             $engine = $request->_searchhighlight['engine'];
72             $html->pushContent(HTML::div(array('class' => 'search-context'),
73                                          fmt("%s: Found %s through %s", 
74                                              $basepage,
75                                              $request->_searchhighlight['query'], 
76                                              $engine)));
77         }
78         if ($hits) {
79             $query = new TextSearchQuery($s, $case_exact, $regex);
80             $lines = array();
81             $hilight_re = $query->getHighlightRegexp();
82             $page = $request->getPage();
83             $html->pushContent($this->showhits($page, $hilight_re));
84         }
85         return $html;
86     }
87
88     function showhits($page, $hilight_re) {
89         $current = $page->getCurrentRevision();
90         $matches = preg_grep("/$hilight_re/i", $current->getContent());
91         $html = HTML::dl();
92         foreach ($matches as $line) {
93             $line = $this->highlight_line($line, $hilight_re);
94             $html->pushContent(HTML::dd(array('class' => 'search-context'),
95                                         HTML::small($line)));
96         }
97         return $html;
98     }
99
100     function highlight_line ($line, $hilight_re) {
101         $html = HTML();
102         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
103             $line = substr($line, strlen($m[0]));
104             // prematch + match
105             $html->pushContent($m[1], HTML::strong(array('class' => 'search-term'), $m[2])); 
106         }
107         $html->pushContent($line);       // postmatch
108         return $html;
109     }
110 };
111
112 // Local Variables:
113 // mode: php
114 // tab-width: 8
115 // c-basic-offset: 4
116 // c-hanging-comment-ender-p: nil
117 // indent-tabs-mode: nil
118 // End:
119 ?>