Merge "Typo fix"
[mediawiki.git] / includes / api / ApiQueryTags.php
blob732df9a4edb1c3547c3fc7337acba974da8f8c60
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 /**
28 * Query module to enumerate change tags.
30 * @ingroup API
32 class ApiQueryTags extends ApiQueryBase {
34 /**
35 * @var ApiResult
37 private $result;
39 private $limit;
40 private $fld_displayname = false, $fld_description = false,
41 $fld_hitcount = false;
43 public function __construct( $query, $moduleName ) {
44 parent::__construct( $query, $moduleName, 'tg' );
47 public function execute() {
48 $params = $this->extractRequestParams();
50 $prop = array_flip( $params['prop'] );
52 $this->fld_displayname = isset( $prop['displayname'] );
53 $this->fld_description = isset( $prop['description'] );
54 $this->fld_hitcount = isset( $prop['hitcount'] );
56 $this->limit = $params['limit'];
57 $this->result = $this->getResult();
59 $this->addTables( 'change_tag' );
60 $this->addFields( 'ct_tag' );
62 $this->addFieldsIf( array( 'hitcount' => 'COUNT(*)' ), $this->fld_hitcount );
64 $this->addOption( 'LIMIT', $this->limit + 1 );
65 $this->addOption( 'GROUP BY', 'ct_tag' );
66 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
68 $res = $this->select( __METHOD__ );
70 $ok = true;
72 foreach ( $res as $row ) {
73 if ( !$ok ) {
74 break;
76 $ok = $this->doTag( $row->ct_tag, $this->fld_hitcount ? $row->hitcount : 0 );
79 // include tags with no hits yet
80 foreach ( ChangeTags::listDefinedTags() as $tag ) {
81 if ( !$ok ) {
82 break;
84 $ok = $this->doTag( $tag, 0 );
87 $this->result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
90 private function doTag( $tagName, $hitcount ) {
91 static $count = 0;
92 static $doneTags = array();
94 if ( in_array( $tagName, $doneTags ) ) {
95 return true;
98 if ( ++$count > $this->limit ) {
99 $this->setContinueEnumParameter( 'continue', $tagName );
100 return false;
103 $tag = array();
104 $tag['name'] = $tagName;
106 if ( $this->fld_displayname ) {
107 $tag['displayname'] = ChangeTags::tagDescription( $tagName );
110 if ( $this->fld_description ) {
111 $msg = wfMessage( "tag-$tagName-description" );
112 $tag['description'] = $msg->exists() ? $msg->text() : '';
115 if ( $this->fld_hitcount ) {
116 $tag['hitcount'] = $hitcount;
119 $doneTags[] = $tagName;
121 $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
122 if ( !$fit ) {
123 $this->setContinueEnumParameter( 'continue', $tagName );
124 return false;
127 return true;
130 public function getCacheMode( $params ) {
131 return 'public';
134 public function getAllowedParams() {
135 return array(
136 'continue' => null,
137 'limit' => array(
138 ApiBase::PARAM_DFLT => 10,
139 ApiBase::PARAM_TYPE => 'limit',
140 ApiBase::PARAM_MIN => 1,
141 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
142 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
144 'prop' => array(
145 ApiBase::PARAM_DFLT => 'name',
146 ApiBase::PARAM_TYPE => array(
147 'name',
148 'displayname',
149 'description',
150 'hitcount'
152 ApiBase::PARAM_ISMULTI => true
157 public function getParamDescription() {
158 return array(
159 'continue' => 'When more results are available, use this to continue',
160 'limit' => 'The maximum number of tags to list',
161 'prop' => array(
162 'Which properties to get',
163 ' name - Adds name of tag',
164 ' displayname - Adds system message for the tag',
165 ' description - Adds description of the tag',
166 ' hitcount - Adds the amount of revisions that have this tag',
171 public function getResultProperties() {
172 return array(
173 '' => array(
174 'name' => 'string'
176 'displayname' => array(
177 'displayname' => 'string'
179 'description' => array(
180 'description' => 'string'
182 'hitcount' => array(
183 'hitcount' => 'integer'
188 public function getDescription() {
189 return 'List change tags';
192 public function getExamples() {
193 return array(
194 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
198 public function getHelpUrls() {
199 return 'https://www.mediawiki.org/wiki/API:Tags';