4 * Created on Jul 9, 2009
6 * API for MediaWiki 1.8+
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.
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.
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
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
32 * Query module to enumerate change tags.
36 class ApiQueryTags
extends ApiQueryBase
{
38 private $limit, $result;
39 private $fld_displayname = false, $fld_description = false,
40 $fld_hitcount = false;
42 public function __construct( $query, $moduleName ) {
43 parent
:: __construct( $query, $moduleName, 'tg' );
46 public function execute() {
47 $params = $this->extractRequestParams();
49 $prop = array_flip( $params['prop'] );
51 $this->fld_displayname
= isset( $prop['displayname'] );
52 $this->fld_description
= isset( $prop['description'] );
53 $this->fld_hitcount
= isset( $prop['hitcount'] );
55 $this->limit
= $params['limit'];
56 $this->result
= $this->getResult();
58 $pageSet = $this->getPageSet();
59 $titles = $pageSet->getTitles();
62 $this->addTables( 'change_tag' );
63 $this->addFields( 'ct_tag' );
65 if ( $this->fld_hitcount
)
66 $this->addFields( 'count(*) AS hitcount' );
68 $this->addOption( 'LIMIT', $this->limit +
1 );
69 $this->addOption( 'GROUP BY', 'ct_tag' );
70 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
72 $res = $this->select( __METHOD__
);
76 while ( $row = $res->fetchObject() ) {
78 $ok = $this->doTag( $row->ct_tag
, $row->hitcount
);
81 // include tags with no hits yet
82 foreach ( ChangeTags
::listDefinedTags() as $tag ) {
84 $ok = $this->doTag( $tag, 0 );
87 $this->result
->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
90 private function doTag( $tagName, $hitcount ) {
92 static $doneTags = array();
94 if ( in_array( $tagName, $doneTags ) ) {
98 if ( ++
$count > $this->limit
)
100 $this->setContinueEnumParameter( 'continue', $tagName );
105 $tag['name'] = $tagName;
107 if ( $this->fld_displayname
)
108 $tag['displayname'] = ChangeTags
::tagDescription( $tagName );
110 if ( $this->fld_description
)
112 $msg = wfMsg( "tag-$tagName-description" );
113 $msg = wfEmptyMsg( "tag-$tagName-description", $msg ) ?
'' : $msg;
114 $tag['description'] = $msg;
117 if ( $this->fld_hitcount
)
118 $tag['hitcount'] = $hitcount;
120 $doneTags[] = $tagName;
122 $fit = $this->result
->addValue( array( 'query', $this->getModuleName() ), null, $tag );
125 $this->setContinueEnumParameter( 'continue', $tagName );
132 public function getAllowedParams() {
137 ApiBase
:: PARAM_DFLT
=> 10,
138 ApiBase
:: PARAM_TYPE
=> 'limit',
139 ApiBase
:: PARAM_MIN
=> 1,
140 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
141 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
144 ApiBase
:: PARAM_DFLT
=> 'name',
145 ApiBase
:: PARAM_TYPE
=> array(
151 ApiBase
:: PARAM_ISMULTI
=> true
156 public function getParamDescription() {
158 'continue' => 'When more results are available, use this to continue',
159 'limit' => 'The maximum number of tags to list',
160 'prop' => 'Which properties to get',
164 public function getDescription() {
165 return 'List change tags.';
168 protected function getExamples() {
170 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
174 public function getVersion() {
175 return __CLASS__
. ': $Id: ApiQueryTags.php';