More parameter documentation
[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 if ( $this->fld_hitcount ) {
68 $this->addFields( 'count(*) AS hitcount' );
69 }
70
71 $this->addOption( 'LIMIT', $this->limit + 1 );
72 $this->addOption( 'GROUP BY', 'ct_tag' );
73 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
74
75 $res = $this->select( __METHOD__ );
76
77 $ok = true;
78
79 foreach ( $res as $row ) {
80 if ( !$ok ) {
81 break;
82 }
83 $ok = $this->doTag( $row->ct_tag, $row->hitcount );
84 }
85
86 // include tags with no hits yet
87 foreach ( ChangeTags::listDefinedTags() as $tag ) {
88 if ( !$ok ) {
89 break;
90 }
91 $ok = $this->doTag( $tag, 0 );
92 }
93
94 $this->result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
95 }
96
97 private function doTag( $tagName, $hitcount ) {
98 static $count = 0;
99 static $doneTags = array();
100
101 if ( in_array( $tagName, $doneTags ) ) {
102 return true;
103 }
104
105 if ( ++$count > $this->limit ) {
106 $this->setContinueEnumParameter( 'continue', $tagName );
107 return false;
108 }
109
110 $tag = array();
111 $tag['name'] = $tagName;
112
113 if ( $this->fld_displayname ) {
114 $tag['displayname'] = ChangeTags::tagDescription( $tagName );
115 }
116
117 if ( $this->fld_description ) {
118 $msg = wfMsg( "tag-$tagName-description" );
119 $msg = wfEmptyMsg( "tag-$tagName-description", $msg ) ? '' : $msg;
120 $tag['description'] = $msg;
121 }
122
123 if ( $this->fld_hitcount ) {
124 $tag['hitcount'] = $hitcount;
125 }
126
127 $doneTags[] = $tagName;
128
129 $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
130 if ( !$fit ) {
131 $this->setContinueEnumParameter( 'continue', $tagName );
132 return false;
133 }
134
135 return true;
136 }
137
138 public function getCacheMode( $params ) {
139 return 'public';
140 }
141
142 public function getAllowedParams() {
143 return array(
144 'continue' => array(
145 ),
146 'limit' => array(
147 ApiBase::PARAM_DFLT => 10,
148 ApiBase::PARAM_TYPE => 'limit',
149 ApiBase::PARAM_MIN => 1,
150 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
151 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
152 ),
153 'prop' => array(
154 ApiBase::PARAM_DFLT => 'name',
155 ApiBase::PARAM_TYPE => array(
156 'name',
157 'displayname',
158 'description',
159 'hitcount'
160 ),
161 ApiBase::PARAM_ISMULTI => true
162 )
163 );
164 }
165
166 public function getParamDescription() {
167 return array(
168 'continue' => 'When more results are available, use this to continue',
169 'limit' => 'The maximum number of tags to list',
170 'prop' => array(
171 'Which properties to get',
172 ' name - Adds name of tag',
173 ' displayname - Adds system messsage for the tag',
174 ' description - Adds description of the tag',
175 ' hitcount - Adds the amount of revisions that have this tag',
176 ),
177 );
178 }
179
180 public function getDescription() {
181 return 'List change tags';
182 }
183
184 protected function getExamples() {
185 return array(
186 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
187 );
188 }
189
190 public function getVersion() {
191 return __CLASS__ . ': $Id$';
192 }
193 }