]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
added quiet, limit and exclude to FullTextSearch,
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.17 2004-02-26 04:03:39 rurban Exp $');
3 /*
4 Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5
6 This file is part of PhpWiki.
7
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22
23 require_once('lib/TextSearchQuery.php');
24
25 /**
26  */
27 class WikiPlugin_FullTextSearch
28 extends WikiPlugin
29 {
30     function getName() {
31         return _("FullTextSearch");
32     }
33
34     function getDescription() {
35         return _("Search the content of all pages in this wiki.");
36     }
37
38     function getVersion() {
39         return preg_replace("/[Revision: $]/", '',
40                             "\$Revision: 1.17 $");
41     }
42
43     function getDefaultArguments() {
44         return array('s'        => false,
45                      'noheader' => false,
46                      'exclude'  => false,  //comma-seperated list of glob
47                      'limit'    => false,
48                      'quiet'    => false); // be less verbose
49     }
50
51     function run($dbi, $argstr, &$request, $basepage) {
52
53         $args = $this->getArgs($argstr, $request);
54         if (empty($args['s']))
55             return '';
56
57         extract($args);
58
59         $query = new TextSearchQuery($s);
60         $pages = $dbi->fullSearch($query);
61         $lines = array();
62         $hilight_re = $query->getHighlightRegexp();
63         $count = 0;
64         $found = 0;
65
66         $list = $quiet ? HTML::ul() : HTML::dl();
67
68         if (!$limit or !is_int($limit))
69             $limit = 0;
70         // expand all page wildcards to a list of pages which should be ignored
71         if ($exclude) $exclude = explodePageList($exclude); 
72         while ($page = $pages->next() and (!$limit or ($count < $limit))) {
73             $name = $page->getName();
74             if ($exclude and in_array($name,$exclude)) continue;
75             $count++;
76             if ($quiet)
77                 $list->pushContent(HTML::li(WikiLink($name)));
78             else {
79                 $list->pushContent(HTML::dt(WikiLink($name)));
80                 if ($hilight_re)
81                     $list->pushContent($this->showhits($page, $hilight_re));
82             }
83         }
84         if (!$quiet) {
85             if ($count >= $limit)
86                 $list->pushContent(HTML::dd(fmt("only %d pages displayed",$limit)));
87             if (!$list->getContent())
88                 $list->pushContent(HTML::dd(_("<no matches>")));
89         }
90
91         if ($noheader)
92             return $list;
93
94         return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
95                     $list);
96     }
97
98     function showhits($page, $hilight_re) {
99         $current = $page->getCurrentRevision();
100         $matches = preg_grep("/$hilight_re/i", $current->getContent());
101         $html = array();
102         foreach ($matches as $line) {
103             $line = $this->highlight_line($line, $hilight_re);
104             $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
105                                            $line));
106         }
107         return $html;
108     }
109
110     function highlight_line ($line, $hilight_re) {
111         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
112             $line = substr($line, strlen($m[0]));
113             $html[] = $m[1];    // prematch
114             $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
115         }
116         $html[] = $line;        // postmatch
117         return $html;
118     }
119 };
120
121 // $Log: not supported by cvs2svn $
122 // Revision 1.16  2004/02/17 12:11:36  rurban
123 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
124 //
125 // Revision 1.15  2003/01/18 21:41:01  carstenklapp
126 // Code cleanup:
127 // Reformatting & tabs to spaces;
128 // Added copyleft, getVersion, getDescription, rcs_id.
129 //
130
131 // Local Variables:
132 // mode: php
133 // tab-width: 8
134 // c-basic-offset: 4
135 // c-hanging-comment-ender-p: nil
136 // indent-tabs-mode: nil
137 // End:
138 ?>