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