(bug 19004) Added support for tags to the API. Patch by Matthew Britton.
[lhc/web/wiklou.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 Matthew Britton <firstname>.<lastname>@btinternet.com
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 $ok = true;
62
63 if($this->fld_hitcount) {
64 foreach( ChangeTags::getHitCounts() as $tag => $count ) {
65 if(!$ok) break;
66 $ok = $this->doTag( $tag, $count );
67 }
68 } else {
69 foreach( ChangeTags::listDefinedTags() as $tag ) {
70 if(!$ok) break;
71 $ok = $this->doTag( $tag, 0 );
72 }
73 }
74
75 $this->result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'tag');
76 }
77
78 private function doTag( $tagName, $hitcount ) {
79 static $count = 0;
80 static $doneTags = array();
81
82 if ( in_array( $tagName, $doneTags ) ) {
83 return true;
84 }
85
86 if(++$count > $this->limit)
87 {
88 $this->setContinueEnumParameter('continue', $tagName);
89 return false;
90 }
91
92 $tag = array();
93 $tag['name'] = $tagName;
94
95 if($this->fld_displayname)
96 $tag['displayname'] = ChangeTags::tagDescription( $tagName );
97
98 if($this->fld_description)
99 {
100 $msg = wfMsg( "tag-$tagName-description" );
101 $msg = wfEmptyMsg( "tag-$tagName-description", $msg ) ? '' : $msg;
102 $tag['description'] = $msg;
103 }
104
105 if($this->fld_hitcount)
106 $tag['hitcount'] = $hitcount;
107
108 $doneTags[] = $tagName;
109
110 $fit = $this->result->addValue(array('query', $this->getModuleName()), null, $tag);
111 if(!$fit)
112 {
113 $this->setContinueEnumParameter('continue', $tagName);
114 return false;
115 }
116
117 return true;
118 }
119
120 public function getAllowedParams() {
121 return array (
122 'continue' => array(
123 ),
124 'end' => array(
125 ),
126 'limit' => array(
127 ApiBase :: PARAM_DFLT => 10,
128 ApiBase :: PARAM_TYPE => 'limit',
129 ApiBase :: PARAM_MIN => 1,
130 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
131 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
132 ),
133 'prop' => array(
134 ApiBase :: PARAM_DFLT => 'name',
135 ApiBase :: PARAM_TYPE => array(
136 'name',
137 'displayname',
138 'description',
139 'hitcount'
140 ),
141 ApiBase :: PARAM_ISMULTI => true
142 )
143 );
144 }
145
146 public function getParamDescription() {
147 return array (
148 'continue' => 'When more results are available, use this to continue',
149 'limit' => 'The maximum number of tags to list',
150 'prop' => 'Which properties to get',
151 );
152 }
153
154 public function getDescription() {
155 return 'List change tags.';
156 }
157
158 protected function getExamples() {
159 return array (
160 'api.php?action=query&list=tags&tgprop=displayname|description|hitcount'
161 );
162 }
163
164 public function getVersion() {
165 return __CLASS__ . ': $Id: ApiQueryTags.php';
166 }
167 }