]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
Two new convenience functions in lib/Theme.php: WikiLink() and Button().
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.10 2002-01-30 23:41:54 dairiki Exp $');
3
4 require_once('lib/TextSearchQuery.php');
5
6 /**
7  */
8 class WikiPlugin_FullTextSearch
9 extends WikiPlugin
10 {
11     function getName () {
12         return _("FullTextSearch");
13     }
14
15     function getDescription () {
16         return _("Full Text Search");
17     }
18
19     function getDefaultArguments() {
20         return array('s'        => false,
21                      'noheader' => false);
22         // TODO: multiple page exclude
23     }
24
25         
26     function run($dbi, $argstr, $request) {
27
28         $args = $this->getArgs($argstr, $request);
29         if (empty($args['s']))
30             return '';
31
32         extract($args);
33         
34         $query = new TextSearchQuery($s);
35         $pages = $dbi->fullSearch($query);
36         $lines = array();
37         $hilight_re = $query->getHighlightRegexp();
38         $count = 0;
39         $found = 0;
40
41         $list = HTML::dl();
42
43         while ($page = $pages->next()) {
44             $count++;
45             $name = $page->getName();
46             $list->pushContent(HTML::dt(WikiLink($name)));
47             if ($hilight_re)
48                 $list->pushContent($this->showhits($page, $hilight_re));
49         }
50         if (!$list->getContent())
51             $list->pushContent(HTML::dd(_("<no matches>")));
52
53         if ($noheader)
54             return $list;
55         
56         return array(HTML::p(fmt("Full text search results for '%s'", $s)),
57                      $list);
58     }
59
60     function showhits($page, $hilight_re) {
61         $current = $page->getCurrentRevision();
62         $matches = preg_grep("/$hilight_re/i", $current->getContent());
63         $html = array();
64         foreach ($matches as $line) {
65             $line = $this->highlight_line($line, $hilight_re);
66             $html[] = HTML::dd(HTML::small(false, $line));
67         }
68         return $html;
69     }
70
71     function highlight_line ($line, $hilight_re) {
72         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
73             $line = substr($line, strlen($m[0]));
74             $html[] = $m[1];    // prematch
75             $html[] = HTML::strong($m[2]); // match
76         }
77         $html[] = $line;        // postmatch
78         return $html;
79     }
80 };
81         
82 // Local Variables:
83 // mode: php
84 // tab-width: 8
85 // c-basic-offset: 4
86 // c-hanging-comment-ender-p: nil
87 // indent-tabs-mode: nil
88 // End:   
89 ?>