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