9b0c3d6d0d143cdb3d424168b0fc08c9f38ba94a
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to enumerate all create-protected pages.
34 *
35 * @ingroup API
36 */
37 class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'pt' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 private function run( $resultPageSet = null ) {
52 $params = $this->extractRequestParams();
53
54 $this->addTables( 'protected_titles' );
55 $this->addFields( array( 'pt_namespace', 'pt_title', 'pt_timestamp' ) );
56
57 $prop = array_flip( $params['prop'] );
58 $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
59 $this->addFieldsIf( 'pt_reason', isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) );
60 $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
61 $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
62
63 $this->addWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
64 $this->addWhereFld( 'pt_namespace', $params['namespace'] );
65 $this->addWhereFld( 'pt_create_perm', $params['level'] );
66
67 if ( isset( $prop['user'] ) ) {
68 $this->addTables( 'user' );
69 $this->addFields( 'user_name' );
70 $this->addJoinConds( array( 'user' => array( 'LEFT JOIN',
71 'user_id=pt_user'
72 ) ) );
73 }
74
75 $this->addOption( 'LIMIT', $params['limit'] + 1 );
76 $res = $this->select( __METHOD__ );
77
78 $count = 0;
79 $result = $this->getResult();
80 foreach ( $res as $row ) {
81 if ( ++ $count > $params['limit'] ) {
82 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
83 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
84 break;
85 }
86
87 $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
88 if ( is_null( $resultPageSet ) ) {
89 $vals = array();
90 ApiQueryBase::addTitleInfo( $vals, $title );
91 if ( isset( $prop['timestamp'] ) ) {
92 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
93 }
94
95 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
96 $vals['user'] = $row->user_name;
97 }
98
99 if ( isset( $prop['user'] ) ) {
100 $vals['userid'] = $row->pt_user;
101 }
102
103 if ( isset( $prop['comment'] ) ) {
104 $vals['comment'] = $row->pt_reason;
105 }
106
107 if ( isset( $prop['parsedcomment'] ) ) {
108 global $wgUser;
109 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->pt_reason, $title );
110 }
111
112 if ( isset( $prop['expiry'] ) ) {
113 $vals['expiry'] = Block::decodeExpiry( $row->pt_expiry, TS_ISO_8601 );
114 }
115
116 if ( isset( $prop['level'] ) ) {
117 $vals['level'] = $row->pt_create_perm;
118 }
119
120 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
121 if ( !$fit ) {
122 $this->setContinueEnumParameter( 'start',
123 wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
124 break;
125 }
126 } else {
127 $titles[] = $title;
128 }
129 }
130
131 if ( is_null( $resultPageSet ) ) {
132 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->getModulePrefix() );
133 } else {
134 $resultPageSet->populateFromTitles( $titles );
135 }
136 }
137
138 public function getCacheMode( $params ) {
139 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
140 // formatComment() calls wfMsg() among other things
141 return 'anon-public-user-private';
142 } else {
143 return 'public';
144 }
145 }
146
147 public function getAllowedParams() {
148 global $wgRestrictionLevels;
149 return array(
150 'namespace' => array(
151 ApiBase::PARAM_ISMULTI => true,
152 ApiBase::PARAM_TYPE => 'namespace',
153 ),
154 'level' => array(
155 ApiBase::PARAM_ISMULTI => true,
156 ApiBase::PARAM_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
157 ),
158 'limit' => array (
159 ApiBase::PARAM_DFLT => 10,
160 ApiBase::PARAM_TYPE => 'limit',
161 ApiBase::PARAM_MIN => 1,
162 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
163 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
164 ),
165 'dir' => array(
166 ApiBase::PARAM_DFLT => 'older',
167 ApiBase::PARAM_TYPE => array(
168 'older',
169 'newer'
170 )
171 ),
172 'start' => array(
173 ApiBase::PARAM_TYPE => 'timestamp'
174 ),
175 'end' => array(
176 ApiBase::PARAM_TYPE => 'timestamp'
177 ),
178 'prop' => array(
179 ApiBase::PARAM_ISMULTI => true,
180 ApiBase::PARAM_DFLT => 'timestamp|level',
181 ApiBase::PARAM_TYPE => array(
182 'timestamp',
183 'user',
184 'userid',
185 'comment',
186 'parsedcomment',
187 'expiry',
188 'level'
189 )
190 ),
191 );
192 }
193
194 public function getParamDescription() {
195 return array(
196 'namespace' => 'Only list titles in these namespaces',
197 'start' => 'Start listing at this protection timestamp',
198 'end' => 'Stop listing at this protection timestamp',
199 'dir' => 'The direction in which to list',
200 'limit' => 'How many total pages to return',
201 'prop' => array(
202 'Which properties to get',
203 ' timestamp - Adds the timestamp of when protection was added',
204 ' user - Adds the user to add the protection',
205 ' userid - Adds the user id to add the protection',
206 ' comment - Adds the comment for the protection',
207 ' parsedcomment - Adds the parsed comment for the protection',
208 ' expiry - Adds the timestamp of when the protection will be lifted',
209 ' level - Adds the protection level',
210 ),
211 'level' => 'Only list titles with these protection levels',
212 );
213 }
214
215 public function getDescription() {
216 return 'List all titles protected from creation';
217 }
218
219 protected function getExamples() {
220 return array(
221 'api.php?action=query&list=protectedtitles',
222 );
223 }
224
225 public function getVersion() {
226 return __CLASS__ . ': $Id$';
227 }
228 }