MediaWiki  1.34.0
ApiQueryTags.php
Go to the documentation of this file.
1 <?php
28 class ApiQueryTags extends ApiQueryBase {
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 );
52  $tagStats = ChangeTags::tagUsageStatistics();
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' => [
133  ApiBase::PARAM_DFLT => 10,
134  ApiBase::PARAM_TYPE => 'limit',
135  ApiBase::PARAM_MIN => 1,
138  ],
139  'prop' => [
140  ApiBase::PARAM_DFLT => '',
142  'displayname',
143  'description',
144  'hitcount',
145  'defined',
146  'source',
147  'active',
148  ],
149  ApiBase::PARAM_ISMULTI => true,
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 }
ApiQueryTags\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryTags.php:30
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:131
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiQueryTags
Query module to enumerate change tags.
Definition: ApiQueryTags.php:28
ApiQueryTags\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryTags.php:123
ChangeTags\listSoftwareDefinedTags
static listSoftwareDefinedTags()
Lists tags defined by core or extensions using the ListDefinedTags hook.
Definition: ChangeTags.php:1476
ApiBase\PARAM_MIN
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:106
ApiQueryBase
This is a base class for all Query modules.
Definition: ApiQueryBase.php:34
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:259
ApiBase\PARAM_MAX
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:97
ApiQueryTags\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryTags.php:155
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
ChangeTags\listExplicitlyDefinedTags
static listExplicitlyDefinedTags()
Lists tags explicitly defined in the change_tag_def table of the database.
Definition: ChangeTags.php:1438
ChangeTags\listSoftwareActivatedTags
static listSoftwareActivatedTags()
Lists those tags which core or extensions report as being "active".
Definition: ChangeTags.php:1393
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:261
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiQueryTags\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryTags.php:127
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
ApiBase\PARAM_MAX2
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition: ApiBase.php:103
ChangeTags\tagUsageStatistics
static tagUsageStatistics()
Returns a map of any tags used on the wiki to number of edits tagged with them, ordered descending by...
Definition: ChangeTags.php:1522
ApiQueryTags\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryTags.php:162
ApiQueryTags\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryTags.php:34
ApiQueryBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Set a query-continue value.
Definition: ApiQueryBase.php:492
ApiBase\PARAM_HELP_MSG_PER_VALUE
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
ChangeTags\tagDescription
static tagDescription( $tag, MessageLocalizer $context)
Get a short description for a tag.
Definition: ChangeTags.php:183