2 rcs_id('$Id: FullTextSearch.php,v 1.19 2004-02-26 04:27:39 rurban Exp $');
4 Copyright 1999, 2000, 2001, 2002 $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");
28 class WikiPlugin_FullTextSearch
32 return _("FullTextSearch");
35 function getDescription() {
36 return _("Search the content of all pages in this wiki.");
39 function getVersion() {
40 return preg_replace("/[Revision: $]/", '',
41 "\$Revision: 1.19 $");
44 function getDefaultArguments() {
45 return array('s' => false,
47 'exclude' => false, //comma-seperated list of glob
49 'quiet' => false); // be less verbose
52 function run($dbi, $argstr, &$request, $basepage) {
54 $args = $this->getArgs($argstr, $request);
55 if (empty($args['s']))
60 $query = new TextSearchQuery($s);
61 $pages = $dbi->fullSearch($query);
63 $hilight_re = $query->getHighlightRegexp();
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 );
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)
78 if (!$limit or !is_int($limit))
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;
86 $list->pushContent(HTML::dt(WikiLink($name)));
88 $list->pushContent($this->showhits($page, $hilight_re));
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>")));
97 return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
101 function showhits($page, $hilight_re) {
102 $current = $page->getCurrentRevision();
103 $matches = preg_grep("/$hilight_re/i", $current->getContent());
105 foreach ($matches as $line) {
106 $line = $this->highlight_line($line, $hilight_re);
107 $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
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
119 $html[] = $line; // postmatch
124 // $Log: not supported by cvs2svn $
125 // Revision 1.18 2004/02/26 04:24:03 rurban
126 // simplify quiet handling by using PageList
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
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, ...)
135 // Revision 1.15 2003/01/18 21:41:01 carstenklapp
137 // Reformatting & tabs to spaces;
138 // Added copyleft, getVersion, getDescription, rcs_id.
145 // c-hanging-comment-ender-p: nil
146 // indent-tabs-mode: nil