Merge "Implement static public Parser::getExternalLinkRel"
[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 return array(
152 'namespace' => array(
153 ApiBase::PARAM_ISMULTI => true,
154 ApiBase::PARAM_TYPE => 'namespace',
155 ),
156 'level' => array(
157 ApiBase::PARAM_ISMULTI => true,
158 ApiBase::PARAM_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
159 ),
160 'limit' => array (
161 ApiBase::PARAM_DFLT => 10,
162 ApiBase::PARAM_TYPE => 'limit',
163 ApiBase::PARAM_MIN => 1,
164 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
165 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
166 ),
167 'dir' => array(
168 ApiBase::PARAM_DFLT => 'older',
169 ApiBase::PARAM_TYPE => array(
170 'newer',
171 'older'
172 )
173 ),
174 'start' => array(
175 ApiBase::PARAM_TYPE => 'timestamp'
176 ),
177 'end' => array(
178 ApiBase::PARAM_TYPE => 'timestamp'
179 ),
180 'prop' => array(
181 ApiBase::PARAM_ISMULTI => true,
182 ApiBase::PARAM_DFLT => 'timestamp|level',
183 ApiBase::PARAM_TYPE => array(
184 'timestamp',
185 'user',
186 'userid',
187 'comment',
188 'parsedcomment',
189 'expiry',
190 'level'
191 )
192 ),
193 );
194 }
195
196 public function getParamDescription() {
197 return array(
198 'namespace' => 'Only list titles in these namespaces',
199 'start' => 'Start listing at this protection timestamp',
200 'end' => 'Stop listing at this protection timestamp',
201 'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
202 'limit' => 'How many total pages to return',
203 'prop' => array(
204 'Which properties to get',
205 ' timestamp - Adds the timestamp of when protection was added',
206 ' user - Adds the user that added the protection',
207 ' userid - Adds the user id that added the protection',
208 ' comment - Adds the comment for the protection',
209 ' parsedcomment - Adds the parsed comment for the protection',
210 ' expiry - Adds the timestamp of when the protection will be lifted',
211 ' level - Adds the protection level',
212 ),
213 'level' => 'Only list titles with these protection levels',
214 );
215 }
216
217 public function getResultProperties() {
218 global $wgRestrictionLevels;
219 return array(
220 '' => array(
221 'ns' => 'namespace',
222 'title' => 'string'
223 ),
224 'timestamp' => array(
225 'timestamp' => 'timestamp'
226 ),
227 'user' => array(
228 'user' => array(
229 ApiBase::PROP_TYPE => 'string',
230 ApiBase::PROP_NULLABLE => true
231 ),
232 'userid' => 'integer'
233 ),
234 'userid' => array(
235 'userid' => 'integer'
236 ),
237 'comment' => array(
238 'comment' => 'string'
239 ),
240 'parsedcomment' => array(
241 'parsedcomment' => 'string'
242 ),
243 'expiry' => array(
244 'expiry' => 'timestamp'
245 ),
246 'level' => array(
247 'level' => array(
248 ApiBase::PROP_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
249 )
250 )
251 );
252 }
253
254 public function getDescription() {
255 return 'List all titles protected from creation';
256 }
257
258 public function getExamples() {
259 return array(
260 'api.php?action=query&list=protectedtitles',
261 );
262 }
263
264 public function getHelpUrls() {
265 return 'https://www.mediawiki.org/wiki/API:Protectedtitles';
266 }
267
268 public function getVersion() {
269 return __CLASS__ . ': $Id$';
270 }
271 }