Merge "Simplify code to avoid interpreting "$" characters in string replacement"
[mediawiki.git] / includes / api / ApiQueryTags.php
blobde995feb4dc4703fa68284ece52ee47ac33b3c8c
1 <?php
2 /**
3 * Copyright © 2009
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 ChangeTags;
26 use MediaWiki\ChangeTags\ChangeTagsStore;
27 use Wikimedia\ParamValidator\ParamValidator;
28 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
30 /**
31 * Query module to enumerate change tags.
33 * @ingroup API
35 class ApiQueryTags extends ApiQueryBase {
37 private ChangeTagsStore $changeTagsStore;
39 public function __construct( ApiQuery $query, $moduleName, ChangeTagsStore $changeTagsStore ) {
40 parent::__construct( $query, $moduleName, 'tg' );
41 $this->changeTagsStore = $changeTagsStore;
44 public function execute() {
45 $params = $this->extractRequestParams();
47 $prop = array_fill_keys( $params['prop'], true );
49 $fld_displayname = isset( $prop['displayname'] );
50 $fld_description = isset( $prop['description'] );
51 $fld_hitcount = isset( $prop['hitcount'] );
52 $fld_defined = isset( $prop['defined'] );
53 $fld_source = isset( $prop['source'] );
54 $fld_active = isset( $prop['active'] );
56 $limit = $params['limit'];
57 $result = $this->getResult();
59 $softwareDefinedTags = array_fill_keys( $this->changeTagsStore->listSoftwareDefinedTags(), 0 );
60 $explicitlyDefinedTags = array_fill_keys( $this->changeTagsStore->listExplicitlyDefinedTags(), 0 );
61 $softwareActivatedTags = array_fill_keys( $this->changeTagsStore->listSoftwareActivatedTags(), 0 );
63 $tagHitcounts = array_merge(
64 $softwareDefinedTags,
65 $explicitlyDefinedTags,
66 $this->changeTagsStore->tagUsageStatistics()
68 $tags = array_keys( $tagHitcounts );
70 # Fetch defined tags that aren't past the continuation
71 if ( $params['continue'] !== null ) {
72 $cont = $params['continue'];
73 $tags = array_filter( $tags, static function ( $v ) use ( $cont ) {
74 return $v >= $cont;
75 } );
78 # Now make sure the array is sorted for proper continuation
79 sort( $tags );
81 $count = 0;
82 foreach ( $tags as $tagName ) {
83 if ( ++$count > $limit ) {
84 $this->setContinueEnumParameter( 'continue', $tagName );
85 break;
88 $tag = [];
89 $tag['name'] = $tagName;
91 if ( $fld_displayname ) {
92 $tag['displayname'] = ChangeTags::tagDescription( $tagName, $this );
95 if ( $fld_description ) {
96 $msg = $this->msg( "tag-$tagName-description" );
97 $tag['description'] = $msg->exists() ? $msg->text() : '';
100 if ( $fld_hitcount ) {
101 $tag['hitcount'] = (int)$tagHitcounts[$tagName];
104 $isSoftware = isset( $softwareDefinedTags[$tagName] );
105 $isExplicit = isset( $explicitlyDefinedTags[$tagName] );
107 if ( $fld_defined ) {
108 $tag['defined'] = $isSoftware || $isExplicit;
111 if ( $fld_source ) {
112 $tag['source'] = [];
113 if ( $isSoftware ) {
114 $tag['source'][] = 'software';
115 // @TODO: remove backwards compatibility entry (T247552)
116 $tag['source'][] = 'extension';
118 if ( $isExplicit ) {
119 $tag['source'][] = 'manual';
123 if ( $fld_active ) {
124 $tag['active'] = $isExplicit || isset( $softwareActivatedTags[$tagName] );
127 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $tag );
128 if ( !$fit ) {
129 $this->setContinueEnumParameter( 'continue', $tagName );
130 break;
134 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'tag' );
137 public function getCacheMode( $params ) {
138 return 'public';
141 public function getAllowedParams() {
142 return [
143 'continue' => [
144 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
146 'limit' => [
147 ParamValidator::PARAM_DEFAULT => 10,
148 ParamValidator::PARAM_TYPE => 'limit',
149 IntegerDef::PARAM_MIN => 1,
150 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
151 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
153 'prop' => [
154 ParamValidator::PARAM_DEFAULT => '',
155 ParamValidator::PARAM_TYPE => [
156 'displayname',
157 'description',
158 'hitcount',
159 'defined',
160 'source',
161 'active',
163 ParamValidator::PARAM_ISMULTI => true,
164 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
169 protected function getExamplesMessages() {
170 return [
171 'action=query&list=tags&tgprop=displayname|description|hitcount|defined'
172 => 'apihelp-query+tags-example-simple',
176 public function getHelpUrls() {
177 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tags';
181 /** @deprecated class alias since 1.43 */
182 class_alias( ApiQueryTags::class, 'ApiQueryTags' );