MediaWiki  master
ApiQueryTags.php
Go to the documentation of this file.
1 <?php
26 
32 class ApiQueryTags extends ApiQueryBase {
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  // TODO: Can we change this to 'software'?
112  $tag['source'][] = 'extension';
113  }
114  if ( $isExplicit ) {
115  $tag['source'][] = 'manual';
116  }
117  }
118 
119  if ( $fld_active ) {
120  $tag['active'] = $isExplicit || isset( $softwareActivatedTags[$tagName] );
121  }
122 
123  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $tag );
124  if ( !$fit ) {
125  $this->setContinueEnumParameter( 'continue', $tagName );
126  break;
127  }
128  }
129 
130  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'tag' );
131  }
132 
133  public function getCacheMode( $params ) {
134  return 'public';
135  }
136 
137  public function getAllowedParams() {
138  return [
139  'continue' => [
140  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
141  ],
142  'limit' => [
143  ParamValidator::PARAM_DEFAULT => 10,
144  ParamValidator::PARAM_TYPE => 'limit',
145  IntegerDef::PARAM_MIN => 1,
146  IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
147  IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
148  ],
149  'prop' => [
150  ParamValidator::PARAM_DEFAULT => '',
151  ParamValidator::PARAM_TYPE => [
152  'displayname',
153  'description',
154  'hitcount',
155  'defined',
156  'source',
157  'active',
158  ],
159  ParamValidator::PARAM_ISMULTI => true,
161  ]
162  ];
163  }
164 
165  protected function getExamplesMessages() {
166  return [
167  'action=query&list=tags&tgprop=displayname|description|hitcount|defined'
168  => 'apihelp-query+tags-example-simple',
169  ];
170  }
171 
172  public function getHelpUrls() {
173  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tags';
174  }
175 }
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition: ApiBase.php:210
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:235
getResult()
Get the result object.
Definition: ApiBase.php:668
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:808
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:170
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:237
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:529
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.
Definition: ChangeTags.php:237
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Gateway class for change_tags table.
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23