More of r61437 (adding/removing whitespace)
[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 (C) 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 $this->addOption( 'LIMIT', $this->limit + 1 );
69 $this->addOption( 'GROUP BY', 'ct_tag' );
70 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
71
72 $res = $this->select( __METHOD__ );
73
74 $ok = true;
75
76 while ( $row = $res->fetchObject() ) {
77 if ( !$ok ) break;
78 $ok = $this->doTag( $row->ct_tag, $row->hitcount );
79 }
80
81 // include tags with no hits yet
82 foreach ( ChangeTags::listDefinedTags() as $tag ) {
83 if ( !$ok ) break;
84 $ok = $this->doTag( $tag, 0 );
85 }
86
87 $this->result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
88 }
89
90 private function doTag( $tagName, $hitcount ) {
91 static $count = 0;
92 static $doneTags = array();
93
94 if ( in_array( $tagName, $doneTags ) ) {
95 return true;
96 }
97
98 if ( ++$count > $this->limit )
99 {
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 if ( $this->fld_description )
111 {
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 $doneTags[] = $tagName;
121
122 $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
123 if ( !$fit )
124 {
125 $this->setContinueEnumParameter( 'continue', $tagName );
126 return false;
127 }
128
129 return true;
130 }
131
132 public function getAllowedParams() {
133 return array (
134 'continue' => array(
135 ),
136 'limit' => array(
137 ApiBase :: PARAM_DFLT => 10,
138 ApiBase :: PARAM_TYPE => 'limit',
139 ApiBase :: PARAM_MIN => 1,
140 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
141 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
142 ),
143 'prop' => array(
144 ApiBase :: PARAM_DFLT => 'name',
145 ApiBase :: PARAM_TYPE => array(
146 'name',
147 'displayname',
148 'description',
149 'hitcount'
150 ),
151 ApiBase :: PARAM_ISMULTI => true
152 )
153 );
154 }
155
156 public function getParamDescription() {
157 return array (
158 'continue' => 'When more results are available, use this to continue',
159 'limit' => 'The maximum number of tags to list',
160 'prop' => 'Which properties to get',
161 );
162 }
163
164 public function getDescription() {
165 return 'List change tags.';
166 }
167
168 protected function getExamples() {
169 return array (
170 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
171 );
172 }
173
174 public function getVersion() {
175 return __CLASS__ . ': $Id$';
176 }
177 }