don't allow querying specific namespace if misermode is enabled
[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 (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 * 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
52 $params = $this->extractRequestParams();
53
54 if ( !isset($params['title']) || is_null($params['title']) )
55 $this->dieUsage("The cmtitle parameter is required", 'notitle');
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 $prop = array_flip($params['prop']);
62 $fld_ids = isset($prop['ids']);
63 $fld_title = isset($prop['title']);
64 $fld_sortkey = isset($prop['sortkey']);
65 $fld_timestamp = isset($prop['timestamp']);
66
67 if (is_null($resultPageSet)) {
68 $this->addFields(array('cl_from', 'cl_sortkey', 'page_namespace', 'page_title'));
69 $this->addFieldsIf('page_id', $fld_ids);
70 } else {
71 $this->addFields($resultPageSet->getPageTableFields()); // will include page_ id, ns, title
72 $this->addFields(array('cl_from', 'cl_sortkey'));
73 }
74
75 $this->addFieldsIf('cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp');
76 $this->addTables(array('page','categorylinks')); // must be in this order for 'USE INDEX'
77 // Not needed after bug 10280 is applied to servers
78 if($params['sort'] == 'timestamp')
79 $this->addOption('USE INDEX', 'cl_timestamp');
80 else
81 $this->addOption('USE INDEX', 'cl_sortkey');
82
83 $this->addWhere('cl_from=page_id');
84 $this->setContinuation($params['continue'], $params['dir']);
85 $this->addWhereFld('cl_to', $categoryTitle->getDBkey());
86 # Scanning large datasets for rare categories sucks, and I already told
87 # how to have efficient subcategory access :-) ~~~~ (oh well, domas)
88 global $wgMiserMode;
89 if (!$wgMiserMode) {
90 $this->addWhereFld('page_namespace', $params['namespace']);
91 }
92 if($params['sort'] == 'timestamp')
93 $this->addWhereRange('cl_timestamp', ($params['dir'] == 'asc' ? 'newer' : 'older'), $params['start'], $params['end']);
94 else
95 {
96 $this->addWhereRange('cl_sortkey', ($params['dir'] == 'asc' ? 'newer' : 'older'), $params['startsortkey'], $params['endsortkey']);
97 $this->addWhereRange('cl_from', ($params['dir'] == 'asc' ? 'newer' : 'older'), null, null);
98 }
99
100 $limit = $params['limit'];
101 $this->addOption('LIMIT', $limit +1);
102
103 $db = $this->getDB();
104
105 $data = array ();
106 $count = 0;
107 $lastSortKey = null;
108 $res = $this->select(__METHOD__);
109 while ($row = $db->fetchObject($res)) {
110 if (++ $count > $limit) {
111 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
112 // TODO: Security issue - if the user has no right to view next title, it will still be shown
113 if ($params['sort'] == 'timestamp')
114 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->cl_timestamp));
115 else
116 $this->setContinueEnumParameter('continue', $this->getContinueStr($row, $lastSortKey));
117 break;
118 }
119
120 if (is_null($resultPageSet)) {
121 $vals = array();
122 if ($fld_ids)
123 $vals['pageid'] = intval($row->page_id);
124 if ($fld_title) {
125 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
126 ApiQueryBase::addTitleInfo($vals, $title);
127 }
128 if ($fld_sortkey)
129 $vals['sortkey'] = $row->cl_sortkey;
130 if ($fld_timestamp)
131 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->cl_timestamp);
132 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()),
133 null, $vals);
134 if(!$fit)
135 {
136 if ($params['sort'] == 'timestamp')
137 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->cl_timestamp));
138 else
139 $this->setContinueEnumParameter('continue', $this->getContinueStr($row, $lastSortKey));
140 break;
141 }
142 } else {
143 $resultPageSet->processDbRow($row);
144 }
145 $lastSortKey = $row->cl_sortkey; // detect duplicate sortkeys
146 }
147 $db->freeResult($res);
148
149 if (is_null($resultPageSet)) {
150 $this->getResult()->setIndexedTagName_internal(
151 array('query', $this->getModuleName()), 'cm');
152 }
153 }
154
155 private function getContinueStr($row, $lastSortKey) {
156 $ret = $row->cl_sortkey . '|';
157 if ($row->cl_sortkey == $lastSortKey) // duplicate sort key, add cl_from
158 $ret .= $row->cl_from;
159 return $ret;
160 }
161
162 /**
163 * Add DB WHERE clause to continue previous query based on 'continue' parameter
164 */
165 private function setContinuation($continue, $dir) {
166 if (is_null($continue))
167 return; // This is not a continuation request
168
169 $pos = strrpos($continue, '|');
170 $sortkey = substr($continue, 0, $pos);
171 $fromstr = substr($continue, $pos + 1);
172 $from = intval($fromstr);
173
174 if ($from == 0 && strlen($fromstr) > 0)
175 $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
176
177 $encSortKey = $this->getDB()->addQuotes($sortkey);
178 $encFrom = $this->getDB()->addQuotes($from);
179
180 $op = ($dir == 'desc' ? '<' : '>');
181
182 if ($from != 0) {
183 // Duplicate sort key continue
184 $this->addWhere( "cl_sortkey$op$encSortKey OR (cl_sortkey=$encSortKey AND cl_from$op=$encFrom)" );
185 } else {
186 $this->addWhere( "cl_sortkey$op=$encSortKey" );
187 }
188 }
189
190 public function getAllowedParams() {
191 return array (
192 'title' => null,
193 'prop' => array (
194 ApiBase :: PARAM_DFLT => 'ids|title',
195 ApiBase :: PARAM_ISMULTI => true,
196 ApiBase :: PARAM_TYPE => array (
197 'ids',
198 'title',
199 'sortkey',
200 'timestamp',
201 )
202 ),
203 'namespace' => array (
204 ApiBase :: PARAM_ISMULTI => true,
205 ApiBase :: PARAM_TYPE => 'namespace',
206 ),
207 'continue' => null,
208 'limit' => array (
209 ApiBase :: PARAM_TYPE => 'limit',
210 ApiBase :: PARAM_DFLT => 10,
211 ApiBase :: PARAM_MIN => 1,
212 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
213 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
214 ),
215 'sort' => array(
216 ApiBase :: PARAM_DFLT => 'sortkey',
217 ApiBase :: PARAM_TYPE => array(
218 'sortkey',
219 'timestamp'
220 )
221 ),
222 'dir' => array(
223 ApiBase :: PARAM_DFLT => 'asc',
224 ApiBase :: PARAM_TYPE => array(
225 'asc',
226 'desc'
227 )
228 ),
229 'start' => array(
230 ApiBase :: PARAM_TYPE => 'timestamp'
231 ),
232 'end' => array(
233 ApiBase :: PARAM_TYPE => 'timestamp'
234 ),
235 'startsortkey' => null,
236 'endsortkey' => null,
237 );
238 }
239
240 public function getParamDescription() {
241 return array (
242 'title' => 'Which category to enumerate (required). Must include Category: prefix',
243 'prop' => 'What pieces of information to include',
244 'namespace' => 'Only include pages in these namespaces',
245 'sort' => 'Property to sort by',
246 'dir' => 'In which direction to sort',
247 'start' => 'Timestamp to start listing from. Can only be used with cmsort=timestamp',
248 'end' => 'Timestamp to end listing at. Can only be used with cmsort=timestamp',
249 'startsortkey' => 'Sortkey to start listing from. Can only be used with cmsort=sortkey',
250 'endsortkey' => 'Sortkey to end listing at. Can only be used with cmsort=sortkey',
251 'continue' => 'For large categories, give the value retured from previous query',
252 'limit' => 'The maximum number of pages to return.',
253 );
254 }
255
256 public function getDescription() {
257 return 'List all pages in a given category';
258 }
259
260 protected function getExamples() {
261 return array (
262 "Get first 10 pages in [[Category:Physics]]:",
263 " api.php?action=query&list=categorymembers&cmtitle=Category:Physics",
264 "Get page info about first 10 pages in [[Category:Physics]]:",
265 " api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info",
266 );
267 }
268
269 public function getVersion() {
270 return __CLASS__ . ': $Id$';
271 }
272 }