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( ApiQuery
$query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'ap' );
38 public function execute() {
42 public function getCacheMode( $params ) {
47 * @param ApiPageSet $resultPageSet
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 ApiPageSet $resultPageSet
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
92 : $this->titlePartToKey( $params['from'], $params['namespace'] ) );
93 $to = ( $params['to'] === null
95 : $this->titlePartToKey( $params['to'], $params['namespace'] ) );
96 $this->addWhereRange( 'page_title', $dir, $from, $to );
98 if ( isset( $params['prefix'] ) ) {
99 $this->addWhere( 'page_title' . $db->buildLike(
100 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
101 $db->anyString() ) );
104 if ( is_null( $resultPageSet ) ) {
105 $selectFields = array(
111 $selectFields = $resultPageSet->getPageTableFields();
114 $this->addFields( $selectFields );
115 $forceNameTitleIndex = true;
116 if ( isset( $params['minsize'] ) ) {
117 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
118 $forceNameTitleIndex = false;
121 if ( isset( $params['maxsize'] ) ) {
122 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
123 $forceNameTitleIndex = false;
126 // Page protection filtering
127 if ( count( $params['prtype'] ) ||
$params['prexpiry'] != 'all' ) {
128 $this->addTables( 'page_restrictions' );
129 $this->addWhere( 'page_id=pr_page' );
130 $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
132 if ( count( $params['prtype'] ) ) {
133 $this->addWhereFld( 'pr_type', $params['prtype'] );
135 if ( isset( $params['prlevel'] ) ) {
136 // Remove the empty string and '*' from the prlevel array
137 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
139 if ( count( $prlevel ) ) {
140 $this->addWhereFld( 'pr_level', $prlevel );
143 if ( $params['prfiltercascade'] == 'cascading' ) {
144 $this->addWhereFld( 'pr_cascade', 1 );
145 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
146 $this->addWhereFld( 'pr_cascade', 0 );
149 $forceNameTitleIndex = false;
151 if ( $params['prexpiry'] == 'indefinite' ) {
152 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
153 } elseif ( $params['prexpiry'] == 'definite' ) {
154 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
157 $this->addOption( 'DISTINCT' );
158 } elseif ( isset( $params['prlevel'] ) ) {
159 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
162 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
163 $this->addTables( 'langlinks' );
164 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
165 $this->addWhere( 'll_from IS NULL' );
166 $forceNameTitleIndex = false;
167 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
168 $this->addTables( 'langlinks' );
169 $this->addWhere( 'page_id=ll_from' );
170 $this->addOption( 'STRAIGHT_JOIN' );
171 // We have to GROUP BY all selected fields to stop
172 // PostgreSQL from whining
173 $this->addOption( 'GROUP BY', $selectFields );
174 $forceNameTitleIndex = false;
177 if ( $forceNameTitleIndex ) {
178 $this->addOption( 'USE INDEX', 'name_title' );
181 $limit = $params['limit'];
182 $this->addOption( 'LIMIT', $limit +
1 );
183 $res = $this->select( __METHOD__
);
185 //Get gender information
186 if ( MWNamespace
::hasGenderDistinction( $params['namespace'] ) ) {
188 foreach ( $res as $row ) {
189 $users[] = $row->page_title
;
191 GenderCache
::singleton()->doQuery( $users, __METHOD__
);
192 $res->rewind(); //reset
196 $result = $this->getResult();
197 foreach ( $res as $row ) {
198 if ( ++
$count > $limit ) {
199 // We've reached the one extra which shows that there are
200 // additional pages to be had. Stop here...
201 $this->setContinueEnumParameter( 'continue', $row->page_title
);
205 if ( is_null( $resultPageSet ) ) {
206 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
208 'pageid' => intval( $row->page_id
),
209 'ns' => intval( $title->getNamespace() ),
210 'title' => $title->getPrefixedText()
212 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
214 $this->setContinueEnumParameter( 'continue', $row->page_title
);
218 $resultPageSet->processDbRow( $row );
222 if ( is_null( $resultPageSet ) ) {
223 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
227 public function getAllowedParams() {
233 'namespace' => array(
234 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
235 ApiBase
::PARAM_TYPE
=> 'namespace',
237 'filterredir' => array(
238 ApiBase
::PARAM_DFLT
=> 'all',
239 ApiBase
::PARAM_TYPE
=> array(
246 ApiBase
::PARAM_TYPE
=> 'integer',
249 ApiBase
::PARAM_TYPE
=> 'integer',
252 ApiBase
::PARAM_TYPE
=> Title
::getFilteredRestrictionTypes( true ),
253 ApiBase
::PARAM_ISMULTI
=> true
256 ApiBase
::PARAM_TYPE
=> $this->getConfig()->get( 'RestrictionLevels' ),
257 ApiBase
::PARAM_ISMULTI
=> true
259 'prfiltercascade' => array(
260 ApiBase
::PARAM_DFLT
=> 'all',
261 ApiBase
::PARAM_TYPE
=> array(
268 ApiBase
::PARAM_DFLT
=> 10,
269 ApiBase
::PARAM_TYPE
=> 'limit',
270 ApiBase
::PARAM_MIN
=> 1,
271 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
272 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
275 ApiBase
::PARAM_DFLT
=> 'ascending',
276 ApiBase
::PARAM_TYPE
=> array(
281 'filterlanglinks' => array(
282 ApiBase
::PARAM_TYPE
=> array(
287 ApiBase
::PARAM_DFLT
=> 'all'
290 ApiBase
::PARAM_TYPE
=> array(
295 ApiBase
::PARAM_DFLT
=> 'all'
300 public function getParamDescription() {
301 $p = $this->getModulePrefix();
304 'from' => 'The page title to start enumerating from',
305 'continue' => 'When more results are available, use this to continue',
306 'to' => 'The page title to stop enumerating at',
307 'prefix' => 'Search for all page titles that begin with this value',
308 'namespace' => 'The namespace to enumerate',
309 'filterredir' => 'Which pages to list',
310 'dir' => 'The direction in which to list',
311 'minsize' => 'Limit to pages with at least this many bytes',
312 'maxsize' => 'Limit to pages with at most this many bytes',
313 'prtype' => 'Limit to protected pages only',
314 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
316 => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
317 'filterlanglinks' => array(
318 'Filter based on whether a page has langlinks',
319 'Note that this may not consider langlinks added by extensions.',
321 'limit' => 'How many total pages to return.',
323 'Which protection expiry to filter the page on',
324 ' indefinite - Get only pages with indefinite protection expiry',
325 ' definite - Get only pages with a definite (specific) protection expiry',
326 ' all - Get pages with any protections expiry'
331 public function getDescription() {
332 return 'Enumerate all pages sequentially in a given namespace.';
335 public function getExamples() {
337 'api.php?action=query&list=allpages&apfrom=B' => array(
339 'Show a list of pages starting at the letter "B"',
341 'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
342 'Using as Generator',
343 'Show info about 4 pages starting at the letter "T"',
345 'api.php?action=query&generator=allpages&gaplimit=2&' .
346 'gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
347 => array( 'Show content of first 2 non-redirect pages beginning at "Re"' )
351 public function getHelpUrls() {
352 return 'https://www.mediawiki.org/wiki/API:Allpages';