]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
New css classes search-context and search-term.
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.14 2002-03-01 17:03:11 carstenklapp Exp $');
3 /*
4 Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5
6 This file is 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
25 /**
26  */
27 class WikiPlugin_FullTextSearch
28 extends WikiPlugin
29 {
30     function getName () {
31         return _("FullTextSearch");
32     }
33
34     function getDescription () {
35         return _("Full Text Search");
36     }
37
38     function getDefaultArguments() {
39         return array('s'        => false,
40                      'noheader' => false);
41         // TODO: multiple page exclude
42     }
43
44
45     function run($dbi, $argstr, $request) {
46
47         $args = $this->getArgs($argstr, $request);
48         if (empty($args['s']))
49             return '';
50
51         extract($args);
52
53         $query = new TextSearchQuery($s);
54         $pages = $dbi->fullSearch($query);
55         $lines = array();
56         $hilight_re = $query->getHighlightRegexp();
57         $count = 0;
58         $found = 0;
59
60         $list = HTML::dl();
61
62         while ($page = $pages->next()) {
63             $count++;
64             $name = $page->getName();
65             $list->pushContent(HTML::dt(WikiLink($name)));
66             if ($hilight_re)
67                 $list->pushContent($this->showhits($page, $hilight_re));
68         }
69         if (!$list->getContent())
70             $list->pushContent(HTML::dd(_("<no matches>")));
71
72         if ($noheader)
73             return $list;
74
75         return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
76                     $list);
77     }
78
79     function showhits($page, $hilight_re) {
80         $current = $page->getCurrentRevision();
81         $matches = preg_grep("/$hilight_re/i", $current->getContent());
82         $html = array();
83         foreach ($matches as $line) {
84             $line = $this->highlight_line($line, $hilight_re);
85             $html[] = HTML::dd(HTML::small(array('class' => 'search-context'), $line));
86         }
87         return $html;
88     }
89
90     function highlight_line ($line, $hilight_re) {
91         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
92             $line = substr($line, strlen($m[0]));
93             $html[] = $m[1];    // prematch
94             $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
95         }
96         $html[] = $line;        // postmatch
97         return $html;
98     }
99 };
100
101 // Local Variables:
102 // mode: php
103 // tab-width: 8
104 // c-basic-offset: 4
105 // c-hanging-comment-ender-p: nil
106 // indent-tabs-mode: nil
107 // End:
108 ?>