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