]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
Jeff's hacks II, continued:
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.1 2001-09-18 19:19:05 dairiki Exp $');
3
4 require_once('lib/TextSearchQuery.php');
5
6 /**
7  */
8 class WikiPlugin_FullTextSearch
9 extends WikiPlugin
10 {
11     var $name = 'FullTextSearch';
12
13     function getDefaultArguments() {
14         // FIXME: how to exclude multiple pages?
15         return array('s'                => false,
16                      'noheader'         => false);
17     }
18
19     function getDefaultFormArguments() {
20         $defaults = parent::getDefaultFormArguments();
21         $defaults['description'] = gettext('Full Text Search');
22         return $defaults;
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         while ($page = $pages->next()) {
41             $count++;
42             $name = $page->getName();
43             $lines[] = Element('dt', LinkExistingWikiWord($name));
44             if ($hilight_re)
45                 $lines[] = $this->showhits($page, $hilight_re);
46         }
47
48         $html = '';
49         if (!$noheader)
50             $html .= QElement('p',
51                               sprintf(gettext("Full text search results for '%s'"), $s));
52         if (!$lines)
53             $lines[] = QElement('dd', gettext("<no matches>"));
54
55         $html .= Element('dl', join("\n", $lines));
56         return $html;
57     }
58
59     function showhits($page, $hilight_re) {
60         $FS = &$GLOBALS['FieldSeparator'];
61         $current = $page->getCurrentRevision();
62         $matches = preg_grep("/$hilight_re/i", $current->getContent());
63         $html = '';
64         foreach ($matches as $line) {
65             $line = str_replace($FS, '', $line);
66             $line = preg_replace("/$hilight_re/i", "${FS}OT\\0${FS}CT", $line);
67             $line = htmlspecialchars($line);
68             $line = str_replace("${FS}OT", '<b>', $line);
69             $line = str_replace("${FS}CT", '</b>', $line);
70             $html .= Element('dd', Element('small', $line)) . "\n";
71         }
72         return $html;
73     }
74
75     function make_form($args) {
76         // FIXME: need more thought about this whole interface.
77         $args['search'] = '()';
78         return MakeWikiForm($GLOBALS['pagename'], $args, 'wikiaction','Full Text Search');
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 ?>