a417dace1f9aab556787290751c0c423cc900af9
[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 (C) 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 if ( empty( $params['protections'] ) )
47 $this->dieUsageMsg( array( 'missingparam', 'protections' ) );
48
49 if ( !$wgUser->matchEditToken( $params['token'] ) )
50 $this->dieUsageMsg( array( 'sessionfailure' ) );
51
52 $titleObj = Title::newFromText( $params['title'] );
53 if ( !$titleObj )
54 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
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 $expiry = (array)$params['expiry'];
62 if ( count( $expiry ) != count( $params['protections'] ) )
63 {
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 $restrictionTypes = $titleObj->getRestrictionTypes();
71
72 $protections = array();
73 $expiryarray = array();
74 $resultProtections = array();
75 foreach ( $params['protections'] as $i => $prot )
76 {
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 if ( !$titleObj->exists() && $p[0] != 'create' )
83 $this->dieUsageMsg( array( 'missingtitle-createonly' ) );
84
85 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' )
86 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
87 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' )
88 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
89
90 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) )
91 $expiryarray[$p[0]] = Block::infinity();
92 else
93 {
94 $exp = strtotime( $expiry[$i] );
95 if ( $exp < 0 || $exp == false )
96 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
97
98 $exp = wfTimestamp( TS_MW, $exp );
99 if ( $exp < wfTimestampNow() )
100 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
101 $expiryarray[$p[0]] = $exp;
102 }
103 $resultProtections[] = array( $p[0] => $protections[$p[0]],
104 'expiry' => ( $expiryarray[$p[0]] == Block::infinity() ?
105 'infinite' :
106 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
107 }
108
109 $cascade = $params['cascade'];
110 $articleObj = new Article( $titleObj );
111 if ( $params['watch'] )
112 $articleObj->doWatch();
113 if ( $titleObj->exists() )
114 $ok = $articleObj->updateRestrictions( $protections, $params['reason'], $cascade, $expiryarray );
115 else
116 $ok = $titleObj->updateTitleProtection( $protections['create'], $params['reason'], $expiryarray['create'] );
117 if ( !$ok )
118 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
119 // Just throw an unknown error in this case, as it's very likely to be a race condition
120 $this->dieUsageMsg( array() );
121 $res = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'] );
122 if ( $cascade )
123 $res['cascade'] = '';
124 $res['protections'] = $resultProtections;
125 $this->getResult()->setIndexedTagName( $res['protections'], 'protection' );
126 $this->getResult()->addValue( null, $this->getModuleName(), $res );
127 }
128
129 public function mustBePosted() {
130 return true;
131 }
132
133 public function isWriteMode() {
134 return true;
135 }
136
137 public function getAllowedParams() {
138 return array (
139 'title' => null,
140 'token' => null,
141 'protections' => array(
142 ApiBase :: PARAM_ISMULTI => true
143 ),
144 'expiry' => array(
145 ApiBase :: PARAM_ISMULTI => true,
146 ApiBase :: PARAM_ALLOW_DUPLICATES => true,
147 ApiBase :: PARAM_DFLT => 'infinite',
148 ),
149 'reason' => '',
150 'cascade' => false,
151 'watch' => false,
152 );
153 }
154
155 public function getParamDescription() {
156 return array (
157 'title' => 'Title of the page you want to (un)protect.',
158 'token' => 'A protect token previously retrieved through prop=info',
159 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
160 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
161 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.' ),
162 'reason' => 'Reason for (un)protecting (optional)',
163 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
164 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
165 'watch' => 'If set, add the page being (un)protected to your watchlist',
166 );
167 }
168
169 public function getDescription() {
170 return array(
171 'Change the protection level of a page.'
172 );
173 }
174
175 public function getPossibleErrors() {
176 return array_merge( parent::getPossibleErrors(), array(
177 array( 'missingparam', 'title' ),
178 array( 'missingparam', 'protections' ),
179 array( 'sessionfailure' ),
180 array( 'invalidtitle', 'title' ),
181 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
182 array( 'create-titleexists' ),
183 array( 'missingtitle-createonly' ),
184 array( 'protect-invalidaction', 'action' ),
185 array( 'protect-invalidlevel', 'level' ),
186 array( 'invalidexpiry', 'expiry' ),
187 array( 'pastexpiry', 'expiry' ),
188 ) );
189 }
190
191 public function requiresToken() {
192 return true;
193 }
194
195 protected function getExamples() {
196 return array (
197 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000|never',
198 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
199 );
200 }
201
202 public function getVersion() {
203 return __CLASS__ . ': $Id$';
204 }
205 }