]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
Test 'limit' argument is numeric to avoid SQL injection
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php // -*-php-*-
2 rcs_id('$Id$');
3 /*
4 Copyright 1999,2000,2001,2002,2004,2005,2007 $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  * See also:
32  *   Hooks to search in external documents: ExternalTextSearch
33  *   Only uploaded: textfiles, PDF, HTML, DOC, XLS, ... or 
34  *   External apps: xapian-omages seems to be the better than lucene, 
35  *   lucene.net, swish, nakamazu, ...
36  *
37  * See http://sf.net/tracker/index.php?aid=927395&group_id=6121&atid=106121
38  * Wordaround to let the dead locks occur somewhat later:
39  *   Increase the memory limit of PHP from 8 MB to 32 MB
40  *   php.ini: memory_limit = 32 MB
41  */
42 class WikiPlugin_FullTextSearch
43 extends WikiPlugin
44 {
45     function getName() {
46         return _("FullTextSearch");
47     }
48
49     function getDescription() {
50         return _("Search the content of all pages in this wiki.");
51     }
52
53     function getVersion() {
54         return preg_replace("/[Revision: $]/", '',
55                             "\$Revision$");
56     }
57
58     function getDefaultArguments() {
59         return array_merge
60             (
61              PageList::supportedArgs(), // paging and more.
62              array('s'        => false,
63                    'hilight'  => true,
64                    'case_exact' => false,
65                    'regex'    => 'auto',
66                    'sortby'   => '-hi_content',
67                    'noheader' => false,
68                    'exclude'  => false,   // comma-seperated list of glob
69                    'quiet'    => true));  // be less verbose
70     }
71
72     function run($dbi, $argstr, &$request, $basepage) {
73
74         $args = $this->getArgs($argstr, $request);
75
76         if (!empty($args['limit']) && !is_numeric($args['limit'])) {
77             return $this->error(_("Illegal 'limit' argument: must be numeric"));
78         }
79
80         if (empty($args['s'])) {
81             return HTML();
82         }
83         extract($args);
84
85         $query = new TextSearchQuery($s, $case_exact, $regex);
86         $pages = $dbi->fullSearch($query, $sortby, $limit, $exclude);
87         $lines = array();
88         $hilight_re = $hilight ? $query->getHighlightRegexp() : false;
89         $count = 0;
90
91         if ($quiet) { // see how easy it is with PageList...
92             unset($args['info']);
93             $args['listtype'] = 'dl';
94             $args['types'] = array(new _PageList_Column_content
95               ('rev:hi_content', _("Content"), "left", $s));
96             $list = new PageList(false, $exclude, $args);
97             $list->setCaption(fmt("Full text search results for '%s'", $s));
98             while ($page = $pages->next()) {
99                 $list->addPage( $page );
100             }
101             return $list;
102         }
103
104         // Todo: we should better define a new PageListDL class for dl/dt/dd lists
105         // But the new column types must have a callback then. (showhits)
106         // See e.g. WikiAdminSearchReplace for custom pagelist columns
107         $list = HTML::dl();
108         if (!$limit or !is_int($limit))
109             $limit = 0;
110         // expand all page wildcards to a list of pages which should be ignored
111         if ($exclude) $exclude = explodePageList($exclude); 
112         while ($page = $pages->next() and (!$limit or ($count < $limit))) {
113             $name = $page->getName();
114             if ($exclude and in_array($name,$exclude)) continue;
115             $count++;
116             $list->pushContent(HTML::dt(WikiLink($page)));
117             if ($hilight_re)
118                 $list->pushContent($this->showhits($page, $hilight_re));
119             unset($page);
120         }
121         if ($limit and $count >= $limit) //todo: pager link to list of next matches
122             $list->pushContent(HTML::dd(fmt("only %d pages displayed",$limit)));
123         if (!$list->getContent())
124             $list->pushContent(HTML::dd(_("<no matches>")));
125
126         if (!empty($pages->stoplisted))
127             $list = HTML(HTML::p(fmt(_("Ignored stoplist words '%s'"), 
128                                      join(', ', $pages->stoplisted))), 
129                          $list);
130         if ($noheader)
131             return $list;
132         return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
133                     $list);
134     }
135
136     function showhits($page, $hilight_re) {
137         $current = $page->getCurrentRevision();
138         $matches = preg_grep("/$hilight_re/i", $current->getContent());
139         $html = array();
140         foreach ($matches as $line) {
141             $line = $this->highlight_line($line, $hilight_re);
142             $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
143                                            $line));
144         }
145         return $html;
146     }
147
148     function highlight_line ($line, $hilight_re) {
149         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
150             $line = substr($line, strlen($m[0]));
151             $html[] = $m[1];    // prematch
152             $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
153         }
154         $html[] = $line;        // postmatch
155         return $html;
156     }
157 };
158
159 /*
160  * List of Links and link to ListLinks
161  */
162 class _PageList_Column_hilight extends _PageList_Column {
163     function _PageList_Column_WantedPages_links (&$params) {
164         $this->parentobj =& $params[3];
165         $this->_PageList_Column($params[0],$params[1],$params[2]);
166     }
167     function _getValue(&$page, $revision_handle) {
168         $html = false;
169         $pagename = $page->getName();
170         $count = count($this->parentobj->_wpagelist[$pagename]);
171         return LinkURL(WikiURL($page, array('action' => 'BackLinks'), false), 
172                         fmt("(%d Links)", $count));
173     }
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 ?>