MediaWiki  REL1_22
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             $this->dieStatus( $status );
00107         }
00108         $res = array(
00109             'title' => $titleObj->getPrefixedText(),
00110             'reason' => $params['reason']
00111         );
00112         if ( $cascade ) {
00113             $res['cascade'] = '';
00114         }
00115         $res['protections'] = $resultProtections;
00116         $result = $this->getResult();
00117         $result->setIndexedTagName( $res['protections'], 'protection' );
00118         $result->addValue( null, $this->getModuleName(), $res );
00119     }
00120 
00121     public function mustBePosted() {
00122         return true;
00123     }
00124 
00125     public function isWriteMode() {
00126         return true;
00127     }
00128 
00129     public function getAllowedParams() {
00130         return array(
00131             'title' => array(
00132                 ApiBase::PARAM_TYPE => 'string',
00133             ),
00134             'pageid' => array(
00135                 ApiBase::PARAM_TYPE => 'integer',
00136             ),
00137             'token' => array(
00138                 ApiBase::PARAM_TYPE => 'string',
00139                 ApiBase::PARAM_REQUIRED => true
00140             ),
00141             'protections' => array(
00142                 ApiBase::PARAM_ISMULTI => true,
00143                 ApiBase::PARAM_REQUIRED => true,
00144             ),
00145             'expiry' => array(
00146                 ApiBase::PARAM_ISMULTI => true,
00147                 ApiBase::PARAM_ALLOW_DUPLICATES => true,
00148                 ApiBase::PARAM_DFLT => 'infinite',
00149             ),
00150             'reason' => '',
00151             'cascade' => false,
00152             'watch' => array(
00153                 ApiBase::PARAM_DFLT => false,
00154                 ApiBase::PARAM_DEPRECATED => true,
00155             ),
00156             'watchlist' => array(
00157                 ApiBase::PARAM_DFLT => 'preferences',
00158                 ApiBase::PARAM_TYPE => array(
00159                     'watch',
00160                     'unwatch',
00161                     'preferences',
00162                     'nochange'
00163                 ),
00164             ),
00165         );
00166     }
00167 
00168     public function getParamDescription() {
00169         $p = $this->getModulePrefix();
00170         return array(
00171             'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
00172             'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
00173             'token' => 'A protect token previously retrieved through prop=info',
00174             'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
00175             'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
00176                     'Use \'infinite\', \'indefinite\' or \'never\', for a never-expiring protection.' ),
00177             'reason' => 'Reason for (un)protecting',
00178             'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
00179                     'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
00180             'watch' => 'If set, add the page being (un)protected to your watchlist',
00181             'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
00182         );
00183     }
00184 
00185     public function getResultProperties() {
00186         return array(
00187             '' => array(
00188                 'title' => 'string',
00189                 'reason' => 'string',
00190                 'cascade' => 'boolean'
00191             )
00192         );
00193     }
00194 
00195     public function getDescription() {
00196         return 'Change the protection level of a page';
00197     }
00198 
00199     public function getPossibleErrors() {
00200         return array_merge( parent::getPossibleErrors(),
00201             $this->getTitleOrPageIdErrorMessage(),
00202             array(
00203                 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
00204                 array( 'create-titleexists' ),
00205                 array( 'missingtitle-createonly' ),
00206                 array( 'protect-invalidaction', 'action' ),
00207                 array( 'protect-invalidlevel', 'level' ),
00208                 array( 'invalidexpiry', 'expiry' ),
00209                 array( 'pastexpiry', 'expiry' ),
00210             )
00211         );
00212     }
00213 
00214     public function needsToken() {
00215         return true;
00216     }
00217 
00218     public function getTokenSalt() {
00219         return '';
00220     }
00221 
00222     public function getExamples() {
00223         return array(
00224             'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
00225             'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
00226         );
00227     }
00228 
00229     public function getHelpUrls() {
00230         return 'https://www.mediawiki.org/wiki/API:Protect';
00231     }
00232 }