API: fix copyright symbol, coding style cleanup, more braces
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 $pageSet = $this->getPageSet();
59 $titles = $pageSet->getTitles();
60 $data = array();
61
62 $this->addTables( 'change_tag' );
63 $this->addFields( 'ct_tag' );
64
65 if ( $this->fld_hitcount ) {
66 $this->addFields( 'count(*) AS hitcount' );
67 }
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 while ( $row = $res->fetchObject() ) {
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 ) ? '' : $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 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' => 'Which properties to get',
165 );
166 }
167
168 public function getDescription() {
169 return 'List change tags.';
170 }
171
172 protected function getExamples() {
173 return array(
174 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
175 );
176 }
177
178 public function getVersion() {
179 return __CLASS__ . ': $Id$';
180 }
181 }