]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryTags.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiQueryTags.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
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         private $limit, $result;
40         private $fld_displayname = false, $fld_description = false,
41                         $fld_hitcount = false;
42
43         public function __construct( $query, $moduleName ) {
44                 parent::__construct( $query, $moduleName, 'tg' );
45         }
46
47         public function execute() {
48                 $params = $this->extractRequestParams();
49
50                 $prop = array_flip( $params['prop'] );
51
52                 $this->fld_displayname = isset( $prop['displayname'] );
53                 $this->fld_description = isset( $prop['description'] );
54                 $this->fld_hitcount = isset( $prop['hitcount'] );
55
56                 $this->limit = $params['limit'];
57                 $this->result = $this->getResult();
58
59                 $this->addTables( 'change_tag' );
60                 $this->addFields( 'ct_tag' );
61
62                 if ( $this->fld_hitcount ) {
63                         $this->addFields( 'count(*) AS hitcount' );
64                 }
65
66                 $this->addOption( 'LIMIT', $this->limit + 1 );
67                 $this->addOption( 'GROUP BY', 'ct_tag' );
68                 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
69
70                 $res = $this->select( __METHOD__ );
71
72                 $ok = true;
73
74                 foreach ( $res as $row ) {
75                         if ( !$ok ) {
76                                 break;
77                         }
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 ) {
84                                 break;
85                         }
86                         $ok = $this->doTag( $tag, 0 );
87                 }
88
89                 $this->result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
90         }
91
92         private function doTag( $tagName, $hitcount ) {
93                 static $count = 0;
94                 static $doneTags = array();
95
96                 if ( in_array( $tagName, $doneTags ) ) {
97                         return true;
98                 }
99
100                 if ( ++$count > $this->limit ) {
101                         $this->setContinueEnumParameter( 'continue', $tagName );
102                         return false;
103                 }
104
105                 $tag = array();
106                 $tag['name'] = $tagName;
107
108                 if ( $this->fld_displayname ) {
109                         $tag['displayname'] = ChangeTags::tagDescription( $tagName );
110                 }
111
112                 if ( $this->fld_description ) {
113                         $msg = wfMsg( "tag-$tagName-description" );
114                         $msg = wfEmptyMsg( "tag-$tagName-description", $msg ) ? '' : $msg;
115                         $tag['description'] = $msg;
116                 }
117
118                 if ( $this->fld_hitcount ) {
119                         $tag['hitcount'] = $hitcount;
120                 }
121
122                 $doneTags[] = $tagName;
123
124                 $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
125                 if ( !$fit ) {
126                         $this->setContinueEnumParameter( 'continue', $tagName );
127                         return false;
128                 }
129
130                 return true;
131         }
132
133         public function getCacheMode( $params ) {
134                 return 'public';
135         }
136
137         public function getAllowedParams() {
138                 return array(
139                         'continue' => array(
140                         ),
141                         'limit' => array(
142                                 ApiBase::PARAM_DFLT => 10,
143                                 ApiBase::PARAM_TYPE => 'limit',
144                                 ApiBase::PARAM_MIN => 1,
145                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
146                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
147                         ),
148                         'prop' => array(
149                                 ApiBase::PARAM_DFLT => 'name',
150                                 ApiBase::PARAM_TYPE => array(
151                                         'name',
152                                         'displayname',
153                                         'description',
154                                         'hitcount'
155                                 ),
156                                 ApiBase::PARAM_ISMULTI => true
157                         )
158                 );
159         }
160
161         public function getParamDescription() {
162                 return array(
163                         'continue' => 'When more results are available, use this to continue',
164                         'limit' => 'The maximum number of tags to list',
165                         'prop' => array(
166                                 'Which properties to get',
167                                 ' name         - Adds name of tag',
168                                 ' displayname  - Adds system messsage for the tag',
169                                 ' description  - Adds description of the tag',
170                                 ' hitcount     - Adds the amount of revisions that have this tag',
171                         ),
172                 );
173         }
174
175         public function getDescription() {
176                 return 'List change tags';
177         }
178
179         protected function getExamples() {
180                 return array(
181                         'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
182                 );
183         }
184
185         public function getVersion() {
186                 return __CLASS__ . ': $Id$';
187         }
188 }