Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / api / ApiQueryTags.php
1 <?php
2 /**
3 * Copyright © 2009
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Query module to enumerate change tags.
25 *
26 * @ingroup API
27 */
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,
136 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
137 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
138 ],
139 'prop' => [
140 ApiBase::PARAM_DFLT => '',
141 ApiBase::PARAM_TYPE => [
142 'displayname',
143 'description',
144 'hitcount',
145 'defined',
146 'source',
147 'active',
148 ],
149 ApiBase::PARAM_ISMULTI => true,
150 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
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 }