Kill "* @return void"
[lhc/web/wiklou.git] / includes / api / ApiQueryAllpages.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
9 * This program 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 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * Query module to enumerate all available pages.
29 *
30 * @ingroup API
31 */
32 class ApiQueryAllpages extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'ap' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function getCacheMode( $params ) {
43 return 'public';
44 }
45
46 /**
47 * @param $resultPageSet ApiPageSet
48 */
49 public function executeGenerator( $resultPageSet ) {
50 if ( $resultPageSet->isResolvingRedirects() ) {
51 $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
52 }
53
54 $this->run( $resultPageSet );
55 }
56
57 /**
58 * @param $resultPageSet ApiPageSet
59 */
60 private function run( $resultPageSet = null ) {
61 $db = $this->getDB();
62
63 $params = $this->extractRequestParams();
64
65 // Page filters
66 $this->addTables( 'page' );
67
68 if ( $params['filterredir'] == 'redirects' ) {
69 $this->addWhereFld( 'page_is_redirect', 1 );
70 } elseif ( $params['filterredir'] == 'nonredirects' ) {
71 $this->addWhereFld( 'page_is_redirect', 0 );
72 }
73
74 $this->addWhereFld( 'page_namespace', $params['namespace'] );
75 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
76 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
77 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
78 $this->addWhereRange( 'page_title', $dir, $from, $to );
79
80 if ( isset( $params['prefix'] ) ) {
81 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
82 }
83
84 if ( is_null( $resultPageSet ) ) {
85 $selectFields = array(
86 'page_namespace',
87 'page_title',
88 'page_id'
89 );
90 } else {
91 $selectFields = $resultPageSet->getPageTableFields();
92 }
93
94 $this->addFields( $selectFields );
95 $forceNameTitleIndex = true;
96 if ( isset( $params['minsize'] ) ) {
97 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
98 $forceNameTitleIndex = false;
99 }
100
101 if ( isset( $params['maxsize'] ) ) {
102 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
103 $forceNameTitleIndex = false;
104 }
105
106 // Page protection filtering
107 if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) {
108 $this->addTables( 'page_restrictions' );
109 $this->addWhere( 'page_id=pr_page' );
110 $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
111
112 if ( count( $params['prtype'] ) ) {
113 $this->addWhereFld( 'pr_type', $params['prtype'] );
114
115 if ( isset( $params['prlevel'] ) ) {
116 // Remove the empty string and '*' from the prlevel array
117 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
118
119 if ( count( $prlevel ) ) {
120 $this->addWhereFld( 'pr_level', $prlevel );
121 }
122 }
123 if ( $params['prfiltercascade'] == 'cascading' ) {
124 $this->addWhereFld( 'pr_cascade', 1 );
125 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
126 $this->addWhereFld( 'pr_cascade', 0 );
127 }
128
129 $this->addOption( 'DISTINCT' );
130 }
131 $forceNameTitleIndex = false;
132
133 if ( $params['prexpiry'] == 'indefinite' ) {
134 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
135 } elseif ( $params['prexpiry'] == 'definite' ) {
136 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
137 }
138
139 } elseif ( isset( $params['prlevel'] ) ) {
140 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
141 }
142
143 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
144 $this->addTables( 'langlinks' );
145 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
146 $this->addWhere( 'll_from IS NULL' );
147 $forceNameTitleIndex = false;
148 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
149 $this->addTables( 'langlinks' );
150 $this->addWhere( 'page_id=ll_from' );
151 $this->addOption( 'STRAIGHT_JOIN' );
152 // We have to GROUP BY all selected fields to stop
153 // PostgreSQL from whining
154 $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
155 $forceNameTitleIndex = false;
156 }
157
158 if ( $forceNameTitleIndex ) {
159 $this->addOption( 'USE INDEX', 'name_title' );
160 }
161
162 $limit = $params['limit'];
163 $this->addOption( 'LIMIT', $limit + 1 );
164 $res = $this->select( __METHOD__ );
165
166 $count = 0;
167 $result = $this->getResult();
168 foreach ( $res as $row ) {
169 if ( ++ $count > $limit ) {
170 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
171 // TODO: Security issue - if the user has no right to view next title, it will still be shown
172 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
173 break;
174 }
175
176 if ( is_null( $resultPageSet ) ) {
177 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
178 $vals = array(
179 'pageid' => intval( $row->page_id ),
180 'ns' => intval( $title->getNamespace() ),
181 'title' => $title->getPrefixedText()
182 );
183 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
184 if ( !$fit ) {
185 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
186 break;
187 }
188 } else {
189 $resultPageSet->processDbRow( $row );
190 }
191 }
192
193 if ( is_null( $resultPageSet ) ) {
194 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
195 }
196 }
197
198 public function getAllowedParams() {
199 global $wgRestrictionLevels;
200
201 return array(
202 'from' => null,
203 'to' => null,
204 'prefix' => null,
205 'namespace' => array(
206 ApiBase::PARAM_DFLT => 0,
207 ApiBase::PARAM_TYPE => 'namespace',
208 ),
209 'filterredir' => array(
210 ApiBase::PARAM_DFLT => 'all',
211 ApiBase::PARAM_TYPE => array(
212 'all',
213 'redirects',
214 'nonredirects'
215 )
216 ),
217 'minsize' => array(
218 ApiBase::PARAM_TYPE => 'integer',
219 ),
220 'maxsize' => array(
221 ApiBase::PARAM_TYPE => 'integer',
222 ),
223 'prtype' => array(
224 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
225 ApiBase::PARAM_ISMULTI => true
226 ),
227 'prlevel' => array(
228 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
229 ApiBase::PARAM_ISMULTI => true
230 ),
231 'prfiltercascade' => array(
232 ApiBase::PARAM_DFLT => 'all',
233 ApiBase::PARAM_TYPE => array(
234 'cascading',
235 'noncascading',
236 'all'
237 ),
238 ),
239 'limit' => array(
240 ApiBase::PARAM_DFLT => 10,
241 ApiBase::PARAM_TYPE => 'limit',
242 ApiBase::PARAM_MIN => 1,
243 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
244 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
245 ),
246 'dir' => array(
247 ApiBase::PARAM_DFLT => 'ascending',
248 ApiBase::PARAM_TYPE => array(
249 'ascending',
250 'descending'
251 )
252 ),
253 'filterlanglinks' => array(
254 ApiBase::PARAM_TYPE => array(
255 'withlanglinks',
256 'withoutlanglinks',
257 'all'
258 ),
259 ApiBase::PARAM_DFLT => 'all'
260 ),
261 'prexpiry' => array(
262 ApiBase::PARAM_TYPE => array(
263 'indefinite',
264 'definite',
265 'all'
266 ),
267 ApiBase::PARAM_DFLT => 'all'
268 ),
269 );
270 }
271
272 public function getParamDescription() {
273 $p = $this->getModulePrefix();
274 return array(
275 'from' => 'The page title to start enumerating from',
276 'to' => 'The page title to stop enumerating at',
277 'prefix' => 'Search for all page titles that begin with this value',
278 'namespace' => 'The namespace to enumerate',
279 'filterredir' => 'Which pages to list',
280 'dir' => 'The direction in which to list',
281 'minsize' => 'Limit to pages with at least this many bytes',
282 'maxsize' => 'Limit to pages with at most this many bytes',
283 'prtype' => 'Limit to protected pages only',
284 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
285 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
286 'filterlanglinks' => 'Filter based on whether a page has langlinks',
287 'limit' => 'How many total pages to return.',
288 'prexpiry' => array(
289 'Which protection expiry to filter the page on',
290 ' indefinite - Get only pages with indefinite protection expiry',
291 ' definite - Get only pages with a definite (specific) protection expiry',
292 ' all - Get pages with any protections expiry'
293 ),
294 );
295 }
296
297 public function getDescription() {
298 return 'Enumerate all pages sequentially in a given namespace';
299 }
300
301 public function getPossibleErrors() {
302 return array_merge( parent::getPossibleErrors(), array(
303 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
304 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
305 ) );
306 }
307
308 public function getExamples() {
309 return array(
310 'api.php?action=query&list=allpages&apfrom=B' => array(
311 'Simple Use',
312 'Show a list of pages starting at the letter "B"',
313 ),
314 'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
315 'Using as Generator',
316 'Show info about 4 pages starting at the letter "T"',
317 ),
318 'api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content' => array(
319 'Show content of first 2 non-redirect pages begining at "Re"',
320 )
321 );
322 }
323
324 public function getHelpUrls() {
325 return 'https://www.mediawiki.org/wiki/API:Allpages';
326 }
327
328 public function getVersion() {
329 return __CLASS__ . ': $Id$';
330 }
331 }