Merge "Typo fix"
[mediawiki.git] / includes / api / ApiQueryAllPages.php
blobd95980c287be5ecc09f5aa2c76a09f7fc9957dd7
1 <?php
2 /**
5 * Created on Sep 25, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * Query module to enumerate all available pages.
30 * @ingroup API
32 class ApiQueryAllPages extends ApiQueryGeneratorBase {
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'ap' );
38 public function execute() {
39 $this->run();
42 public function getCacheMode( $params ) {
43 return 'public';
46 /**
47 * @param $resultPageSet ApiPageSet
48 * @return void
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' );
55 $this->run( $resultPageSet );
58 /**
59 * @param $resultPageSet ApiPageSet
60 * @return void
62 private function run( $resultPageSet = null ) {
63 $db = $this->getDB();
65 $params = $this->extractRequestParams();
67 // Page filters
68 $this->addTables( 'page' );
70 if ( !is_null( $params['continue'] ) ) {
71 $cont = explode( '|', $params['continue'] );
72 $this->dieContinueUsageIf( count( $cont ) != 1 );
73 $op = $params['dir'] == 'descending' ? '<' : '>';
74 $cont_from = $db->addQuotes( $cont[0] );
75 $this->addWhere( "page_title $op= $cont_from" );
78 if ( $params['filterredir'] == 'redirects' ) {
79 $this->addWhereFld( 'page_is_redirect', 1 );
80 } elseif ( $params['filterredir'] == 'nonredirects' ) {
81 $this->addWhereFld( 'page_is_redirect', 0 );
84 $this->addWhereFld( 'page_namespace', $params['namespace'] );
85 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
86 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
87 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
88 $this->addWhereRange( 'page_title', $dir, $from, $to );
90 if ( isset( $params['prefix'] ) ) {
91 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
94 if ( is_null( $resultPageSet ) ) {
95 $selectFields = array(
96 'page_namespace',
97 'page_title',
98 'page_id'
100 } else {
101 $selectFields = $resultPageSet->getPageTableFields();
104 $this->addFields( $selectFields );
105 $forceNameTitleIndex = true;
106 if ( isset( $params['minsize'] ) ) {
107 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
108 $forceNameTitleIndex = false;
111 if ( isset( $params['maxsize'] ) ) {
112 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
113 $forceNameTitleIndex = false;
116 // Page protection filtering
117 if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) {
118 $this->addTables( 'page_restrictions' );
119 $this->addWhere( 'page_id=pr_page' );
120 $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
122 if ( count( $params['prtype'] ) ) {
123 $this->addWhereFld( 'pr_type', $params['prtype'] );
125 if ( isset( $params['prlevel'] ) ) {
126 // Remove the empty string and '*' from the prlevel array
127 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
129 if ( count( $prlevel ) ) {
130 $this->addWhereFld( 'pr_level', $prlevel );
133 if ( $params['prfiltercascade'] == 'cascading' ) {
134 $this->addWhereFld( 'pr_cascade', 1 );
135 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
136 $this->addWhereFld( 'pr_cascade', 0 );
139 $forceNameTitleIndex = false;
141 if ( $params['prexpiry'] == 'indefinite' ) {
142 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
143 } elseif ( $params['prexpiry'] == 'definite' ) {
144 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
147 $this->addOption( 'DISTINCT' );
149 } elseif ( isset( $params['prlevel'] ) ) {
150 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
153 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
154 $this->addTables( 'langlinks' );
155 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
156 $this->addWhere( 'll_from IS NULL' );
157 $forceNameTitleIndex = false;
158 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
159 $this->addTables( 'langlinks' );
160 $this->addWhere( 'page_id=ll_from' );
161 $this->addOption( 'STRAIGHT_JOIN' );
162 // We have to GROUP BY all selected fields to stop
163 // PostgreSQL from whining
164 $this->addOption( 'GROUP BY', $selectFields );
165 $forceNameTitleIndex = false;
168 if ( $forceNameTitleIndex ) {
169 $this->addOption( 'USE INDEX', 'name_title' );
172 $limit = $params['limit'];
173 $this->addOption( 'LIMIT', $limit + 1 );
174 $res = $this->select( __METHOD__ );
176 //Get gender information
177 if ( MWNamespace::hasGenderDistinction( $params['namespace'] ) ) {
178 $users = array();
179 foreach ( $res as $row ) {
180 $users[] = $row->page_title;
182 GenderCache::singleton()->doQuery( $users, __METHOD__ );
183 $res->rewind(); //reset
186 $count = 0;
187 $result = $this->getResult();
188 foreach ( $res as $row ) {
189 if ( ++ $count > $limit ) {
190 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
191 $this->setContinueEnumParameter( 'continue', $row->page_title );
192 break;
195 if ( is_null( $resultPageSet ) ) {
196 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
197 $vals = array(
198 'pageid' => intval( $row->page_id ),
199 'ns' => intval( $title->getNamespace() ),
200 'title' => $title->getPrefixedText()
202 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
203 if ( !$fit ) {
204 $this->setContinueEnumParameter( 'continue', $row->page_title );
205 break;
207 } else {
208 $resultPageSet->processDbRow( $row );
212 if ( is_null( $resultPageSet ) ) {
213 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
217 public function getAllowedParams() {
218 global $wgRestrictionLevels;
220 return array(
221 'from' => null,
222 'continue' => null,
223 'to' => null,
224 'prefix' => null,
225 'namespace' => array(
226 ApiBase::PARAM_DFLT => NS_MAIN,
227 ApiBase::PARAM_TYPE => 'namespace',
229 'filterredir' => array(
230 ApiBase::PARAM_DFLT => 'all',
231 ApiBase::PARAM_TYPE => array(
232 'all',
233 'redirects',
234 'nonredirects'
237 'minsize' => array(
238 ApiBase::PARAM_TYPE => 'integer',
240 'maxsize' => array(
241 ApiBase::PARAM_TYPE => 'integer',
243 'prtype' => array(
244 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
245 ApiBase::PARAM_ISMULTI => true
247 'prlevel' => array(
248 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
249 ApiBase::PARAM_ISMULTI => true
251 'prfiltercascade' => array(
252 ApiBase::PARAM_DFLT => 'all',
253 ApiBase::PARAM_TYPE => array(
254 'cascading',
255 'noncascading',
256 'all'
259 'limit' => array(
260 ApiBase::PARAM_DFLT => 10,
261 ApiBase::PARAM_TYPE => 'limit',
262 ApiBase::PARAM_MIN => 1,
263 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
264 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
266 'dir' => array(
267 ApiBase::PARAM_DFLT => 'ascending',
268 ApiBase::PARAM_TYPE => array(
269 'ascending',
270 'descending'
273 'filterlanglinks' => array(
274 ApiBase::PARAM_TYPE => array(
275 'withlanglinks',
276 'withoutlanglinks',
277 'all'
279 ApiBase::PARAM_DFLT => 'all'
281 'prexpiry' => array(
282 ApiBase::PARAM_TYPE => array(
283 'indefinite',
284 'definite',
285 'all'
287 ApiBase::PARAM_DFLT => 'all'
292 public function getParamDescription() {
293 $p = $this->getModulePrefix();
294 return array(
295 'from' => 'The page title to start enumerating from',
296 'continue' => 'When more results are available, use this to continue',
297 'to' => 'The page title to stop enumerating at',
298 'prefix' => 'Search for all page titles that begin with this value',
299 'namespace' => 'The namespace to enumerate',
300 'filterredir' => 'Which pages to list',
301 'dir' => 'The direction in which to list',
302 'minsize' => 'Limit to pages with at least this many bytes',
303 'maxsize' => 'Limit to pages with at most this many bytes',
304 'prtype' => 'Limit to protected pages only',
305 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
306 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
307 'filterlanglinks' => array(
308 'Filter based on whether a page has langlinks',
309 'Note that this may not consider langlinks added by extensions.',
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'
321 public function getResultProperties() {
322 return array(
323 '' => array(
324 'pageid' => 'integer',
325 'ns' => 'namespace',
326 'title' => 'string'
331 public function getDescription() {
332 return 'Enumerate all pages sequentially in a given namespace';
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 ) );
342 public function getExamples() {
343 return array(
344 'api.php?action=query&list=allpages&apfrom=B' => array(
345 'Simple Use',
346 'Show a list of pages starting at the letter "B"',
348 'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
349 'Using as Generator',
350 'Show info about 4 pages starting at the letter "T"',
352 'api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content' => array(
353 'Show content of first 2 non-redirect pages beginning at "Re"',
358 public function getHelpUrls() {
359 return 'https://www.mediawiki.org/wiki/API:Allpages';