3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Api
;
26 use MediaWiki\Collation\CollationFactory
;
27 use MediaWiki\MainConfigNames
;
28 use MediaWiki\Title\Title
;
29 use Wikimedia\ParamValidator\ParamValidator
;
30 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
33 * A query module to enumerate pages that belong to a category.
37 class ApiQueryCategoryMembers
extends ApiQueryGeneratorBase
{
39 private Collation
$collation;
41 public function __construct(
44 CollationFactory
$collationFactory
46 parent
::__construct( $query, $moduleName, 'cm' );
47 $this->collation
= $collationFactory->getCategoryCollation();
50 public function execute() {
54 public function getCacheMode( $params ) {
58 public function executeGenerator( $resultPageSet ) {
59 $this->run( $resultPageSet );
63 * @param string $hexSortkey
66 private function validateHexSortkey( $hexSortkey ) {
67 // A hex sortkey has an unbound number of 2 letter pairs
68 return (bool)preg_match( '/^(?:[a-fA-F0-9]{2})*$/D', $hexSortkey );
72 * @param ApiPageSet|null $resultPageSet
75 private function run( $resultPageSet = null ) {
76 $params = $this->extractRequestParams();
78 $categoryTitle = $this->getTitleOrPageId( $params )->getTitle();
79 if ( $categoryTitle->getNamespace() !== NS_CATEGORY
) {
80 $this->dieWithError( 'apierror-invalidcategory' );
83 $prop = array_fill_keys( $params['prop'], true );
84 $fld_ids = isset( $prop['ids'] );
85 $fld_title = isset( $prop['title'] );
86 $fld_sortkey = isset( $prop['sortkey'] );
87 $fld_sortkeyprefix = isset( $prop['sortkeyprefix'] );
88 $fld_timestamp = isset( $prop['timestamp'] );
89 $fld_type = isset( $prop['type'] );
91 if ( $resultPageSet === null ) {
92 $this->addFields( [ 'cl_from', 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title' ] );
93 $this->addFieldsIf( 'page_id', $fld_ids );
94 $this->addFieldsIf( 'cl_sortkey_prefix', $fld_sortkeyprefix );
96 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
97 $this->addFields( [ 'cl_from', 'cl_sortkey', 'cl_type' ] );
100 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp ||
$params['sort'] == 'timestamp' );
102 $this->addTables( [ 'page', 'categorylinks' ] ); // must be in this order for 'USE INDEX'
104 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
105 $queryTypes = $params['type'];
108 // Scanning large datasets for rare categories sucks, and I already told
109 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
111 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
112 $miser_ns = $params['namespace'] ?
: [];
114 $this->addWhereFld( 'page_namespace', $params['namespace'] );
117 $dir = in_array( $params['dir'], [ 'asc', 'ascending', 'newer' ] ) ?
'newer' : 'older';
119 if ( $params['sort'] == 'timestamp' ) {
120 $this->addTimestampWhereRange( 'cl_timestamp',
124 // Include in ORDER BY for uniqueness
125 $this->addWhereRange( 'cl_from', $dir, null, null );
127 if ( $params['continue'] !== null ) {
128 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int' ] );
129 $op = ( $dir === 'newer' ?
'>=' : '<=' );
130 $db = $this->getDB();
131 $this->addWhere( $db->buildComparison( $op, [
132 'cl_timestamp' => $db->timestamp( $cont[0] ),
133 'cl_from' => $cont[1],
137 $this->addOption( 'USE INDEX', [ 'categorylinks' => 'cl_timestamp' ] );
139 if ( $params['continue'] ) {
140 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
142 // Remove the types to skip from $queryTypes
143 $contTypeIndex = array_search( $cont[0], $queryTypes );
144 $queryTypes = array_slice( $queryTypes, $contTypeIndex );
146 // Add a WHERE clause for sortkey and from
147 $this->dieContinueUsageIf( !$this->validateHexSortkey( $cont[1] ) );
148 $op = $dir == 'newer' ?
'>=' : '<=';
149 // $contWhere is used further down
150 $contWhere = $this->getDB()->buildComparison( $op, [
151 'cl_sortkey' => hex2bin( $cont[1] ),
152 'cl_from' => $cont[2],
154 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
155 $this->addWhereRange( 'cl_sortkey', $dir, null, null );
156 $this->addWhereRange( 'cl_from', $dir, null, null );
158 if ( $params['startsortkeyprefix'] !== null ) {
159 $startsortkey = $this->collation
->getSortKey( $params['startsortkeyprefix'] );
160 } elseif ( $params['starthexsortkey'] !== null ) {
161 if ( !$this->validateHexSortkey( $params['starthexsortkey'] ) ) {
162 $encParamName = $this->encodeParamName( 'starthexsortkey' );
163 $this->dieWithError( [ 'apierror-badparameter', $encParamName ], "badvalue_$encParamName" );
165 $startsortkey = hex2bin( $params['starthexsortkey'] );
167 $startsortkey = $params['startsortkey'];
169 if ( $params['endsortkeyprefix'] !== null ) {
170 $endsortkey = $this->collation
->getSortKey( $params['endsortkeyprefix'] );
171 } elseif ( $params['endhexsortkey'] !== null ) {
172 if ( !$this->validateHexSortkey( $params['endhexsortkey'] ) ) {
173 $encParamName = $this->encodeParamName( 'endhexsortkey' );
174 $this->dieWithError( [ 'apierror-badparameter', $encParamName ], "badvalue_$encParamName" );
176 $endsortkey = hex2bin( $params['endhexsortkey'] );
178 $endsortkey = $params['endsortkey'];
181 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
182 $this->addWhereRange( 'cl_sortkey',
186 $this->addWhereRange( 'cl_from', $dir, null, null );
188 $this->addOption( 'USE INDEX', [ 'categorylinks' => 'cl_sortkey' ] );
191 $this->addWhere( 'cl_from=page_id' );
193 $limit = $params['limit'];
194 $this->addOption( 'LIMIT', $limit +
1 );
196 if ( $params['sort'] == 'sortkey' ) {
197 // Run a separate SELECT query for each value of cl_type.
198 // This is needed because cl_type is an enum, and MySQL has
199 // inconsistencies between ORDER BY cl_type and
200 // WHERE cl_type >= 'foo' making proper paging impossible
204 foreach ( $queryTypes as $type ) {
205 $extraConds = [ 'cl_type' => $type ];
206 if ( $first && $contWhere ) {
207 // Continuation condition. Only added to the
208 // first query, otherwise we'll skip things
209 $extraConds[] = $contWhere;
211 $res = $this->select( __METHOD__
, [ 'where' => $extraConds ] );
212 if ( $type === 'page' && $resultPageSet === null ) {
213 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__
);
215 $rows = array_merge( $rows, iterator_to_array( $res ) );
216 if ( count( $rows ) >= $limit +
1 ) {
222 // Sorting by timestamp
223 // No need to worry about per-type queries because we
224 // aren't sorting or filtering by type anyway
225 $res = $this->select( __METHOD__
);
226 if ( $resultPageSet === null ) {
227 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__
);
229 $rows = iterator_to_array( $res );
232 $result = $this->getResult();
234 foreach ( $rows as $row ) {
235 if ( ++
$count > $limit ) {
236 // We've reached the one extra which shows that there are
237 // additional pages to be had. Stop here...
238 // @todo Security issue - if the user has no right to view next
239 // title, it will still be shown
240 if ( $params['sort'] == 'timestamp' ) {
241 $this->setContinueEnumParameter(
243 $this->getDB()->timestamp( $row->cl_timestamp
) . "|$row->cl_from"
246 $sortkey = bin2hex( $row->cl_sortkey
);
247 $this->setContinueEnumParameter( 'continue',
248 "{$row->cl_type}|$sortkey|{$row->cl_from}"
254 // Since domas won't tell anyone what he told long ago, apply
255 // cmnamespace here. This means the query may return 0 actual
256 // results, but on the other hand it could save returning 5000
257 // useless results to the client. ~~~~
258 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
262 if ( $resultPageSet === null ) {
264 ApiResult
::META_TYPE
=> 'assoc',
267 $vals['pageid'] = (int)$row->page_id
;
270 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
271 ApiQueryBase
::addTitleInfo( $vals, $title );
273 if ( $fld_sortkey ) {
274 $vals['sortkey'] = bin2hex( $row->cl_sortkey
);
276 if ( $fld_sortkeyprefix ) {
277 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix
;
280 $vals['type'] = $row->cl_type
;
282 if ( $fld_timestamp ) {
283 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
285 $fit = $result->addValue( [ 'query', $this->getModuleName() ],
288 if ( $params['sort'] == 'timestamp' ) {
289 $this->setContinueEnumParameter(
291 $this->getDB()->timestamp( $row->cl_timestamp
) . "|$row->cl_from"
294 $sortkey = bin2hex( $row->cl_sortkey
);
295 $this->setContinueEnumParameter( 'continue',
296 "{$row->cl_type}|$sortkey|{$row->cl_from}"
302 $resultPageSet->processDbRow( $row );
306 if ( $resultPageSet === null ) {
307 $result->addIndexedTagName(
308 [ 'query', $this->getModuleName() ], 'cm' );
312 public function getAllowedParams() {
315 ParamValidator
::PARAM_TYPE
=> 'string',
318 ParamValidator
::PARAM_TYPE
=> 'integer'
321 ParamValidator
::PARAM_DEFAULT
=> 'ids|title',
322 ParamValidator
::PARAM_ISMULTI
=> true,
323 ParamValidator
::PARAM_TYPE
=> [
331 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
334 ParamValidator
::PARAM_ISMULTI
=> true,
335 ParamValidator
::PARAM_TYPE
=> 'namespace',
338 ParamValidator
::PARAM_ISMULTI
=> true,
339 ParamValidator
::PARAM_DEFAULT
=> 'page|subcat|file',
340 ParamValidator
::PARAM_TYPE
=> [
347 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
350 ParamValidator
::PARAM_TYPE
=> 'limit',
351 ParamValidator
::PARAM_DEFAULT
=> 10,
352 IntegerDef
::PARAM_MIN
=> 1,
353 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
354 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
357 ParamValidator
::PARAM_DEFAULT
=> 'sortkey',
358 ParamValidator
::PARAM_TYPE
=> [
364 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
365 ParamValidator
::PARAM_TYPE
=> [
368 // Normalising with other modules
376 ParamValidator
::PARAM_TYPE
=> 'timestamp'
379 ParamValidator
::PARAM_TYPE
=> 'timestamp'
381 'starthexsortkey' => null,
382 'endhexsortkey' => null,
383 'startsortkeyprefix' => null,
384 'endsortkeyprefix' => null,
386 ParamValidator
::PARAM_DEPRECATED
=> true,
389 ParamValidator
::PARAM_DEPRECATED
=> true,
393 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
394 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
395 'api-help-param-limited-in-miser-mode',
402 protected function getExamplesMessages() {
404 'action=query&list=categorymembers&cmtitle=Category:Physics'
405 => 'apihelp-query+categorymembers-example-simple',
406 'action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info'
407 => 'apihelp-query+categorymembers-example-generator',
411 public function getHelpUrls() {
412 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categorymembers';
416 /** @deprecated class alias since 1.43 */
417 class_alias( ApiQueryCategoryMembers
::class, 'ApiQueryCategoryMembers' );