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 = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
91 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
92 $this->addWhereRange( 'page_title', $dir, $from, $to );
94 if ( isset( $params['prefix'] ) ) {
95 $this->addWhere( 'page_title' .
96 $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
99 if ( is_null( $resultPageSet ) ) {
100 $selectFields = array(
106 $selectFields = $resultPageSet->getPageTableFields();
109 $this->addFields( $selectFields );
110 $forceNameTitleIndex = true;
111 if ( isset( $params['minsize'] ) ) {
112 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
113 $forceNameTitleIndex = false;
116 if ( isset( $params['maxsize'] ) ) {
117 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
118 $forceNameTitleIndex = false;
121 // Page protection filtering
122 if ( count( $params['prtype'] ) ||
$params['prexpiry'] != 'all' ) {
123 $this->addTables( 'page_restrictions' );
124 $this->addWhere( 'page_id=pr_page' );
125 $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
127 if ( count( $params['prtype'] ) ) {
128 $this->addWhereFld( 'pr_type', $params['prtype'] );
130 if ( isset( $params['prlevel'] ) ) {
131 // Remove the empty string and '*' from the prlevel array
132 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
134 if ( count( $prlevel ) ) {
135 $this->addWhereFld( 'pr_level', $prlevel );
138 if ( $params['prfiltercascade'] == 'cascading' ) {
139 $this->addWhereFld( 'pr_cascade', 1 );
140 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
141 $this->addWhereFld( 'pr_cascade', 0 );
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 $this->addOption( 'DISTINCT' );
153 } elseif ( isset( $params['prlevel'] ) ) {
154 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
157 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
158 $this->addTables( 'langlinks' );
159 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
160 $this->addWhere( 'll_from IS NULL' );
161 $forceNameTitleIndex = false;
162 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
163 $this->addTables( 'langlinks' );
164 $this->addWhere( 'page_id=ll_from' );
165 $this->addOption( 'STRAIGHT_JOIN' );
166 // We have to GROUP BY all selected fields to stop
167 // PostgreSQL from whining
168 $this->addOption( 'GROUP BY', $selectFields );
169 $forceNameTitleIndex = false;
172 if ( $forceNameTitleIndex ) {
173 $this->addOption( 'USE INDEX', 'name_title' );
176 $limit = $params['limit'];
177 $this->addOption( 'LIMIT', $limit +
1 );
178 $res = $this->select( __METHOD__
);
180 //Get gender information
181 if ( MWNamespace
::hasGenderDistinction( $params['namespace'] ) ) {
183 foreach ( $res as $row ) {
184 $users[] = $row->page_title
;
186 GenderCache
::singleton()->doQuery( $users, __METHOD__
);
187 $res->rewind(); //reset
191 $result = $this->getResult();
192 foreach ( $res as $row ) {
193 if ( ++
$count > $limit ) {
194 // We've reached the one extra which shows that there are
195 // additional pages to be had. Stop here...
196 $this->setContinueEnumParameter( 'continue', $row->page_title
);
200 if ( is_null( $resultPageSet ) ) {
201 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
203 'pageid' => intval( $row->page_id
),
204 'ns' => intval( $title->getNamespace() ),
205 'title' => $title->getPrefixedText()
207 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
209 $this->setContinueEnumParameter( 'continue', $row->page_title
);
213 $resultPageSet->processDbRow( $row );
217 if ( is_null( $resultPageSet ) ) {
218 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
222 public function getAllowedParams() {
223 global $wgRestrictionLevels;
230 'namespace' => array(
231 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
232 ApiBase
::PARAM_TYPE
=> 'namespace',
234 'filterredir' => array(
235 ApiBase
::PARAM_DFLT
=> 'all',
236 ApiBase
::PARAM_TYPE
=> array(
243 ApiBase
::PARAM_TYPE
=> 'integer',
246 ApiBase
::PARAM_TYPE
=> 'integer',
249 ApiBase
::PARAM_TYPE
=> Title
::getFilteredRestrictionTypes( true ),
250 ApiBase
::PARAM_ISMULTI
=> true
253 ApiBase
::PARAM_TYPE
=> $wgRestrictionLevels,
254 ApiBase
::PARAM_ISMULTI
=> true
256 'prfiltercascade' => array(
257 ApiBase
::PARAM_DFLT
=> 'all',
258 ApiBase
::PARAM_TYPE
=> array(
265 ApiBase
::PARAM_DFLT
=> 10,
266 ApiBase
::PARAM_TYPE
=> 'limit',
267 ApiBase
::PARAM_MIN
=> 1,
268 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
269 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
272 ApiBase
::PARAM_DFLT
=> 'ascending',
273 ApiBase
::PARAM_TYPE
=> array(
278 'filterlanglinks' => array(
279 ApiBase
::PARAM_TYPE
=> array(
284 ApiBase
::PARAM_DFLT
=> 'all'
287 ApiBase
::PARAM_TYPE
=> array(
292 ApiBase
::PARAM_DFLT
=> 'all'
297 public function getParamDescription() {
298 $p = $this->getModulePrefix();
301 'from' => 'The page title to start enumerating from',
302 'continue' => 'When more results are available, use this to continue',
303 'to' => 'The page title to stop enumerating at',
304 'prefix' => 'Search for all page titles that begin with this value',
305 'namespace' => 'The namespace to enumerate',
306 'filterredir' => 'Which pages to list',
307 'dir' => 'The direction in which to list',
308 'minsize' => 'Limit to pages with at least this many bytes',
309 'maxsize' => 'Limit to pages with at most this many bytes',
310 'prtype' => 'Limit to protected pages only',
311 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
313 => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
314 'filterlanglinks' => array(
315 'Filter based on whether a page has langlinks',
316 'Note that this may not consider langlinks added by extensions.',
318 'limit' => 'How many total pages to return.',
320 'Which protection expiry to filter the page on',
321 ' indefinite - Get only pages with indefinite protection expiry',
322 ' definite - Get only pages with a definite (specific) protection expiry',
323 ' all - Get pages with any protections expiry'
328 public function getResultProperties() {
331 'pageid' => 'integer',
338 public function getDescription() {
339 return 'Enumerate all pages sequentially in a given namespace';
342 public function getPossibleErrors() {
343 return array_merge( parent
::getPossibleErrors(), array(
346 'info' => 'Use "gapfilterredir=nonredirects" option instead of ' .
347 '"redirects" when using allpages as a generator'
349 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
353 public function getExamples() {
355 'api.php?action=query&list=allpages&apfrom=B' => array(
357 'Show a list of pages starting at the letter "B"',
359 'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
360 'Using as Generator',
361 'Show info about 4 pages starting at the letter "T"',
363 'api.php?action=query&generator=allpages&gaplimit=2&' .
364 'gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
365 => array( 'Show content of first 2 non-redirect pages beginning at "Re"' )
369 public function getHelpUrls() {
370 return 'https://www.mediawiki.org/wiki/API:Allpages';