Localisation updates for core and extension messages from translatewiki.net (2010...
[lhc/web/wiklou.git] / includes / api / ApiQueryAllpages.php
1 <?php
2
3 /**
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to enumerate all available pages.
33 *
34 * @ingroup API
35 */
36 class ApiQueryAllpages extends ApiQueryGeneratorBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ap' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 if ( $resultPageSet->isResolvingRedirects() ) {
48 $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
49 }
50
51 $this->run( $resultPageSet );
52 }
53
54 private function run( $resultPageSet = null ) {
55 $db = $this->getDB();
56
57 $params = $this->extractRequestParams();
58
59 // Page filters
60 $this->addTables( 'page' );
61
62 if ( $params['filterredir'] == 'redirects' ) {
63 $this->addWhereFld( 'page_is_redirect', 1 );
64 } elseif ( $params['filterredir'] == 'nonredirects' ) {
65 $this->addWhereFld( 'page_is_redirect', 0 );
66 }
67
68 $this->addWhereFld( 'page_namespace', $params['namespace'] );
69 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
70 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
71 $this->addWhereRange( 'page_title', $dir, $from, null );
72
73 if ( isset( $params['prefix'] ) ) {
74 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
75 }
76
77 if ( is_null( $resultPageSet ) ) {
78 $selectFields = array(
79 'page_namespace',
80 'page_title',
81 'page_id'
82 );
83 } else {
84 $selectFields = $resultPageSet->getPageTableFields();
85 }
86
87 $this->addFields( $selectFields );
88 $forceNameTitleIndex = true;
89 if ( isset( $params['minsize'] ) ) {
90 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
91 $forceNameTitleIndex = false;
92 }
93
94 if ( isset( $params['maxsize'] ) ) {
95 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
96 $forceNameTitleIndex = false;
97 }
98
99 // Page protection filtering
100 if ( !empty( $params['prtype'] ) ) {
101 $this->addTables( 'page_restrictions' );
102 $this->addWhere( 'page_id=pr_page' );
103 $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
104 $this->addWhereFld( 'pr_type', $params['prtype'] );
105
106 if ( isset( $params['prlevel'] ) ) {
107 // Remove the empty string and '*' from the prlevel array
108 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
109
110 if ( !empty( $prlevel ) ) {
111 $this->addWhereFld( 'pr_level', $prlevel );
112 }
113 }
114 if ( $params['prfiltercascade'] == 'cascading' ) {
115 $this->addWhereFld( 'pr_cascade', 1 );
116 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
117 $this->addWhereFld( 'pr_cascade', 0 );
118 }
119
120 $this->addOption( 'DISTINCT' );
121
122 $forceNameTitleIndex = false;
123
124 } elseif ( isset( $params['prlevel'] ) ) {
125 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
126 }
127
128 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
129 $this->addTables( 'langlinks' );
130 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
131 $this->addWhere( 'll_from IS NULL' );
132 $forceNameTitleIndex = false;
133 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
134 $this->addTables( 'langlinks' );
135 $this->addWhere( 'page_id=ll_from' );
136 $this->addOption( 'STRAIGHT_JOIN' );
137 // We have to GROUP BY all selected fields to stop
138 // PostgreSQL from whining
139 $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
140 $forceNameTitleIndex = false;
141 }
142
143 if ( $forceNameTitleIndex ) {
144 $this->addOption( 'USE INDEX', 'name_title' );
145 }
146
147 $limit = $params['limit'];
148 $this->addOption( 'LIMIT', $limit + 1 );
149 $res = $this->select( __METHOD__ );
150
151 $count = 0;
152 $result = $this->getResult();
153 while ( $row = $db->fetchObject( $res ) ) {
154 if ( ++ $count > $limit ) {
155 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
156 // TODO: Security issue - if the user has no right to view next title, it will still be shown
157 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
158 break;
159 }
160
161 if ( is_null( $resultPageSet ) ) {
162 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
163 $vals = array(
164 'pageid' => intval( $row->page_id ),
165 'ns' => intval( $title->getNamespace() ),
166 'title' => $title->getPrefixedText()
167 );
168 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
169 if ( !$fit ) {
170 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
171 break;
172 }
173 } else {
174 $resultPageSet->processDbRow( $row );
175 }
176 }
177 $db->freeResult( $res );
178
179 if ( is_null( $resultPageSet ) ) {
180 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
181 }
182 }
183
184 public function getAllowedParams() {
185 global $wgRestrictionTypes, $wgRestrictionLevels;
186
187 return array(
188 'from' => null,
189 'prefix' => null,
190 'namespace' => array(
191 ApiBase::PARAM_DFLT => 0,
192 ApiBase::PARAM_TYPE => 'namespace',
193 ),
194 'filterredir' => array(
195 ApiBase::PARAM_DFLT => 'all',
196 ApiBase::PARAM_TYPE => array(
197 'all',
198 'redirects',
199 'nonredirects'
200 )
201 ),
202 'minsize' => array(
203 ApiBase::PARAM_TYPE => 'integer',
204 ),
205 'maxsize' => array(
206 ApiBase::PARAM_TYPE => 'integer',
207 ),
208 'prtype' => array(
209 ApiBase::PARAM_TYPE => $wgRestrictionTypes,
210 ApiBase::PARAM_ISMULTI => true
211 ),
212 'prlevel' => array(
213 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
214 ApiBase::PARAM_ISMULTI => true
215 ),
216 'prfiltercascade' => array(
217 ApiBase::PARAM_DFLT => 'all',
218 ApiBase::PARAM_TYPE => array(
219 'cascading',
220 'noncascading',
221 'all'
222 ),
223 ),
224 'limit' => array(
225 ApiBase::PARAM_DFLT => 10,
226 ApiBase::PARAM_TYPE => 'limit',
227 ApiBase::PARAM_MIN => 1,
228 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
229 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
230 ),
231 'dir' => array(
232 ApiBase::PARAM_DFLT => 'ascending',
233 ApiBase::PARAM_TYPE => array(
234 'ascending',
235 'descending'
236 )
237 ),
238 'filterlanglinks' => array(
239 ApiBase::PARAM_TYPE => array(
240 'withlanglinks',
241 'withoutlanglinks',
242 'all'
243 ),
244 ApiBase::PARAM_DFLT => 'all'
245 )
246 );
247 }
248
249 public function getParamDescription() {
250 return array(
251 'from' => 'The page title to start enumerating from.',
252 'prefix' => 'Search for all page titles that begin with this value.',
253 'namespace' => 'The namespace to enumerate.',
254 'filterredir' => 'Which pages to list.',
255 'dir' => 'The direction in which to list',
256 'minsize' => 'Limit to pages with at least this many bytes',
257 'maxsize' => 'Limit to pages with at most this many bytes',
258 'prtype' => 'Limit to protected pages only',
259 'prlevel' => 'The protection level (must be used with apprtype= parameter)',
260 'prfiltercascade' => 'Filter protections based on cascadingness (ignored when apprtype isn\'t set)',
261 'filterlanglinks' => 'Filter based on whether a page has langlinks',
262 'limit' => 'How many total pages to return.'
263 );
264 }
265
266 public function getDescription() {
267 return 'Enumerate all pages sequentially in a given namespace';
268 }
269
270 public function getPossibleErrors() {
271 return array_merge( parent::getPossibleErrors(), array(
272 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
273 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
274 ) );
275 }
276
277 protected function getExamples() {
278 return array(
279 'Simple Use',
280 ' Show a list of pages starting at the letter "B"',
281 ' api.php?action=query&list=allpages&apfrom=B',
282 'Using as Generator',
283 ' Show info about 4 pages starting at the letter "T"',
284 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
285 ' Show content of first 2 non-redirect pages begining at "Re"',
286 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
287 );
288 }
289
290 public function getVersion() {
291 return __CLASS__ . ': $Id$';
292 }
293 }