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