Big blitz on unused variables (a lot of $db = $this->getDb() )
[lhc/web/wiklou.git] / includes / api / ApiQueryCategoryMembers.php
1 <?php
2
3 /**
4 * Created on June 14, 2007
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 * A query module to enumerate pages that belong to a category.
33 *
34 * @ingroup API
35 */
36 class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'cm' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 private function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
52
53 if ( !isset( $params['title'] ) || is_null( $params['title'] ) ) {
54 $this->dieUsage( 'The cmtitle parameter is required', 'notitle' );
55 }
56 $categoryTitle = Title::newFromText( $params['title'] );
57
58 if ( is_null( $categoryTitle ) || $categoryTitle->getNamespace() != NS_CATEGORY ) {
59 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
60 }
61
62 $prop = array_flip( $params['prop'] );
63 $fld_ids = isset( $prop['ids'] );
64 $fld_title = isset( $prop['title'] );
65 $fld_sortkey = isset( $prop['sortkey'] );
66 $fld_timestamp = isset( $prop['timestamp'] );
67
68 if ( is_null( $resultPageSet ) ) {
69 $this->addFields( array( 'cl_from', 'cl_sortkey', 'page_namespace', 'page_title' ) );
70 $this->addFieldsIf( 'page_id', $fld_ids );
71 } else {
72 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
73 $this->addFields( array( 'cl_from', 'cl_sortkey' ) );
74 }
75
76 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
77 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
78 // Not needed after bug 10280 is applied to servers
79 if ( $params['sort'] == 'timestamp' ) {
80 $this->addOption( 'USE INDEX', 'cl_timestamp' );
81 } else {
82 $this->addOption( 'USE INDEX', 'cl_sortkey' );
83 }
84
85 $this->addWhere( 'cl_from=page_id' );
86 $this->setContinuation( $params['continue'], $params['dir'] );
87 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
88 // Scanning large datasets for rare categories sucks, and I already told
89 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
90 global $wgMiserMode;
91 $miser_ns = array();
92 if ( $wgMiserMode ) {
93 $miser_ns = $params['namespace'];
94 } else {
95 $this->addWhereFld( 'page_namespace', $params['namespace'] );
96 }
97 if ( $params['sort'] == 'timestamp' ) {
98 $this->addWhereRange( 'cl_timestamp', ( $params['dir'] == 'asc' ? 'newer' : 'older' ), $params['start'], $params['end'] );
99 } else {
100 $this->addWhereRange( 'cl_sortkey', ( $params['dir'] == 'asc' ? 'newer' : 'older' ), $params['startsortkey'], $params['endsortkey'] );
101 $this->addWhereRange( 'cl_from', ( $params['dir'] == 'asc' ? 'newer' : 'older' ), null, null );
102 }
103
104 $limit = $params['limit'];
105 $this->addOption( 'LIMIT', $limit + 1 );
106
107 $count = 0;
108 $lastSortKey = null;
109 $res = $this->select( __METHOD__ );
110 foreach ( $res as $row ) {
111 if ( ++ $count > $limit ) {
112 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
113 // TODO: Security issue - if the user has no right to view next title, it will still be shown
114 if ( $params['sort'] == 'timestamp' ) {
115 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
116 } else {
117 $this->setContinueEnumParameter( 'continue', $this->getContinueStr( $row, $lastSortKey ) );
118 }
119 break;
120 }
121
122 // Since domas won't tell anyone what he told long ago, apply
123 // cmnamespace here. This means the query may return 0 actual
124 // results, but on the other hand it could save returning 5000
125 // useless results to the client. ~~~~
126 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
127 continue;
128 }
129
130 if ( is_null( $resultPageSet ) ) {
131 $vals = array();
132 if ( $fld_ids ) {
133 $vals['pageid'] = intval( $row->page_id );
134 }
135 if ( $fld_title ) {
136 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
137 ApiQueryBase::addTitleInfo( $vals, $title );
138 }
139 if ( $fld_sortkey ) {
140 $vals['sortkey'] = $row->cl_sortkey;
141 }
142 if ( $fld_timestamp ) {
143 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
144 }
145 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
146 null, $vals );
147 if ( !$fit ) {
148 if ( $params['sort'] == 'timestamp' ) {
149 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
150 } else {
151 $this->setContinueEnumParameter( 'continue', $this->getContinueStr( $row, $lastSortKey ) );
152 }
153 break;
154 }
155 } else {
156 $resultPageSet->processDbRow( $row );
157 }
158 $lastSortKey = $row->cl_sortkey; // detect duplicate sortkeys
159 }
160
161 if ( is_null( $resultPageSet ) ) {
162 $this->getResult()->setIndexedTagName_internal(
163 array( 'query', $this->getModuleName() ), 'cm' );
164 }
165 }
166
167 private function getContinueStr( $row, $lastSortKey ) {
168 $ret = $row->cl_sortkey . '|';
169 if ( $row->cl_sortkey == $lastSortKey ) { // duplicate sort key, add cl_from
170 $ret .= $row->cl_from;
171 }
172 return $ret;
173 }
174
175 /**
176 * Add DB WHERE clause to continue previous query based on 'continue' parameter
177 */
178 private function setContinuation( $continue, $dir ) {
179 if ( is_null( $continue ) ) {
180 return; // This is not a continuation request
181 }
182
183 $pos = strrpos( $continue, '|' );
184 $sortkey = substr( $continue, 0, $pos );
185 $fromstr = substr( $continue, $pos + 1 );
186 $from = intval( $fromstr );
187
188 if ( $from == 0 && strlen( $fromstr ) > 0 ) {
189 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' );
190 }
191
192 $encSortKey = $this->getDB()->addQuotes( $sortkey );
193 $encFrom = $this->getDB()->addQuotes( $from );
194
195 $op = ( $dir == 'desc' ? '<' : '>' );
196
197 if ( $from != 0 ) {
198 // Duplicate sort key continue
199 $this->addWhere( "cl_sortkey$op$encSortKey OR (cl_sortkey=$encSortKey AND cl_from$op=$encFrom)" );
200 } else {
201 $this->addWhere( "cl_sortkey$op=$encSortKey" );
202 }
203 }
204
205 public function getAllowedParams() {
206 return array(
207 'title' => null,
208 'prop' => array(
209 ApiBase::PARAM_DFLT => 'ids|title',
210 ApiBase::PARAM_ISMULTI => true,
211 ApiBase::PARAM_TYPE => array (
212 'ids',
213 'title',
214 'sortkey',
215 'timestamp',
216 )
217 ),
218 'namespace' => array (
219 ApiBase::PARAM_ISMULTI => true,
220 ApiBase::PARAM_TYPE => 'namespace',
221 ),
222 'continue' => null,
223 'limit' => array(
224 ApiBase::PARAM_TYPE => 'limit',
225 ApiBase::PARAM_DFLT => 10,
226 ApiBase::PARAM_MIN => 1,
227 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
228 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
229 ),
230 'sort' => array(
231 ApiBase::PARAM_DFLT => 'sortkey',
232 ApiBase::PARAM_TYPE => array(
233 'sortkey',
234 'timestamp'
235 )
236 ),
237 'dir' => array(
238 ApiBase::PARAM_DFLT => 'asc',
239 ApiBase::PARAM_TYPE => array(
240 'asc',
241 'desc'
242 )
243 ),
244 'start' => array(
245 ApiBase::PARAM_TYPE => 'timestamp'
246 ),
247 'end' => array(
248 ApiBase::PARAM_TYPE => 'timestamp'
249 ),
250 'startsortkey' => null,
251 'endsortkey' => null,
252 );
253 }
254
255 public function getParamDescription() {
256 global $wgMiserMode;
257 $p = $this->getModulePrefix();
258 $desc = array(
259 'title' => 'Which category to enumerate (required). Must include Category: prefix',
260 'prop' => array(
261 'What pieces of information to include',
262 ' ids - Adds the page id',
263 ' title - Adds the title and namespace id of the page',
264 ' sortkey - Adds the sortkey used for the category',
265 ' timestamp - Adds the timestamp of when the page was included',
266 ),
267 'namespace' => 'Only include pages in these namespaces',
268 'sort' => 'Property to sort by',
269 'dir' => 'In which direction to sort',
270 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
271 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
272 'startsortkey' => "Sortkey to start listing from. Can only be used with {$p}sort=sortkey",
273 'endsortkey' => "Sortkey to end listing at. Can only be used with {$p}sort=sortkey",
274 'continue' => 'For large categories, give the value retured from previous query',
275 'limit' => 'The maximum number of pages to return.',
276 );
277 if ( $wgMiserMode ) {
278 $desc['namespace'] = array(
279 $desc['namespace'],
280 'NOTE: Due to $wgMiserMode, using this may result in fewer than "limit" results',
281 'returned before continuing; in extreme cases, zero results may be returned',
282 );
283 }
284 return $desc;
285 }
286
287 public function getDescription() {
288 return 'List all pages in a given category';
289 }
290
291 public function getPossibleErrors() {
292 return array_merge( parent::getPossibleErrors(), array(
293 array( 'code' => 'notitle', 'info' => 'The cmtitle parameter is required' ),
294 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
295 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
296 ) );
297 }
298
299 protected function getExamples() {
300 return array(
301 'Get first 10 pages in [[Category:Physics]]:',
302 ' api.php?action=query&list=categorymembers&cmtitle=Category:Physics',
303 'Get page info about first 10 pages in [[Category:Physics]]:',
304 ' api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info',
305 );
306 }
307
308 public function getVersion() {
309 return __CLASS__ . ': $Id$';
310 }
311 }