]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
Added input 'size' arg to plugin-form.
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.12 2002-02-27 19:04:30 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                      'size'     => false, //default in WikiPlugin function makeForm
22                      'noheader' => false);
23         // TODO: multiple page exclude
24     }
25
26
27     function run($dbi, $argstr, $request) {
28
29         $args = $this->getArgs($argstr, $request);
30         if (empty($args['s']))
31             return '';
32
33         extract($args);
34
35         $query = new TextSearchQuery($s);
36         $pages = $dbi->fullSearch($query);
37         $lines = array();
38         $hilight_re = $query->getHighlightRegexp();
39         $count = 0;
40         $found = 0;
41
42         $list = HTML::dl();
43
44         while ($page = $pages->next()) {
45             $count++;
46             $name = $page->getName();
47             $list->pushContent(HTML::dt(WikiLink($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 HTML(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 ?>