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