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