Standardised file description headers, added @file
[mediawiki.git] / includes / api / ApiQueryAllpages.php
blob761db977a1b0c486274cc8e20d608252be4434f5
1 <?php
2 /**
3 * API for MediaWiki 1.8+
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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
32 /**
33 * Query module to enumerate all available pages.
35 * @ingroup API
37 class ApiQueryAllpages extends ApiQueryGeneratorBase {
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'ap' );
43 public function execute() {
44 $this->run();
47 public function getCacheMode( $params ) {
48 return 'public';
51 public function executeGenerator( $resultPageSet ) {
52 if ( $resultPageSet->isResolvingRedirects() ) {
53 $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
56 $this->run( $resultPageSet );
59 private function run( $resultPageSet = null ) {
60 $db = $this->getDB();
62 $params = $this->extractRequestParams();
64 // Page filters
65 $this->addTables( 'page' );
67 if ( $params['filterredir'] == 'redirects' ) {
68 $this->addWhereFld( 'page_is_redirect', 1 );
69 } elseif ( $params['filterredir'] == 'nonredirects' ) {
70 $this->addWhereFld( 'page_is_redirect', 0 );
73 $this->addWhereFld( 'page_namespace', $params['namespace'] );
74 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
75 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
76 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
77 $this->addWhereRange( 'page_title', $dir, $from, $to );
80 if ( isset( $params['prefix'] ) ) {
81 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
84 if ( is_null( $resultPageSet ) ) {
85 $selectFields = array(
86 'page_namespace',
87 'page_title',
88 'page_id'
90 } else {
91 $selectFields = $resultPageSet->getPageTableFields();
94 $this->addFields( $selectFields );
95 $forceNameTitleIndex = true;
96 if ( isset( $params['minsize'] ) ) {
97 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
98 $forceNameTitleIndex = false;
101 if ( isset( $params['maxsize'] ) ) {
102 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
103 $forceNameTitleIndex = false;
106 // Page protection filtering
107 if ( !empty( $params['prtype'] ) ) {
108 $this->addTables( 'page_restrictions' );
109 $this->addWhere( 'page_id=pr_page' );
110 $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
111 $this->addWhereFld( 'pr_type', $params['prtype'] );
113 if ( isset( $params['prlevel'] ) ) {
114 // Remove the empty string and '*' from the prlevel array
115 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
117 if ( !empty( $prlevel ) ) {
118 $this->addWhereFld( 'pr_level', $prlevel );
121 if ( $params['prfiltercascade'] == 'cascading' ) {
122 $this->addWhereFld( 'pr_cascade', 1 );
123 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
124 $this->addWhereFld( 'pr_cascade', 0 );
127 $this->addOption( 'DISTINCT' );
129 $forceNameTitleIndex = false;
131 } elseif ( isset( $params['prlevel'] ) ) {
132 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
135 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
136 $this->addTables( 'langlinks' );
137 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
138 $this->addWhere( 'll_from IS NULL' );
139 $forceNameTitleIndex = false;
140 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
141 $this->addTables( 'langlinks' );
142 $this->addWhere( 'page_id=ll_from' );
143 $this->addOption( 'STRAIGHT_JOIN' );
144 // We have to GROUP BY all selected fields to stop
145 // PostgreSQL from whining
146 $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
147 $forceNameTitleIndex = false;
150 if ( $forceNameTitleIndex ) {
151 $this->addOption( 'USE INDEX', 'name_title' );
154 $limit = $params['limit'];
155 $this->addOption( 'LIMIT', $limit + 1 );
156 $res = $this->select( __METHOD__ );
158 $count = 0;
159 $result = $this->getResult();
160 foreach ( $res as $row ) {
161 if ( ++ $count > $limit ) {
162 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
163 // TODO: Security issue - if the user has no right to view next title, it will still be shown
164 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
165 break;
168 if ( is_null( $resultPageSet ) ) {
169 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
170 $vals = array(
171 'pageid' => intval( $row->page_id ),
172 'ns' => intval( $title->getNamespace() ),
173 'title' => $title->getPrefixedText()
175 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
176 if ( !$fit ) {
177 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
178 break;
180 } else {
181 $resultPageSet->processDbRow( $row );
185 if ( is_null( $resultPageSet ) ) {
186 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
190 public function getAllowedParams() {
191 global $wgRestrictionTypes, $wgRestrictionLevels;
193 return array(
194 'from' => null,
195 'to' => null,
196 'prefix' => null,
197 'namespace' => array(
198 ApiBase::PARAM_DFLT => 0,
199 ApiBase::PARAM_TYPE => 'namespace',
201 'filterredir' => array(
202 ApiBase::PARAM_DFLT => 'all',
203 ApiBase::PARAM_TYPE => array(
204 'all',
205 'redirects',
206 'nonredirects'
209 'minsize' => array(
210 ApiBase::PARAM_TYPE => 'integer',
212 'maxsize' => array(
213 ApiBase::PARAM_TYPE => 'integer',
215 'prtype' => array(
216 ApiBase::PARAM_TYPE => $wgRestrictionTypes,
217 ApiBase::PARAM_ISMULTI => true
219 'prlevel' => array(
220 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
221 ApiBase::PARAM_ISMULTI => true
223 'prfiltercascade' => array(
224 ApiBase::PARAM_DFLT => 'all',
225 ApiBase::PARAM_TYPE => array(
226 'cascading',
227 'noncascading',
228 'all'
231 'limit' => array(
232 ApiBase::PARAM_DFLT => 10,
233 ApiBase::PARAM_TYPE => 'limit',
234 ApiBase::PARAM_MIN => 1,
235 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
236 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
238 'dir' => array(
239 ApiBase::PARAM_DFLT => 'ascending',
240 ApiBase::PARAM_TYPE => array(
241 'ascending',
242 'descending'
245 'filterlanglinks' => array(
246 ApiBase::PARAM_TYPE => array(
247 'withlanglinks',
248 'withoutlanglinks',
249 'all'
251 ApiBase::PARAM_DFLT => 'all'
256 public function getParamDescription() {
257 $p = $this->getModulePrefix();
258 return array(
259 'from' => 'The page title to start enumerating from',
260 'to' => 'The page title to stop enumerating at',
261 'prefix' => 'Search for all page titles that begin with this value',
262 'namespace' => 'The namespace to enumerate',
263 'filterredir' => 'Which pages to list',
264 'dir' => 'The direction in which to list',
265 'minsize' => 'Limit to pages with at least this many bytes',
266 'maxsize' => 'Limit to pages with at most this many bytes',
267 'prtype' => 'Limit to protected pages only',
268 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
269 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
270 'filterlanglinks' => 'Filter based on whether a page has langlinks',
271 'limit' => 'How many total pages to return.'
275 public function getDescription() {
276 return 'Enumerate all pages sequentially in a given namespace';
279 public function getPossibleErrors() {
280 return array_merge( parent::getPossibleErrors(), array(
281 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
282 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
283 ) );
286 protected function getExamples() {
287 return array(
288 'Simple Use',
289 ' Show a list of pages starting at the letter "B"',
290 ' api.php?action=query&list=allpages&apfrom=B',
291 'Using as Generator',
292 ' Show info about 4 pages starting at the letter "T"',
293 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
294 ' Show content of first 2 non-redirect pages begining at "Re"',
295 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
299 public function getVersion() {
300 return __CLASS__ . ': $Id$';