]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
Finish conversion to new OO HTML generation scheme.
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.7 2002-01-22 03:17:47 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     }
23
24         
25     function run($dbi, $argstr, $request) {
26
27         $args = $this->getArgs($argstr, $request);
28         if (empty($args['s']))
29             return '';
30
31         extract($args);
32         
33         $query = new TextSearchQuery($s);
34         $pages = $dbi->fullSearch($query);
35         $lines = array();
36         $hilight_re = $query->getHighlightRegexp();
37         $count = 0;
38         $found = 0;
39
40         $list = HTML::dl();
41         
42         while ($page = $pages->next()) {
43             $count++;
44             $name = $page->getName();
45             $list->pushContent(HTML::dt(LinkExistingWikiWord($name)));
46             if ($hilight_re)
47                 $list->pushContent($this->showhits($page, $hilight_re));
48         }
49         if (!$list->getContent())
50             $list->pushContent(HTML::dd(_("<no matches>")));
51
52         if ($noheader)
53             return $list;
54         
55         return array(HTML::p(fmt("Full text search results for '%s'", $s)),
56                      $list);
57     }
58
59     function showhits($page, $hilight_re) {
60         $current = $page->getCurrentRevision();
61         $matches = preg_grep("/$hilight_re/i", $current->getContent());
62         $html = array();
63         foreach ($matches as $line) {
64             $line = $this->highlight_line($line, $hilight_re);
65             $html[] = HTML::dd(HTML::small(false, $line));
66         }
67         return $html;
68     }
69
70     function highlight_line ($line, $hilight_re) {
71         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
72             $line = substr($line, strlen($m[0]));
73             $html[] = $m[1];    // prematch
74             $html[] = HTML::strong($m[2]); // match
75         }
76         $html[] = $line;        // postmatch
77         return $html;
78     }
79 };
80         
81 // Local Variables:
82 // mode: php
83 // tab-width: 8
84 // c-basic-offset: 4
85 // c-hanging-comment-ender-p: nil
86 // indent-tabs-mode: nil
87 // End:   
88 ?>