4 * Created on June 14, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiQueryBase.php");
32 * A query module to enumerate pages that belong to a category.
36 class ApiQueryCategoryMembers
extends ApiQueryGeneratorBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'cm');
42 public function execute() {
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
50 private function run($resultPageSet = null) {
52 $params = $this->extractRequestParams();
54 if ( !isset($params['title']) ||
is_null($params['title']) )
55 $this->dieUsage("The cmtitle parameter is required", 'notitle');
56 $categoryTitle = Title
::newFromText($params['title']);
58 if ( is_null( $categoryTitle ) ||
$categoryTitle->getNamespace() != NS_CATEGORY
)
59 $this->dieUsage("The category name you entered is not valid", 'invalidcategory');
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']);
67 if (is_null($resultPageSet)) {
68 $this->addFields(array('cl_from', 'cl_sortkey', 'page_namespace', 'page_title'));
69 $this->addFieldsIf('page_id', $fld_ids);
71 $this->addFields($resultPageSet->getPageTableFields()); // will include page_ id, ns, title
72 $this->addFields(array('cl_from', 'cl_sortkey'));
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');
81 $this->addOption('USE INDEX', 'cl_sortkey');
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)
89 if ( $wgMiserMode && isset($params['namespace']) ) {
90 $this->dieUsage("The cmnamespace option is disabled on this site", 'namespacedisabled');
92 $this->addWhereFld('page_namespace', $params['namespace']);
94 if($params['sort'] == 'timestamp')
95 $this->addWhereRange('cl_timestamp', ($params['dir'] == 'asc' ?
'newer' : 'older'), $params['start'], $params['end']);
98 $this->addWhereRange('cl_sortkey', ($params['dir'] == 'asc' ?
'newer' : 'older'), $params['startsortkey'], $params['endsortkey']);
99 $this->addWhereRange('cl_from', ($params['dir'] == 'asc' ?
'newer' : 'older'), null, null);
102 $limit = $params['limit'];
103 $this->addOption('LIMIT', $limit +
1);
105 $db = $this->getDB();
110 $res = $this->select(__METHOD__
);
111 while ($row = $db->fetchObject($res)) {
112 if (++
$count > $limit) {
113 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
114 // TODO: Security issue - if the user has no right to view next title, it will still be shown
115 if ($params['sort'] == 'timestamp')
116 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->cl_timestamp
));
118 $this->setContinueEnumParameter('continue', $this->getContinueStr($row, $lastSortKey));
122 if (is_null($resultPageSet)) {
125 $vals['pageid'] = intval($row->page_id
);
127 $title = Title
:: makeTitle($row->page_namespace
, $row->page_title
);
128 ApiQueryBase
::addTitleInfo($vals, $title);
131 $vals['sortkey'] = $row->cl_sortkey
;
133 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->cl_timestamp
);
134 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()),
138 if ($params['sort'] == 'timestamp')
139 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->cl_timestamp
));
141 $this->setContinueEnumParameter('continue', $this->getContinueStr($row, $lastSortKey));
145 $resultPageSet->processDbRow($row);
147 $lastSortKey = $row->cl_sortkey
; // detect duplicate sortkeys
149 $db->freeResult($res);
151 if (is_null($resultPageSet)) {
152 $this->getResult()->setIndexedTagName_internal(
153 array('query', $this->getModuleName()), 'cm');
157 private function getContinueStr($row, $lastSortKey) {
158 $ret = $row->cl_sortkey
. '|';
159 if ($row->cl_sortkey
== $lastSortKey) // duplicate sort key, add cl_from
160 $ret .= $row->cl_from
;
165 * Add DB WHERE clause to continue previous query based on 'continue' parameter
167 private function setContinuation($continue, $dir) {
168 if (is_null($continue))
169 return; // This is not a continuation request
171 $pos = strrpos($continue, '|');
172 $sortkey = substr($continue, 0, $pos);
173 $fromstr = substr($continue, $pos +
1);
174 $from = intval($fromstr);
176 if ($from == 0 && strlen($fromstr) > 0)
177 $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
179 $encSortKey = $this->getDB()->addQuotes($sortkey);
180 $encFrom = $this->getDB()->addQuotes($from);
182 $op = ($dir == 'desc' ?
'<' : '>');
185 // Duplicate sort key continue
186 $this->addWhere( "cl_sortkey$op$encSortKey OR (cl_sortkey=$encSortKey AND cl_from$op=$encFrom)" );
188 $this->addWhere( "cl_sortkey$op=$encSortKey" );
192 public function getAllowedParams() {
196 ApiBase
:: PARAM_DFLT
=> 'ids|title',
197 ApiBase
:: PARAM_ISMULTI
=> true,
198 ApiBase
:: PARAM_TYPE
=> array (
205 'namespace' => array (
206 ApiBase
:: PARAM_ISMULTI
=> true,
207 ApiBase
:: PARAM_TYPE
=> 'namespace',
211 ApiBase
:: PARAM_TYPE
=> 'limit',
212 ApiBase
:: PARAM_DFLT
=> 10,
213 ApiBase
:: PARAM_MIN
=> 1,
214 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
215 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
218 ApiBase
:: PARAM_DFLT
=> 'sortkey',
219 ApiBase
:: PARAM_TYPE
=> array(
225 ApiBase
:: PARAM_DFLT
=> 'asc',
226 ApiBase
:: PARAM_TYPE
=> array(
232 ApiBase
:: PARAM_TYPE
=> 'timestamp'
235 ApiBase
:: PARAM_TYPE
=> 'timestamp'
237 'startsortkey' => null,
238 'endsortkey' => null,
242 public function getParamDescription() {
244 'title' => 'Which category to enumerate (required). Must include Category: prefix',
245 'prop' => 'What pieces of information to include',
246 'sort' => 'Property to sort by',
247 'dir' => 'In which direction to sort',
248 'start' => 'Timestamp to start listing from. Can only be used with cmsort=timestamp',
249 'end' => 'Timestamp to end listing at. Can only be used with cmsort=timestamp',
250 'startsortkey' => 'Sortkey to start listing from. Can only be used with cmsort=sortkey',
251 'endsortkey' => 'Sortkey to end listing at. Can only be used with cmsort=sortkey',
252 'continue' => 'For large categories, give the value retured from previous query',
253 'limit' => 'The maximum number of pages to return.',
256 // We can't remove it from the param list entirely without removing it from the
257 // allowed params, but then we could only silently ignore it, which could cause
258 // problems for people unaware of the change
260 $desc['namespace'] = 'Disabled on this site for performance reasons';
262 $desc['namespace'] = 'Only include pages in these namespaces';
266 public function getDescription() {
267 return 'List all pages in a given category';
270 protected function getExamples() {
272 "Get first 10 pages in [[Category:Physics]]:",
273 " api.php?action=query&list=categorymembers&cmtitle=Category:Physics",
274 "Get page info about first 10 pages in [[Category:Physics]]:",
275 " api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info",
279 public function getVersion() {
280 return __CLASS__
. ': $Id$';