dd63f80929f6580eef98d39309d8f5f813482ee8
[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 (is_null($params['category'])) {
55 if (is_null($params['title']))
56 $this->dieUsage("Either the cmcategory or the cmtitle parameter is required", 'notitle');
57 else
58 $categoryTitle = Title::newFromText($params['title']);
59 } else if(is_null($params['title']))
60 $categoryTitle = Title::makeTitleSafe(NS_CATEGORY, $params['category']);
61 else
62 $this->dieUsage("The category and title parameters can't be used together", 'titleandcategory');
63
64 if ( is_null( $categoryTitle ) || $categoryTitle->getNamespace() != NS_CATEGORY )
65 $this->dieUsage("The category name you entered is not valid", 'invalidcategory');
66
67 $prop = array_flip($params['prop']);
68 $fld_ids = isset($prop['ids']);
69 $fld_title = isset($prop['title']);
70 $fld_sortkey = isset($prop['sortkey']);
71 $fld_timestamp = isset($prop['timestamp']);
72
73 if (is_null($resultPageSet)) {
74 $this->addFields(array('cl_from', 'cl_sortkey', 'page_namespace', 'page_title'));
75 $this->addFieldsIf('page_id', $fld_ids);
76 } else {
77 $this->addFields($resultPageSet->getPageTableFields()); // will include page_ id, ns, title
78 $this->addFields(array('cl_from', 'cl_sortkey'));
79 }
80
81 $this->addFieldsIf('cl_timestamp', $fld_timestamp);
82 $this->addTables(array('page','categorylinks')); // must be in this order for 'USE INDEX'
83 // Not needed after bug 10280 is applied to servers
84 if($params['sort'] == 'timestamp')
85 {
86 $this->addOption('USE INDEX', 'cl_timestamp');
87 $this->addOption('ORDER BY', 'cl_to, cl_timestamp' . ($params['dir'] == 'desc' ? ' DESC' : ''));
88 }
89 else
90 {
91 $this->addOption('USE INDEX', 'cl_sortkey');
92 $this->addOption('ORDER BY', 'cl_to, cl_sortkey' . ($params['dir'] == 'desc' ? ' DESC' : '') . ', cl_from');
93 }
94
95 $this->addWhere('cl_from=page_id');
96 $this->setContinuation($params['continue']);
97 $this->addWhereFld('cl_to', $categoryTitle->getDBkey());
98 $this->addWhereFld('page_namespace', $params['namespace']);
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 $this->setContinueEnumParameter('continue', $this->getContinueStr($row, $lastSortKey));
114 break;
115 }
116
117 $lastSortKey = $row->cl_sortkey; // detect duplicate sortkeys
118
119 if (is_null($resultPageSet)) {
120 $vals = array();
121 if ($fld_ids)
122 $vals['pageid'] = intval($row->page_id);
123 if ($fld_title) {
124 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
125 $vals['ns'] = intval($title->getNamespace());
126 $vals['title'] = $title->getPrefixedText();
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 $data[] = $vals;
133 } else {
134 $resultPageSet->processDbRow($row);
135 }
136 }
137 $db->freeResult($res);
138
139 if (is_null($resultPageSet)) {
140 $this->getResult()->setIndexedTagName($data, 'cm');
141 $this->getResult()->addValue('query', $this->getModuleName(), $data);
142 }
143 }
144
145 private function getContinueStr($row, $lastSortKey) {
146 $ret = $row->cl_sortkey . '|';
147 if ($row->cl_sortkey == $lastSortKey) // duplicate sort key, add cl_from
148 $ret .= $row->cl_from;
149 return $ret;
150 }
151
152 /**
153 * Add DB WHERE clause to continue previous query based on 'continue' parameter
154 */
155 private function setContinuation($continue) {
156 if (is_null($continue))
157 return; // This is not a continuation request
158
159 $continueList = explode('|', $continue);
160 $hasError = count($continueList) != 2;
161 $from = 0;
162 if (!$hasError && strlen($continueList[1]) > 0) {
163 $from = intval($continueList[1]);
164 $hasError = ($from == 0);
165 }
166
167 if ($hasError)
168 $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
169
170 $encSortKey = $this->getDB()->addQuotes($continueList[0]);
171 $encFrom = $this->getDB()->addQuotes($from);
172
173 if ($from != 0) {
174 // Duplicate sort key continue
175 $this->addWhere( "cl_sortkey>$encSortKey OR (cl_sortkey=$encSortKey AND cl_from>=$encFrom)" );
176 } else {
177 $this->addWhere( "cl_sortkey>=$encSortKey" );
178 }
179 }
180
181 public function getAllowedParams() {
182 return array (
183 'title' => null,
184 'category' => null, // DEPRECATED, will be removed in early March
185 'prop' => array (
186 ApiBase :: PARAM_DFLT => 'ids|title',
187 ApiBase :: PARAM_ISMULTI => true,
188 ApiBase :: PARAM_TYPE => array (
189 'ids',
190 'title',
191 'sortkey',
192 'timestamp',
193 )
194 ),
195 'namespace' => array (
196 ApiBase :: PARAM_ISMULTI => true,
197 ApiBase :: PARAM_TYPE => 'namespace',
198 ),
199 'continue' => null,
200 'limit' => array (
201 ApiBase :: PARAM_TYPE => 'limit',
202 ApiBase :: PARAM_DFLT => 10,
203 ApiBase :: PARAM_MIN => 1,
204 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
205 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
206 ),
207 'sort' => array(
208 ApiBase :: PARAM_DFLT => 'sortkey',
209 ApiBase :: PARAM_TYPE => array(
210 'sortkey',
211 'timestamp'
212 )
213 ),
214 'dir' => array(
215 ApiBase :: PARAM_DFLT => 'asc',
216 ApiBase :: PARAM_TYPE => array(
217 'asc',
218 'desc'
219 )
220 )
221 );
222 }
223
224 public function getParamDescription() {
225 return array (
226 'title' => 'Which category to enumerate (required). Must include Category: prefix',
227 'prop' => 'What pieces of information to include',
228 'namespace' => 'Only include pages in these namespaces',
229 'sort' => 'Property to sort by',
230 'dir' => 'In which direction to sort',
231 'continue' => 'For large categories, give the value retured from previous query',
232 'limit' => 'The maximum number of pages to return.',
233 'category' => 'DEPRECATED. Like title, but without the Category: prefix.',
234 );
235 }
236
237 public function getDescription() {
238 return 'List all pages in a given category';
239 }
240
241 protected function getExamples() {
242 return array (
243 "Get first 10 pages in [[Category:Physics]]:",
244 " api.php?action=query&list=categorymembers&cmtitle=Category:Physics",
245 "Get page info about first 10 pages in [[Category:Physics]]:",
246 " api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info",
247 );
248 }
249
250 public function getVersion() {
251 return __CLASS__ . ': $Id$';
252 }
253 }
254