Followup 100905, fixing usages of getContext()
[lhc/web/wiklou.git] / includes / api / ApiQueryAllpages.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to enumerate all available pages.
34 *
35 * @ingroup API
36 */
37 class ApiQueryAllpages extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'ap' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function getCacheMode( $params ) {
48 return 'public';
49 }
50
51 /**
52 * @param $resultPageSet ApiPageSet
53 * @return void
54 */
55 public function executeGenerator( $resultPageSet ) {
56 if ( $resultPageSet->isResolvingRedirects() ) {
57 $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
58 }
59
60 $this->run( $resultPageSet );
61 }
62
63 /**
64 * @param $resultPageSet ApiPageSet
65 * @return void
66 */
67 private function run( $resultPageSet = null ) {
68 $db = $this->getDB();
69
70 $params = $this->extractRequestParams();
71
72 // Page filters
73 $this->addTables( 'page' );
74
75 if ( $params['filterredir'] == 'redirects' ) {
76 $this->addWhereFld( 'page_is_redirect', 1 );
77 } elseif ( $params['filterredir'] == 'nonredirects' ) {
78 $this->addWhereFld( 'page_is_redirect', 0 );
79 }
80
81 $this->addWhereFld( 'page_namespace', $params['namespace'] );
82 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
83 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
84 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
85 $this->addWhereRange( 'page_title', $dir, $from, $to );
86
87 if ( isset( $params['prefix'] ) ) {
88 $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
89 }
90
91 if ( is_null( $resultPageSet ) ) {
92 $selectFields = array(
93 'page_namespace',
94 'page_title',
95 'page_id'
96 );
97 } else {
98 $selectFields = $resultPageSet->getPageTableFields();
99 }
100
101 $this->addFields( $selectFields );
102 $forceNameTitleIndex = true;
103 if ( isset( $params['minsize'] ) ) {
104 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
105 $forceNameTitleIndex = false;
106 }
107
108 if ( isset( $params['maxsize'] ) ) {
109 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
110 $forceNameTitleIndex = false;
111 }
112
113 // Page protection filtering
114 if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) {
115 $this->addTables( 'page_restrictions' );
116 $this->addWhere( 'page_id=pr_page' );
117 $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
118
119 if ( count( $params['prtype'] ) ) {
120 $this->addWhereFld( 'pr_type', $params['prtype'] );
121
122 if ( isset( $params['prlevel'] ) ) {
123 // Remove the empty string and '*' from the prlevel array
124 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
125
126 if ( count( $prlevel ) ) {
127 $this->addWhereFld( 'pr_level', $prlevel );
128 }
129 }
130 if ( $params['prfiltercascade'] == 'cascading' ) {
131 $this->addWhereFld( 'pr_cascade', 1 );
132 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
133 $this->addWhereFld( 'pr_cascade', 0 );
134 }
135
136 $this->addOption( 'DISTINCT' );
137 }
138 $forceNameTitleIndex = false;
139
140 if ( $params['prexpiry'] == 'indefinite' ) {
141 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
142 } elseif ( $params['prexpiry'] == 'definite' ) {
143 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
144 }
145
146 } elseif ( isset( $params['prlevel'] ) ) {
147 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
148 }
149
150 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
151 $this->addTables( 'langlinks' );
152 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
153 $this->addWhere( 'll_from IS NULL' );
154 $forceNameTitleIndex = false;
155 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
156 $this->addTables( 'langlinks' );
157 $this->addWhere( 'page_id=ll_from' );
158 $this->addOption( 'STRAIGHT_JOIN' );
159 // We have to GROUP BY all selected fields to stop
160 // PostgreSQL from whining
161 $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
162 $forceNameTitleIndex = false;
163 }
164
165 if ( $forceNameTitleIndex ) {
166 $this->addOption( 'USE INDEX', 'name_title' );
167 }
168
169 $limit = $params['limit'];
170 $this->addOption( 'LIMIT', $limit + 1 );
171 $res = $this->select( __METHOD__ );
172
173 $count = 0;
174 $result = $this->getResult();
175 foreach ( $res as $row ) {
176 if ( ++ $count > $limit ) {
177 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
178 // TODO: Security issue - if the user has no right to view next title, it will still be shown
179 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
180 break;
181 }
182
183 if ( is_null( $resultPageSet ) ) {
184 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
185 $vals = array(
186 'pageid' => intval( $row->page_id ),
187 'ns' => intval( $title->getNamespace() ),
188 'title' => $title->getPrefixedText()
189 );
190 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
191 if ( !$fit ) {
192 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
193 break;
194 }
195 } else {
196 $resultPageSet->processDbRow( $row );
197 }
198 }
199
200 if ( is_null( $resultPageSet ) ) {
201 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
202 }
203 }
204
205 public function getAllowedParams() {
206 global $wgRestrictionLevels;
207
208 return array(
209 'from' => null,
210 'to' => null,
211 'prefix' => null,
212 'namespace' => array(
213 ApiBase::PARAM_DFLT => 0,
214 ApiBase::PARAM_TYPE => 'namespace',
215 ),
216 'filterredir' => array(
217 ApiBase::PARAM_DFLT => 'all',
218 ApiBase::PARAM_TYPE => array(
219 'all',
220 'redirects',
221 'nonredirects'
222 )
223 ),
224 'minsize' => array(
225 ApiBase::PARAM_TYPE => 'integer',
226 ),
227 'maxsize' => array(
228 ApiBase::PARAM_TYPE => 'integer',
229 ),
230 'prtype' => array(
231 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
232 ApiBase::PARAM_ISMULTI => true
233 ),
234 'prlevel' => array(
235 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
236 ApiBase::PARAM_ISMULTI => true
237 ),
238 'prfiltercascade' => array(
239 ApiBase::PARAM_DFLT => 'all',
240 ApiBase::PARAM_TYPE => array(
241 'cascading',
242 'noncascading',
243 'all'
244 ),
245 ),
246 'limit' => array(
247 ApiBase::PARAM_DFLT => 10,
248 ApiBase::PARAM_TYPE => 'limit',
249 ApiBase::PARAM_MIN => 1,
250 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
251 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
252 ),
253 'dir' => array(
254 ApiBase::PARAM_DFLT => 'ascending',
255 ApiBase::PARAM_TYPE => array(
256 'ascending',
257 'descending'
258 )
259 ),
260 'filterlanglinks' => array(
261 ApiBase::PARAM_TYPE => array(
262 'withlanglinks',
263 'withoutlanglinks',
264 'all'
265 ),
266 ApiBase::PARAM_DFLT => 'all'
267 ),
268 'prexpiry' => array(
269 ApiBase::PARAM_TYPE => array(
270 'indefinite',
271 'definite',
272 'all'
273 ),
274 ApiBase::PARAM_DFLT => 'all'
275 ),
276 );
277 }
278
279 public function getParamDescription() {
280 $p = $this->getModulePrefix();
281 return array(
282 'from' => 'The page title to start enumerating from',
283 'to' => 'The page title to stop enumerating at',
284 'prefix' => 'Search for all page titles that begin with this value',
285 'namespace' => 'The namespace to enumerate',
286 'filterredir' => 'Which pages to list',
287 'dir' => 'The direction in which to list',
288 'minsize' => 'Limit to pages with at least this many bytes',
289 'maxsize' => 'Limit to pages with at most this many bytes',
290 'prtype' => 'Limit to protected pages only',
291 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
292 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
293 'filterlanglinks' => 'Filter based on whether a page has langlinks',
294 'limit' => 'How many total pages to return.',
295 'prexpiry' => array(
296 'Which protection expiry to filter the page on',
297 ' indefinite - Get only pages with indefinite protection expiry',
298 ' definite - Get only pages with a definite (specific) protection expiry',
299 ' all - Get pages with any protections expiry'
300 ),
301 );
302 }
303
304 public function getDescription() {
305 return 'Enumerate all pages sequentially in a given namespace';
306 }
307
308 public function getPossibleErrors() {
309 return array_merge( parent::getPossibleErrors(), array(
310 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
311 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
312 ) );
313 }
314
315 public function getExamples() {
316 return array(
317 'Simple Use',
318 ' Show a list of pages starting at the letter "B"',
319 ' api.php?action=query&list=allpages&apfrom=B',
320 'Using as Generator',
321 ' Show info about 4 pages starting at the letter "T"',
322 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
323 ' Show content of first 2 non-redirect pages begining at "Re"',
324 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
325 );
326 }
327
328 public function getHelpUrls() {
329 return 'http://www.mediawiki.org/wiki/API:Allpages';
330 }
331
332 public function getVersion() {
333 return __CLASS__ . ': $Id$';
334 }
335 }