MediaWiki
REL1_19
|
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 $titleObj = Title::newFromText( $params['title'] ); 00041 if ( !$titleObj ) { 00042 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) ); 00043 } 00044 00045 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() ); 00046 if ( $errors ) { 00047 // We don't care about multiple errors, just report one of them 00048 $this->dieUsageMsg( reset( $errors ) ); 00049 } 00050 00051 $expiry = (array)$params['expiry']; 00052 if ( count( $expiry ) != count( $params['protections'] ) ) { 00053 if ( count( $expiry ) == 1 ) { 00054 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] ); 00055 } else { 00056 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) ); 00057 } 00058 } 00059 00060 $restrictionTypes = $titleObj->getRestrictionTypes(); 00061 $dbr = wfGetDB( DB_SLAVE ); 00062 00063 $protections = array(); 00064 $expiryarray = array(); 00065 $resultProtections = array(); 00066 foreach ( $params['protections'] as $i => $prot ) { 00067 $p = explode( '=', $prot ); 00068 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] ); 00069 00070 if ( $titleObj->exists() && $p[0] == 'create' ) { 00071 $this->dieUsageMsg( 'create-titleexists' ); 00072 } 00073 if ( !$titleObj->exists() && $p[0] != 'create' ) { 00074 $this->dieUsageMsg( 'missingtitle-createonly' ); 00075 } 00076 00077 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) { 00078 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) ); 00079 } 00080 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) { 00081 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) ); 00082 } 00083 00084 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) { 00085 $expiryarray[$p[0]] = $dbr->getInfinity(); 00086 } else { 00087 $exp = strtotime( $expiry[$i] ); 00088 if ( $exp < 0 || !$exp ) { 00089 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) ); 00090 } 00091 00092 $exp = wfTimestamp( TS_MW, $exp ); 00093 if ( $exp < wfTimestampNow() ) { 00094 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) ); 00095 } 00096 $expiryarray[$p[0]] = $exp; 00097 } 00098 $resultProtections[] = array( $p[0] => $protections[$p[0]], 00099 'expiry' => ( $expiryarray[$p[0]] == $dbr->getInfinity() ? 00100 'infinite' : 00101 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) ); 00102 } 00103 00104 $cascade = $params['cascade']; 00105 00106 $watch = $params['watch'] ? 'watch' : $params['watchlist']; 00107 $this->setWatch( $watch, $titleObj ); 00108 00109 $pageObj = WikiPage::factory( $titleObj ); 00110 $status = $pageObj->doUpdateRestrictions( $protections, $expiryarray, $cascade, $params['reason'], $this->getUser() ); 00111 00112 if ( !$status->isOK() ) { 00113 $errors = $status->getErrorsArray(); 00114 $this->dieUsageMsg( $errors[0] ); 00115 } 00116 $res = array( 00117 'title' => $titleObj->getPrefixedText(), 00118 'reason' => $params['reason'] 00119 ); 00120 if ( $cascade ) { 00121 $res['cascade'] = ''; 00122 } 00123 $res['protections'] = $resultProtections; 00124 $result = $this->getResult(); 00125 $result->setIndexedTagName( $res['protections'], 'protection' ); 00126 $result->addValue( null, $this->getModuleName(), $res ); 00127 } 00128 00129 public function mustBePosted() { 00130 return true; 00131 } 00132 00133 public function isWriteMode() { 00134 return true; 00135 } 00136 00137 public function getAllowedParams() { 00138 return array( 00139 'title' => array( 00140 ApiBase::PARAM_TYPE => 'string', 00141 ApiBase::PARAM_REQUIRED => true 00142 ), 00143 'token' => null, 00144 'protections' => array( 00145 ApiBase::PARAM_ISMULTI => true, 00146 ApiBase::PARAM_REQUIRED => true, 00147 ), 00148 'expiry' => array( 00149 ApiBase::PARAM_ISMULTI => true, 00150 ApiBase::PARAM_ALLOW_DUPLICATES => true, 00151 ApiBase::PARAM_DFLT => 'infinite', 00152 ), 00153 'reason' => '', 00154 'cascade' => false, 00155 'watch' => array( 00156 ApiBase::PARAM_DFLT => false, 00157 ApiBase::PARAM_DEPRECATED => true, 00158 ), 00159 'watchlist' => array( 00160 ApiBase::PARAM_DFLT => 'preferences', 00161 ApiBase::PARAM_TYPE => array( 00162 'watch', 00163 'unwatch', 00164 'preferences', 00165 'nochange' 00166 ), 00167 ), 00168 ); 00169 } 00170 00171 public function getParamDescription() { 00172 return array( 00173 'title' => 'Title of the page you want to (un)protect', 00174 'token' => 'A protect token previously retrieved through prop=info', 00175 'protections' => 'Pipe-separated 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 neverexpiring protection.' ), 00178 'reason' => 'Reason for (un)protecting (optional)', 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 getDescription() { 00187 return 'Change the protection level of a page'; 00188 } 00189 00190 public function getPossibleErrors() { 00191 return array_merge( parent::getPossibleErrors(), array( 00192 array( 'invalidtitle', 'title' ), 00193 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ), 00194 array( 'create-titleexists' ), 00195 array( 'missingtitle-createonly' ), 00196 array( 'protect-invalidaction', 'action' ), 00197 array( 'protect-invalidlevel', 'level' ), 00198 array( 'invalidexpiry', 'expiry' ), 00199 array( 'pastexpiry', 'expiry' ), 00200 ) ); 00201 } 00202 00203 public function needsToken() { 00204 return true; 00205 } 00206 00207 public function getTokenSalt() { 00208 return ''; 00209 } 00210 00211 public function getExamples() { 00212 return array( 00213 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never', 00214 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions' 00215 ); 00216 } 00217 00218 public function getHelpUrls() { 00219 return 'https://www.mediawiki.org/wiki/API:Protect'; 00220 } 00221 00222 public function getVersion() { 00223 return __CLASS__ . ': $Id$'; 00224 } 00225 }