Merge "Revert "SECURITY: Do not show log action if revdeleted" and fix UI message"
[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
48 $limit = $params['limit'];
49 $result = $this->getResult();
50
51 $definedTags = array_fill_keys( ChangeTags::listDefinedTags(), 0 );
52
53 # Fetch defined tags that aren't past the continuation
54 if ( $params['continue'] !== null ) {
55 $cont = $params['continue'];
56 $tags = array_filter( array_keys( $definedTags ), function ( $v ) use ( $cont ) {
57 return $v >= $cont;
58 } );
59 $tags = array_fill_keys( $tags, 0 );
60 } else {
61 $tags = $definedTags;
62 }
63
64 # Merge in all used tags
65 $this->addTables( 'change_tag' );
66 $this->addFields( 'ct_tag' );
67 $this->addFields( array( 'hitcount' => $fld_hitcount ? 'COUNT(*)' : '0' ) );
68 $this->addOption( 'LIMIT', $limit + 1 );
69 $this->addOption( 'GROUP BY', 'ct_tag' );
70 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
71 $res = $this->select( __METHOD__ );
72 foreach ( $res as $row ) {
73 $tags[$row->ct_tag] = (int)$row->hitcount;
74 }
75
76 # Now make sure the array is sorted for proper continuation
77 ksort( $tags );
78
79 $count = 0;
80 foreach ( $tags as $tagName => $hitcount ) {
81 if ( ++$count > $limit ) {
82 $this->setContinueEnumParameter( 'continue', $tagName );
83 break;
84 }
85
86 $tag = array();
87 $tag['name'] = $tagName;
88
89 if ( $fld_displayname ) {
90 $tag['displayname'] = ChangeTags::tagDescription( $tagName );
91 }
92
93 if ( $fld_description ) {
94 $msg = $this->msg( "tag-$tagName-description" );
95 $tag['description'] = $msg->exists() ? $msg->text() : '';
96 }
97
98 if ( $fld_hitcount ) {
99 $tag['hitcount'] = $hitcount;
100 }
101
102 if ( $fld_defined && isset( $definedTags[$tagName] ) ) {
103 $tag['defined'] = '';
104 }
105
106 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
107 if ( !$fit ) {
108 $this->setContinueEnumParameter( 'continue', $tagName );
109 break;
110 }
111 }
112
113 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
114 }
115
116 public function getCacheMode( $params ) {
117 return 'public';
118 }
119
120 public function getAllowedParams() {
121 return array(
122 'continue' => array(
123 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
124 ),
125 'limit' => array(
126 ApiBase::PARAM_DFLT => 10,
127 ApiBase::PARAM_TYPE => 'limit',
128 ApiBase::PARAM_MIN => 1,
129 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
130 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
131 ),
132 'prop' => array(
133 ApiBase::PARAM_DFLT => 'name',
134 ApiBase::PARAM_TYPE => array(
135 'name',
136 'displayname',
137 'description',
138 'hitcount',
139 'defined',
140 ),
141 ApiBase::PARAM_ISMULTI => true
142 )
143 );
144 }
145
146 protected function getExamplesMessages() {
147 return array(
148 'action=query&list=tags&tgprop=displayname|description|hitcount|defined'
149 => 'apihelp-query+tags-example-simple',
150 );
151 }
152
153 public function getHelpUrls() {
154 return 'https://www.mediawiki.org/wiki/API:Tags';
155 }
156 }