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
28 * Query module to enumerate all available pages.
32 class ApiQueryAllPages
extends ApiQueryGeneratorBase
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'ap' );
38 public function execute() {
42 public function getCacheMode( $params ) {
47 * @param $resultPageSet ApiPageSet
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 );
59 * @param $resultPageSet ApiPageSet
62 private function run( $resultPageSet = null ) {
65 $params = $this->extractRequestParams();
68 $this->addTables( 'page' );
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" );
76 $op = $params['dir'] == 'descending' ?
'<' : '>';
77 $cont_from = $db->addQuotes( $cont[0] );
78 $this->addWhere( "page_title $op= $cont_from" );
81 if ( $params['filterredir'] == 'redirects' ) {
82 $this->addWhereFld( 'page_is_redirect', 1 );
83 } elseif ( $params['filterredir'] == 'nonredirects' ) {
84 $this->addWhereFld( 'page_is_redirect', 0 );
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 );
93 if ( isset( $params['prefix'] ) ) {
94 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
97 if ( is_null( $resultPageSet ) ) {
98 $selectFields = array(
104 $selectFields = $resultPageSet->getPageTableFields();
107 $this->addFields( $selectFields );
108 $forceNameTitleIndex = true;
109 if ( isset( $params['minsize'] ) ) {
110 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
111 $forceNameTitleIndex = false;
114 if ( isset( $params['maxsize'] ) ) {
115 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
116 $forceNameTitleIndex = false;
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() ) );
125 if ( count( $params['prtype'] ) ) {
126 $this->addWhereFld( 'pr_type', $params['prtype'] );
128 if ( isset( $params['prlevel'] ) ) {
129 // Remove the empty string and '*' from the prlevel array
130 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
132 if ( count( $prlevel ) ) {
133 $this->addWhereFld( 'pr_level', $prlevel );
136 if ( $params['prfiltercascade'] == 'cascading' ) {
137 $this->addWhereFld( 'pr_cascade', 1 );
138 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
139 $this->addWhereFld( 'pr_cascade', 0 );
142 $this->addOption( 'DISTINCT' );
144 $forceNameTitleIndex = false;
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() )}" );
152 } elseif ( isset( $params['prlevel'] ) ) {
153 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
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;
171 if ( $forceNameTitleIndex ) {
172 $this->addOption( 'USE INDEX', 'name_title' );
175 $limit = $params['limit'];
176 $this->addOption( 'LIMIT', $limit +
1 );
177 $res = $this->select( __METHOD__
);
179 //Get gender information
180 if( MWNamespace
::hasGenderDistinction( $params['namespace'] ) ) {
182 foreach ( $res as $row ) {
183 $users[] = $row->page_title
;
185 GenderCache
::singleton()->doQuery( $users, __METHOD__
);
186 $res->rewind(); //reset
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
);
198 if ( is_null( $resultPageSet ) ) {
199 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
201 'pageid' => intval( $row->page_id
),
202 'ns' => intval( $title->getNamespace() ),
203 'title' => $title->getPrefixedText()
205 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
207 $this->setContinueEnumParameter( 'continue', $row->page_title
);
211 $resultPageSet->processDbRow( $row );
215 if ( is_null( $resultPageSet ) ) {
216 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
220 public function getAllowedParams() {
221 global $wgRestrictionLevels;
228 'namespace' => array(
229 ApiBase
::PARAM_DFLT
=> 0,
230 ApiBase
::PARAM_TYPE
=> 'namespace',
232 'filterredir' => array(
233 ApiBase
::PARAM_DFLT
=> 'all',
234 ApiBase
::PARAM_TYPE
=> array(
241 ApiBase
::PARAM_TYPE
=> 'integer',
244 ApiBase
::PARAM_TYPE
=> 'integer',
247 ApiBase
::PARAM_TYPE
=> Title
::getFilteredRestrictionTypes( true ),
248 ApiBase
::PARAM_ISMULTI
=> true
251 ApiBase
::PARAM_TYPE
=> $wgRestrictionLevels,
252 ApiBase
::PARAM_ISMULTI
=> true
254 'prfiltercascade' => array(
255 ApiBase
::PARAM_DFLT
=> 'all',
256 ApiBase
::PARAM_TYPE
=> 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
270 ApiBase
::PARAM_DFLT
=> 'ascending',
271 ApiBase
::PARAM_TYPE
=> array(
276 'filterlanglinks' => array(
277 ApiBase
::PARAM_TYPE
=> array(
282 ApiBase
::PARAM_DFLT
=> 'all'
285 ApiBase
::PARAM_TYPE
=> array(
290 ApiBase
::PARAM_DFLT
=> 'all'
295 public function getParamDescription() {
296 $p = $this->getModulePrefix();
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.',
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() {
324 'pageid' => 'integer',
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 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
343 public function getExamples() {
345 'api.php?action=query&list=allpages&apfrom=B' => array(
347 'Show a list of pages starting at the letter "B"',
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"',
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"',
359 public function getHelpUrls() {
360 return 'https://www.mediawiki.org/wiki/API:Allpages';
363 public function getVersion() {
364 return __CLASS__
. ': $Id$';