2 rcs_id('$Id: FullTextSearch.php,v 1.21 2004-04-18 01:11:52 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.21 $");
51 function getDefaultArguments() {
52 return array('s' => false,
53 'case_exact' => false, //not yet supported
54 'regex' => false, //not yet supported
56 'exclude' => false, //comma-seperated list of glob
58 'quiet' => false); // be less verbose
61 function run($dbi, $argstr, &$request, $basepage) {
63 $args = $this->getArgs($argstr, $request);
64 if (empty($args['s']))
69 $query = new TextSearchQuery($s, $case_exact, $regex);
70 $pages = $dbi->fullSearch($query);
72 $hilight_re = $query->getHighlightRegexp();
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 );
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
88 if (!$limit or !is_int($limit))
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;
96 $list->pushContent(HTML::dt(WikiLink($name)));
98 $list->pushContent($this->showhits($page, $hilight_re));
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>")));
108 return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
112 function showhits($page, $hilight_re) {
113 $current = $page->getCurrentRevision();
114 $matches = preg_grep("/$hilight_re/i", $current->getContent());
116 foreach ($matches as $line) {
117 $line = $this->highlight_line($line, $hilight_re);
118 $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
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
130 $html[] = $line; // postmatch
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
145 // Revision 1.19 2004/02/26 04:27:39 rurban
146 // wrong limit notification
148 // Revision 1.18 2004/02/26 04:24:03 rurban
149 // simplify quiet handling by using PageList
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
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, ...)
158 // Revision 1.15 2003/01/18 21:41:01 carstenklapp
160 // Reformatting & tabs to spaces;
161 // Added copyleft, getVersion, getDescription, rcs_id.
168 // c-hanging-comment-ender-p: nil
169 // indent-tabs-mode: nil