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