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
;
25 use MediaWiki\Title\Title
;
26 use Wikimedia\ParamValidator\ParamValidator
;
27 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
30 * A query module to enumerate categories the set of pages belong to.
34 class ApiQueryCategories
extends ApiQueryGeneratorBase
{
36 public function __construct( ApiQuery
$query, string $moduleName ) {
37 parent
::__construct( $query, $moduleName, 'cl' );
40 public function execute() {
44 public function getCacheMode( $params ) {
48 public function executeGenerator( $resultPageSet ) {
49 $this->run( $resultPageSet );
53 * @param ApiPageSet|null $resultPageSet
55 private function run( $resultPageSet = null ) {
56 $pages = $this->getPageSet()->getGoodPages();
57 if ( $pages === [] ) {
58 return; // nothing to do
61 $params = $this->extractRequestParams();
62 $prop = array_fill_keys( (array)$params['prop'], true );
63 $show = array_fill_keys( (array)$params['show'], true );
70 $this->addFieldsIf( [ 'cl_sortkey', 'cl_sortkey_prefix' ], isset( $prop['sortkey'] ) );
71 $this->addFieldsIf( 'cl_timestamp', isset( $prop['timestamp'] ) );
73 $this->addTables( 'categorylinks' );
74 $this->addWhereFld( 'cl_from', array_keys( $pages ) );
75 if ( $params['categories'] ) {
77 foreach ( $params['categories'] as $cat ) {
78 $title = Title
::newFromText( $cat );
79 if ( !$title ||
$title->getNamespace() !== NS_CATEGORY
) {
80 $this->addWarning( [ 'apiwarn-invalidcategory', wfEscapeWikiText( $cat ) ] );
82 $cats[] = $title->getDBkey();
86 // No titles so no results
89 $this->addWhereFld( 'cl_to', $cats );
92 if ( $params['continue'] !== null ) {
94 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
95 $op = $params['dir'] == 'descending' ?
'<=' : '>=';
96 $this->addWhere( $db->buildComparison( $op, [
97 'cl_from' => $cont[0],
102 if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) {
103 $this->dieWithError( 'apierror-show' );
105 if ( isset( $show['hidden'] ) ||
isset( $show['!hidden'] ) ||
isset( $prop['hidden'] ) ) {
106 $this->addOption( 'STRAIGHT_JOIN' );
107 $this->addTables( [ 'page', 'page_props' ] );
108 $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) );
109 $this->addJoinConds( [
110 'page' => [ 'LEFT JOIN', [
111 'page_namespace' => NS_CATEGORY
,
112 'page_title = cl_to' ] ],
113 'page_props' => [ 'LEFT JOIN', [
115 'pp_propname' => 'hiddencat' ] ]
117 if ( isset( $show['hidden'] ) ) {
118 $this->addWhere( $this->getDB()->expr( 'pp_propname', '!=', null ) );
119 } elseif ( isset( $show['!hidden'] ) ) {
120 $this->addWhere( [ 'pp_propname' => null ] );
124 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
125 // Don't order by cl_from if it's constant in the WHERE clause
126 if ( count( $pages ) === 1 ) {
127 $this->addOption( 'ORDER BY', 'cl_to' . $sort );
129 $this->addOption( 'ORDER BY', [
134 $this->addOption( 'LIMIT', $params['limit'] +
1 );
136 $res = $this->select( __METHOD__
);
139 if ( $resultPageSet === null ) {
140 foreach ( $res as $row ) {
141 if ( ++
$count > $params['limit'] ) {
142 // We've reached the one extra which shows that
143 // there are additional pages to be had. Stop here...
144 $this->setContinueEnumParameter( 'continue', $row->cl_from
. '|' . $row->cl_to
);
148 $title = Title
::makeTitle( NS_CATEGORY
, $row->cl_to
);
150 ApiQueryBase
::addTitleInfo( $vals, $title );
151 if ( isset( $prop['sortkey'] ) ) {
152 $vals['sortkey'] = bin2hex( $row->cl_sortkey
);
153 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix
;
155 if ( isset( $prop['timestamp'] ) ) {
156 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
158 if ( isset( $prop['hidden'] ) ) {
159 $vals['hidden'] = $row->pp_propname
!== null;
162 $fit = $this->addPageSubItem( $row->cl_from
, $vals );
164 $this->setContinueEnumParameter( 'continue', $row->cl_from
. '|' . $row->cl_to
);
170 foreach ( $res as $row ) {
171 if ( ++
$count > $params['limit'] ) {
172 // We've reached the one extra which shows that
173 // there are additional pages to be had. Stop here...
174 $this->setContinueEnumParameter( 'continue', $row->cl_from
. '|' . $row->cl_to
);
178 $titles[] = Title
::makeTitle( NS_CATEGORY
, $row->cl_to
);
180 $resultPageSet->populateFromTitles( $titles );
184 public function getAllowedParams() {
187 ParamValidator
::PARAM_ISMULTI
=> true,
188 ParamValidator
::PARAM_TYPE
=> [
193 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
196 ParamValidator
::PARAM_ISMULTI
=> true,
197 ParamValidator
::PARAM_TYPE
=> [
203 ParamValidator
::PARAM_DEFAULT
=> 10,
204 ParamValidator
::PARAM_TYPE
=> 'limit',
205 IntegerDef
::PARAM_MIN
=> 1,
206 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
207 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
210 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
213 ParamValidator
::PARAM_ISMULTI
=> true,
216 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
217 ParamValidator
::PARAM_TYPE
=> [
225 protected function getExamplesMessages() {
227 'action=query&prop=categories&titles=Albert%20Einstein'
228 => 'apihelp-query+categories-example-simple',
229 'action=query&generator=categories&titles=Albert%20Einstein&prop=info'
230 => 'apihelp-query+categories-example-generator',
234 public function getHelpUrls() {
235 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categories';
239 /** @deprecated class alias since 1.43 */
240 class_alias( ApiQueryCategories
::class, 'ApiQueryCategories' );