]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
convert presentational markup to semantic markup
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.5 2002-01-10 23:23:39 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         // FIXME: how to exclude multiple pages?
21         return array('s'                => false,
22                      'noheader'         => false);
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         while ($page = $pages->next()) {
42             $count++;
43             $name = $page->getName();
44             $lines[] = Element('dt', LinkExistingWikiWord($name));
45             if ($hilight_re)
46                 $lines[] = $this->showhits($page, $hilight_re);
47         }
48
49         $html = '';
50         if (!$noheader)
51             $html .= QElement('p',
52                               sprintf(_("Full text search results for '%s'"), $s));
53         if (!$lines)
54             $lines[] = QElement('dd', _("<no matches>"));
55
56         $html .= Element('dl', join("\n", $lines));
57         return $html;
58     }
59
60     function showhits($page, $hilight_re) {
61         $FS = &$GLOBALS['FieldSeparator'];
62         $current = $page->getCurrentRevision();
63         $matches = preg_grep("/$hilight_re/i", $current->getContent());
64         $html = '';
65         foreach ($matches as $line) {
66             $line = str_replace($FS, '', $line);
67             $line = preg_replace("/$hilight_re/i", "${FS}OT\\0${FS}CT", $line);
68             $line = htmlspecialchars($line);
69             $line = str_replace("${FS}OT", '<strong>', $line);
70             $line = str_replace("${FS}CT", '</strong>', $line);
71             $html .= Element('dd', Element('small', $line)) . "\n";
72         }
73         return $html;
74     }
75 };
76         
77 // Local Variables:
78 // mode: php
79 // tab-width: 8
80 // c-basic-offset: 4
81 // c-hanging-comment-ender-p: nil
82 // indent-tabs-mode: nil
83 // End:   
84 ?>