4 * Copyright 1999-2002,2004,2005,2007,2009 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 require_once 'lib/TextSearchQuery.php';
24 require_once 'lib/PageList.php';
27 * Case insensitive fulltext search
28 * Options: case_exact, regex, hilight
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, ...
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
42 class WikiPlugin_FullTextSearch
47 return _("FullTextSearch");
50 function getDescription()
52 return _("Search the content of all pages in this wiki.");
55 function getDefaultArguments()
59 PageList::supportedArgs(), // paging and more.
62 'case_exact' => false,
64 'sortby' => '-hi_content',
66 'exclude' => false, // comma-seperated list of glob
67 'quiet' => true)); // be less verbose
70 function run($dbi, $argstr, &$request, $basepage)
73 $args = $this->getArgs($argstr, $request);
75 if (empty($args['s'])) {
80 $query = new TextSearchQuery($s, $case_exact, $regex);
81 $pages = $dbi->fullSearch($query, $sortby, $limit, $exclude);
83 $hilight_re = $hilight ? $query->getHighlightRegexp() : false;
86 if ($quiet) { // see how easy it is with PageList...
88 $args['listtype'] = 'dl';
89 $args['types'] = array(new _PageList_Column_content
90 ('rev:hi_content', _("Content"), "left", $s, $hilight_re));
91 $list = new PageList(false, $exclude, $args);
92 $list->setCaption(fmt("Full text search results for “%s”", $s));
93 while ($page = $pages->next()) {
94 $list->addPage($page);
99 // Todo: we should better define a new PageListDL class for dl/dt/dd lists
100 // But the new column types must have a callback then. (showhits)
101 // See e.g. WikiAdminSearchReplace for custom pagelist columns
103 if (!$limit or !is_int($limit))
105 // expand all page wildcards to a list of pages which should be ignored
106 if ($exclude) $exclude = explodePageList($exclude);
107 while ($page = $pages->next() and (!$limit or ($count < $limit))) {
108 $name = $page->getName();
109 if ($exclude and in_array($name, $exclude)) continue;
111 $list->pushContent(HTML::dt(WikiLink($page)));
113 $list->pushContent($this->showhits($page, $hilight_re));
116 if ($limit and $count >= $limit) //todo: pager link to list of next matches
117 $list->pushContent(HTML::dd(fmt("only %d pages displayed", $limit)));
118 if (!$list->getContent())
119 $list->pushContent(HTML::dd(_("<no matches>")));
121 if (!empty($pages->stoplisted))
122 $list = HTML(HTML::p(fmt(_("Ignored stoplist words “%s”"),
123 join(', ', $pages->stoplisted))),
127 return HTML(HTML::p(fmt("Full text search results for “%s”", $s)),
131 function showhits($page, $hilight_re)
133 $current = $page->getCurrentRevision();
134 $matches = preg_grep("/$hilight_re/i", $current->getContent());
136 foreach ($matches as $line) {
137 $line = $this->highlight_line($line, $hilight_re);
138 $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
144 function highlight_line($line, $hilight_re)
146 while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
147 $line = substr($line, strlen($m[0]));
148 $html[] = $m[1]; // prematch
149 $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
151 $html[] = $line; // postmatch
157 * List of Links and link to ListLinks
159 class _PageList_Column_hilight extends _PageList_Column
161 function _PageList_Column_WantedPages_links(&$params)
163 $this->parentobj =& $params[3];
164 $this->_PageList_Column($params[0], $params[1], $params[2]);
167 function _getValue(&$page, $revision_handle)
170 $pagename = $page->getName();
171 $count = count($this->parentobj->_wpagelist[$pagename]);
172 return LinkURL(WikiURL($page, array('action' => 'BackLinks'), false),
173 fmt("(%d Links)", $count));
181 // c-hanging-comment-ender-p: nil
182 // indent-tabs-mode: nil