Fix regression from r36678: we can't use $this->dieUsageMsg() in a static method...
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
1 <?php
2
3 /*
4 * Created on July 30, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query module to perform full text search within wiki titles and content
33 *
34 * @ingroup API
35 */
36 class ApiQuerySearch extends ApiQueryGeneratorBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'sr');
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
48 }
49
50 private function run($resultPageSet = null) {
51
52 $params = $this->extractRequestParams();
53
54 $limit = $params['limit'];
55 $query = $params['search'];
56 if (is_null($query) || empty($query))
57 $this->dieUsage("empty search string is not allowed", 'param-search');
58
59 $search = SearchEngine::create();
60 $search->setLimitOffset( $limit+1, $params['offset'] );
61 $search->setNamespaces( $params['namespace'] );
62 $search->showRedirects = $params['redirects'];
63
64 if ($params['what'] == 'text')
65 $matches = $search->searchText( $query );
66 else
67 $matches = $search->searchTitle( $query );
68 if (is_null($matches))
69 $this->dieUsage("{$params['what']} search is disabled",
70 "search-{$params['what']}-disabled");
71
72 $data = array ();
73 $count = 0;
74 while( $result = $matches->next() ) {
75 if (++ $count > $limit) {
76 // We've reached the one extra which shows that there are additional items to be had. Stop here...
77 $this->setContinueEnumParameter('offset', $params['offset'] + $params['limit']);
78 break;
79 }
80
81 // Silently skip broken titles
82 if ($result->isBrokenTitle()) continue;
83
84 $title = $result->getTitle();
85 if (is_null($resultPageSet)) {
86 $data[] = array(
87 'ns' => intval($title->getNamespace()),
88 'title' => $title->getPrefixedText());
89 } else {
90 $data[] = $title;
91 }
92 }
93
94 if (is_null($resultPageSet)) {
95 $result = $this->getResult();
96 $result->setIndexedTagName($data, 'p');
97 $result->addValue('query', $this->getModuleName(), $data);
98 } else {
99 $resultPageSet->populateFromTitles($data);
100 }
101 }
102
103 public function getAllowedParams() {
104 return array (
105 'search' => null,
106 'namespace' => array (
107 ApiBase :: PARAM_DFLT => 0,
108 ApiBase :: PARAM_TYPE => 'namespace',
109 ApiBase :: PARAM_ISMULTI => true,
110 ),
111 'what' => array (
112 ApiBase :: PARAM_DFLT => 'title',
113 ApiBase :: PARAM_TYPE => array (
114 'title',
115 'text',
116 )
117 ),
118 'redirects' => false,
119 'offset' => 0,
120 'limit' => array (
121 ApiBase :: PARAM_DFLT => 10,
122 ApiBase :: PARAM_TYPE => 'limit',
123 ApiBase :: PARAM_MIN => 1,
124 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
125 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
126 )
127 );
128 }
129
130 public function getParamDescription() {
131 return array (
132 'search' => 'Search for all page titles (or content) that has this value.',
133 'namespace' => 'The namespace(s) to enumerate.',
134 'what' => 'Search inside the text or titles.',
135 'redirects' => 'Include redirect pages in the search.',
136 'offset' => 'Use this value to continue paging (return by query)',
137 'limit' => 'How many total pages to return.'
138 );
139 }
140
141 public function getDescription() {
142 return 'Perform a full text search';
143 }
144
145 protected function getExamples() {
146 return array (
147 'api.php?action=query&list=search&srsearch=meaning',
148 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
149 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
150 );
151 }
152
153 public function getVersion() {
154 return __CLASS__ . ': $Id$';
155 }
156 }