MediaWiki  master
ApiQueryTags.php
Go to the documentation of this file.
1 <?php
25 
31 class ApiQueryTags extends ApiQueryBase {
32 
33  public function __construct( ApiQuery $query, $moduleName ) {
34  parent::__construct( $query, $moduleName, 'tg' );
35  }
36 
37  public function execute() {
38  $params = $this->extractRequestParams();
39 
40  $prop = array_fill_keys( $params['prop'], true );
41 
42  $fld_displayname = isset( $prop['displayname'] );
43  $fld_description = isset( $prop['description'] );
44  $fld_hitcount = isset( $prop['hitcount'] );
45  $fld_defined = isset( $prop['defined'] );
46  $fld_source = isset( $prop['source'] );
47  $fld_active = isset( $prop['active'] );
48 
49  $limit = $params['limit'];
50  $result = $this->getResult();
51 
52  $softwareDefinedTags = array_fill_keys( ChangeTags::listSoftwareDefinedTags(), 0 );
53  $explicitlyDefinedTags = array_fill_keys( ChangeTags::listExplicitlyDefinedTags(), 0 );
54  $softwareActivatedTags = array_fill_keys( ChangeTags::listSoftwareActivatedTags(), 0 );
55  $tagStats = ChangeTags::tagUsageStatistics();
56 
57  $tagHitcounts = array_merge( $softwareDefinedTags, $explicitlyDefinedTags, $tagStats );
58  $tags = array_keys( $tagHitcounts );
59 
60  # Fetch defined tags that aren't past the continuation
61  if ( $params['continue'] !== null ) {
62  $cont = $params['continue'];
63  $tags = array_filter( $tags, static function ( $v ) use ( $cont ) {
64  return $v >= $cont;
65  } );
66  }
67 
68  # Now make sure the array is sorted for proper continuation
69  sort( $tags );
70 
71  $count = 0;
72  foreach ( $tags as $tagName ) {
73  if ( ++$count > $limit ) {
74  $this->setContinueEnumParameter( 'continue', $tagName );
75  break;
76  }
77 
78  $tag = [];
79  $tag['name'] = $tagName;
80 
81  if ( $fld_displayname ) {
82  $tag['displayname'] = ChangeTags::tagDescription( $tagName, $this );
83  }
84 
85  if ( $fld_description ) {
86  $msg = $this->msg( "tag-$tagName-description" );
87  $tag['description'] = $msg->exists() ? $msg->text() : '';
88  }
89 
90  if ( $fld_hitcount ) {
91  $tag['hitcount'] = (int)$tagHitcounts[$tagName];
92  }
93 
94  $isSoftware = isset( $softwareDefinedTags[$tagName] );
95  $isExplicit = isset( $explicitlyDefinedTags[$tagName] );
96 
97  if ( $fld_defined ) {
98  $tag['defined'] = $isSoftware || $isExplicit;
99  }
100 
101  if ( $fld_source ) {
102  $tag['source'] = [];
103  if ( $isSoftware ) {
104  // TODO: Can we change this to 'software'?
105  $tag['source'][] = 'extension';
106  }
107  if ( $isExplicit ) {
108  $tag['source'][] = 'manual';
109  }
110  }
111 
112  if ( $fld_active ) {
113  $tag['active'] = $isExplicit || isset( $softwareActivatedTags[$tagName] );
114  }
115 
116  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $tag );
117  if ( !$fit ) {
118  $this->setContinueEnumParameter( 'continue', $tagName );
119  break;
120  }
121  }
122 
123  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'tag' );
124  }
125 
126  public function getCacheMode( $params ) {
127  return 'public';
128  }
129 
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 
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 
165  public function getHelpUrls() {
166  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Tags';
167  }
168 }
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:196
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:221
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:163
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:223
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:498
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:41
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.
Definition: ChangeTags.php:283
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()
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23