MediaWiki master
ApiQueryTags.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
15
22
23 public function __construct(
24 ApiQuery $query,
25 string $moduleName,
26 private readonly ChangeTagsStore $changeTagsStore,
27 ) {
28 parent::__construct( $query, $moduleName, 'tg' );
29 }
30
31 public function execute() {
32 $params = $this->extractRequestParams();
33
34 $prop = array_fill_keys( $params['prop'], true );
35
36 $fld_displayname = isset( $prop['displayname'] );
37 $fld_description = isset( $prop['description'] );
38 $fld_hitcount = isset( $prop['hitcount'] );
39 $fld_defined = isset( $prop['defined'] );
40 $fld_source = isset( $prop['source'] );
41 $fld_active = isset( $prop['active'] );
42
43 $limit = $params['limit'];
44 $result = $this->getResult();
45
46 $softwareDefinedTags = array_fill_keys( $this->changeTagsStore->listSoftwareDefinedTags(), 0 );
47 $explicitlyDefinedTags = array_fill_keys( $this->changeTagsStore->listExplicitlyDefinedTags(), 0 );
48 $softwareActivatedTags = array_fill_keys( $this->changeTagsStore->listSoftwareActivatedTags(), 0 );
49
50 $tagHitcounts = array_merge(
51 $softwareDefinedTags,
52 $explicitlyDefinedTags,
53 $this->changeTagsStore->tagUsageStatistics()
54 );
55 $tags = array_keys( $tagHitcounts );
56
57 # Fetch defined tags that aren't past the continuation
58 if ( $params['continue'] !== null ) {
59 $cont = $params['continue'];
60 $tags = array_filter( $tags, static function ( $v ) use ( $cont ) {
61 return $v >= $cont;
62 } );
63 }
64
65 # Now make sure the array is sorted for proper continuation
66 sort( $tags );
67
68 $count = 0;
69 foreach ( $tags as $tagName ) {
70 if ( ++$count > $limit ) {
71 $this->setContinueEnumParameter( 'continue', $tagName );
72 break;
73 }
74
75 $tag = [];
76 $tag['name'] = $tagName;
77
78 if ( $fld_displayname ) {
79 $tag['displayname'] = ChangeTags::tagDescription( $tagName, $this );
80 }
81
82 if ( $fld_description ) {
83 $msg = $this->msg( "tag-$tagName-description" );
84 $tag['description'] = $msg->exists() ? $msg->text() : '';
85 }
86
87 if ( $fld_hitcount ) {
88 $tag['hitcount'] = (int)$tagHitcounts[$tagName];
89 }
90
91 $isSoftware = isset( $softwareDefinedTags[$tagName] );
92 $isExplicit = isset( $explicitlyDefinedTags[$tagName] );
93
94 if ( $fld_defined ) {
95 $tag['defined'] = $isSoftware || $isExplicit;
96 }
97
98 if ( $fld_source ) {
99 $tag['source'] = [];
100 if ( $isSoftware ) {
101 $tag['source'][] = 'software';
102 // @TODO: remove backwards compatibility entry (T247552)
103 $tag['source'][] = 'extension';
104 }
105 if ( $isExplicit ) {
106 $tag['source'][] = 'manual';
107 }
108 }
109
110 if ( $fld_active ) {
111 $tag['active'] = $isExplicit || isset( $softwareActivatedTags[$tagName] );
112 }
113
114 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $tag );
115 if ( !$fit ) {
116 $this->setContinueEnumParameter( 'continue', $tagName );
117 break;
118 }
119 }
120
121 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'tag' );
122 }
123
125 public function getCacheMode( $params ) {
126 return 'public';
127 }
128
130 public function getAllowedParams() {
131 return [
132 'continue' => [
133 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
134 ],
135 'limit' => [
136 ParamValidator::PARAM_DEFAULT => 10,
137 ParamValidator::PARAM_TYPE => 'limit',
138 IntegerDef::PARAM_MIN => 1,
139 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
140 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
141 ],
142 'prop' => [
143 ParamValidator::PARAM_DEFAULT => '',
144 ParamValidator::PARAM_TYPE => [
145 'displayname',
146 'description',
147 'hitcount',
148 'defined',
149 'source',
150 'active',
151 ],
152 ParamValidator::PARAM_ISMULTI => true,
154 ]
155 ];
156 }
157
159 protected function getExamplesMessages() {
160 return [
161 'action=query&list=tags&tgprop=displayname|description|hitcount|defined'
162 => 'apihelp-query+tags-example-simple',
163 ];
164 }
165
167 public function getHelpUrls() {
168 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tags';
169 }
170}
171
173class_alias( ApiQueryTags::class, 'ApiQueryTags' );
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:206
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:233
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:231
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...
__construct(ApiQuery $query, string $moduleName, private readonly ChangeTagsStore $changeTagsStore,)
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
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.Override this in the module subclass....
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
This is the main query class.
Definition ApiQuery.php:36
Read-write access to the change_tags table.
Recent changes tagging.
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.