]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TitleSearch.php
Use real list instead of middot
[SourceForge/phpwiki.git] / lib / plugin / TitleSearch.php
1 <?php
2
3 /**
4  * Copyright 1999,2000,2001,2002,2004,2005,2010 $ThePhpWikiProgrammingTeam
5  * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
9  * PhpWiki is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * PhpWiki is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 require_once 'lib/TextSearchQuery.php';
25 require_once 'lib/PageList.php';
26
27 /**
28  * Display results of pagename search.
29  * Provides no own input box, just <?plugin-form TitleSearch?> is enough.
30  * Fancier Inputforms can be made using <<WikiFormRich ...>> to support regex and case_exact args.
31  *
32  * If only one pages is found and auto_redirect is true, this page is displayed immediatly,
33  * otherwise the found pagelist is displayed.
34  * The workhorse TextSearchQuery converts the query string from google-style words
35  * to the required DB backend expression.
36  *   (word and word) OR word, -word, "two words"
37  * regex=auto tries to detect simple glob-style wildcards and expressions,
38  * like xx*, *xx, ^xx, xx$, ^word$.
39  */
40 class WikiPlugin_TitleSearch
41     extends WikiPlugin
42 {
43     function getDescription()
44     {
45         return _("Search the titles of all pages in this wiki.");
46     }
47
48     function getDefaultArguments()
49     {
50         // All PageList::supportedArgs, except 'pagename'
51         $args = array_merge(
52             PageList::supportedArgs(), // paging and more.
53             array('s' => false,
54                 'auto_redirect' => false,
55                 'noheader' => false,
56                 'exclude' => false,
57                 'info' => false,
58                 'case_exact' => false,
59                 'regex' => 'auto',
60                 'format' => false,
61             ));
62          unset($args['pagename']);
63          return $args;
64     }
65
66     // info arg allows multiple columns
67     // info=mtime,hits,summary,version,author,locked,minor
68     // exclude arg allows multiple pagenames exclude=Php*,RecentChanges
69
70     /**
71      * @param WikiDB $dbi
72      * @param string $argstr
73      * @param WikiRequest $request
74      * @param string $basepage
75      * @return mixed
76      */
77     function run($dbi, $argstr, &$request, $basepage)
78     {
79         $args = $this->getArgs($argstr, $request);
80
81         if (isset($args['limit']) && !is_limit($args['limit'])) {
82             return HTML::p(array('class' => "error"),
83                            _("Illegal “limit” argument: must be an integer or two integers separated by comma"));
84         }
85
86         if (empty($args['s'])) {
87             return HTML::p(array('class' => 'warning'),
88                            _("You must enter a search term."));
89         }
90
91         if (empty($args['sortby'])) {
92             $args['sortby'] = '-pagename';
93         }
94
95         // ^S != S*   ^  matches only beginning of phrase, not of word.
96         //            x* matches any word beginning with x
97         $query = new TextSearchQuery($args['s'], $args['case_exact'], $args['regex']);
98         $pages = $dbi->titleSearch($query, $args['sortby'], $args['limit'], $args['exclude']);
99
100         $pagelist = new PageList($args['info'], $args['exclude'], $args);
101         $pagelist->addPages($pages);
102
103         // Provide an unknown WikiWord link to allow for page creation
104         // when a search returns no results
105         if (!$args['noheader']) {
106             $s = $args['s'];
107             $total = $pagelist->getTotal();
108             if (!$total and !$query->_regex) {
109                 $s = WikiLink($args['s'], 'auto');
110             }
111             if ($total) {
112                 $pagelist->setCaption(fmt("Title search results for “%s” (%d total)", $s, $total));
113             } else {
114                 $pagelist->setCaption(fmt("Title search results for “%s”", $s));
115             }
116         }
117
118         if ($args['auto_redirect'] && ($pagelist->getTotal() == 1)) {
119             $page = $pagelist->first();
120             $request->redirect(WikiURL($page->getName(), array(), 'absurl'), false);
121         }
122
123         return $pagelist;
124     }
125 }
126
127 // Local Variables:
128 // mode: php
129 // tab-width: 8
130 // c-basic-offset: 4
131 // c-hanging-comment-ender-p: nil
132 // indent-tabs-mode: nil
133 // End: