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