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