34283f295f9b11dc19865471ae2eb77a74d42c37
4 * Created on Sep 1, 2007
5 * API for MediaWiki 1.8+
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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.
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.
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
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once ( "ApiBase.php" );
33 class ApiProtect
extends ApiBase
{
35 public function __construct( $main, $action ) {
36 parent
:: __construct( $main, $action );
39 public function execute() {
40 global $wgUser, $wgRestrictionTypes, $wgRestrictionLevels;
41 $params = $this->extractRequestParams();
44 if ( !isset( $params['title'] ) )
45 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
46 if ( !isset( $params['token'] ) )
47 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
48 if ( empty( $params['protections'] ) )
49 $this->dieUsageMsg( array( 'missingparam', 'protections' ) );
51 if ( !$wgUser->matchEditToken( $params['token'] ) )
52 $this->dieUsageMsg( array( 'sessionfailure' ) );
54 $titleObj = Title
::newFromText( $params['title'] );
56 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
58 $errors = $titleObj->getUserPermissionsErrors( 'protect', $wgUser );
60 // We don't care about multiple errors, just report one of them
61 $this->dieUsageMsg( reset( $errors ) );
63 $expiry = (array)$params['expiry'];
64 if ( count( $expiry ) != count( $params['protections'] ) )
66 if ( count( $expiry ) == 1 )
67 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
69 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
72 $restrictionTypes = $titleObj->getRestrictionTypes();
74 $protections = array();
75 $expiryarray = array();
76 $resultProtections = array();
77 foreach ( $params['protections'] as $i => $prot )
79 $p = explode( '=', $prot );
80 $protections[$p[0]] = ( $p[1] == 'all' ?
'' : $p[1] );
82 if ( $titleObj->exists() && $p[0] == 'create' )
83 $this->dieUsageMsg( array( 'create-titleexists' ) );
84 if ( !$titleObj->exists() && $p[0] != 'create' )
85 $this->dieUsageMsg( array( 'missingtitle-createonly' ) );
87 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' )
88 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
89 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' )
90 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
92 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) )
93 $expiryarray[$p[0]] = Block
::infinity();
96 $exp = strtotime( $expiry[$i] );
97 if ( $exp < 0 ||
$exp == false )
98 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
100 $exp = wfTimestamp( TS_MW
, $exp );
101 if ( $exp < wfTimestampNow() )
102 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
103 $expiryarray[$p[0]] = $exp;
105 $resultProtections[] = array( $p[0] => $protections[$p[0]],
106 'expiry' => ( $expiryarray[$p[0]] == Block
::infinity() ?
108 wfTimestamp( TS_ISO_8601
, $expiryarray[$p[0]] ) ) );
111 $cascade = $params['cascade'];
112 $articleObj = new Article( $titleObj );
113 if ( $params['watch'] )
114 $articleObj->doWatch();
115 if ( $titleObj->exists() )
116 $ok = $articleObj->updateRestrictions( $protections, $params['reason'], $cascade, $expiryarray );
118 $ok = $titleObj->updateTitleProtection( $protections['create'], $params['reason'], $expiryarray['create'] );
120 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
121 // Just throw an unknown error in this case, as it's very likely to be a race condition
122 $this->dieUsageMsg( array() );
123 $res = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'] );
125 $res['cascade'] = '';
126 $res['protections'] = $resultProtections;
127 $this->getResult()->setIndexedTagName( $res['protections'], 'protection' );
128 $this->getResult()->addValue( null, $this->getModuleName(), $res );
131 public function mustBePosted() { return true; }
133 public function isWriteMode() {
137 public function getAllowedParams() {
141 'protections' => array(
142 ApiBase
:: PARAM_ISMULTI
=> true
145 ApiBase
:: PARAM_ISMULTI
=> true,
146 ApiBase
:: PARAM_ALLOW_DUPLICATES
=> true,
147 ApiBase
:: PARAM_DFLT
=> 'infinite',
155 public function getParamDescription() {
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',
169 public function getDescription() {
171 'Change the protection level of a page.'
175 public function getPossibleErrors() {
176 return array_merge( parent
::getPossibleErrors(), array(
177 array( 'missingparam', 'title' ),
178 array( 'missingparam', 'token' ),
179 array( 'missingparam', 'protections' ),
180 array( 'sessionfailure' ),
181 array( 'invalidtitle', 'title' ),
182 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ) ),
183 array( 'create-titleexists' ),
184 array( 'missingtitle-createonly' ),
185 array( 'protect-invalidaction', 'action'] ),
186 array( 'protect-invalidlevel', 'level' ),
187 array( 'invalidexpiry', 'expiry' ),
188 array( 'pastexpiry', 'expiry' ),
192 protected function getExamples() {
194 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000|never',
195 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
199 public function getVersion() {
200 return __CLASS__
. ': $Id$';