Rewrote r69339 etc. to clean up API cache header handling.
[lhc/web/wiklou.git] / includes / api / ApiQueryTags.php
1 <?php
2
3 /**
4 * Created on Jul 9, 2009
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2009
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to enumerate change tags.
33 *
34 * @ingroup API
35 */
36 class ApiQueryTags extends ApiQueryBase {
37
38 private $limit, $result;
39 private $fld_displayname = false, $fld_description = false,
40 $fld_hitcount = false;
41
42 public function __construct( $query, $moduleName ) {
43 parent::__construct( $query, $moduleName, 'tg' );
44 }
45
46 public function execute() {
47 $params = $this->extractRequestParams();
48
49 $prop = array_flip( $params['prop'] );
50
51 $this->fld_displayname = isset( $prop['displayname'] );
52 $this->fld_description = isset( $prop['description'] );
53 $this->fld_hitcount = isset( $prop['hitcount'] );
54
55 $this->limit = $params['limit'];
56 $this->result = $this->getResult();
57
58 $this->addTables( 'change_tag' );
59 $this->addFields( 'ct_tag' );
60
61 if ( $this->fld_hitcount ) {
62 $this->addFields( 'count(*) AS hitcount' );
63 }
64
65 $this->addOption( 'LIMIT', $this->limit + 1 );
66 $this->addOption( 'GROUP BY', 'ct_tag' );
67 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
68
69 $res = $this->select( __METHOD__ );
70
71 $ok = true;
72
73 while ( $row = $res->fetchObject() ) {
74 if ( !$ok ) {
75 break;
76 }
77 $ok = $this->doTag( $row->ct_tag, $row->hitcount );
78 }
79
80 // include tags with no hits yet
81 foreach ( ChangeTags::listDefinedTags() as $tag ) {
82 if ( !$ok ) {
83 break;
84 }
85 $ok = $this->doTag( $tag, 0 );
86 }
87
88 $this->result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
89 }
90
91 private function doTag( $tagName, $hitcount ) {
92 static $count = 0;
93 static $doneTags = array();
94
95 if ( in_array( $tagName, $doneTags ) ) {
96 return true;
97 }
98
99 if ( ++$count > $this->limit ) {
100 $this->setContinueEnumParameter( 'continue', $tagName );
101 return false;
102 }
103
104 $tag = array();
105 $tag['name'] = $tagName;
106
107 if ( $this->fld_displayname ) {
108 $tag['displayname'] = ChangeTags::tagDescription( $tagName );
109 }
110
111 if ( $this->fld_description ) {
112 $msg = wfMsg( "tag-$tagName-description" );
113 $msg = wfEmptyMsg( "tag-$tagName-description", $msg ) ? '' : $msg;
114 $tag['description'] = $msg;
115 }
116
117 if ( $this->fld_hitcount ) {
118 $tag['hitcount'] = $hitcount;
119 }
120
121 $doneTags[] = $tagName;
122
123 $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
124 if ( !$fit ) {
125 $this->setContinueEnumParameter( 'continue', $tagName );
126 return false;
127 }
128
129 return true;
130 }
131
132 public function getCacheMode( $params ) {
133 return 'public';
134 }
135
136 public function getAllowedParams() {
137 return array(
138 'continue' => array(
139 ),
140 'limit' => array(
141 ApiBase::PARAM_DFLT => 10,
142 ApiBase::PARAM_TYPE => 'limit',
143 ApiBase::PARAM_MIN => 1,
144 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
145 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
146 ),
147 'prop' => array(
148 ApiBase::PARAM_DFLT => 'name',
149 ApiBase::PARAM_TYPE => array(
150 'name',
151 'displayname',
152 'description',
153 'hitcount'
154 ),
155 ApiBase::PARAM_ISMULTI => true
156 )
157 );
158 }
159
160 public function getParamDescription() {
161 return array(
162 'continue' => 'When more results are available, use this to continue',
163 'limit' => 'The maximum number of tags to list',
164 'prop' => array(
165 'Which properties to get',
166 ' name - Adds name of tag',
167 ' displayname - Adds system messsage for the tag',
168 ' description - Adds description of the tag',
169 ' hitcount - Adds the amount of revisions that have this tag',
170 ),
171 );
172 }
173
174 public function getDescription() {
175 return 'List change tags';
176 }
177
178 protected function getExamples() {
179 return array(
180 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
181 );
182 }
183
184 public function getVersion() {
185 return __CLASS__ . ': $Id$';
186 }
187 }