MediaWiki master
ApiQueryTags.php
Go to the documentation of this file.
1<?php
26
33
34 private ChangeTagsStore $changeTagsStore;
35
36 public function __construct( ApiQuery $query, $moduleName, ChangeTagsStore $changeTagsStore ) {
37 parent::__construct( $query, $moduleName, 'tg' );
38 $this->changeTagsStore = $changeTagsStore;
39 }
40
41 public function execute() {
42 $params = $this->extractRequestParams();
43
44 $prop = array_fill_keys( $params['prop'], true );
45
46 $fld_displayname = isset( $prop['displayname'] );
47 $fld_description = isset( $prop['description'] );
48 $fld_hitcount = isset( $prop['hitcount'] );
49 $fld_defined = isset( $prop['defined'] );
50 $fld_source = isset( $prop['source'] );
51 $fld_active = isset( $prop['active'] );
52
53 $limit = $params['limit'];
54 $result = $this->getResult();
55
56 $softwareDefinedTags = array_fill_keys( $this->changeTagsStore->listSoftwareDefinedTags(), 0 );
57 $explicitlyDefinedTags = array_fill_keys( $this->changeTagsStore->listExplicitlyDefinedTags(), 0 );
58 $softwareActivatedTags = array_fill_keys( $this->changeTagsStore->listSoftwareActivatedTags(), 0 );
59
60 $tagHitcounts = array_merge(
61 $softwareDefinedTags,
62 $explicitlyDefinedTags,
63 $this->changeTagsStore->tagUsageStatistics()
64 );
65 $tags = array_keys( $tagHitcounts );
66
67 # Fetch defined tags that aren't past the continuation
68 if ( $params['continue'] !== null ) {
69 $cont = $params['continue'];
70 $tags = array_filter( $tags, static function ( $v ) use ( $cont ) {
71 return $v >= $cont;
72 } );
73 }
74
75 # Now make sure the array is sorted for proper continuation
76 sort( $tags );
77
78 $count = 0;
79 foreach ( $tags as $tagName ) {
80 if ( ++$count > $limit ) {
81 $this->setContinueEnumParameter( 'continue', $tagName );
82 break;
83 }
84
85 $tag = [];
86 $tag['name'] = $tagName;
87
88 if ( $fld_displayname ) {
89 $tag['displayname'] = ChangeTags::tagDescription( $tagName, $this );
90 }
91
92 if ( $fld_description ) {
93 $msg = $this->msg( "tag-$tagName-description" );
94 $tag['description'] = $msg->exists() ? $msg->text() : '';
95 }
96
97 if ( $fld_hitcount ) {
98 $tag['hitcount'] = (int)$tagHitcounts[$tagName];
99 }
100
101 $isSoftware = isset( $softwareDefinedTags[$tagName] );
102 $isExplicit = isset( $explicitlyDefinedTags[$tagName] );
103
104 if ( $fld_defined ) {
105 $tag['defined'] = $isSoftware || $isExplicit;
106 }
107
108 if ( $fld_source ) {
109 $tag['source'] = [];
110 if ( $isSoftware ) {
111 $tag['source'][] = 'software';
112 // @TODO: remove backwards compatibility entry (T247552)
113 $tag['source'][] = 'extension';
114 }
115 if ( $isExplicit ) {
116 $tag['source'][] = 'manual';
117 }
118 }
119
120 if ( $fld_active ) {
121 $tag['active'] = $isExplicit || isset( $softwareActivatedTags[$tagName] );
122 }
123
124 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $tag );
125 if ( !$fit ) {
126 $this->setContinueEnumParameter( 'continue', $tagName );
127 break;
128 }
129 }
130
131 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'tag' );
132 }
133
134 public function getCacheMode( $params ) {
135 return 'public';
136 }
137
138 public function getAllowedParams() {
139 return [
140 'continue' => [
141 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
142 ],
143 'limit' => [
144 ParamValidator::PARAM_DEFAULT => 10,
145 ParamValidator::PARAM_TYPE => 'limit',
146 IntegerDef::PARAM_MIN => 1,
147 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
148 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
149 ],
150 'prop' => [
151 ParamValidator::PARAM_DEFAULT => '',
152 ParamValidator::PARAM_TYPE => [
153 'displayname',
154 'description',
155 'hitcount',
156 'defined',
157 'source',
158 'active',
159 ],
160 ParamValidator::PARAM_ISMULTI => true,
162 ]
163 ];
164 }
165
166 protected function getExamplesMessages() {
167 return [
168 'action=query&list=tags&tgprop=displayname|description|hitcount|defined'
169 => 'apihelp-query+tags-example-simple',
170 ];
171 }
172
173 public function getHelpUrls() {
174 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tags';
175 }
176}
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:211
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:236
getResult()
Get the result object.
Definition ApiBase.php:671
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:811
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:171
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:238
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:532
This is a base class for all Query modules.
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Query module to enumerate change tags.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName, ChangeTagsStore $changeTagsStore)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.
This is the main query class.
Definition ApiQuery.php:43
static tagDescription( $tag, MessageLocalizer $context)
Get a short description for a tag.
Gateway class for 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.