Revert my api changes to pre r64815
[lhc/web/wiklou.git] / includes / api / ApiProtect.php
1 <?php
2
3 /**
4 * Created on Sep 1, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * @ingroup API
32 */
33 class ApiProtect extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 public function execute() {
40 global $wgUser, $wgRestrictionTypes, $wgRestrictionLevels;
41 $params = $this->extractRequestParams();
42
43 $titleObj = null;
44 if ( !isset( $params['title'] ) ) {
45 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
46 }
47 if ( empty( $params['protections'] ) ) {
48 $this->dieUsageMsg( array( 'missingparam', 'protections' ) );
49 }
50
51 $titleObj = Title::newFromText( $params['title'] );
52 if ( !$titleObj ) {
53 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
54 }
55
56 $errors = $titleObj->getUserPermissionsErrors( 'protect', $wgUser );
57 if ( $errors ) {
58 // We don't care about multiple errors, just report one of them
59 $this->dieUsageMsg( reset( $errors ) );
60 }
61
62 $expiry = (array)$params['expiry'];
63 if ( count( $expiry ) != count( $params['protections'] ) ) {
64 if ( count( $expiry ) == 1 ) {
65 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
66 } else {
67 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
68 }
69 }
70
71 $restrictionTypes = $titleObj->getRestrictionTypes();
72
73 $protections = array();
74 $expiryarray = array();
75 $resultProtections = array();
76 foreach ( $params['protections'] as $i => $prot ) {
77 $p = explode( '=', $prot );
78 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
79
80 if ( $titleObj->exists() && $p[0] == 'create' ) {
81 $this->dieUsageMsg( array( 'create-titleexists' ) );
82 }
83 if ( !$titleObj->exists() && $p[0] != 'create' ) {
84 $this->dieUsageMsg( array( 'missingtitle-createonly' ) );
85 }
86
87 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
88 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
89 }
90 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
91 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
92 }
93
94 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
95 $expiryarray[$p[0]] = Block::infinity();
96 } else {
97 $exp = strtotime( $expiry[$i] );
98 if ( $exp < 0 || $exp == false ) {
99 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
100 }
101
102 $exp = wfTimestamp( TS_MW, $exp );
103 if ( $exp < wfTimestampNow() ) {
104 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
105 }
106 $expiryarray[$p[0]] = $exp;
107 }
108 $resultProtections[] = array( $p[0] => $protections[$p[0]],
109 'expiry' => ( $expiryarray[$p[0]] == Block::infinity() ?
110 'infinite' :
111 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
112 }
113
114 $cascade = $params['cascade'];
115 $articleObj = new Article( $titleObj );
116 if ( $params['watch'] ) {
117 $articleObj->doWatch();
118 }
119 if ( $titleObj->exists() ) {
120 $ok = $articleObj->updateRestrictions( $protections, $params['reason'], $cascade, $expiryarray );
121 } else {
122 $ok = $titleObj->updateTitleProtection( $protections['create'], $params['reason'], $expiryarray['create'] );
123 }
124 if ( !$ok ) {
125 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
126 // Just throw an unknown error in this case, as it's very likely to be a race condition
127 $this->dieUsageMsg( array() );
128 }
129 $res = array(
130 'title' => $titleObj->getPrefixedText(),
131 'reason' => $params['reason']
132 );
133 if ( $cascade ) {
134 $res['cascade'] = '';
135 }
136 $res['protections'] = $resultProtections;
137 $this->getResult()->setIndexedTagName( $res['protections'], 'protection' );
138 $this->getResult()->addValue( null, $this->getModuleName(), $res );
139 }
140
141 public function mustBePosted() {
142 return true;
143 }
144
145 public function isWriteMode() {
146 return true;
147 }
148
149 public function getAllowedParams() {
150 return array(
151 'title' => null,
152 'token' => null,
153 'protections' => array(
154 ApiBase::PARAM_ISMULTI => true
155 ),
156 'expiry' => array(
157 ApiBase::PARAM_ISMULTI => true,
158 ApiBase::PARAM_ALLOW_DUPLICATES => true,
159 ApiBase::PARAM_DFLT => 'infinite',
160 ),
161 'reason' => '',
162 'cascade' => false,
163 'watch' => false,
164 );
165 }
166
167 public function getParamDescription() {
168 return array(
169 'title' => 'Title of the page you want to (un)protect.',
170 'token' => 'A protect token previously retrieved through prop=info',
171 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
172 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
173 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.' ),
174 'reason' => 'Reason for (un)protecting (optional)',
175 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
176 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
177 'watch' => 'If set, add the page being (un)protected to your watchlist',
178 );
179 }
180
181 public function getDescription() {
182 return array(
183 'Change the protection level of a page.'
184 );
185 }
186
187 public function getPossibleErrors() {
188 return array_merge( parent::getPossibleErrors(), array(
189 array( 'missingparam', 'title' ),
190 array( 'missingparam', 'protections' ),
191 array( 'invalidtitle', 'title' ),
192 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
193 array( 'create-titleexists' ),
194 array( 'missingtitle-createonly' ),
195 array( 'protect-invalidaction', 'action' ),
196 array( 'protect-invalidlevel', 'level' ),
197 array( 'invalidexpiry', 'expiry' ),
198 array( 'pastexpiry', 'expiry' ),
199 ) );
200 }
201
202 public function getTokenSalt() {
203 return null;
204 }
205
206 protected function getExamples() {
207 return array(
208 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000|never',
209 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
210 );
211 }
212
213 public function getVersion() {
214 return __CLASS__ . ': $Id$';
215 }
216 }