Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiQueryAllPages.php
blobe243593b1b231812a06895b1f8bd183380fecda2
1 <?php
2 /**
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
24 * @file
27 /**
28 * Query module to enumerate all available pages.
30 * @ingroup API
32 class ApiQueryAllPages extends ApiQueryGeneratorBase {
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'ap' );
38 public function execute() {
39 $this->run();
42 public function getCacheMode( $params ) {
43 return 'public';
46 /**
47 * @param ApiPageSet $resultPageSet
48 * @return void
50 public function executeGenerator( $resultPageSet ) {
51 if ( $resultPageSet->isResolvingRedirects() ) {
52 $this->dieUsage(
53 'Use "gapfilterredir=nonredirects" option instead of "redirects" ' .
54 'when using allpages as a generator',
55 'params'
59 $this->run( $resultPageSet );
62 /**
63 * @param ApiPageSet $resultPageSet
64 * @return void
66 private function run( $resultPageSet = null ) {
67 $db = $this->getDB();
69 $params = $this->extractRequestParams();
71 // Page filters
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
91 ? null
92 : $this->titlePartToKey( $params['from'], $params['namespace'] ) );
93 $to = ( $params['to'] === null
94 ? 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(
106 'page_namespace',
107 'page_title',
108 'page_id'
110 } else {
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' );
172 // MySQL filesorts if we use a GROUP BY that works with the rules
173 // in the 1992 SQL standard (it doesn't like having the
174 // constant-in-WHERE page_namespace column in there). Using the
175 // 1999 rules works fine, but that breaks other DBs. Sigh.
176 /// @todo Once we drop support for 1992-rule DBs, we can simplify this.
177 $dbType = $db->getType();
178 if ( $dbType === 'mysql' || $dbType === 'sqlite' ||
179 $dbType === 'postgres' && $db->getServerVersion() >= 9.1
181 // 1999 rules, or screw-the-rules
182 $this->addOption( 'GROUP BY', array( 'page_title', 'page_id' ) );
183 } else {
184 // 1992 rules
185 $this->addOption( 'GROUP BY', $selectFields );
188 $forceNameTitleIndex = false;
191 if ( $forceNameTitleIndex ) {
192 $this->addOption( 'USE INDEX', 'name_title' );
195 $limit = $params['limit'];
196 $this->addOption( 'LIMIT', $limit + 1 );
197 $res = $this->select( __METHOD__ );
199 //Get gender information
200 if ( MWNamespace::hasGenderDistinction( $params['namespace'] ) ) {
201 $users = array();
202 foreach ( $res as $row ) {
203 $users[] = $row->page_title;
205 GenderCache::singleton()->doQuery( $users, __METHOD__ );
206 $res->rewind(); //reset
209 $count = 0;
210 $result = $this->getResult();
211 foreach ( $res as $row ) {
212 if ( ++$count > $limit ) {
213 // We've reached the one extra which shows that there are
214 // additional pages to be had. Stop here...
215 $this->setContinueEnumParameter( 'continue', $row->page_title );
216 break;
219 if ( is_null( $resultPageSet ) ) {
220 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
221 $vals = array(
222 'pageid' => intval( $row->page_id ),
223 'ns' => intval( $title->getNamespace() ),
224 'title' => $title->getPrefixedText()
226 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
227 if ( !$fit ) {
228 $this->setContinueEnumParameter( 'continue', $row->page_title );
229 break;
231 } else {
232 $resultPageSet->processDbRow( $row );
236 if ( is_null( $resultPageSet ) ) {
237 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
241 public function getAllowedParams() {
242 return array(
243 'from' => null,
244 'continue' => array(
245 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
247 'to' => null,
248 'prefix' => null,
249 'namespace' => array(
250 ApiBase::PARAM_DFLT => NS_MAIN,
251 ApiBase::PARAM_TYPE => 'namespace',
253 'filterredir' => array(
254 ApiBase::PARAM_DFLT => 'all',
255 ApiBase::PARAM_TYPE => array(
256 'all',
257 'redirects',
258 'nonredirects'
261 'minsize' => array(
262 ApiBase::PARAM_TYPE => 'integer',
264 'maxsize' => array(
265 ApiBase::PARAM_TYPE => 'integer',
267 'prtype' => array(
268 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
269 ApiBase::PARAM_ISMULTI => true
271 'prlevel' => array(
272 ApiBase::PARAM_TYPE => $this->getConfig()->get( 'RestrictionLevels' ),
273 ApiBase::PARAM_ISMULTI => true
275 'prfiltercascade' => array(
276 ApiBase::PARAM_DFLT => 'all',
277 ApiBase::PARAM_TYPE => array(
278 'cascading',
279 'noncascading',
280 'all'
283 'limit' => array(
284 ApiBase::PARAM_DFLT => 10,
285 ApiBase::PARAM_TYPE => 'limit',
286 ApiBase::PARAM_MIN => 1,
287 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
288 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
290 'dir' => array(
291 ApiBase::PARAM_DFLT => 'ascending',
292 ApiBase::PARAM_TYPE => array(
293 'ascending',
294 'descending'
297 'filterlanglinks' => array(
298 ApiBase::PARAM_TYPE => array(
299 'withlanglinks',
300 'withoutlanglinks',
301 'all'
303 ApiBase::PARAM_DFLT => 'all'
305 'prexpiry' => array(
306 ApiBase::PARAM_TYPE => array(
307 'indefinite',
308 'definite',
309 'all'
311 ApiBase::PARAM_DFLT => 'all'
316 protected function getExamplesMessages() {
317 return array(
318 'action=query&list=allpages&apfrom=B'
319 => 'apihelp-query+allpages-example-B',
320 'action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info'
321 => 'apihelp-query+allpages-example-generator',
322 'action=query&generator=allpages&gaplimit=2&' .
323 'gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
324 => 'apihelp-query+allpages-example-generator-revisions',
328 public function getHelpUrls() {
329 return 'https://www.mediawiki.org/wiki/API:Allpages';