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