escape HTML elements in docblock with double quotes
[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 //Get gender information
169 if( MWNamespace::hasGenderDistinction( $params['namespace'] ) ) {
170 $users = array();
171 foreach ( $res as $row ) {
172 $users[] = $row->page_title;
173 }
174 GenderCache::singleton()->doQuery( $users, __METHOD__ );
175 $res->rewind(); //reset
176 }
177
178 $count = 0;
179 $result = $this->getResult();
180 foreach ( $res as $row ) {
181 if ( ++ $count > $limit ) {
182 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
183 // TODO: Security issue - if the user has no right to view next title, it will still be shown
184 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
185 break;
186 }
187
188 if ( is_null( $resultPageSet ) ) {
189 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
190 $vals = array(
191 'pageid' => intval( $row->page_id ),
192 'ns' => intval( $title->getNamespace() ),
193 'title' => $title->getPrefixedText()
194 );
195 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
196 if ( !$fit ) {
197 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
198 break;
199 }
200 } else {
201 $resultPageSet->processDbRow( $row );
202 }
203 }
204
205 if ( is_null( $resultPageSet ) ) {
206 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
207 }
208 }
209
210 public function getAllowedParams() {
211 global $wgRestrictionLevels;
212
213 return array(
214 'from' => null,
215 'to' => null,
216 'prefix' => null,
217 'namespace' => array(
218 ApiBase::PARAM_DFLT => 0,
219 ApiBase::PARAM_TYPE => 'namespace',
220 ),
221 'filterredir' => array(
222 ApiBase::PARAM_DFLT => 'all',
223 ApiBase::PARAM_TYPE => array(
224 'all',
225 'redirects',
226 'nonredirects'
227 )
228 ),
229 'minsize' => array(
230 ApiBase::PARAM_TYPE => 'integer',
231 ),
232 'maxsize' => array(
233 ApiBase::PARAM_TYPE => 'integer',
234 ),
235 'prtype' => array(
236 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
237 ApiBase::PARAM_ISMULTI => true
238 ),
239 'prlevel' => array(
240 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
241 ApiBase::PARAM_ISMULTI => true
242 ),
243 'prfiltercascade' => array(
244 ApiBase::PARAM_DFLT => 'all',
245 ApiBase::PARAM_TYPE => array(
246 'cascading',
247 'noncascading',
248 'all'
249 ),
250 ),
251 'limit' => array(
252 ApiBase::PARAM_DFLT => 10,
253 ApiBase::PARAM_TYPE => 'limit',
254 ApiBase::PARAM_MIN => 1,
255 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
256 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
257 ),
258 'dir' => array(
259 ApiBase::PARAM_DFLT => 'ascending',
260 ApiBase::PARAM_TYPE => array(
261 'ascending',
262 'descending'
263 )
264 ),
265 'filterlanglinks' => array(
266 ApiBase::PARAM_TYPE => array(
267 'withlanglinks',
268 'withoutlanglinks',
269 'all'
270 ),
271 ApiBase::PARAM_DFLT => 'all'
272 ),
273 'prexpiry' => array(
274 ApiBase::PARAM_TYPE => array(
275 'indefinite',
276 'definite',
277 'all'
278 ),
279 ApiBase::PARAM_DFLT => 'all'
280 ),
281 );
282 }
283
284 public function getParamDescription() {
285 $p = $this->getModulePrefix();
286 return array(
287 'from' => 'The page title to start enumerating from',
288 'to' => 'The page title to stop enumerating at',
289 'prefix' => 'Search for all page titles that begin with this value',
290 'namespace' => 'The namespace to enumerate',
291 'filterredir' => 'Which pages to list',
292 'dir' => 'The direction in which to list',
293 'minsize' => 'Limit to pages with at least this many bytes',
294 'maxsize' => 'Limit to pages with at most this many bytes',
295 'prtype' => 'Limit to protected pages only',
296 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
297 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
298 'filterlanglinks' => 'Filter based on whether a page has langlinks',
299 'limit' => 'How many total pages to return.',
300 'prexpiry' => array(
301 'Which protection expiry to filter the page on',
302 ' indefinite - Get only pages with indefinite protection expiry',
303 ' definite - Get only pages with a definite (specific) protection expiry',
304 ' all - Get pages with any protections expiry'
305 ),
306 );
307 }
308
309 public function getResultProperties() {
310 return array(
311 '' => array(
312 'pageid' => 'integer',
313 'ns' => 'namespace',
314 'title' => 'string'
315 )
316 );
317 }
318
319 public function getDescription() {
320 return 'Enumerate all pages sequentially in a given namespace';
321 }
322
323 public function getPossibleErrors() {
324 return array_merge( parent::getPossibleErrors(), array(
325 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
326 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
327 ) );
328 }
329
330 public function getExamples() {
331 return array(
332 'api.php?action=query&list=allpages&apfrom=B' => array(
333 'Simple Use',
334 'Show a list of pages starting at the letter "B"',
335 ),
336 'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
337 'Using as Generator',
338 'Show info about 4 pages starting at the letter "T"',
339 ),
340 'api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content' => array(
341 'Show content of first 2 non-redirect pages begining at "Re"',
342 )
343 );
344 }
345
346 public function getHelpUrls() {
347 return 'https://www.mediawiki.org/wiki/API:Allpages';
348 }
349
350 public function getVersion() {
351 return __CLASS__ . ': $Id$';
352 }
353 }