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
26 use MediaWiki\MediaWikiServices
;
29 * Query module to enumerate all available pages.
33 class ApiQueryAllPages
extends ApiQueryGeneratorBase
{
35 public function __construct( ApiQuery
$query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'ap' );
39 public function execute() {
43 public function getCacheMode( $params ) {
48 * @param ApiPageSet $resultPageSet
51 public function executeGenerator( $resultPageSet ) {
52 if ( $resultPageSet->isResolvingRedirects() ) {
54 'Use "gapfilterredir=nonredirects" option instead of "redirects" ' .
55 'when using allpages as a generator',
60 $this->run( $resultPageSet );
64 * @param ApiPageSet $resultPageSet
67 private function run( $resultPageSet = null ) {
70 $params = $this->extractRequestParams();
73 $this->addTables( 'page' );
75 if ( !is_null( $params['continue'] ) ) {
76 $cont = explode( '|', $params['continue'] );
77 $this->dieContinueUsageIf( count( $cont ) != 1 );
78 $op = $params['dir'] == 'descending' ?
'<' : '>';
79 $cont_from = $db->addQuotes( $cont[0] );
80 $this->addWhere( "page_title $op= $cont_from" );
83 if ( $params['filterredir'] == 'redirects' ) {
84 $this->addWhereFld( 'page_is_redirect', 1 );
85 } elseif ( $params['filterredir'] == 'nonredirects' ) {
86 $this->addWhereFld( 'page_is_redirect', 0 );
89 $this->addWhereFld( 'page_namespace', $params['namespace'] );
90 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
91 $from = ( $params['from'] === null
93 : $this->titlePartToKey( $params['from'], $params['namespace'] ) );
94 $to = ( $params['to'] === null
96 : $this->titlePartToKey( $params['to'], $params['namespace'] ) );
97 $this->addWhereRange( 'page_title', $dir, $from, $to );
99 if ( isset( $params['prefix'] ) ) {
100 $this->addWhere( 'page_title' . $db->buildLike(
101 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
102 $db->anyString() ) );
105 if ( is_null( $resultPageSet ) ) {
112 $selectFields = $resultPageSet->getPageTableFields();
115 $this->addFields( $selectFields );
116 $forceNameTitleIndex = true;
117 if ( isset( $params['minsize'] ) ) {
118 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
119 $forceNameTitleIndex = false;
122 if ( isset( $params['maxsize'] ) ) {
123 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
124 $forceNameTitleIndex = false;
127 // Page protection filtering
128 if ( count( $params['prtype'] ) ||
$params['prexpiry'] != 'all' ) {
129 $this->addTables( 'page_restrictions' );
130 $this->addWhere( 'page_id=pr_page' );
131 $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
133 if ( count( $params['prtype'] ) ) {
134 $this->addWhereFld( 'pr_type', $params['prtype'] );
136 if ( isset( $params['prlevel'] ) ) {
137 // Remove the empty string and '*' from the prlevel array
138 $prlevel = array_diff( $params['prlevel'], [ '', '*' ] );
140 if ( count( $prlevel ) ) {
141 $this->addWhereFld( 'pr_level', $prlevel );
144 if ( $params['prfiltercascade'] == 'cascading' ) {
145 $this->addWhereFld( 'pr_cascade', 1 );
146 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
147 $this->addWhereFld( 'pr_cascade', 0 );
150 $forceNameTitleIndex = false;
152 if ( $params['prexpiry'] == 'indefinite' ) {
153 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
154 } elseif ( $params['prexpiry'] == 'definite' ) {
155 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
158 $this->addOption( 'DISTINCT' );
159 } elseif ( isset( $params['prlevel'] ) ) {
160 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
163 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
164 $this->addTables( 'langlinks' );
165 $this->addJoinConds( [ 'langlinks' => [ 'LEFT JOIN', 'page_id=ll_from' ] ] );
166 $this->addWhere( 'll_from IS NULL' );
167 $forceNameTitleIndex = false;
168 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
169 $this->addTables( 'langlinks' );
170 $this->addWhere( 'page_id=ll_from' );
171 $this->addOption( 'STRAIGHT_JOIN' );
173 // MySQL filesorts if we use a GROUP BY that works with the rules
174 // in the 1992 SQL standard (it doesn't like having the
175 // constant-in-WHERE page_namespace column in there). Using the
176 // 1999 rules works fine, but that breaks other DBs. Sigh.
177 /// @todo Once we drop support for 1992-rule DBs, we can simplify this.
178 $dbType = $db->getType();
179 if ( $dbType === 'mysql' ||
$dbType === 'sqlite' ) {
180 // Ignore the rules, or 1999 rules if you count unique keys
181 // over non-NULL columns as satisfying the requirement for
182 // "functional dependency" and don't require including
183 // constant-in-WHERE columns in the GROUP BY.
184 $this->addOption( 'GROUP BY', [ 'page_title' ] );
185 } elseif ( $dbType === 'postgres' && $db->getServerVersion() >= 9.1 ) {
186 // 1999 rules only counting primary keys
187 $this->addOption( 'GROUP BY', [ 'page_title', 'page_id' ] );
190 $this->addOption( 'GROUP BY', $selectFields );
193 $forceNameTitleIndex = false;
196 if ( $forceNameTitleIndex ) {
197 $this->addOption( 'USE INDEX', 'name_title' );
200 $limit = $params['limit'];
201 $this->addOption( 'LIMIT', $limit +
1 );
202 $res = $this->select( __METHOD__
);
204 // Get gender information
205 if ( MWNamespace
::hasGenderDistinction( $params['namespace'] ) ) {
207 foreach ( $res as $row ) {
208 $users[] = $row->page_title
;
210 MediaWikiServices
::getInstance()->getGenderCache()->doQuery( $users, __METHOD__
);
211 $res->rewind(); // reset
215 $result = $this->getResult();
216 foreach ( $res as $row ) {
217 if ( ++
$count > $limit ) {
218 // We've reached the one extra which shows that there are
219 // additional pages to be had. Stop here...
220 $this->setContinueEnumParameter( 'continue', $row->page_title
);
224 if ( is_null( $resultPageSet ) ) {
225 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
227 'pageid' => intval( $row->page_id
),
228 'ns' => intval( $title->getNamespace() ),
229 'title' => $title->getPrefixedText()
231 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
233 $this->setContinueEnumParameter( 'continue', $row->page_title
);
237 $resultPageSet->processDbRow( $row );
241 if ( is_null( $resultPageSet ) ) {
242 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'p' );
246 public function getAllowedParams() {
250 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
255 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
256 ApiBase
::PARAM_TYPE
=> 'namespace',
259 ApiBase
::PARAM_DFLT
=> 'all',
260 ApiBase
::PARAM_TYPE
=> [
267 ApiBase
::PARAM_TYPE
=> 'integer',
270 ApiBase
::PARAM_TYPE
=> 'integer',
273 ApiBase
::PARAM_TYPE
=> Title
::getFilteredRestrictionTypes( true ),
274 ApiBase
::PARAM_ISMULTI
=> true
277 ApiBase
::PARAM_TYPE
=> $this->getConfig()->get( 'RestrictionLevels' ),
278 ApiBase
::PARAM_ISMULTI
=> true
280 'prfiltercascade' => [
281 ApiBase
::PARAM_DFLT
=> 'all',
282 ApiBase
::PARAM_TYPE
=> [
289 ApiBase
::PARAM_DFLT
=> 10,
290 ApiBase
::PARAM_TYPE
=> 'limit',
291 ApiBase
::PARAM_MIN
=> 1,
292 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
293 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
296 ApiBase
::PARAM_DFLT
=> 'ascending',
297 ApiBase
::PARAM_TYPE
=> [
302 'filterlanglinks' => [
303 ApiBase
::PARAM_TYPE
=> [
308 ApiBase
::PARAM_DFLT
=> 'all'
311 ApiBase
::PARAM_TYPE
=> [
316 ApiBase
::PARAM_DFLT
=> 'all'
321 protected function getExamplesMessages() {
323 'action=query&list=allpages&apfrom=B'
324 => 'apihelp-query+allpages-example-B',
325 'action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info'
326 => 'apihelp-query+allpages-example-generator',
327 'action=query&generator=allpages&gaplimit=2&' .
328 'gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
329 => 'apihelp-query+allpages-example-generator-revisions',
333 public function getHelpUrls() {
334 return 'https://www.mediawiki.org/wiki/API:Allpages';