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() ) {
53 'Use "gapfilterredir=nonredirects" option instead of "redirects" ' .
54 'when using allpages as a generator',
59 $this->run( $resultPageSet );
63 * @param $resultPageSet ApiPageSet
66 private function run( $resultPageSet = null ) {
69 $params = $this->extractRequestParams();
72 $this->addTables( 'page' );
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" );
82 if ( $params['filterredir'] == 'redirects' ) {
83 $this->addWhereFld( 'page_is_redirect', 1 );
84 } elseif ( $params['filterredir'] == 'nonredirects' ) {
85 $this->addWhereFld( 'page_is_redirect', 0 );
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 );
94 if ( isset( $params['prefix'] ) ) {
95 $this->addWhere( 'page_title' . $db->buildLike(
96 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
100 if ( is_null( $resultPageSet ) ) {
101 $selectFields = array(
107 $selectFields = $resultPageSet->getPageTableFields();
110 $this->addFields( $selectFields );
111 $forceNameTitleIndex = true;
112 if ( isset( $params['minsize'] ) ) {
113 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
114 $forceNameTitleIndex = false;
117 if ( isset( $params['maxsize'] ) ) {
118 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
119 $forceNameTitleIndex = false;
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" );
128 if ( count( $params['prtype'] ) ) {
129 $this->addWhereFld( 'pr_type', $params['prtype'] );
131 if ( isset( $params['prlevel'] ) ) {
132 // Remove the empty string and '*' from the prlevel array
133 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
135 if ( count( $prlevel ) ) {
136 $this->addWhereFld( 'pr_level', $prlevel );
139 if ( $params['prfiltercascade'] == 'cascading' ) {
140 $this->addWhereFld( 'pr_cascade', 1 );
141 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
142 $this->addWhereFld( 'pr_cascade', 0 );
145 $forceNameTitleIndex = false;
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() )}" );
153 $this->addOption( 'DISTINCT' );
154 } elseif ( isset( $params['prlevel'] ) ) {
155 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
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;
173 if ( $forceNameTitleIndex ) {
174 $this->addOption( 'USE INDEX', 'name_title' );
177 $limit = $params['limit'];
178 $this->addOption( 'LIMIT', $limit +
1 );
179 $res = $this->select( __METHOD__
);
181 //Get gender information
182 if ( MWNamespace
::hasGenderDistinction( $params['namespace'] ) ) {
184 foreach ( $res as $row ) {
185 $users[] = $row->page_title
;
187 GenderCache
::singleton()->doQuery( $users, __METHOD__
);
188 $res->rewind(); //reset
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
);
201 if ( is_null( $resultPageSet ) ) {
202 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
204 'pageid' => intval( $row->page_id
),
205 'ns' => intval( $title->getNamespace() ),
206 'title' => $title->getPrefixedText()
208 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
210 $this->setContinueEnumParameter( 'continue', $row->page_title
);
214 $resultPageSet->processDbRow( $row );
218 if ( is_null( $resultPageSet ) ) {
219 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
223 public function getAllowedParams() {
224 global $wgRestrictionLevels;
231 'namespace' => array(
232 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
233 ApiBase
::PARAM_TYPE
=> 'namespace',
235 'filterredir' => array(
236 ApiBase
::PARAM_DFLT
=> 'all',
237 ApiBase
::PARAM_TYPE
=> array(
244 ApiBase
::PARAM_TYPE
=> 'integer',
247 ApiBase
::PARAM_TYPE
=> 'integer',
250 ApiBase
::PARAM_TYPE
=> Title
::getFilteredRestrictionTypes( true ),
251 ApiBase
::PARAM_ISMULTI
=> true
254 ApiBase
::PARAM_TYPE
=> $wgRestrictionLevels,
255 ApiBase
::PARAM_ISMULTI
=> true
257 'prfiltercascade' => array(
258 ApiBase
::PARAM_DFLT
=> 'all',
259 ApiBase
::PARAM_TYPE
=> 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
273 ApiBase
::PARAM_DFLT
=> 'ascending',
274 ApiBase
::PARAM_TYPE
=> array(
279 'filterlanglinks' => array(
280 ApiBase
::PARAM_TYPE
=> array(
285 ApiBase
::PARAM_DFLT
=> 'all'
288 ApiBase
::PARAM_TYPE
=> array(
293 ApiBase
::PARAM_DFLT
=> 'all'
298 public function getParamDescription() {
299 $p = $this->getModulePrefix();
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)",
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.',
319 'limit' => 'How many total pages to return.',
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'
329 public function getResultProperties() {
332 'pageid' => 'integer',
339 public function getDescription() {
340 return 'Enumerate all pages sequentially in a given namespace';
343 public function getPossibleErrors() {
344 return array_merge( parent
::getPossibleErrors(), array(
347 'info' => 'Use "gapfilterredir=nonredirects" option instead of ' .
348 '"redirects" when using allpages as a generator'
350 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
354 public function getExamples() {
356 'api.php?action=query&list=allpages&apfrom=B' => array(
358 'Show a list of pages starting at the letter "B"',
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"',
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"' )
370 public function getHelpUrls() {
371 return 'https://www.mediawiki.org/wiki/API:Allpages';