]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
more numeric pagename fixes.
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.21 2004-04-18 01:11:52 rurban Exp $');
3 /*
4 Copyright 1999,2000,2001,2002,2004 $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  * Simple case insensitive fulltext search
28  * TODO: case-sensitivity argument, regex argument
29  *
30  * See https://sourceforge.net/tracker/index.php?func=detail&aid=927395&group_id=6121&atid=106121
31  * Wordaround to let the dead locks occur somewhat later:
32  * increased the memory limit of PHP4 from 8 MB to 32 MB
33  * php.ini: memory_limit = 32 MB
34  */
35 class WikiPlugin_FullTextSearch
36 extends WikiPlugin
37 {
38     function getName() {
39         return _("FullTextSearch");
40     }
41
42     function getDescription() {
43         return _("Search the content of all pages in this wiki.");
44     }
45
46     function getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision: 1.21 $");
49     }
50
51     function getDefaultArguments() {
52         return array('s'        => false,
53                      'case_exact' => false,  //not yet supported
54                      'regex'    => false,    //not yet supported
55                      'noheader' => false,
56                      'exclude'  => false,  //comma-seperated list of glob
57                      'limit'    => false,
58                      'quiet'    => false); // be less verbose
59     }
60
61     function run($dbi, $argstr, &$request, $basepage) {
62
63         $args = $this->getArgs($argstr, $request);
64         if (empty($args['s']))
65             return '';
66
67         extract($args);
68
69         $query = new TextSearchQuery($s, $case_exact, $regex);
70         $pages = $dbi->fullSearch($query);
71         $lines = array();
72         $hilight_re = $query->getHighlightRegexp();
73         $count = 0;
74         $found = 0;
75
76         if ($quiet) { // see how easy it is with PageList...
77             $list = new PageList(false,$exclude,$args);
78             while ($page = $pages->next() and (!$limit or ($count < $limit))) {
79                 $list->addPage( $page );
80             }
81             return $list;
82         }
83
84         // Todo: we should better define a new PageListDL class for dl/dt/dd lists
85         // But the new column types must have a callback then. (showhits)
86         // See e.g. WikiAdminSearchReplace for custom pagelist columns
87         $list = HTML::dl();
88         if (!$limit or !is_int($limit))
89             $limit = 0;
90         // expand all page wildcards to a list of pages which should be ignored
91         if ($exclude) $exclude = explodePageList($exclude); 
92         while ($page = $pages->next() and (!$limit or ($count < $limit))) {
93             $name = $page->getName();
94             if ($exclude and in_array($name,$exclude)) continue;
95             $count++;
96             $list->pushContent(HTML::dt(WikiLink($name)));
97             if ($hilight_re)
98                 $list->pushContent($this->showhits($page, $hilight_re));
99             unset($page);
100         }
101         if ($limit and $count >= $limit) //todo: pager link to list of next matches
102             $list->pushContent(HTML::dd(fmt("only %d pages displayed",$limit)));
103         if (!$list->getContent())
104             $list->pushContent(HTML::dd(_("<no matches>")));
105
106         if ($noheader)
107             return $list;
108         return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
109                     $list);
110     }
111
112     function showhits($page, $hilight_re) {
113         $current = $page->getCurrentRevision();
114         $matches = preg_grep("/$hilight_re/i", $current->getContent());
115         $html = array();
116         foreach ($matches as $line) {
117             $line = $this->highlight_line($line, $hilight_re);
118             $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
119                                            $line));
120         }
121         return $html;
122     }
123
124     function highlight_line ($line, $hilight_re) {
125         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
126             $line = substr($line, strlen($m[0]));
127             $html[] = $m[1];    // prematch
128             $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
129         }
130         $html[] = $line;        // postmatch
131         return $html;
132     }
133 };
134
135 // $Log: not supported by cvs2svn $
136 // Revision 1.20  2004/02/28 21:14:08  rurban
137 // generally more PHPDOC docs
138 //   see http://xarch.tu-graz.ac.at/home/rurban/phpwiki/xref/
139 // fxied WikiUserNew pref handling: empty theme not stored, save only
140 //   changed prefs, sql prefs improved, fixed password update,
141 //   removed REPLACE sql (dangerous)
142 // moved gettext init after the locale was guessed
143 // + some minor changes
144 //
145 // Revision 1.19  2004/02/26 04:27:39  rurban
146 // wrong limit notification
147 //
148 // Revision 1.18  2004/02/26 04:24:03  rurban
149 // simplify quiet handling by using PageList
150 //
151 // Revision 1.17  2004/02/26 04:03:39  rurban
152 // added quiet, limit and exclude to FullTextSearch,
153 // fixed explodePageList with previously unloaded PageList
154 //
155 // Revision 1.16  2004/02/17 12:11:36  rurban
156 // 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, ...)
157 //
158 // Revision 1.15  2003/01/18 21:41:01  carstenklapp
159 // Code cleanup:
160 // Reformatting & tabs to spaces;
161 // Added copyleft, getVersion, getDescription, rcs_id.
162 //
163
164 // Local Variables:
165 // mode: php
166 // tab-width: 8
167 // c-basic-offset: 4
168 // c-hanging-comment-ender-p: nil
169 // indent-tabs-mode: nil
170 // End:
171 ?>