Rewrote r69339 etc. to clean up API cache header handling.
[lhc/web/wiklou.git] / includes / api / ApiQueryProtectedTitles.php
1 <?php
2
3 /**
4 * Created on Feb 13, 2009
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to enumerate all create-protected pages.
33 *
34 * @ingroup API
35 */
36 class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'pt' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
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'] ) );
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->addWhereRange( '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 foreach ( $res as $row ) {
80 if ( ++ $count > $params['limit'] ) {
81 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
82 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
83 break;
84 }
85
86 $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
87 if ( is_null( $resultPageSet ) ) {
88 $vals = array();
89 ApiQueryBase::addTitleInfo( $vals, $title );
90 if ( isset( $prop['timestamp'] ) ) {
91 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
92 }
93
94 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
95 $vals['user'] = $row->user_name;
96 }
97
98 if ( isset( $prop['comment'] ) ) {
99 $vals['comment'] = $row->pt_reason;
100 }
101
102 if ( isset( $prop['parsedcomment'] ) ) {
103 global $wgUser;
104 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->pt_reason, $title );
105 }
106
107 if ( isset( $prop['expiry'] ) ) {
108 $vals['expiry'] = Block::decodeExpiry( $row->pt_expiry, TS_ISO_8601 );
109 }
110
111 if ( isset( $prop['level'] ) ) {
112 $vals['level'] = $row->pt_create_perm;
113 }
114
115 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
116 if ( !$fit ) {
117 $this->setContinueEnumParameter( 'start',
118 wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
119 break;
120 }
121 } else {
122 $titles[] = $title;
123 }
124 }
125
126 if ( is_null( $resultPageSet ) ) {
127 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->getModulePrefix() );
128 } else {
129 $resultPageSet->populateFromTitles( $titles );
130 }
131 }
132
133 public function getCacheMode( $params ) {
134 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
135 // formatComment() calls wfMsg() among other things
136 return 'anon-public-user-private';
137 } else {
138 return 'public';
139 }
140 }
141
142 public function getAllowedParams() {
143 global $wgRestrictionLevels;
144 return array(
145 'namespace' => array(
146 ApiBase::PARAM_ISMULTI => true,
147 ApiBase::PARAM_TYPE => 'namespace',
148 ),
149 'level' => array(
150 ApiBase::PARAM_ISMULTI => true,
151 ApiBase::PARAM_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
152 ),
153 'limit' => array (
154 ApiBase::PARAM_DFLT => 10,
155 ApiBase::PARAM_TYPE => 'limit',
156 ApiBase::PARAM_MIN => 1,
157 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
158 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
159 ),
160 'dir' => array(
161 ApiBase::PARAM_DFLT => 'older',
162 ApiBase::PARAM_TYPE => array(
163 'older',
164 'newer'
165 )
166 ),
167 'start' => array(
168 ApiBase::PARAM_TYPE => 'timestamp'
169 ),
170 'end' => array(
171 ApiBase::PARAM_TYPE => 'timestamp'
172 ),
173 'prop' => array(
174 ApiBase::PARAM_ISMULTI => true,
175 ApiBase::PARAM_DFLT => 'timestamp|level',
176 ApiBase::PARAM_TYPE => array(
177 'timestamp',
178 'user',
179 'comment',
180 'parsedcomment',
181 'expiry',
182 'level'
183 )
184 ),
185 );
186 }
187
188 public function getParamDescription() {
189 return array(
190 'namespace' => 'Only list titles in these namespaces',
191 'start' => 'Start listing at this protection timestamp',
192 'end' => 'Stop listing at this protection timestamp',
193 'dir' => 'The direction in which to list',
194 'limit' => 'How many total pages to return',
195 'prop' => array(
196 'Which properties to get',
197 ' timestamp - Adds the timestamp of when protection was added',
198 ' user - Adds the user to add the protection',
199 ' comment - Adds the comment for the protection',
200 ' parsedcomment - Adds the parsed comment for the protection',
201 ' expiry - Adds the timestamp of when the protection will be lifted',
202 ' level - Adds the protection level',
203 ),
204 'level' => 'Only list titles with these protection levels',
205 );
206 }
207
208 public function getDescription() {
209 return 'List all titles protected from creation';
210 }
211
212 protected function getExamples() {
213 return array(
214 'api.php?action=query&list=protectedtitles',
215 );
216 }
217
218 public function getVersion() {
219 return __CLASS__ . ': $Id$';
220 }
221 }