* remove end of line whitespace
[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 * @addtogroup 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 {
80 $this->addOption('USE INDEX', 'cl_timestamp');
81 $this->addOption('ORDER BY', 'cl_to, cl_timestamp' . ($params['dir'] == 'desc' ? ' DESC' : ''));
82 }
83 else
84 {
85 $this->addOption('USE INDEX', 'cl_sortkey');
86 $this->addOption('ORDER BY', 'cl_to, cl_sortkey' . ($params['dir'] == 'desc' ? ' DESC' : '') . ', cl_from');
87 }
88
89 $this->addWhere('cl_from=page_id');
90 $this->setContinuation($params['continue']);
91 $this->addWhereFld('cl_to', $categoryTitle->getDBkey());
92 $this->addWhereFld('page_namespace', $params['namespace']);
93 $this->addWhereRange('cl_timestamp', ($params['dir'] == 'asc' ? 'newer' : 'older'), $params['start'], $params['end']);
94
95 $limit = $params['limit'];
96 $this->addOption('LIMIT', $limit +1);
97
98 $db = $this->getDB();
99
100 $data = array ();
101 $count = 0;
102 $lastSortKey = null;
103 $res = $this->select(__METHOD__);
104 while ($row = $db->fetchObject($res)) {
105 if (++ $count > $limit) {
106 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
107 // TODO: Security issue - if the user has no right to view next title, it will still be shown
108 if ($params['sort'] == 'timestamp')
109 $this->setContinueEnumParameter('start', $row->cl_timestamp);
110 else
111 $this->setContinueEnumParameter('continue', $this->getContinueStr($row, $lastSortKey));
112 break;
113 }
114
115 $lastSortKey = $row->cl_sortkey; // detect duplicate sortkeys
116
117 if (is_null($resultPageSet)) {
118 $vals = array();
119 if ($fld_ids)
120 $vals['pageid'] = intval($row->page_id);
121 if ($fld_title) {
122 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
123 $vals['ns'] = intval($title->getNamespace());
124 $vals['title'] = $title->getPrefixedText();
125 }
126 if ($fld_sortkey)
127 $vals['sortkey'] = $row->cl_sortkey;
128 if ($fld_timestamp)
129 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->cl_timestamp);
130 $data[] = $vals;
131 } else {
132 $resultPageSet->processDbRow($row);
133 }
134 }
135 $db->freeResult($res);
136
137 if (is_null($resultPageSet)) {
138 $this->getResult()->setIndexedTagName($data, 'cm');
139 $this->getResult()->addValue('query', $this->getModuleName(), $data);
140 }
141 }
142
143 private function getContinueStr($row, $lastSortKey) {
144 $ret = $row->cl_sortkey . '|';
145 if ($row->cl_sortkey == $lastSortKey) // duplicate sort key, add cl_from
146 $ret .= $row->cl_from;
147 return $ret;
148 }
149
150 /**
151 * Add DB WHERE clause to continue previous query based on 'continue' parameter
152 */
153 private function setContinuation($continue) {
154 if (is_null($continue))
155 return; // This is not a continuation request
156
157 $continueList = explode('|', $continue);
158 $hasError = count($continueList) != 2;
159 $from = 0;
160 if (!$hasError && strlen($continueList[1]) > 0) {
161 $from = intval($continueList[1]);
162 $hasError = ($from == 0);
163 }
164
165 if ($hasError)
166 $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
167
168 $encSortKey = $this->getDB()->addQuotes($continueList[0]);
169 $encFrom = $this->getDB()->addQuotes($from);
170
171 if ($from != 0) {
172 // Duplicate sort key continue
173 $this->addWhere( "cl_sortkey>$encSortKey OR (cl_sortkey=$encSortKey AND cl_from>=$encFrom)" );
174 } else {
175 $this->addWhere( "cl_sortkey>=$encSortKey" );
176 }
177 }
178
179 public function getAllowedParams() {
180 return array (
181 'title' => null,
182 'prop' => array (
183 ApiBase :: PARAM_DFLT => 'ids|title',
184 ApiBase :: PARAM_ISMULTI => true,
185 ApiBase :: PARAM_TYPE => array (
186 'ids',
187 'title',
188 'sortkey',
189 'timestamp',
190 )
191 ),
192 'namespace' => array (
193 ApiBase :: PARAM_ISMULTI => true,
194 ApiBase :: PARAM_TYPE => 'namespace',
195 ),
196 'continue' => null,
197 'limit' => array (
198 ApiBase :: PARAM_TYPE => 'limit',
199 ApiBase :: PARAM_DFLT => 10,
200 ApiBase :: PARAM_MIN => 1,
201 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
202 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
203 ),
204 'sort' => array(
205 ApiBase :: PARAM_DFLT => 'sortkey',
206 ApiBase :: PARAM_TYPE => array(
207 'sortkey',
208 'timestamp'
209 )
210 ),
211 'dir' => array(
212 ApiBase :: PARAM_DFLT => 'asc',
213 ApiBase :: PARAM_TYPE => array(
214 'asc',
215 'desc'
216 )
217 ),
218 'start' => array(
219 ApiBase :: PARAM_TYPE => 'timestamp'
220 ),
221 'end' => array(
222 ApiBase :: PARAM_TYPE => 'timestamp'
223 )
224 );
225 }
226
227 public function getParamDescription() {
228 return array (
229 'title' => 'Which category to enumerate (required). Must include Category: prefix',
230 'prop' => 'What pieces of information to include',
231 'namespace' => 'Only include pages in these namespaces',
232 'sort' => 'Property to sort by',
233 'dir' => 'In which direction to sort',
234 'start' => 'Timestamp to start listing from',
235 'end' => 'Timestamp to end listing at',
236 'continue' => 'For large categories, give the value retured from previous query',
237 'limit' => 'The maximum number of pages to return.',
238 );
239 }
240
241 public function getDescription() {
242 return 'List all pages in a given category';
243 }
244
245 protected function getExamples() {
246 return array (
247 "Get first 10 pages in [[Category:Physics]]:",
248 " api.php?action=query&list=categorymembers&cmtitle=Category:Physics",
249 "Get page info about first 10 pages in [[Category:Physics]]:",
250 " api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info",
251 );
252 }
253
254 public function getVersion() {
255 return __CLASS__ . ': $Id$';
256 }
257 }