More parameter documentation
[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 ( !empty( $params['prtype'] ) ) {
115 $this->addTables( 'page_restrictions' );
116 $this->addWhere( 'page_id=pr_page' );
117 $this->addWhere( 'pr_expiry>' . $db->addQuotes( $db->timestamp() ) );
118 $this->addWhereFld( 'pr_type', $params['prtype'] );
119
120 if ( isset( $params['prlevel'] ) ) {
121 // Remove the empty string and '*' from the prlevel array
122 $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
123
124 if ( !empty( $prlevel ) ) {
125 $this->addWhereFld( 'pr_level', $prlevel );
126 }
127 }
128 if ( $params['prfiltercascade'] == 'cascading' ) {
129 $this->addWhereFld( 'pr_cascade', 1 );
130 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
131 $this->addWhereFld( 'pr_cascade', 0 );
132 }
133
134 $this->addOption( 'DISTINCT' );
135
136 $forceNameTitleIndex = false;
137
138 } elseif ( isset( $params['prlevel'] ) ) {
139 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
140 }
141
142 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
143 $this->addTables( 'langlinks' );
144 $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
145 $this->addWhere( 'll_from IS NULL' );
146 $forceNameTitleIndex = false;
147 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
148 $this->addTables( 'langlinks' );
149 $this->addWhere( 'page_id=ll_from' );
150 $this->addOption( 'STRAIGHT_JOIN' );
151 // We have to GROUP BY all selected fields to stop
152 // PostgreSQL from whining
153 $this->addOption( 'GROUP BY', implode( ', ', $selectFields ) );
154 $forceNameTitleIndex = false;
155 }
156
157 if ( $forceNameTitleIndex ) {
158 $this->addOption( 'USE INDEX', 'name_title' );
159 }
160
161 $limit = $params['limit'];
162 $this->addOption( 'LIMIT', $limit + 1 );
163 $res = $this->select( __METHOD__ );
164
165 $count = 0;
166 $result = $this->getResult();
167 foreach ( $res as $row ) {
168 if ( ++ $count > $limit ) {
169 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
170 // TODO: Security issue - if the user has no right to view next title, it will still be shown
171 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
172 break;
173 }
174
175 if ( is_null( $resultPageSet ) ) {
176 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
177 $vals = array(
178 'pageid' => intval( $row->page_id ),
179 'ns' => intval( $title->getNamespace() ),
180 'title' => $title->getPrefixedText()
181 );
182 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
183 if ( !$fit ) {
184 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->page_title ) );
185 break;
186 }
187 } else {
188 $resultPageSet->processDbRow( $row );
189 }
190 }
191
192 if ( is_null( $resultPageSet ) ) {
193 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
194 }
195 }
196
197 public function getAllowedParams() {
198 global $wgRestrictionTypes, $wgRestrictionLevels;
199
200 return array(
201 'from' => null,
202 'to' => null,
203 'prefix' => null,
204 'namespace' => array(
205 ApiBase::PARAM_DFLT => 0,
206 ApiBase::PARAM_TYPE => 'namespace',
207 ),
208 'filterredir' => array(
209 ApiBase::PARAM_DFLT => 'all',
210 ApiBase::PARAM_TYPE => array(
211 'all',
212 'redirects',
213 'nonredirects'
214 )
215 ),
216 'minsize' => array(
217 ApiBase::PARAM_TYPE => 'integer',
218 ),
219 'maxsize' => array(
220 ApiBase::PARAM_TYPE => 'integer',
221 ),
222 'prtype' => array(
223 ApiBase::PARAM_TYPE => $wgRestrictionTypes,
224 ApiBase::PARAM_ISMULTI => true
225 ),
226 'prlevel' => array(
227 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
228 ApiBase::PARAM_ISMULTI => true
229 ),
230 'prfiltercascade' => array(
231 ApiBase::PARAM_DFLT => 'all',
232 ApiBase::PARAM_TYPE => array(
233 'cascading',
234 'noncascading',
235 'all'
236 ),
237 ),
238 'limit' => array(
239 ApiBase::PARAM_DFLT => 10,
240 ApiBase::PARAM_TYPE => 'limit',
241 ApiBase::PARAM_MIN => 1,
242 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
243 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
244 ),
245 'dir' => array(
246 ApiBase::PARAM_DFLT => 'ascending',
247 ApiBase::PARAM_TYPE => array(
248 'ascending',
249 'descending'
250 )
251 ),
252 'filterlanglinks' => array(
253 ApiBase::PARAM_TYPE => array(
254 'withlanglinks',
255 'withoutlanglinks',
256 'all'
257 ),
258 ApiBase::PARAM_DFLT => 'all'
259 )
260 );
261 }
262
263 public function getParamDescription() {
264 $p = $this->getModulePrefix();
265 return array(
266 'from' => 'The page title to start enumerating from',
267 'to' => 'The page title to stop enumerating at',
268 'prefix' => 'Search for all page titles that begin with this value',
269 'namespace' => 'The namespace to enumerate',
270 'filterredir' => 'Which pages to list',
271 'dir' => 'The direction in which to list',
272 'minsize' => 'Limit to pages with at least this many bytes',
273 'maxsize' => 'Limit to pages with at most this many bytes',
274 'prtype' => 'Limit to protected pages only',
275 'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
276 'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
277 'filterlanglinks' => 'Filter based on whether a page has langlinks',
278 'limit' => 'How many total pages to return.'
279 );
280 }
281
282 public function getDescription() {
283 return 'Enumerate all pages sequentially in a given namespace';
284 }
285
286 public function getPossibleErrors() {
287 return array_merge( parent::getPossibleErrors(), array(
288 array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
289 array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
290 ) );
291 }
292
293 protected function getExamples() {
294 return array(
295 'Simple Use',
296 ' Show a list of pages starting at the letter "B"',
297 ' api.php?action=query&list=allpages&apfrom=B',
298 'Using as Generator',
299 ' Show info about 4 pages starting at the letter "T"',
300 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
301 ' Show content of first 2 non-redirect pages begining at "Re"',
302 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
303 );
304 }
305
306 public function getVersion() {
307 return __CLASS__ . ': $Id$';
308 }
309 }