MediaWiki REL1_34
ApiQueryTags.php
Go to the documentation of this file.
1<?php
29
30 public function __construct( ApiQuery $query, $moduleName ) {
31 parent::__construct( $query, $moduleName, 'tg' );
32 }
33
34 public function execute() {
35 $params = $this->extractRequestParams();
36
37 $prop = array_flip( $params['prop'] );
38
39 $fld_displayname = isset( $prop['displayname'] );
40 $fld_description = isset( $prop['description'] );
41 $fld_hitcount = isset( $prop['hitcount'] );
42 $fld_defined = isset( $prop['defined'] );
43 $fld_source = isset( $prop['source'] );
44 $fld_active = isset( $prop['active'] );
45
46 $limit = $params['limit'];
47 $result = $this->getResult();
48
49 $softwareDefinedTags = array_fill_keys( ChangeTags::listSoftwareDefinedTags(), 0 );
50 $explicitlyDefinedTags = array_fill_keys( ChangeTags::listExplicitlyDefinedTags(), 0 );
51 $softwareActivatedTags = array_fill_keys( ChangeTags::listSoftwareActivatedTags(), 0 );
53
54 $tagHitcounts = array_merge( $softwareDefinedTags, $explicitlyDefinedTags, $tagStats );
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, 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 // TODO: Can we change this to 'software'?
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
123 public function getCacheMode( $params ) {
124 return 'public';
125 }
126
127 public function getAllowedParams() {
128 return [
129 'continue' => [
130 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
131 ],
132 'limit' => [
134 ApiBase::PARAM_TYPE => 'limit',
138 ],
139 'prop' => [
142 'displayname',
143 'description',
144 'hitcount',
145 'defined',
146 'source',
147 'active',
148 ],
151 ]
152 ];
153 }
154
155 protected function getExamplesMessages() {
156 return [
157 'action=query&list=tags&tgprop=displayname|description|hitcount|defined'
158 => 'apihelp-query+tags-example-simple',
159 ];
160 }
161
162 public function getHelpUrls() {
163 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tags';
164 }
165}
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition ApiBase.php:103
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:97
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:55
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition ApiBase.php:164
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition ApiBase.php:106
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:259
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:131
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:261
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:58
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)
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:37
static listSoftwareDefinedTags()
Lists tags defined by core or extensions using the ListDefinedTags hook.
static listSoftwareActivatedTags()
Lists those tags which core or extensions report as being "active".
static tagUsageStatistics()
Returns a map of any tags used on the wiki to number of edits tagged with them, ordered descending by...
static tagDescription( $tag, MessageLocalizer $context)
Get a short description for a tag.
static listExplicitlyDefinedTags()
Lists tags explicitly defined in the change_tag_def table of the database.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()