00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 if ( !defined( 'MEDIAWIKI' ) ) {
00026
00027 require_once ( "ApiBase.php" );
00028 }
00029
00033 class ApiProtect extends ApiBase {
00034
00035 public function __construct( $main, $action ) {
00036 parent :: __construct( $main, $action );
00037 }
00038
00039 public function execute() {
00040 global $wgUser, $wgRestrictionTypes, $wgRestrictionLevels;
00041 $params = $this->extractRequestParams();
00042
00043 $titleObj = null;
00044 if ( !isset( $params['title'] ) )
00045 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
00046 if ( empty( $params['protections'] ) )
00047 $this->dieUsageMsg( array( 'missingparam', 'protections' ) );
00048
00049 $titleObj = Title::newFromText( $params['title'] );
00050 if ( !$titleObj )
00051 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00052
00053 $errors = $titleObj->getUserPermissionsErrors( 'protect', $wgUser );
00054 if ( $errors )
00055
00056 $this->dieUsageMsg( reset( $errors ) );
00057
00058 $expiry = (array)$params['expiry'];
00059 if ( count( $expiry ) != count( $params['protections'] ) )
00060 {
00061 if ( count( $expiry ) == 1 )
00062 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
00063 else
00064 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
00065 }
00066
00067 $restrictionTypes = $titleObj->getRestrictionTypes();
00068
00069 $protections = array();
00070 $expiryarray = array();
00071 $resultProtections = array();
00072 foreach ( $params['protections'] as $i => $prot )
00073 {
00074 $p = explode( '=', $prot );
00075 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
00076
00077 if ( $titleObj->exists() && $p[0] == 'create' )
00078 $this->dieUsageMsg( array( 'create-titleexists' ) );
00079 if ( !$titleObj->exists() && $p[0] != 'create' )
00080 $this->dieUsageMsg( array( 'missingtitle-createonly' ) );
00081
00082 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' )
00083 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
00084 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' )
00085 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
00086
00087 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) )
00088 $expiryarray[$p[0]] = Block::infinity();
00089 else
00090 {
00091 $exp = strtotime( $expiry[$i] );
00092 if ( $exp < 0 || $exp == false )
00093 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
00094
00095 $exp = wfTimestamp( TS_MW, $exp );
00096 if ( $exp < wfTimestampNow() )
00097 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
00098 $expiryarray[$p[0]] = $exp;
00099 }
00100 $resultProtections[] = array( $p[0] => $protections[$p[0]],
00101 'expiry' => ( $expiryarray[$p[0]] == Block::infinity() ?
00102 'infinite' :
00103 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
00104 }
00105
00106 $cascade = $params['cascade'];
00107 $articleObj = new Article( $titleObj );
00108 if ( $params['watch'] )
00109 $articleObj->doWatch();
00110 if ( $titleObj->exists() )
00111 $ok = $articleObj->updateRestrictions( $protections, $params['reason'], $cascade, $expiryarray );
00112 else
00113 $ok = $titleObj->updateTitleProtection( $protections['create'], $params['reason'], $expiryarray['create'] );
00114 if ( !$ok )
00115
00116
00117 $this->dieUsageMsg( array() );
00118 $res = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'] );
00119 if ( $cascade )
00120 $res['cascade'] = '';
00121 $res['protections'] = $resultProtections;
00122 $this->getResult()->setIndexedTagName( $res['protections'], 'protection' );
00123 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00124 }
00125
00126 public function mustBePosted() {
00127 return true;
00128 }
00129
00130 public function isWriteMode() {
00131 return true;
00132 }
00133
00134 public function getAllowedParams() {
00135 return array (
00136 'title' => null,
00137 'token' => null,
00138 'protections' => array(
00139 ApiBase :: PARAM_ISMULTI => true
00140 ),
00141 'expiry' => array(
00142 ApiBase :: PARAM_ISMULTI => true,
00143 ApiBase :: PARAM_ALLOW_DUPLICATES => true,
00144 ApiBase :: PARAM_DFLT => 'infinite',
00145 ),
00146 'reason' => '',
00147 'cascade' => false,
00148 'watch' => false,
00149 );
00150 }
00151
00152 public function getParamDescription() {
00153 return array (
00154 'title' => 'Title of the page you want to (un)protect.',
00155 'token' => 'A protect token previously retrieved through prop=info',
00156 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
00157 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
00158 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.' ),
00159 'reason' => 'Reason for (un)protecting (optional)',
00160 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
00161 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
00162 'watch' => 'If set, add the page being (un)protected to your watchlist',
00163 );
00164 }
00165
00166 public function getDescription() {
00167 return array(
00168 'Change the protection level of a page.'
00169 );
00170 }
00171
00172 public function getPossibleErrors() {
00173 return array_merge( parent::getPossibleErrors(), array(
00174 array( 'missingparam', 'title' ),
00175 array( 'missingparam', 'protections' ),
00176 array( 'invalidtitle', 'title' ),
00177 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
00178 array( 'create-titleexists' ),
00179 array( 'missingtitle-createonly' ),
00180 array( 'protect-invalidaction', 'action' ),
00181 array( 'protect-invalidlevel', 'level' ),
00182 array( 'invalidexpiry', 'expiry' ),
00183 array( 'pastexpiry', 'expiry' ),
00184 ) );
00185 }
00186
00187 public function needsToken() {
00188 return true;
00189 }
00190
00191 public function getTokenSalt() {
00192 return '';
00193 }
00194
00195 protected function getExamples() {
00196 return array (
00197 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000|never',
00198 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
00199 );
00200 }
00201
00202 public function getVersion() {
00203 return __CLASS__ . ': $Id: ApiProtect.php 74217 2010-10-03 15:53:07Z reedy $';
00204 }
00205 }