Merge "Fix usage of $wgDebugDumpSql"
[lhc/web/wiklou.git] / includes / api / ApiQueryProtectedTitles.php
1 <?php
2 /**
3 *
4 *
5 * Created on Feb 13, 2009
6 *
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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 all create-protected pages.
29 *
30 * @ingroup API
31 */
32 class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'pt' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
44 }
45
46 /**
47 * @param $resultPageSet ApiPageSet
48 * @return void
49 */
50 private function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
52
53 $this->addTables( 'protected_titles' );
54 $this->addFields( array( 'pt_namespace', 'pt_title', 'pt_timestamp' ) );
55
56 $prop = array_flip( $params['prop'] );
57 $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
58 $this->addFieldsIf( 'pt_reason', isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) );
59 $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
60 $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
61
62 $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
63 $this->addWhereFld( 'pt_namespace', $params['namespace'] );
64 $this->addWhereFld( 'pt_create_perm', $params['level'] );
65
66 if ( isset( $prop['user'] ) ) {
67 $this->addTables( 'user' );
68 $this->addFields( 'user_name' );
69 $this->addJoinConds( array( 'user' => array( 'LEFT JOIN',
70 'user_id=pt_user'
71 ) ) );
72 }
73
74 $this->addOption( 'LIMIT', $params['limit'] + 1 );
75 $res = $this->select( __METHOD__ );
76
77 $count = 0;
78 $result = $this->getResult();
79
80 $titles = array();
81
82 foreach ( $res as $row ) {
83 if ( ++$count > $params['limit'] ) {
84 // We've reached the one extra which shows that there are
85 // additional pages to be had. Stop here...
86 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
87 break;
88 }
89
90 $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
91 if ( is_null( $resultPageSet ) ) {
92 $vals = array();
93 ApiQueryBase::addTitleInfo( $vals, $title );
94 if ( isset( $prop['timestamp'] ) ) {
95 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
96 }
97
98 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
99 $vals['user'] = $row->user_name;
100 }
101
102 if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
103 $vals['userid'] = $row->pt_user;
104 }
105
106 if ( isset( $prop['comment'] ) ) {
107 $vals['comment'] = $row->pt_reason;
108 }
109
110 if ( isset( $prop['parsedcomment'] ) ) {
111 $vals['parsedcomment'] = Linker::formatComment( $row->pt_reason, $title );
112 }
113
114 if ( isset( $prop['expiry'] ) ) {
115 global $wgContLang;
116 $vals['expiry'] = $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 );
117 }
118
119 if ( isset( $prop['level'] ) ) {
120 $vals['level'] = $row->pt_create_perm;
121 }
122
123 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
124 if ( !$fit ) {
125 $this->setContinueEnumParameter( 'start',
126 wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
127 break;
128 }
129 } else {
130 $titles[] = $title;
131 }
132 }
133
134 if ( is_null( $resultPageSet ) ) {
135 $result->setIndexedTagName_internal(
136 array( 'query', $this->getModuleName() ),
137 $this->getModulePrefix()
138 );
139 } else {
140 $resultPageSet->populateFromTitles( $titles );
141 }
142 }
143
144 public function getCacheMode( $params ) {
145 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
146 // formatComment() calls wfMessage() among other things
147 return 'anon-public-user-private';
148 } else {
149 return 'public';
150 }
151 }
152
153 public function getAllowedParams() {
154 global $wgRestrictionLevels;
155
156 return array(
157 'namespace' => array(
158 ApiBase::PARAM_ISMULTI => true,
159 ApiBase::PARAM_TYPE => 'namespace',
160 ),
161 'level' => array(
162 ApiBase::PARAM_ISMULTI => true,
163 ApiBase::PARAM_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
164 ),
165 'limit' => array(
166 ApiBase::PARAM_DFLT => 10,
167 ApiBase::PARAM_TYPE => 'limit',
168 ApiBase::PARAM_MIN => 1,
169 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
170 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
171 ),
172 'dir' => array(
173 ApiBase::PARAM_DFLT => 'older',
174 ApiBase::PARAM_TYPE => array(
175 'newer',
176 'older'
177 )
178 ),
179 'start' => array(
180 ApiBase::PARAM_TYPE => 'timestamp'
181 ),
182 'end' => array(
183 ApiBase::PARAM_TYPE => 'timestamp'
184 ),
185 'prop' => array(
186 ApiBase::PARAM_ISMULTI => true,
187 ApiBase::PARAM_DFLT => 'timestamp|level',
188 ApiBase::PARAM_TYPE => array(
189 'timestamp',
190 'user',
191 'userid',
192 'comment',
193 'parsedcomment',
194 'expiry',
195 'level'
196 )
197 ),
198 );
199 }
200
201 public function getParamDescription() {
202 return array(
203 'namespace' => 'Only list titles in these namespaces',
204 'start' => 'Start listing at this protection timestamp',
205 'end' => 'Stop listing at this protection timestamp',
206 'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
207 'limit' => 'How many total pages to return',
208 'prop' => array(
209 'Which properties to get',
210 ' timestamp - Adds the timestamp of when protection was added',
211 ' user - Adds the user that added the protection',
212 ' userid - Adds the user id that added the protection',
213 ' comment - Adds the comment for the protection',
214 ' parsedcomment - Adds the parsed comment for the protection',
215 ' expiry - Adds the timestamp of when the protection will be lifted',
216 ' level - Adds the protection level',
217 ),
218 'level' => 'Only list titles with these protection levels',
219 );
220 }
221
222 public function getResultProperties() {
223 global $wgRestrictionLevels;
224
225 return array(
226 '' => array(
227 'ns' => 'namespace',
228 'title' => 'string'
229 ),
230 'timestamp' => array(
231 'timestamp' => 'timestamp'
232 ),
233 'user' => array(
234 'user' => array(
235 ApiBase::PROP_TYPE => 'string',
236 ApiBase::PROP_NULLABLE => true
237 ),
238 'userid' => 'integer'
239 ),
240 'userid' => array(
241 'userid' => 'integer'
242 ),
243 'comment' => array(
244 'comment' => 'string'
245 ),
246 'parsedcomment' => array(
247 'parsedcomment' => 'string'
248 ),
249 'expiry' => array(
250 'expiry' => 'timestamp'
251 ),
252 'level' => array(
253 'level' => array(
254 ApiBase::PROP_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
255 )
256 )
257 );
258 }
259
260 public function getDescription() {
261 return 'List all titles protected from creation.';
262 }
263
264 public function getExamples() {
265 return array(
266 'api.php?action=query&list=protectedtitles',
267 );
268 }
269
270 public function getHelpUrls() {
271 return 'https://www.mediawiki.org/wiki/API:Protectedtitles';
272 }
273 }