Introduced Maintenance::getDB() and corresponding setDB() to control externally what...
[mediawiki.git] / includes / api / ApiQueryTags.php
blob61b34669db724083288cce7c78d1099a0fccfeb6
1 <?php
2 /**
5 * Created on Jul 9, 2009
7 * Copyright © 2009
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.
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.
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
24 * @file
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
32 /**
33 * Query module to enumerate change tags.
35 * @ingroup API
37 class ApiQueryTags extends ApiQueryBase {
39 /**
40 * @var ApiResult
42 private $result;
44 private $limit;
45 private $fld_displayname = false, $fld_description = false,
46 $fld_hitcount = false;
48 public function __construct( $query, $moduleName ) {
49 parent::__construct( $query, $moduleName, 'tg' );
52 public function execute() {
53 $params = $this->extractRequestParams();
55 $prop = array_flip( $params['prop'] );
57 $this->fld_displayname = isset( $prop['displayname'] );
58 $this->fld_description = isset( $prop['description'] );
59 $this->fld_hitcount = isset( $prop['hitcount'] );
61 $this->limit = $params['limit'];
62 $this->result = $this->getResult();
64 $this->addTables( 'change_tag' );
65 $this->addFields( 'ct_tag' );
67 if ( $this->fld_hitcount ) {
68 $this->addFields( 'count(*) AS hitcount' );
71 $this->addOption( 'LIMIT', $this->limit + 1 );
72 $this->addOption( 'GROUP BY', 'ct_tag' );
73 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
75 $res = $this->select( __METHOD__ );
77 $ok = true;
79 foreach ( $res as $row ) {
80 if ( !$ok ) {
81 break;
83 $ok = $this->doTag( $row->ct_tag, $row->hitcount );
86 // include tags with no hits yet
87 foreach ( ChangeTags::listDefinedTags() as $tag ) {
88 if ( !$ok ) {
89 break;
91 $ok = $this->doTag( $tag, 0 );
94 $this->result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
97 private function doTag( $tagName, $hitcount ) {
98 static $count = 0;
99 static $doneTags = array();
101 if ( in_array( $tagName, $doneTags ) ) {
102 return true;
105 if ( ++$count > $this->limit ) {
106 $this->setContinueEnumParameter( 'continue', $tagName );
107 return false;
110 $tag = array();
111 $tag['name'] = $tagName;
113 if ( $this->fld_displayname ) {
114 $tag['displayname'] = ChangeTags::tagDescription( $tagName );
117 if ( $this->fld_description ) {
118 $msg = wfMsg( "tag-$tagName-description" );
119 $msg = wfEmptyMsg( "tag-$tagName-description" ) ? '' : $msg;
120 $tag['description'] = $msg;
123 if ( $this->fld_hitcount ) {
124 $tag['hitcount'] = $hitcount;
127 $doneTags[] = $tagName;
129 $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
130 if ( !$fit ) {
131 $this->setContinueEnumParameter( 'continue', $tagName );
132 return false;
135 return true;
138 public function getCacheMode( $params ) {
139 return 'public';
142 public function getAllowedParams() {
143 return array(
144 'continue' => array(
146 'limit' => array(
147 ApiBase::PARAM_DFLT => 10,
148 ApiBase::PARAM_TYPE => 'limit',
149 ApiBase::PARAM_MIN => 1,
150 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
151 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
153 'prop' => array(
154 ApiBase::PARAM_DFLT => 'name',
155 ApiBase::PARAM_TYPE => array(
156 'name',
157 'displayname',
158 'description',
159 'hitcount'
161 ApiBase::PARAM_ISMULTI => true
166 public function getParamDescription() {
167 return array(
168 'continue' => 'When more results are available, use this to continue',
169 'limit' => 'The maximum number of tags to list',
170 'prop' => array(
171 'Which properties to get',
172 ' name - Adds name of tag',
173 ' displayname - Adds system messsage for the tag',
174 ' description - Adds description of the tag',
175 ' hitcount - Adds the amount of revisions that have this tag',
180 public function getDescription() {
181 return 'List change tags';
184 protected function getExamples() {
185 return array(
186 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
190 public function getVersion() {
191 return __CLASS__ . ': $Id$';