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() ) {
53 $this->dieWithError( 'apierror-allpages-generator-redirects', 'params' );
56 $this->run( $resultPageSet );
60 * @param ApiPageSet $resultPageSet
63 private function run( $resultPageSet = null ) {
66 $params = $this->extractRequestParams();
69 $this->addTables( 'page' );
71 if ( !is_null( $params['continue'] ) ) {
72 $cont = explode( '|', $params['continue'] );
73 $this->dieContinueUsageIf( count( $cont ) != 1 );
74 $op = $params['dir'] == 'descending' ?
'<' : '>';
75 $cont_from = $db->addQuotes( $cont[0] );
76 $this->addWhere( "page_title $op= $cont_from" );
79 if ( $params['filterredir'] == 'redirects' ) {
80 $this->addWhereFld( 'page_is_redirect', 1 );
81 } elseif ( $params['filterredir'] == 'nonredirects' ) {
82 $this->addWhereFld( 'page_is_redirect', 0 );
85 $this->addWhereFld( 'page_namespace', $params['namespace'] );
86 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
87 $from = ( $params['from'] === null
89 : $this->titlePartToKey( $params['from'], $params['namespace'] ) );
90 $to = ( $params['to'] === null
92 : $this->titlePartToKey( $params['to'], $params['namespace'] ) );
93 $this->addWhereRange( 'page_title', $dir, $from, $to );
95 if ( isset( $params['prefix'] ) ) {
96 $this->addWhere( 'page_title' . $db->buildLike(
97 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
101 if ( is_null( $resultPageSet ) ) {
108 $selectFields = $resultPageSet->getPageTableFields();
111 $this->addFields( $selectFields );
112 $forceNameTitleIndex = true;
113 if ( isset( $params['minsize'] ) ) {
114 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
115 $forceNameTitleIndex = false;
118 if ( isset( $params['maxsize'] ) ) {
119 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
120 $forceNameTitleIndex = false;
123 // Page protection filtering
124 if ( count( $params['prtype'] ) ||
$params['prexpiry'] != 'all' ) {
125 $this->addTables( 'page_restrictions' );
126 $this->addWhere( 'page_id=pr_page' );
127 $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
129 if ( count( $params['prtype'] ) ) {
130 $this->addWhereFld( 'pr_type', $params['prtype'] );
132 if ( isset( $params['prlevel'] ) ) {
133 // Remove the empty string and '*' from the prlevel array
134 $prlevel = array_diff( $params['prlevel'], [ '', '*' ] );
136 if ( count( $prlevel ) ) {
137 $this->addWhereFld( 'pr_level', $prlevel );
140 if ( $params['prfiltercascade'] == 'cascading' ) {
141 $this->addWhereFld( 'pr_cascade', 1 );
142 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
143 $this->addWhereFld( 'pr_cascade', 0 );
146 $forceNameTitleIndex = false;
148 if ( $params['prexpiry'] == 'indefinite' ) {
149 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
150 } elseif ( $params['prexpiry'] == 'definite' ) {
151 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
154 $this->addOption( 'DISTINCT' );
155 } elseif ( isset( $params['prlevel'] ) ) {
157 [ 'apierror-invalidparammix-mustusewith', 'prlevel', 'prtype' ], 'invalidparammix'
161 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
162 $this->addTables( 'langlinks' );
163 $this->addJoinConds( [ 'langlinks' => [ 'LEFT JOIN', 'page_id=ll_from' ] ] );
164 $this->addWhere( 'll_from IS NULL' );
165 $forceNameTitleIndex = false;
166 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
167 $this->addTables( 'langlinks' );
168 $this->addWhere( 'page_id=ll_from' );
169 $this->addOption( 'STRAIGHT_JOIN' );
171 // MySQL filesorts if we use a GROUP BY that works with the rules
172 // in the 1992 SQL standard (it doesn't like having the
173 // constant-in-WHERE page_namespace column in there). Using the
174 // 1999 rules works fine, but that breaks other DBs. Sigh.
175 /// @todo Once we drop support for 1992-rule DBs, we can simplify this.
176 $dbType = $db->getType();
177 if ( $dbType === 'mysql' ||
$dbType === 'sqlite' ) {
178 // Ignore the rules, or 1999 rules if you count unique keys
179 // over non-NULL columns as satisfying the requirement for
180 // "functional dependency" and don't require including
181 // constant-in-WHERE columns in the GROUP BY.
182 $this->addOption( 'GROUP BY', [ 'page_title' ] );
183 } elseif ( $dbType === 'postgres' && $db->getServerVersion() >= 9.1 ) {
184 // 1999 rules only counting primary keys
185 $this->addOption( 'GROUP BY', [ 'page_title', 'page_id' ] );
188 $this->addOption( 'GROUP BY', $selectFields );
191 $forceNameTitleIndex = false;
194 if ( $forceNameTitleIndex ) {
195 $this->addOption( 'USE INDEX', 'name_title' );
198 $limit = $params['limit'];
199 $this->addOption( 'LIMIT', $limit +
1 );
200 $res = $this->select( __METHOD__
);
202 // Get gender information
203 if ( MWNamespace
::hasGenderDistinction( $params['namespace'] ) ) {
205 foreach ( $res as $row ) {
206 $users[] = $row->page_title
;
208 MediaWikiServices
::getInstance()->getGenderCache()->doQuery( $users, __METHOD__
);
209 $res->rewind(); // reset
213 $result = $this->getResult();
214 foreach ( $res as $row ) {
215 if ( ++
$count > $limit ) {
216 // We've reached the one extra which shows that there are
217 // additional pages to be had. Stop here...
218 $this->setContinueEnumParameter( 'continue', $row->page_title
);
222 if ( is_null( $resultPageSet ) ) {
223 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
225 'pageid' => intval( $row->page_id
),
226 'ns' => intval( $title->getNamespace() ),
227 'title' => $title->getPrefixedText()
229 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
231 $this->setContinueEnumParameter( 'continue', $row->page_title
);
235 $resultPageSet->processDbRow( $row );
239 if ( is_null( $resultPageSet ) ) {
240 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'p' );
244 public function getAllowedParams() {
248 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
253 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
254 ApiBase
::PARAM_TYPE
=> 'namespace',
257 ApiBase
::PARAM_DFLT
=> 'all',
258 ApiBase
::PARAM_TYPE
=> [
265 ApiBase
::PARAM_TYPE
=> 'integer',
268 ApiBase
::PARAM_TYPE
=> 'integer',
271 ApiBase
::PARAM_TYPE
=> Title
::getFilteredRestrictionTypes( true ),
272 ApiBase
::PARAM_ISMULTI
=> true
275 ApiBase
::PARAM_TYPE
=> $this->getConfig()->get( 'RestrictionLevels' ),
276 ApiBase
::PARAM_ISMULTI
=> true
278 'prfiltercascade' => [
279 ApiBase
::PARAM_DFLT
=> 'all',
280 ApiBase
::PARAM_TYPE
=> [
287 ApiBase
::PARAM_DFLT
=> 10,
288 ApiBase
::PARAM_TYPE
=> 'limit',
289 ApiBase
::PARAM_MIN
=> 1,
290 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
291 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
294 ApiBase
::PARAM_DFLT
=> 'ascending',
295 ApiBase
::PARAM_TYPE
=> [
300 'filterlanglinks' => [
301 ApiBase
::PARAM_TYPE
=> [
306 ApiBase
::PARAM_DFLT
=> 'all'
309 ApiBase
::PARAM_TYPE
=> [
314 ApiBase
::PARAM_DFLT
=> 'all'
319 protected function getExamplesMessages() {
321 'action=query&list=allpages&apfrom=B'
322 => 'apihelp-query+allpages-example-B',
323 'action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info'
324 => 'apihelp-query+allpages-example-generator',
325 'action=query&generator=allpages&gaplimit=2&' .
326 'gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
327 => 'apihelp-query+allpages-example-generator-revisions',
331 public function getHelpUrls() {
332 return 'https://www.mediawiki.org/wiki/API:Allpages';