]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
Activated Revision substitution for Subversion
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4 Copyright 1999,2000,2001,2002,2004,2005 $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  * Case insensitive fulltext search
28  * Options: case_exact, regex, hilight
29  *          Stoplist
30  *
31  * TODO: Hooks to search in external documents: ExternalTextSearch
32  *   Only uploaded: textfiles, PDF, HTML, DOC, XLS, ... or 
33  *   External apps: xapian-omages seems to be the best, over lucene.net, 
34  *   swish, nakamazu, ...
35  *
36  * See http://sf.net/tracker/index.php?aid=927395&group_id=6121&atid=106121
37  * Wordaround to let the dead locks occur somewhat later:
38  * increased the memory limit of PHP4 from 8 MB to 32 MB
39  * php.ini: memory_limit = 32 MB
40  */
41 class WikiPlugin_FullTextSearch
42 extends WikiPlugin
43 {
44     function getName() {
45         return _("FullTextSearch");
46     }
47
48     function getDescription() {
49         return _("Search the content of all pages in this wiki.");
50     }
51
52     function getVersion() {
53         return preg_replace("/[Revision: $]/", '',
54                             "\$Revision$");
55     }
56
57     function getDefaultArguments() {
58         return array_merge
59             (
60              PageList::supportedArgs(), // paging and more.
61              array('s'        => false,
62                    'hilight'  => true,
63                    'case_exact' => false,
64                    'regex'    => 'auto',
65                    'sortby'   => '-hi_content',
66                    'noheader' => false,
67                    'exclude'  => false,   // comma-seperated list of glob
68                    'quiet'    => true));  // be less verbose
69     }
70
71     function run($dbi, $argstr, &$request, $basepage) {
72
73         $args = $this->getArgs($argstr, $request);
74         if (empty($args['s']))
75             return '';
76         extract($args);
77
78         $query = new TextSearchQuery($s, $case_exact, $regex);
79         $pages = $dbi->fullSearch($query, $sortby, $limit, $exclude);
80         $lines = array();
81         $hilight_re = $hilight ? $query->getHighlightRegexp() : false;
82         $count = 0;
83
84         if ($quiet) { // see how easy it is with PageList...
85             unset($args['info']);
86             $args['listtype'] = 'dl';
87             $args['types'] = array(new _PageList_Column_content
88               ('rev:hi_content', _("Content"), "left", $s));
89             $list = new PageList(false, $exclude, $args);
90             $list->setCaption(fmt("Full text search results for '%s'", $s));
91             while ($page = $pages->next()) {
92                 $list->addPage( $page );
93             }
94             return $list;
95         }
96
97         // Todo: we should better define a new PageListDL class for dl/dt/dd lists
98         // But the new column types must have a callback then. (showhits)
99         // See e.g. WikiAdminSearchReplace for custom pagelist columns
100         $list = HTML::dl();
101         if (!$limit or !is_int($limit))
102             $limit = 0;
103         // expand all page wildcards to a list of pages which should be ignored
104         if ($exclude) $exclude = explodePageList($exclude); 
105         while ($page = $pages->next() and (!$limit or ($count < $limit))) {
106             $name = $page->getName();
107             if ($exclude and in_array($name,$exclude)) continue;
108             $count++;
109             $list->pushContent(HTML::dt(WikiLink($page)));
110             if ($hilight_re)
111                 $list->pushContent($this->showhits($page, $hilight_re));
112             unset($page);
113         }
114         if ($limit and $count >= $limit) //todo: pager link to list of next matches
115             $list->pushContent(HTML::dd(fmt("only %d pages displayed",$limit)));
116         if (!$list->getContent())
117             $list->pushContent(HTML::dd(_("<no matches>")));
118
119         if (!empty($pages->stoplisted))
120             $list = HTML(HTML::p(fmt(_("Ignored stoplist words '%s'"), 
121                                      join(', ', $pages->stoplisted))), 
122                          $list);
123         if ($noheader)
124             return $list;
125         return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
126                     $list);
127     }
128
129     function showhits($page, $hilight_re) {
130         $current = $page->getCurrentRevision();
131         $matches = preg_grep("/$hilight_re/i", $current->getContent());
132         $html = array();
133         foreach ($matches as $line) {
134             $line = $this->highlight_line($line, $hilight_re);
135             $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
136                                            $line));
137         }
138         return $html;
139     }
140
141     function highlight_line ($line, $hilight_re) {
142         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
143             $line = substr($line, strlen($m[0]));
144             $html[] = $m[1];    // prematch
145             $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
146         }
147         $html[] = $line;        // postmatch
148         return $html;
149     }
150 };
151
152 /*
153  * List of Links and link to ListLinks
154  */
155 class _PageList_Column_hilight extends _PageList_Column {
156     function _PageList_Column_WantedPages_links (&$params) {
157         $this->parentobj =& $params[3];
158         $this->_PageList_Column($params[0],$params[1],$params[2]);
159     }
160     function _getValue(&$page, $revision_handle) {
161         $html = false;
162         $pagename = $page->getName();
163         $count = count($this->parentobj->_wpagelist[$pagename]);
164         return LinkURL(WikiURL($page, array('action' => 'BackLinks'), false), 
165                         fmt("(%d Links)", $count));
166     }
167 }
168
169
170 // $Log: not supported by cvs2svn $
171 // Revision 1.28  2007/06/07 17:02:42  rurban
172 // fix display of pagenames containing ":" in certain lists
173 //
174 // Revision 1.27  2007/01/04 16:46:40  rurban
175 // Only notes
176 //
177 // Revision 1.26  2005/11/14 22:33:04  rurban
178 // print ignored stoplist words
179 //
180 // Revision 1.25  2005/09/11 14:55:05  rurban
181 // implement fulltext stoplist
182 //
183 // Revision 1.24  2004/11/26 18:39:02  rurban
184 // new regex search parser and SQL backends (90% complete, glob and pcre backends missing)
185 //
186 // Revision 1.23  2004/11/23 15:17:19  rurban
187 // better support for case_exact search (not caseexact for consistency),
188 // plugin args simplification:
189 //   handle and explode exclude and pages argument in WikiPlugin::getArgs
190 //     and exclude in advance (at the sql level if possible)
191 //   handle sortby and limit from request override in WikiPlugin::getArgs
192 // ListSubpages: renamed pages to maxpages
193 //
194 // Revision 1.22  2004/05/28 11:01:58  rurban
195 // support to disable highlighting
196 // example: s=ReiniUrban&hilight=0&noheader=1
197 //
198 // Revision 1.21  2004/04/18 01:11:52  rurban
199 // more numeric pagename fixes.
200 // fixed action=upload with merge conflict warnings.
201 // charset changed from constant to global (dynamic utf-8 switching)
202 //
203 // Revision 1.20  2004/02/28 21:14:08  rurban
204 // generally more PHPDOC docs
205 //   see http://xarch.tu-graz.ac.at/home/rurban/phpwiki/xref/
206 // fxied WikiUserNew pref handling: empty theme not stored, save only
207 //   changed prefs, sql prefs improved, fixed password update,
208 //   removed REPLACE sql (dangerous)
209 // moved gettext init after the locale was guessed
210 // + some minor changes
211 //
212 // Revision 1.19  2004/02/26 04:27:39  rurban
213 // wrong limit notification
214 //
215 // Revision 1.18  2004/02/26 04:24:03  rurban
216 // simplify quiet handling by using PageList
217 //
218 // Revision 1.17  2004/02/26 04:03:39  rurban
219 // added quiet, limit and exclude to FullTextSearch,
220 // fixed explodePageList with previously unloaded PageList
221 //
222 // Revision 1.16  2004/02/17 12:11:36  rurban
223 // 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, ...)
224 //
225 // Revision 1.15  2003/01/18 21:41:01  carstenklapp
226 // Code cleanup:
227 // Reformatting & tabs to spaces;
228 // Added copyleft, getVersion, getDescription, rcs_id.
229 //
230
231 // Local Variables:
232 // mode: php
233 // tab-width: 8
234 // c-basic-offset: 4
235 // c-hanging-comment-ender-p: nil
236 // indent-tabs-mode: nil
237 // End:
238 ?>