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