]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
wrong limit notification
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.19 2004-02-26 04:27: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 require_once("lib/PageList.php");
25
26 /**
27  */
28 class WikiPlugin_FullTextSearch
29 extends WikiPlugin
30 {
31     function getName() {
32         return _("FullTextSearch");
33     }
34
35     function getDescription() {
36         return _("Search the content of all pages in this wiki.");
37     }
38
39     function getVersion() {
40         return preg_replace("/[Revision: $]/", '',
41                             "\$Revision: 1.19 $");
42     }
43
44     function getDefaultArguments() {
45         return array('s'        => false,
46                      'noheader' => false,
47                      'exclude'  => false,  //comma-seperated list of glob
48                      'limit'    => false,
49                      'quiet'    => false); // be less verbose
50     }
51
52     function run($dbi, $argstr, &$request, $basepage) {
53
54         $args = $this->getArgs($argstr, $request);
55         if (empty($args['s']))
56             return '';
57
58         extract($args);
59
60         $query = new TextSearchQuery($s);
61         $pages = $dbi->fullSearch($query);
62         $lines = array();
63         $hilight_re = $query->getHighlightRegexp();
64         $count = 0;
65         $found = 0;
66
67         if ($quiet) { // see how easy it is with PageList...
68             $list = new PageList(false,$exclude,$args);
69             while ($page = $pages->next() and (!$limit or ($count < $limit))) {
70                 $list->addPage( $page );
71             }
72             return $list;
73         }
74
75         // Todo: we should better define a new PageListDL class for dl/dt/dd lists
76         // But the new column types must have a callback then. (showhits)
77         $list = HTML::dl();
78         if (!$limit or !is_int($limit))
79             $limit = 0;
80         // expand all page wildcards to a list of pages which should be ignored
81         if ($exclude) $exclude = explodePageList($exclude); 
82         while ($page = $pages->next() and (!$limit or ($count < $limit))) {
83             $name = $page->getName();
84             if ($exclude and in_array($name,$exclude)) continue;
85             $count++;
86             $list->pushContent(HTML::dt(WikiLink($name)));
87             if ($hilight_re)
88                 $list->pushContent($this->showhits($page, $hilight_re));
89         }
90         if ($limit and $count >= $limit)
91             $list->pushContent(HTML::dd(fmt("only %d pages displayed",$limit)));
92         if (!$list->getContent())
93             $list->pushContent(HTML::dd(_("<no matches>")));
94
95         if ($noheader)
96             return $list;
97         return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
98                     $list);
99     }
100
101     function showhits($page, $hilight_re) {
102         $current = $page->getCurrentRevision();
103         $matches = preg_grep("/$hilight_re/i", $current->getContent());
104         $html = array();
105         foreach ($matches as $line) {
106             $line = $this->highlight_line($line, $hilight_re);
107             $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
108                                            $line));
109         }
110         return $html;
111     }
112
113     function highlight_line ($line, $hilight_re) {
114         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
115             $line = substr($line, strlen($m[0]));
116             $html[] = $m[1];    // prematch
117             $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
118         }
119         $html[] = $line;        // postmatch
120         return $html;
121     }
122 };
123
124 // $Log: not supported by cvs2svn $
125 // Revision 1.18  2004/02/26 04:24:03  rurban
126 // simplify quiet handling by using PageList
127 //
128 // Revision 1.17  2004/02/26 04:03:39  rurban
129 // added quiet, limit and exclude to FullTextSearch,
130 // fixed explodePageList with previously unloaded PageList
131 //
132 // Revision 1.16  2004/02/17 12:11:36  rurban
133 // 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, ...)
134 //
135 // Revision 1.15  2003/01/18 21:41:01  carstenklapp
136 // Code cleanup:
137 // Reformatting & tabs to spaces;
138 // Added copyleft, getVersion, getDescription, rcs_id.
139 //
140
141 // Local Variables:
142 // mode: php
143 // tab-width: 8
144 // c-basic-offset: 4
145 // c-hanging-comment-ender-p: nil
146 // indent-tabs-mode: nil
147 // End:
148 ?>