MediaWiki master
ApiQueryTags.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
25use ChangeTags;
29
36
37 private ChangeTagsStore $changeTagsStore;
38
39 public function __construct( ApiQuery $query, string $moduleName, ChangeTagsStore $changeTagsStore ) {
40 parent::__construct( $query, $moduleName, 'tg' );
41 $this->changeTagsStore = $changeTagsStore;
42 }
43
44 public function execute() {
46
47 $prop = array_fill_keys( $params['prop'], true );
48
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'] );
55
56 $limit = $params['limit'];
57 $result = $this->getResult();
58
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 );
62
63 $tagHitcounts = array_merge(
64 $softwareDefinedTags,
65 $explicitlyDefinedTags,
66 $this->changeTagsStore->tagUsageStatistics()
67 );
68 $tags = array_keys( $tagHitcounts );
69
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 } );
76 }
77
78 # Now make sure the array is sorted for proper continuation
79 sort( $tags );
80
81 $count = 0;
82 foreach ( $tags as $tagName ) {
83 if ( ++$count > $limit ) {
84 $this->setContinueEnumParameter( 'continue', $tagName );
85 break;
86 }
87
88 $tag = [];
89 $tag['name'] = $tagName;
90
91 if ( $fld_displayname ) {
92 $tag['displayname'] = ChangeTags::tagDescription( $tagName, $this );
93 }
94
95 if ( $fld_description ) {
96 $msg = $this->msg( "tag-$tagName-description" );
97 $tag['description'] = $msg->exists() ? $msg->text() : '';
98 }
99
100 if ( $fld_hitcount ) {
101 $tag['hitcount'] = (int)$tagHitcounts[$tagName];
102 }
103
104 $isSoftware = isset( $softwareDefinedTags[$tagName] );
105 $isExplicit = isset( $explicitlyDefinedTags[$tagName] );
106
107 if ( $fld_defined ) {
108 $tag['defined'] = $isSoftware || $isExplicit;
109 }
110
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';
117 }
118 if ( $isExplicit ) {
119 $tag['source'][] = 'manual';
120 }
121 }
122
123 if ( $fld_active ) {
124 $tag['active'] = $isExplicit || isset( $softwareActivatedTags[$tagName] );
125 }
126
127 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $tag );
128 if ( !$fit ) {
129 $this->setContinueEnumParameter( 'continue', $tagName );
130 break;
131 }
132 }
133
134 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'tag' );
135 }
136
137 public function getCacheMode( $params ) {
138 return 'public';
139 }
140
141 public function getAllowedParams() {
142 return [
143 'continue' => [
144 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
145 ],
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
152 ],
153 'prop' => [
154 ParamValidator::PARAM_DEFAULT => '',
155 ParamValidator::PARAM_TYPE => [
156 'displayname',
157 'description',
158 'hitcount',
159 'defined',
160 'source',
161 'active',
162 ],
163 ParamValidator::PARAM_ISMULTI => true,
165 ]
166 ];
167 }
168
169 protected function getExamplesMessages() {
170 return [
171 'action=query&list=tags&tgprop=displayname|description|hitcount|defined'
172 => 'apihelp-query+tags-example-simple',
173 ];
174 }
175
176 public function getHelpUrls() {
177 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tags';
178 }
179}
180
182class_alias( ApiQueryTags::class, 'ApiQueryTags' );
array $params
The job parameters.
Recent changes tagging.
static tagDescription( $tag, MessageLocalizer $context)
Get a short description for a tag.
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getResult()
Get the result object.
Definition ApiBase.php:696
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:221
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:181
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:248
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:246
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Query module to enumerate change tags.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getCacheMode( $params)
Get the cache mode for the data generated by this module.
__construct(ApiQuery $query, string $moduleName, ChangeTagsStore $changeTagsStore)
getHelpUrls()
Return links to more detailed help pages about the module.
This is the main query class.
Definition ApiQuery.php:48
Read-write access to the change_tags table.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Service for formatting and validating API parameters.
Type definition for integer types.