4 * Created on Sep 1, 2007
5 * API for MediaWiki 1.8+
7 * Copyright © 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();
43 if ( isset( $params['watch'] ) && isset( $params['unwatch'] ) ) {
44 $this->dieUsageMsg( array( 'show' ) );
48 if ( !isset( $params['title'] ) ) {
49 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
51 if ( empty( $params['protections'] ) ) {
52 $this->dieUsageMsg( array( 'missingparam', 'protections' ) );
55 $titleObj = Title
::newFromText( $params['title'] );
57 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
60 $errors = $titleObj->getUserPermissionsErrors( 'protect', $wgUser );
62 // We don't care about multiple errors, just report one of them
63 $this->dieUsageMsg( reset( $errors ) );
66 $expiry = (array)$params['expiry'];
67 if ( count( $expiry ) != count( $params['protections'] ) ) {
68 if ( count( $expiry ) == 1 ) {
69 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
71 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
75 $restrictionTypes = $titleObj->getRestrictionTypes();
77 $protections = array();
78 $expiryarray = array();
79 $resultProtections = array();
80 foreach ( $params['protections'] as $i => $prot ) {
81 $p = explode( '=', $prot );
82 $protections[$p[0]] = ( $p[1] == 'all' ?
'' : $p[1] );
84 if ( $titleObj->exists() && $p[0] == 'create' ) {
85 $this->dieUsageMsg( array( 'create-titleexists' ) );
87 if ( !$titleObj->exists() && $p[0] != 'create' ) {
88 $this->dieUsageMsg( array( 'missingtitle-createonly' ) );
91 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
92 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
94 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
95 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
98 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
99 $expiryarray[$p[0]] = Block
::infinity();
101 $exp = strtotime( $expiry[$i] );
102 if ( $exp < 0 ||
$exp == false ) {
103 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
106 $exp = wfTimestamp( TS_MW
, $exp );
107 if ( $exp < wfTimestampNow() ) {
108 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
110 $expiryarray[$p[0]] = $exp;
112 $resultProtections[] = array( $p[0] => $protections[$p[0]],
113 'expiry' => ( $expiryarray[$p[0]] == Block
::infinity() ?
115 wfTimestamp( TS_ISO_8601
, $expiryarray[$p[0]] ) ) );
118 $cascade = $params['cascade'];
119 $articleObj = new Article( $titleObj );
120 if ( $params['watch'] ) {
121 $articleObj->doWatch();
122 } elseif ( $params['unwatch'] ) {
123 $articleObj->doUnwatch();
126 if ( $titleObj->exists() ) {
127 $ok = $articleObj->updateRestrictions( $protections, $params['reason'], $cascade, $expiryarray );
129 $ok = $titleObj->updateTitleProtection( $protections['create'], $params['reason'], $expiryarray['create'] );
132 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
133 // Just throw an unknown error in this case, as it's very likely to be a race condition
134 $this->dieUsageMsg( array() );
137 'title' => $titleObj->getPrefixedText(),
138 'reason' => $params['reason']
141 $res['cascade'] = '';
143 $res['protections'] = $resultProtections;
144 $this->getResult()->setIndexedTagName( $res['protections'], 'protection' );
145 $this->getResult()->addValue( null, $this->getModuleName(), $res );
148 public function mustBePosted() {
152 public function isWriteMode() {
156 public function getAllowedParams() {
160 'protections' => array(
161 ApiBase
::PARAM_ISMULTI
=> true
164 ApiBase
::PARAM_ISMULTI
=> true,
165 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
166 ApiBase
::PARAM_DFLT
=> 'infinite',
175 public function getParamDescription() {
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 'unwatch' => 'Remove the page being (un)protected from your watchlist',
190 public function getDescription() {
192 'Change the protection level of a page.'
196 public function getPossibleErrors() {
197 return array_merge( parent
::getPossibleErrors(), array(
198 array( 'missingparam', 'title' ),
199 array( 'missingparam', 'protections' ),
200 array( 'invalidtitle', 'title' ),
201 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
202 array( 'create-titleexists' ),
203 array( 'missingtitle-createonly' ),
204 array( 'protect-invalidaction', 'action' ),
205 array( 'protect-invalidlevel', 'level' ),
206 array( 'invalidexpiry', 'expiry' ),
207 array( 'pastexpiry', 'expiry' ),
211 public function getTokenSalt() {
215 protected function getExamples() {
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'
222 public function getVersion() {
223 return __CLASS__
. ': $Id$';