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