]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FullTextSearch.php
FullTextSearch: better test quiet argument
[SourceForge/phpwiki.git] / lib / plugin / FullTextSearch.php
1 <?php
2
3 /*
4  * Copyright 1999-2002,2004,2005,2007,2009 $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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 class WikiPlugin_FullTextSearch
38     extends WikiPlugin
39 {
40     function getDescription()
41     {
42         return _("Search the content of all pages in this wiki.");
43     }
44
45     function getDefaultArguments()
46     {
47         // All PageList::supportedArgs, except 'pagename'
48         $args = array_merge(
49             PageList::supportedArgs(), // paging and more.
50             array('s' => false,
51                 'hilight' => true,
52                 'case_exact' => false,
53                 'regex' => 'auto',
54                 'sortby' => '-hi_content',
55                 'noheader' => false,
56                 'exclude' => false, // comma-separated list of glob
57                 'quiet' => true)); // be less verbose
58          unset($args['pagename']);
59          return $args;
60     }
61
62     /**
63      * @param WikiDB $dbi
64      * @param string $argstr
65      * @param WikiRequest $request
66      * @param string $basepage
67      * @return mixed
68      */
69     function run($dbi, $argstr, &$request, $basepage)
70     {
71         $args = $this->getArgs($argstr, $request);
72
73         if (empty($args['s'])) {
74             return HTML::p(array('class' => 'warning'),
75                            _("You must enter a search term."));
76         }
77         extract($args);
78         if ($quiet === 'false') {
79             $quiet = false;
80         }
81
82         $query = new TextSearchQuery($s, $case_exact, $regex);
83         $pages = $dbi->fullSearch($query, $sortby, $limit, $exclude);
84         $hilight_re = $hilight ? $query->getHighlightRegexp() : false;
85         $count = 0;
86
87         if ($quiet) { // see how easy it is with PageList...
88             unset($args['info']);
89             $args['listtype'] = 'dl';
90             $args['types'] = array(new _PageList_Column_content
91             ('rev:hi_content', _("Content"), "left", $s, $hilight_re));
92             $list = new PageList(array(), $exclude, $args);
93             $list->setCaption(fmt("Full text search results for “%s”", $s));
94             while ($page = $pages->next()) {
95                 $list->addPage($page);
96             }
97             return $list;
98         }
99
100         // Todo: we should better define a new PageListDL class for dl/dt/dd lists
101         // But the new column types must have a callback then. (showhits)
102         // See e.g. WikiAdminSearchReplace for custom pagelist columns
103         $list = HTML::dl();
104         if (!$limit or !is_int($limit))
105             $limit = 0;
106         // expand all page wildcards to a list of pages which should be ignored
107         if ($exclude) {
108             $exclude = explodePageList($exclude);
109         }
110         while ($page = $pages->next() and (!$limit or ($count < $limit))) {
111             $name = $page->getName();
112             if ($exclude and in_array($name, $exclude)) continue;
113             $count++;
114             $list->pushContent(HTML::dt(WikiLink($page)));
115             if ($hilight_re)
116                 $list->pushContent($this->showhits($page, $hilight_re));
117             unset($page);
118         }
119         if ($limit and $count >= $limit) //todo: pager link to list of next matches
120             $list->pushContent(HTML::dd(fmt("only %d pages displayed", $limit)));
121         if (!$list->getContent())
122             $list->pushContent(HTML::dd(_("No matches")));
123
124         if (!empty($pages->stoplisted))
125             $list = HTML(HTML::p(fmt(_("Ignored stoplist words “%s”"),
126                     join(', ', $pages->stoplisted))),
127                 $list);
128         if ($noheader)
129             return $list;
130         return HTML(HTML::p(fmt("Full text search results for “%s”", $s)),
131             $list);
132     }
133
134     /**
135      * @param WikiDB_Page $page
136      * @param string $hilight_re
137      * @return array
138      */
139     function showhits($page, $hilight_re)
140     {
141         $current = $page->getCurrentRevision();
142         $matches = preg_grep("/$hilight_re/i", $current->getContent());
143         $html = array();
144         foreach ($matches as $line) {
145             $line = $this->highlight_line($line, $hilight_re);
146             $html[] = HTML::dd(HTML::small(array('class' => 'search-context'),
147                 $line));
148         }
149         return $html;
150     }
151
152     static function highlight_line($line, $hilight_re)
153     {
154         while (preg_match("/^(.*?)($hilight_re)/i", $line, $m)) {
155             $line = substr($line, strlen($m[0]));
156             $html[] = $m[1]; // prematch
157             $html[] = HTML::strong(array('class' => 'search-term'), $m[2]); // match
158         }
159         $html[] = $line; // postmatch
160         return $html;
161     }
162 }
163
164 /*
165  * List of Links and link to ListLinks
166  */
167 class _PageList_Column_hilight extends _PageList_Column
168 {
169     private $parentobj;
170
171     function _PageList_Column_WantedPages_links(&$params)
172     {
173         $this->parentobj =& $params[3];
174         $this->_PageList_Column($params[0], $params[1], $params[2]);
175     }
176
177     function _getValue($page, $revision_handle)
178     {
179         $pagename = $page->getName();
180         $count = count($this->parentobj->_wpagelist[$pagename]);
181         return LinkURL(WikiURL($page, array('action' => 'BackLinks'), false),
182             fmt("(%d Links)", $count));
183     }
184 }