4 * Copyright 2004,2007 $ThePhpWikiProgrammingTeam
6 * This file is NOT part of PhpWiki.
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.
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.
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.
23 require_once 'lib/TextSearchQuery.php';
24 require_once 'lib/PageList.php';
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
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.
35 class WikiPlugin_SearchHighlight
38 function getDescription()
40 return _("Hilight referred search terms.");
43 function getDefaultArguments()
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
49 'case_exact' => false, //not yet supported
50 'regex' => false, //not yet supported
54 function run($dbi, $argstr, &$request, $basepage)
56 $args = $this->getArgs($argstr, $request);
57 if (empty($args['s']) and isset($request->_searchhighlight)) {
58 $args['s'] = $request->_searchhighlight['query'];
60 if (empty($args['s'])) {
65 if (!$noheader and isset($request->_searchhighlight)) {
66 $engine = $request->_searchhighlight['engine'];
67 $html->pushContent(HTML::div(array('class' => 'search-context'),
68 fmt("%s: Found %s through %s",
70 $request->_searchhighlight['query'],
74 $query = new TextSearchQuery($s, $case_exact, $regex);
75 $hilight_re = $query->getHighlightRegexp();
76 $page = $request->getPage();
77 $html->pushContent($this->showhits($page, $hilight_re));
82 function showhits($page, $hilight_re)
84 $current = $page->getCurrentRevision();
85 $matches = preg_grep("/$hilight_re/i", $current->getContent());
87 foreach ($matches as $line) {
88 $line = $this->highlight_line($line, $hilight_re);
89 $html->pushContent(HTML::dd(array('class' => 'search-context'),
95 function highlight_line($line, $hilight_re)
98 while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
99 $line = substr($line, strlen($m[0]));
101 $html->pushContent($m[1], HTML::strong(array('class' => 'search-term'), $m[2]));
103 $html->pushContent($line); // postmatch
112 // c-hanging-comment-ender-p: nil
113 // indent-tabs-mode: nil