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