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