5 * Created on Jul 9, 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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * Query module to enumerate change tags.
37 class ApiQueryTags
extends ApiQueryBase
{
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__
);
79 foreach ( $res as $row ) {
83 $ok = $this->doTag( $row->ct_tag
, $row->hitcount
);
86 // include tags with no hits yet
87 foreach ( ChangeTags
::listDefinedTags() as $tag ) {
91 $ok = $this->doTag( $tag, 0 );
94 $this->result
->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
97 private function doTag( $tagName, $hitcount ) {
99 static $doneTags = array();
101 if ( in_array( $tagName, $doneTags ) ) {
105 if ( ++
$count > $this->limit
) {
106 $this->setContinueEnumParameter( 'continue', $tagName );
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 );
131 $this->setContinueEnumParameter( 'continue', $tagName );
138 public function getCacheMode( $params ) {
142 public function getAllowedParams() {
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
154 ApiBase
::PARAM_DFLT
=> 'name',
155 ApiBase
::PARAM_TYPE
=> array(
161 ApiBase
::PARAM_ISMULTI
=> true
166 public function getParamDescription() {
168 'continue' => 'When more results are available, use this to continue',
169 'limit' => 'The maximum number of tags to list',
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() {
186 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
190 public function getVersion() {
191 return __CLASS__
. ': $Id$';