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