Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 243
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryCategoryMembers
0.00% covered (danger)
0.00%
0 / 243
0.00% covered (danger)
0.00%
0 / 9
2550
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validateHexSortkey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 147
0.00% covered (danger)
0.00%
0 / 1
1722
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 83
0.00% covered (danger)
0.00%
0 / 1
6
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23use MediaWiki\Collation\CollationFactory;
24use MediaWiki\MainConfigNames;
25use MediaWiki\Title\Title;
26use Wikimedia\ParamValidator\ParamValidator;
27use Wikimedia\ParamValidator\TypeDef\IntegerDef;
28
29/**
30 * A query module to enumerate pages that belong to a category.
31 *
32 * @ingroup API
33 */
34class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
35
36    private Collation $collation;
37
38    /**
39     * @param ApiQuery $query
40     * @param string $moduleName
41     * @param CollationFactory $collationFactory
42     */
43    public function __construct(
44        ApiQuery $query,
45        $moduleName,
46        CollationFactory $collationFactory
47    ) {
48        parent::__construct( $query, $moduleName, 'cm' );
49        $this->collation = $collationFactory->getCategoryCollation();
50    }
51
52    public function execute() {
53        $this->run();
54    }
55
56    public function getCacheMode( $params ) {
57        return 'public';
58    }
59
60    public function executeGenerator( $resultPageSet ) {
61        $this->run( $resultPageSet );
62    }
63
64    /**
65     * @param string $hexSortkey
66     * @return bool
67     */
68    private function validateHexSortkey( $hexSortkey ) {
69        // A hex sortkey has an unbound number of 2 letter pairs
70        return (bool)preg_match( '/^(?:[a-fA-F0-9]{2})*$/D', $hexSortkey );
71    }
72
73    /**
74     * @param ApiPageSet|null $resultPageSet
75     * @return void
76     */
77    private function run( $resultPageSet = null ) {
78        $params = $this->extractRequestParams();
79
80        $categoryTitle = $this->getTitleOrPageId( $params )->getTitle();
81        if ( $categoryTitle->getNamespace() !== NS_CATEGORY ) {
82            $this->dieWithError( 'apierror-invalidcategory' );
83        }
84
85        $prop = array_fill_keys( $params['prop'], true );
86        $fld_ids = isset( $prop['ids'] );
87        $fld_title = isset( $prop['title'] );
88        $fld_sortkey = isset( $prop['sortkey'] );
89        $fld_sortkeyprefix = isset( $prop['sortkeyprefix'] );
90        $fld_timestamp = isset( $prop['timestamp'] );
91        $fld_type = isset( $prop['type'] );
92
93        if ( $resultPageSet === null ) {
94            $this->addFields( [ 'cl_from', 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title' ] );
95            $this->addFieldsIf( 'page_id', $fld_ids );
96            $this->addFieldsIf( 'cl_sortkey_prefix', $fld_sortkeyprefix );
97        } else {
98            $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
99            $this->addFields( [ 'cl_from', 'cl_sortkey', 'cl_type' ] );
100        }
101
102        $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
103
104        $this->addTables( [ 'page', 'categorylinks' ] ); // must be in this order for 'USE INDEX'
105
106        $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
107        $queryTypes = $params['type'];
108        $contWhere = false;
109
110        // Scanning large datasets for rare categories sucks, and I already told
111        // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
112        $miser_ns = [];
113        if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
114            $miser_ns = $params['namespace'] ?: [];
115        } else {
116            $this->addWhereFld( 'page_namespace', $params['namespace'] );
117        }
118
119        $dir = in_array( $params['dir'], [ 'asc', 'ascending', 'newer' ] ) ? 'newer' : 'older';
120
121        if ( $params['sort'] == 'timestamp' ) {
122            $this->addTimestampWhereRange( 'cl_timestamp',
123                $dir,
124                $params['start'],
125                $params['end'] );
126            // Include in ORDER BY for uniqueness
127            $this->addWhereRange( 'cl_from', $dir, null, null );
128
129            if ( $params['continue'] !== null ) {
130                $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int' ] );
131                $op = ( $dir === 'newer' ? '>=' : '<=' );
132                $db = $this->getDB();
133                $this->addWhere( $db->buildComparison( $op, [
134                    'cl_timestamp' => $db->timestamp( $cont[0] ),
135                    'cl_from' => $cont[1],
136                ] ) );
137            }
138
139            $this->addOption( 'USE INDEX', [ 'categorylinks' => 'cl_timestamp' ] );
140        } else {
141            if ( $params['continue'] ) {
142                $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
143
144                // Remove the types to skip from $queryTypes
145                $contTypeIndex = array_search( $cont[0], $queryTypes );
146                $queryTypes = array_slice( $queryTypes, $contTypeIndex );
147
148                // Add a WHERE clause for sortkey and from
149                $this->dieContinueUsageIf( !$this->validateHexSortkey( $cont[1] ) );
150                $op = $dir == 'newer' ? '>=' : '<=';
151                // $contWhere is used further down
152                $contWhere = $this->getDB()->buildComparison( $op, [
153                    'cl_sortkey' => hex2bin( $cont[1] ),
154                    'cl_from' => $cont[2],
155                ] );
156                // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
157                $this->addWhereRange( 'cl_sortkey', $dir, null, null );
158                $this->addWhereRange( 'cl_from', $dir, null, null );
159            } else {
160                if ( $params['startsortkeyprefix'] !== null ) {
161                    $startsortkey = $this->collation->getSortKey( $params['startsortkeyprefix'] );
162                } elseif ( $params['starthexsortkey'] !== null ) {
163                    if ( !$this->validateHexSortkey( $params['starthexsortkey'] ) ) {
164                        $encParamName = $this->encodeParamName( 'starthexsortkey' );
165                        $this->dieWithError( [ 'apierror-badparameter', $encParamName ], "badvalue_$encParamName" );
166                    }
167                    $startsortkey = hex2bin( $params['starthexsortkey'] );
168                } else {
169                    $startsortkey = $params['startsortkey'];
170                }
171                if ( $params['endsortkeyprefix'] !== null ) {
172                    $endsortkey = $this->collation->getSortKey( $params['endsortkeyprefix'] );
173                } elseif ( $params['endhexsortkey'] !== null ) {
174                    if ( !$this->validateHexSortkey( $params['endhexsortkey'] ) ) {
175                        $encParamName = $this->encodeParamName( 'endhexsortkey' );
176                        $this->dieWithError( [ 'apierror-badparameter', $encParamName ], "badvalue_$encParamName" );
177                    }
178                    $endsortkey = hex2bin( $params['endhexsortkey'] );
179                } else {
180                    $endsortkey = $params['endsortkey'];
181                }
182
183                // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
184                $this->addWhereRange( 'cl_sortkey',
185                    $dir,
186                    $startsortkey,
187                    $endsortkey );
188                $this->addWhereRange( 'cl_from', $dir, null, null );
189            }
190            $this->addOption( 'USE INDEX', [ 'categorylinks' => 'cl_sortkey' ] );
191        }
192
193        $this->addWhere( 'cl_from=page_id' );
194
195        $limit = $params['limit'];
196        $this->addOption( 'LIMIT', $limit + 1 );
197
198        if ( $params['sort'] == 'sortkey' ) {
199            // Run a separate SELECT query for each value of cl_type.
200            // This is needed because cl_type is an enum, and MySQL has
201            // inconsistencies between ORDER BY cl_type and
202            // WHERE cl_type >= 'foo' making proper paging impossible
203            // and unindexed.
204            $rows = [];
205            $first = true;
206            foreach ( $queryTypes as $type ) {
207                $extraConds = [ 'cl_type' => $type ];
208                if ( $first && $contWhere ) {
209                    // Continuation condition. Only added to the
210                    // first query, otherwise we'll skip things
211                    $extraConds[] = $contWhere;
212                }
213                $res = $this->select( __METHOD__, [ 'where' => $extraConds ] );
214                if ( $type === 'page' && $resultPageSet === null ) {
215                    $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
216                }
217                $rows = array_merge( $rows, iterator_to_array( $res ) );
218                if ( count( $rows ) >= $limit + 1 ) {
219                    break;
220                }
221                $first = false;
222            }
223        } else {
224            // Sorting by timestamp
225            // No need to worry about per-type queries because we
226            // aren't sorting or filtering by type anyway
227            $res = $this->select( __METHOD__ );
228            if ( $resultPageSet === null ) {
229                $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
230            }
231            $rows = iterator_to_array( $res );
232        }
233
234        $result = $this->getResult();
235        $count = 0;
236        foreach ( $rows as $row ) {
237            if ( ++$count > $limit ) {
238                // We've reached the one extra which shows that there are
239                // additional pages to be had. Stop here...
240                // @todo Security issue - if the user has no right to view next
241                // title, it will still be shown
242                if ( $params['sort'] == 'timestamp' ) {
243                    $this->setContinueEnumParameter(
244                        'continue',
245                        $this->getDB()->timestamp( $row->cl_timestamp ) . "|$row->cl_from"
246                    );
247                } else {
248                    $sortkey = bin2hex( $row->cl_sortkey );
249                    $this->setContinueEnumParameter( 'continue',
250                        "{$row->cl_type}|$sortkey|{$row->cl_from}"
251                    );
252                }
253                break;
254            }
255
256            // Since domas won't tell anyone what he told long ago, apply
257            // cmnamespace here. This means the query may return 0 actual
258            // results, but on the other hand it could save returning 5000
259            // useless results to the client. ~~~~
260            if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
261                continue;
262            }
263
264            if ( $resultPageSet === null ) {
265                $vals = [
266                    ApiResult::META_TYPE => 'assoc',
267                ];
268                if ( $fld_ids ) {
269                    $vals['pageid'] = (int)$row->page_id;
270                }
271                if ( $fld_title ) {
272                    $title = Title::makeTitle( $row->page_namespace, $row->page_title );
273                    ApiQueryBase::addTitleInfo( $vals, $title );
274                }
275                if ( $fld_sortkey ) {
276                    $vals['sortkey'] = bin2hex( $row->cl_sortkey );
277                }
278                if ( $fld_sortkeyprefix ) {
279                    $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
280                }
281                if ( $fld_type ) {
282                    $vals['type'] = $row->cl_type;
283                }
284                if ( $fld_timestamp ) {
285                    $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
286                }
287                $fit = $result->addValue( [ 'query', $this->getModuleName() ],
288                    null, $vals );
289                if ( !$fit ) {
290                    if ( $params['sort'] == 'timestamp' ) {
291                        $this->setContinueEnumParameter(
292                            'continue',
293                            $this->getDB()->timestamp( $row->cl_timestamp ) . "|$row->cl_from"
294                        );
295                    } else {
296                        $sortkey = bin2hex( $row->cl_sortkey );
297                        $this->setContinueEnumParameter( 'continue',
298                            "{$row->cl_type}|$sortkey|{$row->cl_from}"
299                        );
300                    }
301                    break;
302                }
303            } else {
304                $resultPageSet->processDbRow( $row );
305            }
306        }
307
308        if ( $resultPageSet === null ) {
309            $result->addIndexedTagName(
310                [ 'query', $this->getModuleName() ], 'cm' );
311        }
312    }
313
314    public function getAllowedParams() {
315        $ret = [
316            'title' => [
317                ParamValidator::PARAM_TYPE => 'string',
318            ],
319            'pageid' => [
320                ParamValidator::PARAM_TYPE => 'integer'
321            ],
322            'prop' => [
323                ParamValidator::PARAM_DEFAULT => 'ids|title',
324                ParamValidator::PARAM_ISMULTI => true,
325                ParamValidator::PARAM_TYPE => [
326                    'ids',
327                    'title',
328                    'sortkey',
329                    'sortkeyprefix',
330                    'type',
331                    'timestamp',
332                ],
333                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
334            ],
335            'namespace' => [
336                ParamValidator::PARAM_ISMULTI => true,
337                ParamValidator::PARAM_TYPE => 'namespace',
338            ],
339            'type' => [
340                ParamValidator::PARAM_ISMULTI => true,
341                ParamValidator::PARAM_DEFAULT => 'page|subcat|file',
342                ParamValidator::PARAM_TYPE => [
343                    'page',
344                    'subcat',
345                    'file'
346                ]
347            ],
348            'continue' => [
349                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
350            ],
351            'limit' => [
352                ParamValidator::PARAM_TYPE => 'limit',
353                ParamValidator::PARAM_DEFAULT => 10,
354                IntegerDef::PARAM_MIN => 1,
355                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
356                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
357            ],
358            'sort' => [
359                ParamValidator::PARAM_DEFAULT => 'sortkey',
360                ParamValidator::PARAM_TYPE => [
361                    'sortkey',
362                    'timestamp'
363                ]
364            ],
365            'dir' => [
366                ParamValidator::PARAM_DEFAULT => 'ascending',
367                ParamValidator::PARAM_TYPE => [
368                    'asc',
369                    'desc',
370                    // Normalising with other modules
371                    'ascending',
372                    'descending',
373                    'newer',
374                    'older',
375                ]
376            ],
377            'start' => [
378                ParamValidator::PARAM_TYPE => 'timestamp'
379            ],
380            'end' => [
381                ParamValidator::PARAM_TYPE => 'timestamp'
382            ],
383            'starthexsortkey' => null,
384            'endhexsortkey' => null,
385            'startsortkeyprefix' => null,
386            'endsortkeyprefix' => null,
387            'startsortkey' => [
388                ParamValidator::PARAM_DEPRECATED => true,
389            ],
390            'endsortkey' => [
391                ParamValidator::PARAM_DEPRECATED => true,
392            ],
393        ];
394
395        if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
396            $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
397                'api-help-param-limited-in-miser-mode',
398            ];
399        }
400
401        return $ret;
402    }
403
404    protected function getExamplesMessages() {
405        return [
406            'action=query&list=categorymembers&cmtitle=Category:Physics'
407                => 'apihelp-query+categorymembers-example-simple',
408            'action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info'
409                => 'apihelp-query+categorymembers-example-generator',
410        ];
411    }
412
413    public function getHelpUrls() {
414        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categorymembers';
415    }
416}