Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryCategories.php
blob9fa49b1b96f7b647de3bcd22685c33ab1bca7637
1 <?php
2 /**
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
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\Title\Title;
26 use Wikimedia\ParamValidator\ParamValidator;
27 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
29 /**
30 * A query module to enumerate categories the set of pages belong to.
32 * @ingroup API
34 class ApiQueryCategories extends ApiQueryGeneratorBase {
36 public function __construct( ApiQuery $query, string $moduleName ) {
37 parent::__construct( $query, $moduleName, 'cl' );
40 public function execute() {
41 $this->run();
44 public function getCacheMode( $params ) {
45 return 'public';
48 public function executeGenerator( $resultPageSet ) {
49 $this->run( $resultPageSet );
52 /**
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 );
65 $this->addFields( [
66 'cl_from',
67 'cl_to'
68 ] );
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'] ) {
76 $cats = [];
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 ) ] );
81 } else {
82 $cats[] = $title->getDBkey();
85 if ( !$cats ) {
86 // No titles so no results
87 return;
89 $this->addWhereFld( 'cl_to', $cats );
92 if ( $params['continue'] !== null ) {
93 $db = $this->getDB();
94 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
95 $op = $params['dir'] == 'descending' ? '<=' : '>=';
96 $this->addWhere( $db->buildComparison( $op, [
97 'cl_from' => $cont[0],
98 'cl_to' => $cont[1],
99 ] ) );
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', [
114 'pp_page=page_id',
115 'pp_propname' => 'hiddencat' ] ]
116 ] );
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 );
128 } else {
129 $this->addOption( 'ORDER BY', [
130 'cl_from' . $sort,
131 'cl_to' . $sort
132 ] );
134 $this->addOption( 'LIMIT', $params['limit'] + 1 );
136 $res = $this->select( __METHOD__ );
138 $count = 0;
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 );
145 break;
148 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
149 $vals = [];
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 );
163 if ( !$fit ) {
164 $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
165 break;
168 } else {
169 $titles = [];
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 );
175 break;
178 $titles[] = Title::makeTitle( NS_CATEGORY, $row->cl_to );
180 $resultPageSet->populateFromTitles( $titles );
184 public function getAllowedParams() {
185 return [
186 'prop' => [
187 ParamValidator::PARAM_ISMULTI => true,
188 ParamValidator::PARAM_TYPE => [
189 'sortkey',
190 'timestamp',
191 'hidden',
193 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
195 'show' => [
196 ParamValidator::PARAM_ISMULTI => true,
197 ParamValidator::PARAM_TYPE => [
198 'hidden',
199 '!hidden',
202 'limit' => [
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
209 'continue' => [
210 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
212 'categories' => [
213 ParamValidator::PARAM_ISMULTI => true,
215 'dir' => [
216 ParamValidator::PARAM_DEFAULT => 'ascending',
217 ParamValidator::PARAM_TYPE => [
218 'ascending',
219 'descending'
225 protected function getExamplesMessages() {
226 return [
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' );