Merge "Make sure images that don't have an explicit alignment get aligned right"
[mediawiki.git] / includes / api / ApiQueryProtectedTitles.php
blob8f120c60fe92158cd9747b35fbae7e3f3c6ebe0a
1 <?php
2 /**
5 * Created on Feb 13, 2009
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * Query module to enumerate all create-protected pages.
30 * @ingroup API
32 class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'pt' );
38 public function execute() {
39 $this->run();
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
46 /**
47 * @param ApiPageSet $resultPageSet
48 * @return void
50 private function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
53 $this->addTables( 'protected_titles' );
54 $this->addFields( array( 'pt_namespace', 'pt_title', 'pt_timestamp' ) );
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'] ) );
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'] );
66 // Include in ORDER BY for uniqueness
67 $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
68 $this->addWhereRange( 'pt_title', $params['dir'], null, null );
70 if ( !is_null( $params['continue'] ) ) {
71 $cont = explode( '|', $params['continue'] );
72 $this->dieContinueUsageIf( count( $cont ) != 3 );
73 $op = ( $params['dir'] === 'newer' ? '>' : '<' );
74 $db = $this->getDB();
75 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
76 $continueNs = (int)$cont[1];
77 $this->dieContinueUsageIf( $continueNs != $cont[1] );
78 $continueTitle = $db->addQuotes( $cont[2] );
79 $this->addWhere( "pt_timestamp $op $continueTimestamp OR " .
80 "(pt_timestamp = $continueTimestamp AND " .
81 "(pt_namespace $op $continueNs OR " .
82 "(pt_namespace = $continueNs AND " .
83 "pt_title $op= $continueTitle)))"
87 if ( isset( $prop['user'] ) ) {
88 $this->addTables( 'user' );
89 $this->addFields( 'user_name' );
90 $this->addJoinConds( array( 'user' => array( 'LEFT JOIN',
91 'user_id=pt_user'
92 ) ) );
95 $this->addOption( 'LIMIT', $params['limit'] + 1 );
96 $res = $this->select( __METHOD__ );
98 $count = 0;
99 $result = $this->getResult();
101 $titles = array();
103 foreach ( $res as $row ) {
104 if ( ++$count > $params['limit'] ) {
105 // We've reached the one extra which shows that there are
106 // additional pages to be had. Stop here...
107 $this->setContinueEnumParameter( 'continue',
108 "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
110 break;
113 $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
114 if ( is_null( $resultPageSet ) ) {
115 $vals = array();
116 ApiQueryBase::addTitleInfo( $vals, $title );
117 if ( isset( $prop['timestamp'] ) ) {
118 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
121 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
122 $vals['user'] = $row->user_name;
125 if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
126 $vals['userid'] = $row->pt_user;
129 if ( isset( $prop['comment'] ) ) {
130 $vals['comment'] = $row->pt_reason;
133 if ( isset( $prop['parsedcomment'] ) ) {
134 $vals['parsedcomment'] = Linker::formatComment( $row->pt_reason, $title );
137 if ( isset( $prop['expiry'] ) ) {
138 global $wgContLang;
139 $vals['expiry'] = $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 );
142 if ( isset( $prop['level'] ) ) {
143 $vals['level'] = $row->pt_create_perm;
146 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
147 if ( !$fit ) {
148 $this->setContinueEnumParameter( 'continue',
149 "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
151 break;
153 } else {
154 $titles[] = $title;
158 if ( is_null( $resultPageSet ) ) {
159 $result->setIndexedTagName_internal(
160 array( 'query', $this->getModuleName() ),
161 $this->getModulePrefix()
163 } else {
164 $resultPageSet->populateFromTitles( $titles );
168 public function getCacheMode( $params ) {
169 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
170 // formatComment() calls wfMessage() among other things
171 return 'anon-public-user-private';
172 } else {
173 return 'public';
177 public function getAllowedParams() {
178 global $wgRestrictionLevels;
180 return array(
181 'namespace' => array(
182 ApiBase::PARAM_ISMULTI => true,
183 ApiBase::PARAM_TYPE => 'namespace',
185 'level' => array(
186 ApiBase::PARAM_ISMULTI => true,
187 ApiBase::PARAM_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
189 'limit' => array(
190 ApiBase::PARAM_DFLT => 10,
191 ApiBase::PARAM_TYPE => 'limit',
192 ApiBase::PARAM_MIN => 1,
193 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
194 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
196 'dir' => array(
197 ApiBase::PARAM_DFLT => 'older',
198 ApiBase::PARAM_TYPE => array(
199 'newer',
200 'older'
203 'start' => array(
204 ApiBase::PARAM_TYPE => 'timestamp'
206 'end' => array(
207 ApiBase::PARAM_TYPE => 'timestamp'
209 'prop' => array(
210 ApiBase::PARAM_ISMULTI => true,
211 ApiBase::PARAM_DFLT => 'timestamp|level',
212 ApiBase::PARAM_TYPE => array(
213 'timestamp',
214 'user',
215 'userid',
216 'comment',
217 'parsedcomment',
218 'expiry',
219 'level'
222 'continue' => null,
226 public function getParamDescription() {
227 return array(
228 'namespace' => 'Only list titles in these namespaces',
229 'start' => 'Start listing at this protection timestamp',
230 'end' => 'Stop listing at this protection timestamp',
231 'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
232 'limit' => 'How many total pages to return',
233 'prop' => array(
234 'Which properties to get',
235 ' timestamp - Adds the timestamp of when protection was added',
236 ' user - Adds the user that added the protection',
237 ' userid - Adds the user id that added the protection',
238 ' comment - Adds the comment for the protection',
239 ' parsedcomment - Adds the parsed comment for the protection',
240 ' expiry - Adds the timestamp of when the protection will be lifted',
241 ' level - Adds the protection level',
243 'level' => 'Only list titles with these protection levels',
244 'continue' => 'When more results are available, use this to continue',
248 public function getResultProperties() {
249 global $wgRestrictionLevels;
251 return array(
252 '' => array(
253 'ns' => 'namespace',
254 'title' => 'string'
256 'timestamp' => array(
257 'timestamp' => 'timestamp'
259 'user' => array(
260 'user' => array(
261 ApiBase::PROP_TYPE => 'string',
262 ApiBase::PROP_NULLABLE => true
264 'userid' => 'integer'
266 'userid' => array(
267 'userid' => 'integer'
269 'comment' => array(
270 'comment' => 'string'
272 'parsedcomment' => array(
273 'parsedcomment' => 'string'
275 'expiry' => array(
276 'expiry' => 'timestamp'
278 'level' => array(
279 'level' => array(
280 ApiBase::PROP_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
286 public function getDescription() {
287 return 'List all titles protected from creation.';
290 public function getExamples() {
291 return array(
292 'api.php?action=query&list=protectedtitles',
296 public function getHelpUrls() {
297 return 'https://www.mediawiki.org/wiki/API:Protectedtitles';