Merge "Don't try to verify XML well-formedness for partial SVG uploads"
[lhc/web/wiklou.git] / includes / api / ApiQueryTags.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jul 9, 2009
6 *
7 * Copyright © 2009
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * Query module to enumerate change tags.
29 *
30 * @ingroup API
31 */
32 class ApiQueryTags extends ApiQueryBase {
33
34 /**
35 * @var ApiResult
36 */
37 private $result;
38
39 private $limit;
40 private $fld_displayname = false, $fld_description = false,
41 $fld_hitcount = false;
42
43 public function __construct( ApiQuery $query, $moduleName ) {
44 parent::__construct( $query, $moduleName, 'tg' );
45 }
46
47 public function execute() {
48 $params = $this->extractRequestParams();
49
50 $prop = array_flip( $params['prop'] );
51
52 $this->fld_displayname = isset( $prop['displayname'] );
53 $this->fld_description = isset( $prop['description'] );
54 $this->fld_hitcount = isset( $prop['hitcount'] );
55
56 $this->limit = $params['limit'];
57 $this->result = $this->getResult();
58
59 $this->addTables( 'change_tag' );
60 $this->addFields( 'ct_tag' );
61
62 $this->addFieldsIf( array( 'hitcount' => 'COUNT(*)' ), $this->fld_hitcount );
63
64 $this->addOption( 'LIMIT', $this->limit + 1 );
65 $this->addOption( 'GROUP BY', 'ct_tag' );
66 $this->addWhereRange( 'ct_tag', 'newer', $params['continue'], null );
67
68 $res = $this->select( __METHOD__ );
69
70 $ok = true;
71
72 foreach ( $res as $row ) {
73 if ( !$ok ) {
74 break;
75 }
76 $ok = $this->doTag( $row->ct_tag, $this->fld_hitcount ? $row->hitcount : 0 );
77 }
78
79 // include tags with no hits yet
80 foreach ( ChangeTags::listDefinedTags() as $tag ) {
81 if ( !$ok ) {
82 break;
83 }
84 $ok = $this->doTag( $tag, 0 );
85 }
86
87 $this->result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'tag' );
88 }
89
90 private function doTag( $tagName, $hitcount ) {
91 static $count = 0;
92 static $doneTags = array();
93
94 if ( in_array( $tagName, $doneTags ) ) {
95 return true;
96 }
97
98 if ( ++$count > $this->limit ) {
99 $this->setContinueEnumParameter( 'continue', $tagName );
100
101 return false;
102 }
103
104 $tag = array();
105 $tag['name'] = $tagName;
106
107 if ( $this->fld_displayname ) {
108 $tag['displayname'] = ChangeTags::tagDescription( $tagName );
109 }
110
111 if ( $this->fld_description ) {
112 $msg = wfMessage( "tag-$tagName-description" );
113 $tag['description'] = $msg->exists() ? $msg->text() : '';
114 }
115
116 if ( $this->fld_hitcount ) {
117 $tag['hitcount'] = $hitcount;
118 }
119
120 $doneTags[] = $tagName;
121
122 $fit = $this->result->addValue( array( 'query', $this->getModuleName() ), null, $tag );
123 if ( !$fit ) {
124 $this->setContinueEnumParameter( 'continue', $tagName );
125
126 return false;
127 }
128
129 return true;
130 }
131
132 public function getCacheMode( $params ) {
133 return 'public';
134 }
135
136 public function getAllowedParams() {
137 return array(
138 'continue' => array(
139 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
140 ),
141 'limit' => array(
142 ApiBase::PARAM_DFLT => 10,
143 ApiBase::PARAM_TYPE => 'limit',
144 ApiBase::PARAM_MIN => 1,
145 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
146 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
147 ),
148 'prop' => array(
149 ApiBase::PARAM_DFLT => 'name',
150 ApiBase::PARAM_TYPE => array(
151 'name',
152 'displayname',
153 'description',
154 'hitcount'
155 ),
156 ApiBase::PARAM_ISMULTI => true
157 )
158 );
159 }
160
161 protected function getExamplesMessages() {
162 return array(
163 'action=query&list=tags&tgprop=displayname|description|hitcount'
164 => 'apihelp-query+tags-example-simple',
165 );
166 }
167
168 public function getHelpUrls() {
169 return 'https://www.mediawiki.org/wiki/API:Tags';
170 }
171 }