]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
Display error message if no argument provided
[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 HTML::div(array('class' => "error"), "Please provide 's' argument to the plugin.");
76         }
77         extract($args);
78
79         $query = new TextSearchQuery($s, $case_exact, $regex);
80         $pages = $dbi->fullSearch($query, $sortby, $limit, $exclude);
81         $lines = array();
82         $hilight_re = $hilight ? $query->getHighlightRegexp() : false;
83         $count = 0;
84
85         if ($quiet) { // see how easy it is with PageList...
86             unset($args['info']);
87             $args['listtype'] = 'dl';
88             $args['types'] = array(new _PageList_Column_content
89               ('rev:hi_content', _("Content"), "left", $s));
90             $list = new PageList(false, $exclude, $args);
91             $list->setCaption(fmt("Full text search results for '%s'", $s));
92             while ($page = $pages->next()) {
93                 $list->addPage( $page );
94             }
95             return $list;
96         }
97
98         // Todo: we should better define a new PageListDL class for dl/dt/dd lists
99         // But the new column types must have a callback then. (showhits)
100         // See e.g. WikiAdminSearchReplace for custom pagelist columns
101         $list = HTML::dl();
102         if (!$limit or !is_int($limit))
103             $limit = 0;
104         // expand all page wildcards to a list of pages which should be ignored
105         if ($exclude) $exclude = explodePageList($exclude); 
106         while ($page = $pages->next() and (!$limit or ($count < $limit))) {
107             $name = $page->getName();
108             if ($exclude and in_array($name,$exclude)) continue;
109             $count++;
110             $list->pushContent(HTML::dt(WikiLink($page)));
111             if ($hilight_re)
112                 $list->pushContent($this->showhits($page, $hilight_re));
113             unset($page);
114         }
115         if ($limit and $count >= $limit) //todo: pager link to list of next matches
116             $list->pushContent(HTML::dd(fmt("only %d pages displayed",$limit)));
117         if (!$list->getContent())
118             $list->pushContent(HTML::dd(_("<no matches>")));
119
120         if (!empty($pages->stoplisted))
121             $list = HTML(HTML::p(fmt(_("Ignored stoplist words '%s'"), 
122                                      join(', ', $pages->stoplisted))), 
123                          $list);
124         if ($noheader)
125             return $list;
126         return HTML(HTML::p(fmt("Full text search results for '%s'", $s)),
127                     $list);
128     }
129
130     function showhits($page, $hilight_re) {
131         $current = $page->getCurrentRevision();
132         $matches = preg_grep("/$hilight_re/i", $current->getContent());
133         $html = array();
134         foreach ($matches as $line) {
135             $line = $this->highlight_line($line, $hilight_re);
136             $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
137                                            $line));
138         }
139         return $html;
140     }
141
142     function highlight_line ($line, $hilight_re) {
143         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
144             $line = substr($line, strlen($m[0]));
145             $html[] = $m[1];    // prematch
146             $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
147         }
148         $html[] = $line;        // postmatch
149         return $html;
150     }
151 };
152
153 /*
154  * List of Links and link to ListLinks
155  */
156 class _PageList_Column_hilight extends _PageList_Column {
157     function _PageList_Column_WantedPages_links (&$params) {
158         $this->parentobj =& $params[3];
159         $this->_PageList_Column($params[0],$params[1],$params[2]);
160     }
161     function _getValue(&$page, $revision_handle) {
162         $html = false;
163         $pagename = $page->getName();
164         $count = count($this->parentobj->_wpagelist[$pagename]);
165         return LinkURL(WikiURL($page, array('action' => 'BackLinks'), false), 
166                         fmt("(%d Links)", $count));
167     }
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 ?>