Merge "Fix and make PHPDoc tags in FileBackend more specific"
[lhc/web/wiklou.git] / includes / api / ApiQueryTags.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jul 9, 2009
6 *
7 * Copyright © 2009
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * Query module to enumerate change tags.
29 *
30 * @ingroup API
31 */
32 class ApiQueryTags extends ApiQueryBase {
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'tg' );
36 }
37
38 public function execute() {
39 $params = $this->extractRequestParams();
40
41 $prop = array_flip( $params['prop'] );
42
43 $fld_displayname = isset( $prop['displayname'] );
44 $fld_description = isset( $prop['description'] );
45 $fld_hitcount = isset( $prop['hitcount'] );
46 $fld_defined = isset( $prop['defined'] );
47 $fld_source = isset( $prop['source'] );
48 $fld_active = isset( $prop['active'] );
49
50 $limit = $params['limit'];
51 $result = $this->getResult();
52
53 $extensionDefinedTags = array_fill_keys( ChangeTags::listExtensionDefinedTags(), 0 );
54 $explicitlyDefinedTags = array_fill_keys( ChangeTags::listExplicitlyDefinedTags(), 0 );
55 $extensionActivatedTags = array_fill_keys( ChangeTags::listExtensionActivatedTags(), 0 );
56
57 $definedTags = array_merge( $extensionDefinedTags, $explicitlyDefinedTags );
58
59 # Fetch defined tags that aren't past the continuation
60 if ( $params['continue'] !== null ) {
61 $cont = $params['continue'];
62 $tags = array_filter( array_keys( $definedTags ), function ( $v ) use ( $cont ) {
63 return $v >= $cont;
64 } );
65 $tags = array_fill_keys( $tags, 0 );
66 } else {
67 $tags = $definedTags;
68 }
69
70 # Merge in all used tags
71 $this->addTables( 'change_tag' );
72 $this->addFields( 'ct_tag' );
73 $this->addFields( array( 'hitcount' => $fld_hitcount ? 'COUNT(*)' : '0' ) );
74 $this->addOption( 'LIMIT', $limit + 1 );
75 $this->addOption( 'GROUP BY', 'ct_tag' );
76 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
77 $res = $this->select( __METHOD__ );
78 foreach ( $res as $row ) {
79 $tags[$row->ct_tag] = (int)$row->hitcount;
80 }
81
82 # Now make sure the array is sorted for proper continuation
83 ksort( $tags );
84
85 $count = 0;
86 foreach ( $tags as $tagName => $hitcount ) {
87 if ( ++$count > $limit ) {
88 $this->setContinueEnumParameter( 'continue', $tagName );
89 break;
90 }
91
92 $tag = array();
93 $tag['name'] = $tagName;
94
95 if ( $fld_displayname ) {
96 $tag['displayname'] = ChangeTags::tagDescription( $tagName );
97 }
98
99 if ( $fld_description ) {
100 $msg = $this->msg( "tag-$tagName-description" );
101 $tag['description'] = $msg->exists() ? $msg->text() : '';
102 }
103
104 if ( $fld_hitcount ) {
105 $tag['hitcount'] = $hitcount;
106 }
107
108 $isExtension = isset( $extensionDefinedTags[$tagName] );
109 $isExplicit = isset( $explicitlyDefinedTags[$tagName] );
110
111 if ( $fld_defined && ( $isExtension || $isExplicit ) ) {
112 $tag['defined'] = '';
113 }
114
115 if ( $fld_source ) {
116 $tag['source'] = array();
117 if ( $isExtension ) {
118 $tag['source'][] = 'extension';
119 }
120 if ( $isExplicit ) {
121 $tag['source'][] = 'manual';
122 }
123 }
124
125 if ( $fld_active &&
126 ( $isExplicit || isset( $extensionActivatedTags[$tagName] ) )
127 ) {
128 $tag['active'] = '';
129 }
130
131 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
132 if ( !$fit ) {
133 $this->setContinueEnumParameter( 'continue', $tagName );
134 break;
135 }
136 }
137
138 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
139 }
140
141 public function getCacheMode( $params ) {
142 return 'public';
143 }
144
145 public function getAllowedParams() {
146 return array(
147 'continue' => array(
148 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
149 ),
150 'limit' => array(
151 ApiBase::PARAM_DFLT => 10,
152 ApiBase::PARAM_TYPE => 'limit',
153 ApiBase::PARAM_MIN => 1,
154 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
155 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
156 ),
157 'prop' => array(
158 ApiBase::PARAM_DFLT => 'name',
159 ApiBase::PARAM_TYPE => array(
160 'name',
161 'displayname',
162 'description',
163 'hitcount',
164 'defined',
165 'source',
166 'active',
167 ),
168 ApiBase::PARAM_ISMULTI => true
169 )
170 );
171 }
172
173 protected function getExamplesMessages() {
174 return array(
175 'action=query&list=tags&tgprop=displayname|description|hitcount|defined'
176 => 'apihelp-query+tags-example-simple',
177 );
178 }
179
180 public function getHelpUrls() {
181 return 'https://www.mediawiki.org/wiki/API:Tags';
182 }
183 }