12f3eed29742f1d82b3d912d8a8a81cde0df9e9d
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to enumerate change tags.
34 *
35 * @ingroup API
36 */
37 class ApiQueryTags extends ApiQueryBase {
38
39 /**
40 * @var ApiResult
41 */
42 private $result;
43
44 private $limit;
45 private $fld_displayname = false, $fld_description = false,
46 $fld_hitcount = false;
47
48 public function __construct( $query, $moduleName ) {
49 parent::__construct( $query, $moduleName, 'tg' );
50 }
51
52 public function execute() {
53 $params = $this->extractRequestParams();
54
55 $prop = array_flip( $params['prop'] );
56
57 $this->fld_displayname = isset( $prop['displayname'] );
58 $this->fld_description = isset( $prop['description'] );
59 $this->fld_hitcount = isset( $prop['hitcount'] );
60
61 $this->limit = $params['limit'];
62 $this->result = $this->getResult();
63
64 $this->addTables( 'change_tag' );
65 $this->addFields( 'ct_tag' );
66
67 $this->addFieldsIf( 'count(*) AS hitcount', $this->fld_hitcount );
68
69 $this->addOption( 'LIMIT', $this->limit + 1 );
70 $this->addOption( 'GROUP BY', 'ct_tag' );
71 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
72
73 $res = $this->select( __METHOD__ );
74
75 $ok = true;
76
77 foreach ( $res as $row ) {
78 if ( !$ok ) {
79 break;
80 }
81 $ok = $this->doTag( $row->ct_tag, $row->hitcount );
82 }
83
84 // include tags with no hits yet
85 foreach ( ChangeTags::listDefinedTags() as $tag ) {
86 if ( !$ok ) {
87 break;
88 }
89 $ok = $this->doTag( $tag, 0 );
90 }
91
92 $this->result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
93 }
94
95 private function doTag( $tagName, $hitcount ) {
96 static $count = 0;
97 static $doneTags = array();
98
99 if ( in_array( $tagName, $doneTags ) ) {
100 return true;
101 }
102
103 if ( ++$count > $this->limit ) {
104 $this->setContinueEnumParameter( 'continue', $tagName );
105 return false;
106 }
107
108 $tag = array();
109 $tag['name'] = $tagName;
110
111 if ( $this->fld_displayname ) {
112 $tag['displayname'] = ChangeTags::tagDescription( $tagName );
113 }
114
115 if ( $this->fld_description ) {
116 $msg = wfMsg( "tag-$tagName-description" );
117 $msg = wfEmptyMsg( "tag-$tagName-description" ) ? '' : $msg;
118 $tag['description'] = $msg;
119 }
120
121 if ( $this->fld_hitcount ) {
122 $tag['hitcount'] = $hitcount;
123 }
124
125 $doneTags[] = $tagName;
126
127 $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
128 if ( !$fit ) {
129 $this->setContinueEnumParameter( 'continue', $tagName );
130 return false;
131 }
132
133 return true;
134 }
135
136 public function getCacheMode( $params ) {
137 return 'public';
138 }
139
140 public function getAllowedParams() {
141 return array(
142 'continue' => array(
143 ),
144 'limit' => array(
145 ApiBase::PARAM_DFLT => 10,
146 ApiBase::PARAM_TYPE => 'limit',
147 ApiBase::PARAM_MIN => 1,
148 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
149 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
150 ),
151 'prop' => array(
152 ApiBase::PARAM_DFLT => 'name',
153 ApiBase::PARAM_TYPE => array(
154 'name',
155 'displayname',
156 'description',
157 'hitcount'
158 ),
159 ApiBase::PARAM_ISMULTI => true
160 )
161 );
162 }
163
164 public function getParamDescription() {
165 return array(
166 'continue' => 'When more results are available, use this to continue',
167 'limit' => 'The maximum number of tags to list',
168 'prop' => array(
169 'Which properties to get',
170 ' name - Adds name of tag',
171 ' displayname - Adds system messsage for the tag',
172 ' description - Adds description of the tag',
173 ' hitcount - Adds the amount of revisions that have this tag',
174 ),
175 );
176 }
177
178 public function getDescription() {
179 return 'List change tags';
180 }
181
182 protected function getExamples() {
183 return array(
184 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
185 );
186 }
187
188 public function getVersion() {
189 return __CLASS__ . ': $Id$';
190 }
191 }