]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
added comment TODO: multiple page exclude
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.9 2002-01-30 18:31:20 carstenklapp 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         global $Theme;
44         while ($page = $pages->next()) {
45             $count++;
46             $name = $page->getName();
47             $list->pushContent(HTML::dt($Theme->LinkExistingWikiWord($name)));
48             if ($hilight_re)
49                 $list->pushContent($this->showhits($page, $hilight_re));
50         }
51         if (!$list->getContent())
52             $list->pushContent(HTML::dd(_("<no matches>")));
53
54         if ($noheader)
55             return $list;
56         
57         return array(HTML::p(fmt("Full text search results for '%s'", $s)),
58                      $list);
59     }
60
61     function showhits($page, $hilight_re) {
62         $current = $page->getCurrentRevision();
63         $matches = preg_grep("/$hilight_re/i", $current->getContent());
64         $html = array();
65         foreach ($matches as $line) {
66             $line = $this->highlight_line($line, $hilight_re);
67             $html[] = HTML::dd(HTML::small(false, $line));
68         }
69         return $html;
70     }
71
72     function highlight_line ($line, $hilight_re) {
73         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
74             $line = substr($line, strlen($m[0]));
75             $html[] = $m[1];    // prematch
76             $html[] = HTML::strong($m[2]); // match
77         }
78         $html[] = $line;        // postmatch
79         return $html;
80     }
81 };
82         
83 // Local Variables:
84 // mode: php
85 // tab-width: 8
86 // c-basic-offset: 4
87 // c-hanging-comment-ender-p: nil
88 // indent-tabs-mode: nil
89 // End:   
90 ?>