2 rcs_id('$Id: FullTextSearch.php,v 1.23 2004-11-23 15:17:19 rurban Exp $');
4 Copyright 1999,2000,2001,2002,2004 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
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.
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.
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
23 require_once('lib/TextSearchQuery.php');
24 require_once("lib/PageList.php");
27 * Simple case insensitive fulltext search
28 * TODO: case-sensitivity argument, regex argument
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
35 class WikiPlugin_FullTextSearch
39 return _("FullTextSearch");
42 function getDescription() {
43 return _("Search the content of all pages in this wiki.");
46 function getVersion() {
47 return preg_replace("/[Revision: $]/", '',
48 "\$Revision: 1.23 $");
51 function getDefaultArguments() {
54 PageList::supportedArgs(), // paging and more.
57 'case_exact' => false, //not yet supported
58 'regex' => false, //not yet supported
60 'exclude' => false, //comma-seperated list of glob
62 'quiet' => false)); // be less verbose
65 function run($dbi, $argstr, &$request, $basepage) {
67 $args = $this->getArgs($argstr, $request);
68 if (empty($args['s']))
72 $query = new TextSearchQuery($s, $case_exact, $regex);
73 $pages = $dbi->fullSearch($query);
75 $hilight_re = $hilight ? $query->getHighlightRegexp() : false;
79 if ($quiet) { // see how easy it is with PageList...
80 $list = new PageList(false,$exclude,$args);
81 while ($page = $pages->next() and (!$limit or ($count < $limit))) {
82 $list->addPage( $page );
87 // Todo: we should better define a new PageListDL class for dl/dt/dd lists
88 // But the new column types must have a callback then. (showhits)
89 // See e.g. WikiAdminSearchReplace for custom pagelist columns
91 if (!$limit or !is_int($limit))
93 // expand all page wildcards to a list of pages which should be ignored
94 if ($exclude) $exclude = explodePageList($exclude);
95 while ($page = $pages->next() and (!$limit or ($count < $limit))) {
96 $name = $page->getName();
97 if ($exclude and in_array($name,$exclude)) continue;
99 $list->pushContent(HTML::dt(WikiLink($name)));
101 $list->pushContent($this->showhits($page, $hilight_re));
104 if ($limit and $count >= $limit) //todo: pager link to list of next matches
105 $list->pushContent(HTML::dd(fmt("only %d pages displayed",$limit)));
106 if (!$list->getContent())
107 $list->pushContent(HTML::dd(_("<no matches>")));
111 return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
115 function showhits($page, $hilight_re) {
116 $current = $page->getCurrentRevision();
117 $matches = preg_grep("/$hilight_re/i", $current->getContent());
119 foreach ($matches as $line) {
120 $line = $this->highlight_line($line, $hilight_re);
121 $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
127 function highlight_line ($line, $hilight_re) {
128 while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
129 $line = substr($line, strlen($m[0]));
130 $html[] = $m[1]; // prematch
131 $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
133 $html[] = $line; // postmatch
138 // $Log: not supported by cvs2svn $
139 // Revision 1.22 2004/05/28 11:01:58 rurban
140 // support to disable highlighting
141 // example: s=ReiniUrban&hilight=0&noheader=1
143 // Revision 1.21 2004/04/18 01:11:52 rurban
144 // more numeric pagename fixes.
145 // fixed action=upload with merge conflict warnings.
146 // charset changed from constant to global (dynamic utf-8 switching)
148 // Revision 1.20 2004/02/28 21:14:08 rurban
149 // generally more PHPDOC docs
150 // see http://xarch.tu-graz.ac.at/home/rurban/phpwiki/xref/
151 // fxied WikiUserNew pref handling: empty theme not stored, save only
152 // changed prefs, sql prefs improved, fixed password update,
153 // removed REPLACE sql (dangerous)
154 // moved gettext init after the locale was guessed
155 // + some minor changes
157 // Revision 1.19 2004/02/26 04:27:39 rurban
158 // wrong limit notification
160 // Revision 1.18 2004/02/26 04:24:03 rurban
161 // simplify quiet handling by using PageList
163 // Revision 1.17 2004/02/26 04:03:39 rurban
164 // added quiet, limit and exclude to FullTextSearch,
165 // fixed explodePageList with previously unloaded PageList
167 // Revision 1.16 2004/02/17 12:11:36 rurban
168 // 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, ...)
170 // Revision 1.15 2003/01/18 21:41:01 carstenklapp
172 // Reformatting & tabs to spaces;
173 // Added copyleft, getVersion, getDescription, rcs_id.
180 // c-hanging-comment-ender-p: nil
181 // indent-tabs-mode: nil