MediaWiki  REL1_20
ApiProtect.php
Go to the documentation of this file.
00001 <?php
00030 class ApiProtect extends ApiBase {
00031 
00032         public function __construct( $main, $action ) {
00033                 parent::__construct( $main, $action );
00034         }
00035 
00036         public function execute() {
00037                 global $wgRestrictionLevels;
00038                 $params = $this->extractRequestParams();
00039 
00040                 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
00041                 $titleObj = $pageObj->getTitle();
00042 
00043                 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
00044                 if ( $errors ) {
00045                         // We don't care about multiple errors, just report one of them
00046                         $this->dieUsageMsg( reset( $errors ) );
00047                 }
00048 
00049                 $expiry = (array)$params['expiry'];
00050                 if ( count( $expiry ) != count( $params['protections'] ) ) {
00051                         if ( count( $expiry ) == 1 ) {
00052                                 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
00053                         } else {
00054                                 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
00055                         }
00056                 }
00057 
00058                 $restrictionTypes = $titleObj->getRestrictionTypes();
00059                 $db = $this->getDB();
00060 
00061                 $protections = array();
00062                 $expiryarray = array();
00063                 $resultProtections = array();
00064                 foreach ( $params['protections'] as $i => $prot ) {
00065                         $p = explode( '=', $prot );
00066                         $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
00067 
00068                         if ( $titleObj->exists() && $p[0] == 'create' ) {
00069                                 $this->dieUsageMsg( 'create-titleexists' );
00070                         }
00071                         if ( !$titleObj->exists() && $p[0] != 'create' ) {
00072                                 $this->dieUsageMsg( 'missingtitle-createonly' );
00073                         }
00074 
00075                         if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
00076                                 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
00077                         }
00078                         if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
00079                                 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
00080                         }
00081 
00082                         if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
00083                                 $expiryarray[$p[0]] = $db->getInfinity();
00084                         } else {
00085                                 $exp = strtotime( $expiry[$i] );
00086                                 if ( $exp < 0 || !$exp ) {
00087                                         $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
00088                                 }
00089 
00090                                 $exp = wfTimestamp( TS_MW, $exp );
00091                                 if ( $exp < wfTimestampNow() ) {
00092                                         $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
00093                                 }
00094                                 $expiryarray[$p[0]] = $exp;
00095                         }
00096                         $resultProtections[] = array( $p[0] => $protections[$p[0]],
00097                                         'expiry' => ( $expiryarray[$p[0]] == $db->getInfinity() ?
00098                                                                 'infinite' :
00099                                                                 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
00100                 }
00101 
00102                 $cascade = $params['cascade'];
00103 
00104                 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
00105                 $this->setWatch( $watch, $titleObj );
00106 
00107                 $status = $pageObj->doUpdateRestrictions( $protections, $expiryarray, $cascade, $params['reason'], $this->getUser() );
00108 
00109                 if ( !$status->isOK() ) {
00110                         $errors = $status->getErrorsArray();
00111                         $this->dieUsageMsg( $errors[0] );
00112                 }
00113                 $res = array(
00114                         'title' => $titleObj->getPrefixedText(),
00115                         'reason' => $params['reason']
00116                 );
00117                 if ( $cascade ) {
00118                         $res['cascade'] = '';
00119                 }
00120                 $res['protections'] = $resultProtections;
00121                 $result = $this->getResult();
00122                 $result->setIndexedTagName( $res['protections'], 'protection' );
00123                 $result->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' => array(
00137                                 ApiBase::PARAM_TYPE => 'string',
00138                         ),
00139                         'pageid' => array(
00140                                 ApiBase::PARAM_TYPE => 'integer',
00141                         ),
00142                         'token' => array(
00143                                 ApiBase::PARAM_TYPE => 'string',
00144                                 ApiBase::PARAM_REQUIRED => true
00145                         ),
00146                         'protections' => array(
00147                                 ApiBase::PARAM_ISMULTI => true,
00148                                 ApiBase::PARAM_REQUIRED => true,
00149                         ),
00150                         'expiry' => array(
00151                                 ApiBase::PARAM_ISMULTI => true,
00152                                 ApiBase::PARAM_ALLOW_DUPLICATES => true,
00153                                 ApiBase::PARAM_DFLT => 'infinite',
00154                         ),
00155                         'reason' => '',
00156                         'cascade' => false,
00157                         'watch' => array(
00158                                 ApiBase::PARAM_DFLT => false,
00159                                 ApiBase::PARAM_DEPRECATED => true,
00160                         ),
00161                         'watchlist' => array(
00162                                 ApiBase::PARAM_DFLT => 'preferences',
00163                                 ApiBase::PARAM_TYPE => array(
00164                                         'watch',
00165                                         'unwatch',
00166                                         'preferences',
00167                                         'nochange'
00168                                 ),
00169                         ),
00170                 );
00171         }
00172 
00173         public function getParamDescription() {
00174                 $p = $this->getModulePrefix();
00175                 return array(
00176                         'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
00177                         'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
00178                         'token' => 'A protect token previously retrieved through prop=info',
00179                         'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
00180                         'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
00181                                         'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.' ),
00182                         'reason' => 'Reason for (un)protecting',
00183                         'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
00184                                         'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
00185                         'watch' => 'If set, add the page being (un)protected to your watchlist',
00186                         'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
00187                 );
00188         }
00189 
00190         public function getResultProperties() {
00191                 return array(
00192                         '' => array(
00193                                 'title' => 'string',
00194                                 'reason' => 'string',
00195                                 'cascade' => 'boolean'
00196                         )
00197                 );
00198         }
00199 
00200         public function getDescription() {
00201                 return 'Change the protection level of a page';
00202         }
00203 
00204         public function getPossibleErrors() {
00205                 return array_merge( parent::getPossibleErrors(),
00206                         $this->getTitleOrPageIdErrorMessage(),
00207                         array(
00208                                 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
00209                                 array( 'create-titleexists' ),
00210                                 array( 'missingtitle-createonly' ),
00211                                 array( 'protect-invalidaction', 'action' ),
00212                                 array( 'protect-invalidlevel', 'level' ),
00213                                 array( 'invalidexpiry', 'expiry' ),
00214                                 array( 'pastexpiry', 'expiry' ),
00215                         )
00216                 );
00217         }
00218 
00219         public function needsToken() {
00220                 return true;
00221         }
00222 
00223         public function getTokenSalt() {
00224                 return '';
00225         }
00226 
00227         public function getExamples() {
00228                 return array(
00229                         'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
00230                         'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
00231                 );
00232         }
00233 
00234         public function getHelpUrls() {
00235                 return 'https://www.mediawiki.org/wiki/API:Protect';
00236         }
00237 
00238         public function getVersion() {
00239                 return __CLASS__ . ': $Id$';
00240         }
00241 }