]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
better support for case_exact search (not caseexact for consistency),
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FullTextSearch.php,v 1.23 2004-11-23 15:17:19 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.23 $");
49     }
50
51     function getDefaultArguments() {
52         return array_merge
53             (
54              PageList::supportedArgs(), // paging and more.
55              array('s'        => false,
56                    'hilight'  => true,
57                    'case_exact' => false, //not yet supported
58                    'regex'    => false,   //not yet supported
59                    'noheader' => false,
60                    'exclude'  => false,   //comma-seperated list of glob
61                    'limit'    => false,
62                    'quiet'    => false));  // be less verbose
63     }
64
65     function run($dbi, $argstr, &$request, $basepage) {
66
67         $args = $this->getArgs($argstr, $request);
68         if (empty($args['s']))
69             return '';
70         extract($args);
71
72         $query = new TextSearchQuery($s, $case_exact, $regex);
73         $pages = $dbi->fullSearch($query);
74         $lines = array();
75         $hilight_re = $hilight ? $query->getHighlightRegexp() : false;
76         $count = 0;
77         $found = 0;
78
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 );
83             }
84             return $list;
85         }
86
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
90         $list = HTML::dl();
91         if (!$limit or !is_int($limit))
92             $limit = 0;
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;
98             $count++;
99             $list->pushContent(HTML::dt(WikiLink($name)));
100             if ($hilight_re)
101                 $list->pushContent($this->showhits($page, $hilight_re));
102             unset($page);
103         }
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>")));
108
109         if ($noheader)
110             return $list;
111         return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
112                     $list);
113     }
114
115     function showhits($page, $hilight_re) {
116         $current = $page->getCurrentRevision();
117         $matches = preg_grep("/$hilight_re/i", $current->getContent());
118         $html = array();
119         foreach ($matches as $line) {
120             $line = $this->highlight_line($line, $hilight_re);
121             $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
122                                            $line));
123         }
124         return $html;
125     }
126
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
132         }
133         $html[] = $line;        // postmatch
134         return $html;
135     }
136 };
137
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
142 //
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)
147 //
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
156 //
157 // Revision 1.19  2004/02/26 04:27:39  rurban
158 // wrong limit notification
159 //
160 // Revision 1.18  2004/02/26 04:24:03  rurban
161 // simplify quiet handling by using PageList
162 //
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
166 //
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, ...)
169 //
170 // Revision 1.15  2003/01/18 21:41:01  carstenklapp
171 // Code cleanup:
172 // Reformatting & tabs to spaces;
173 // Added copyleft, getVersion, getDescription, rcs_id.
174 //
175
176 // Local Variables:
177 // mode: php
178 // tab-width: 8
179 // c-basic-offset: 4
180 // c-hanging-comment-ender-p: nil
181 // indent-tabs-mode: nil
182 // End:
183 ?>