Followup r92396 rename method, fix boolean comparison that was supposed to be inverte...
[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 = wfMessage( "tag-$tagName-description" );
117 $tag['description'] = $msg->exists() ? $msg->text() : '';
118 }
119
120 if ( $this->fld_hitcount ) {
121 $tag['hitcount'] = $hitcount;
122 }
123
124 $doneTags[] = $tagName;
125
126 $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
127 if ( !$fit ) {
128 $this->setContinueEnumParameter( 'continue', $tagName );
129 return false;
130 }
131
132 return true;
133 }
134
135 public function getCacheMode( $params ) {
136 return 'public';
137 }
138
139 public function getAllowedParams() {
140 return array(
141 'continue' => array(
142 ),
143 'limit' => array(
144 ApiBase::PARAM_DFLT => 10,
145 ApiBase::PARAM_TYPE => 'limit',
146 ApiBase::PARAM_MIN => 1,
147 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
148 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
149 ),
150 'prop' => array(
151 ApiBase::PARAM_DFLT => 'name',
152 ApiBase::PARAM_TYPE => array(
153 'name',
154 'displayname',
155 'description',
156 'hitcount'
157 ),
158 ApiBase::PARAM_ISMULTI => true
159 )
160 );
161 }
162
163 public function getParamDescription() {
164 return array(
165 'continue' => 'When more results are available, use this to continue',
166 'limit' => 'The maximum number of tags to list',
167 'prop' => array(
168 'Which properties to get',
169 ' name - Adds name of tag',
170 ' displayname - Adds system messsage for the tag',
171 ' description - Adds description of the tag',
172 ' hitcount - Adds the amount of revisions that have this tag',
173 ),
174 );
175 }
176
177 public function getDescription() {
178 return 'List change tags';
179 }
180
181 protected function getExamples() {
182 return array(
183 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
184 );
185 }
186
187 public function getVersion() {
188 return __CLASS__ . ': $Id$';
189 }
190 }