Merge "Add pp_propname_page index to page_props"
[lhc/web/wiklou.git] / includes / api / ApiProtect.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 1, 2007
6 *
7 * Copyright © 2007 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 * @ingroup API
29 */
30 class ApiProtect extends ApiBase {
31
32 public function execute() {
33 global $wgRestrictionLevels;
34 $params = $this->extractRequestParams();
35
36 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
37 $titleObj = $pageObj->getTitle();
38
39 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
40 if ( $errors ) {
41 // We don't care about multiple errors, just report one of them
42 $this->dieUsageMsg( reset( $errors ) );
43 }
44
45 $expiry = (array)$params['expiry'];
46 if ( count( $expiry ) != count( $params['protections'] ) ) {
47 if ( count( $expiry ) == 1 ) {
48 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
49 } else {
50 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
51 }
52 }
53
54 $restrictionTypes = $titleObj->getRestrictionTypes();
55 $db = $this->getDB();
56
57 $protections = array();
58 $expiryarray = array();
59 $resultProtections = array();
60 foreach ( $params['protections'] as $i => $prot ) {
61 $p = explode( '=', $prot );
62 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
63
64 if ( $titleObj->exists() && $p[0] == 'create' ) {
65 $this->dieUsageMsg( 'create-titleexists' );
66 }
67 if ( !$titleObj->exists() && $p[0] != 'create' ) {
68 $this->dieUsageMsg( 'missingtitle-createonly' );
69 }
70
71 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
72 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
73 }
74 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
75 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
76 }
77
78 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
79 $expiryarray[$p[0]] = $db->getInfinity();
80 } else {
81 $exp = strtotime( $expiry[$i] );
82 if ( $exp < 0 || !$exp ) {
83 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
84 }
85
86 $exp = wfTimestamp( TS_MW, $exp );
87 if ( $exp < wfTimestampNow() ) {
88 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
89 }
90 $expiryarray[$p[0]] = $exp;
91 }
92 $resultProtections[] = array( $p[0] => $protections[$p[0]],
93 'expiry' => ( $expiryarray[$p[0]] == $db->getInfinity() ?
94 'infinite' :
95 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
96 }
97
98 $cascade = $params['cascade'];
99
100 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
101 $this->setWatch( $watch, $titleObj );
102
103 $status = $pageObj->doUpdateRestrictions( $protections, $expiryarray, $cascade, $params['reason'], $this->getUser() );
104
105 if ( !$status->isOK() ) {
106 $errors = $status->getErrorsArray();
107 $this->dieUsageMsg( $errors[0] );
108 }
109 $res = array(
110 'title' => $titleObj->getPrefixedText(),
111 'reason' => $params['reason']
112 );
113 if ( $cascade ) {
114 $res['cascade'] = '';
115 }
116 $res['protections'] = $resultProtections;
117 $result = $this->getResult();
118 $result->setIndexedTagName( $res['protections'], 'protection' );
119 $result->addValue( null, $this->getModuleName(), $res );
120 }
121
122 public function mustBePosted() {
123 return true;
124 }
125
126 public function isWriteMode() {
127 return true;
128 }
129
130 public function getAllowedParams() {
131 return array(
132 'title' => array(
133 ApiBase::PARAM_TYPE => 'string',
134 ),
135 'pageid' => array(
136 ApiBase::PARAM_TYPE => 'integer',
137 ),
138 'token' => array(
139 ApiBase::PARAM_TYPE => 'string',
140 ApiBase::PARAM_REQUIRED => true
141 ),
142 'protections' => array(
143 ApiBase::PARAM_ISMULTI => true,
144 ApiBase::PARAM_REQUIRED => true,
145 ),
146 'expiry' => array(
147 ApiBase::PARAM_ISMULTI => true,
148 ApiBase::PARAM_ALLOW_DUPLICATES => true,
149 ApiBase::PARAM_DFLT => 'infinite',
150 ),
151 'reason' => '',
152 'cascade' => false,
153 'watch' => array(
154 ApiBase::PARAM_DFLT => false,
155 ApiBase::PARAM_DEPRECATED => true,
156 ),
157 'watchlist' => array(
158 ApiBase::PARAM_DFLT => 'preferences',
159 ApiBase::PARAM_TYPE => array(
160 'watch',
161 'unwatch',
162 'preferences',
163 'nochange'
164 ),
165 ),
166 );
167 }
168
169 public function getParamDescription() {
170 $p = $this->getModulePrefix();
171 return array(
172 'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
173 'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
174 'token' => 'A protect token previously retrieved through prop=info',
175 'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
176 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
177 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.' ),
178 'reason' => 'Reason for (un)protecting',
179 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
180 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
181 'watch' => 'If set, add the page being (un)protected to your watchlist',
182 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
183 );
184 }
185
186 public function getResultProperties() {
187 return array(
188 '' => array(
189 'title' => 'string',
190 'reason' => 'string',
191 'cascade' => 'boolean'
192 )
193 );
194 }
195
196 public function getDescription() {
197 return 'Change the protection level of a page';
198 }
199
200 public function getPossibleErrors() {
201 return array_merge( parent::getPossibleErrors(),
202 $this->getTitleOrPageIdErrorMessage(),
203 array(
204 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
205 array( 'create-titleexists' ),
206 array( 'missingtitle-createonly' ),
207 array( 'protect-invalidaction', 'action' ),
208 array( 'protect-invalidlevel', 'level' ),
209 array( 'invalidexpiry', 'expiry' ),
210 array( 'pastexpiry', 'expiry' ),
211 )
212 );
213 }
214
215 public function needsToken() {
216 return true;
217 }
218
219 public function getTokenSalt() {
220 return '';
221 }
222
223 public function getExamples() {
224 return array(
225 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
226 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
227 );
228 }
229
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/API:Protect';
232 }
233 }