]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryTags.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.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 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' => '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: ApiQueryTags.php 69932 2010-07-26 08:03:21Z tstarling $';
180         }
181 }