4273c0dea851d1534ac5a2ed0d9a4ebfc8be4791
[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 additional pages to be had. Stop here...
85 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
86 break;
87 }
88
89 $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
90 if ( is_null( $resultPageSet ) ) {
91 $vals = array();
92 ApiQueryBase::addTitleInfo( $vals, $title );
93 if ( isset( $prop['timestamp'] ) ) {
94 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
95 }
96
97 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
98 $vals['user'] = $row->user_name;
99 }
100
101 if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
102 $vals['userid'] = $row->pt_user;
103 }
104
105 if ( isset( $prop['comment'] ) ) {
106 $vals['comment'] = $row->pt_reason;
107 }
108
109 if ( isset( $prop['parsedcomment'] ) ) {
110 $vals['parsedcomment'] = Linker::formatComment( $row->pt_reason, $title );
111 }
112
113 if ( isset( $prop['expiry'] ) ) {
114 global $wgContLang;
115 $vals['expiry'] = $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 );
116 }
117
118 if ( isset( $prop['level'] ) ) {
119 $vals['level'] = $row->pt_create_perm;
120 }
121
122 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
123 if ( !$fit ) {
124 $this->setContinueEnumParameter( 'start',
125 wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
126 break;
127 }
128 } else {
129 $titles[] = $title;
130 }
131 }
132
133 if ( is_null( $resultPageSet ) ) {
134 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->getModulePrefix() );
135 } else {
136 $resultPageSet->populateFromTitles( $titles );
137 }
138 }
139
140 public function getCacheMode( $params ) {
141 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
142 // formatComment() calls wfMessage() among other things
143 return 'anon-public-user-private';
144 } else {
145 return 'public';
146 }
147 }
148
149 public function getAllowedParams() {
150 global $wgRestrictionLevels;
151
152 return array(
153 'namespace' => array(
154 ApiBase::PARAM_ISMULTI => true,
155 ApiBase::PARAM_TYPE => 'namespace',
156 ),
157 'level' => array(
158 ApiBase::PARAM_ISMULTI => true,
159 ApiBase::PARAM_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
160 ),
161 'limit' => array(
162 ApiBase::PARAM_DFLT => 10,
163 ApiBase::PARAM_TYPE => 'limit',
164 ApiBase::PARAM_MIN => 1,
165 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
166 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
167 ),
168 'dir' => array(
169 ApiBase::PARAM_DFLT => 'older',
170 ApiBase::PARAM_TYPE => array(
171 'newer',
172 'older'
173 )
174 ),
175 'start' => array(
176 ApiBase::PARAM_TYPE => 'timestamp'
177 ),
178 'end' => array(
179 ApiBase::PARAM_TYPE => 'timestamp'
180 ),
181 'prop' => array(
182 ApiBase::PARAM_ISMULTI => true,
183 ApiBase::PARAM_DFLT => 'timestamp|level',
184 ApiBase::PARAM_TYPE => array(
185 'timestamp',
186 'user',
187 'userid',
188 'comment',
189 'parsedcomment',
190 'expiry',
191 'level'
192 )
193 ),
194 );
195 }
196
197 public function getParamDescription() {
198 return array(
199 'namespace' => 'Only list titles in these namespaces',
200 'start' => 'Start listing at this protection timestamp',
201 'end' => 'Stop listing at this protection timestamp',
202 'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
203 'limit' => 'How many total pages to return',
204 'prop' => array(
205 'Which properties to get',
206 ' timestamp - Adds the timestamp of when protection was added',
207 ' user - Adds the user that added the protection',
208 ' userid - Adds the user id that added the protection',
209 ' comment - Adds the comment for the protection',
210 ' parsedcomment - Adds the parsed comment for the protection',
211 ' expiry - Adds the timestamp of when the protection will be lifted',
212 ' level - Adds the protection level',
213 ),
214 'level' => 'Only list titles with these protection levels',
215 );
216 }
217
218 public function getResultProperties() {
219 global $wgRestrictionLevels;
220
221 return array(
222 '' => array(
223 'ns' => 'namespace',
224 'title' => 'string'
225 ),
226 'timestamp' => array(
227 'timestamp' => 'timestamp'
228 ),
229 'user' => array(
230 'user' => array(
231 ApiBase::PROP_TYPE => 'string',
232 ApiBase::PROP_NULLABLE => true
233 ),
234 'userid' => 'integer'
235 ),
236 'userid' => array(
237 'userid' => 'integer'
238 ),
239 'comment' => array(
240 'comment' => 'string'
241 ),
242 'parsedcomment' => array(
243 'parsedcomment' => 'string'
244 ),
245 'expiry' => array(
246 'expiry' => 'timestamp'
247 ),
248 'level' => array(
249 'level' => array(
250 ApiBase::PROP_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
251 )
252 )
253 );
254 }
255
256 public function getDescription() {
257 return 'List all titles protected from creation';
258 }
259
260 public function getExamples() {
261 return array(
262 'api.php?action=query&list=protectedtitles',
263 );
264 }
265
266 public function getHelpUrls() {
267 return 'https://www.mediawiki.org/wiki/API:Protectedtitles';
268 }
269 }