Added post-commit callback support to DB classes.
[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 * @return void
49 */
50 public function executeGenerator( $resultPageSet ) {
51 if ( $resultPageSet->isResolvingRedirects() ) {
52 $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
53 }
54
55 $this->run( $resultPageSet );
56 }
57
58 /**
59 * @param $resultPageSet ApiPageSet
60 * @return void
61 */
62 private function run( $resultPageSet = null ) {
63 $db = $this->getDB();
64
65 $params = $this->extractRequestParams();
66
67 // Page filters
68 $this->addTables( 'page' );
69
70 if ( !is_null( $params['continue'] ) ) {
71 $cont = explode( '|', $params['continue'] );
72 if ( count( $cont ) != 1 ) {
73 $this->dieUsage( "Invalid continue param. You should pass the " .
74 "original value returned by the previous query", "_badcontinue" );
75 }
76 $op = $params['dir'] == 'descending' ? '<' : '>';
77 $cont_from = $db->addQuotes( $cont[0] );
78 $this->addWhere( "page_title $op= $cont_from" );
79 }
80
81 if ( $params['filterredir'] == 'redirects' ) {
82 $this->addWhereFld( 'page_is_redirect', 1 );
83 } elseif ( $params['filterredir'] == 'nonredirects' ) {
84 $this->addWhereFld( 'page_is_redirect', 0 );
85 }
86
87 $this->addWhereFld( 'page_namespace', $params['namespace'] );
88 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
89 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
90 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
91 $this->addWhereRange( 'page_title', $dir, $from, $to );
92
93 if ( isset( $params['prefix'] ) ) {
94 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
95 }
96
97 if ( is_null( $resultPageSet ) ) {
98 $selectFields = array(
99 'page_namespace',
100 'page_title',
101 'page_id'
102 );
103 } else {
104 $selectFields = $resultPageSet->getPageTableFields();
105 }
106
107 $this->addFields( $selectFields );
108 $forceNameTitleIndex = true;
109 if ( isset( $params['minsize'] ) ) {
110 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
111 $forceNameTitleIndex = false;
112 }
113
114 if ( isset( $params['maxsize'] ) ) {
115 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
116 $forceNameTitleIndex = false;
117 }
118
119 // Page protection filtering
120 if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) {
121 $this->addTables( 'page_restrictions' );
122 $this->addWhere( 'page_id=pr_page' );
123 $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
124
125 if ( count( $params['prtype'] ) ) {
126 $this->addWhereFld( 'pr_type', $params['prtype'] );
127
128 if ( isset( $params['prlevel'] ) ) {
129 // Remove the empty string and '*' from the prlevel array
130 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
131
132 if ( count( $prlevel ) ) {
133 $this->addWhereFld( 'pr_level', $prlevel );
134 }
135 }
136 if ( $params['prfiltercascade'] == 'cascading' ) {
137 $this->addWhereFld( 'pr_cascade', 1 );
138 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
139 $this->addWhereFld( 'pr_cascade', 0 );
140 }
141
142 $this->addOption( 'DISTINCT' );
143 }
144 $forceNameTitleIndex = false;
145
146 if ( $params['prexpiry'] == 'indefinite' ) {
147 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
148 } elseif ( $params['prexpiry'] == 'definite' ) {
149 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
150 }
151
152 } elseif ( isset( $params['prlevel'] ) ) {
153 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
154 }
155
156 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
157 $this->addTables( 'langlinks' );
158 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
159 $this->addWhere( 'll_from IS NULL' );
160 $forceNameTitleIndex = false;
161 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
162 $this->addTables( 'langlinks' );
163 $this->addWhere( 'page_id=ll_from' );
164 $this->addOption( 'STRAIGHT_JOIN' );
165 // We have to GROUP BY all selected fields to stop
166 // PostgreSQL from whining
167 $this->addOption( 'GROUP BY', $selectFields );
168 $forceNameTitleIndex = false;
169 }
170
171 if ( $forceNameTitleIndex ) {
172 $this->addOption( 'USE INDEX', 'name_title' );
173 }
174
175 $limit = $params['limit'];
176 $this->addOption( 'LIMIT', $limit + 1 );
177 $res = $this->select( __METHOD__ );
178
179 //Get gender information
180 if( MWNamespace::hasGenderDistinction( $params['namespace'] ) ) {
181 $users = array();
182 foreach ( $res as $row ) {
183 $users[] = $row->page_title;
184 }
185 GenderCache::singleton()->doQuery( $users, __METHOD__ );
186 $res->rewind(); //reset
187 }
188
189 $count = 0;
190 $result = $this->getResult();
191 foreach ( $res as $row ) {
192 if ( ++ $count > $limit ) {
193 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
194 $this->setContinueEnumParameter( 'continue', $row->page_title );
195 break;
196 }
197
198 if ( is_null( $resultPageSet ) ) {
199 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
200 $vals = array(
201 'pageid' => intval( $row->page_id ),
202 'ns' => intval( $title->getNamespace() ),
203 'title' => $title->getPrefixedText()
204 );
205 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
206 if ( !$fit ) {
207 $this->setContinueEnumParameter( 'continue', $row->page_title );
208 break;
209 }
210 } else {
211 $resultPageSet->processDbRow( $row );
212 }
213 }
214
215 if ( is_null( $resultPageSet ) ) {
216 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
217 }
218 }
219
220 public function getAllowedParams() {
221 global $wgRestrictionLevels;
222
223 return array(
224 'from' => null,
225 'continue' => null,
226 'to' => null,
227 'prefix' => null,
228 'namespace' => array(
229 ApiBase::PARAM_DFLT => 0,
230 ApiBase::PARAM_TYPE => 'namespace',
231 ),
232 'filterredir' => array(
233 ApiBase::PARAM_DFLT => 'all',
234 ApiBase::PARAM_TYPE => array(
235 'all',
236 'redirects',
237 'nonredirects'
238 )
239 ),
240 'minsize' => array(
241 ApiBase::PARAM_TYPE => 'integer',
242 ),
243 'maxsize' => array(
244 ApiBase::PARAM_TYPE => 'integer',
245 ),
246 'prtype' => array(
247 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
248 ApiBase::PARAM_ISMULTI => true
249 ),
250 'prlevel' => array(
251 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
252 ApiBase::PARAM_ISMULTI => true
253 ),
254 'prfiltercascade' => array(
255 ApiBase::PARAM_DFLT => 'all',
256 ApiBase::PARAM_TYPE => array(
257 'cascading',
258 'noncascading',
259 'all'
260 ),
261 ),
262 'limit' => array(
263 ApiBase::PARAM_DFLT => 10,
264 ApiBase::PARAM_TYPE => 'limit',
265 ApiBase::PARAM_MIN => 1,
266 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
267 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
268 ),
269 'dir' => array(
270 ApiBase::PARAM_DFLT => 'ascending',
271 ApiBase::PARAM_TYPE => array(
272 'ascending',
273 'descending'
274 )
275 ),
276 'filterlanglinks' => array(
277 ApiBase::PARAM_TYPE => array(
278 'withlanglinks',
279 'withoutlanglinks',
280 'all'
281 ),
282 ApiBase::PARAM_DFLT => 'all'
283 ),
284 'prexpiry' => array(
285 ApiBase::PARAM_TYPE => array(
286 'indefinite',
287 'definite',
288 'all'
289 ),
290 ApiBase::PARAM_DFLT => 'all'
291 ),
292 );
293 }
294
295 public function getParamDescription() {
296 $p = $this->getModulePrefix();
297 return array(
298 'from' => 'The page title to start enumerating from',
299 'continue' => 'When more results are available, use this to continue',
300 'to' => 'The page title to stop enumerating at',
301 'prefix' => 'Search for all page titles that begin with this value',
302 'namespace' => 'The namespace to enumerate',
303 'filterredir' => 'Which pages to list',
304 'dir' => 'The direction in which to list',
305 'minsize' => 'Limit to pages with at least this many bytes',
306 'maxsize' => 'Limit to pages with at most this many bytes',
307 'prtype' => 'Limit to protected pages only',
308 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
309 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
310 'filterlanglinks' => 'Filter based on whether a page has langlinks',
311 'limit' => 'How many total pages to return.',
312 'prexpiry' => array(
313 'Which protection expiry to filter the page on',
314 ' indefinite - Get only pages with indefinite protection expiry',
315 ' definite - Get only pages with a definite (specific) protection expiry',
316 ' all - Get pages with any protections expiry'
317 ),
318 );
319 }
320
321 public function getResultProperties() {
322 return array(
323 '' => array(
324 'pageid' => 'integer',
325 'ns' => 'namespace',
326 'title' => 'string'
327 )
328 );
329 }
330
331 public function getDescription() {
332 return 'Enumerate all pages sequentially in a given namespace';
333 }
334
335 public function getPossibleErrors() {
336 return array_merge( parent::getPossibleErrors(), array(
337 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
338 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
339 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
340 ) );
341 }
342
343 public function getExamples() {
344 return array(
345 'api.php?action=query&list=allpages&apfrom=B' => array(
346 'Simple Use',
347 'Show a list of pages starting at the letter "B"',
348 ),
349 'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
350 'Using as Generator',
351 'Show info about 4 pages starting at the letter "T"',
352 ),
353 'api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content' => array(
354 'Show content of first 2 non-redirect pages begining at "Re"',
355 )
356 );
357 }
358
359 public function getHelpUrls() {
360 return 'https://www.mediawiki.org/wiki/API:Allpages';
361 }
362
363 public function getVersion() {
364 return __CLASS__ . ': $Id$';
365 }
366 }