MediaWiki
master
|
00001 <?php 00033 class ApiDelete extends ApiBase { 00034 00035 public function __construct( $main, $action ) { 00036 parent::__construct( $main, $action ); 00037 } 00038 00046 public function execute() { 00047 $params = $this->extractRequestParams(); 00048 00049 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' ); 00050 if ( !$pageObj->exists() ) { 00051 $this->dieUsageMsg( 'notanarticle' ); 00052 } 00053 00054 $titleObj = $pageObj->getTitle(); 00055 $reason = $params['reason']; 00056 $user = $this->getUser(); 00057 00058 if ( $titleObj->getNamespace() == NS_FILE ) { 00059 $status = self::deleteFile( $pageObj, $user, $params['token'], $params['oldimage'], $reason, false ); 00060 } else { 00061 $status = self::delete( $pageObj, $user, $params['token'], $reason ); 00062 } 00063 00064 if ( is_array( $status ) ) { 00065 $this->dieUsageMsg( $status[0] ); 00066 } 00067 if ( !$status->isGood() ) { 00068 $errors = $status->getErrorsArray(); 00069 $this->dieUsageMsg( $errors[0] ); // We don't care about multiple errors, just report one of them 00070 } 00071 00072 // Deprecated parameters 00073 if ( $params['watch'] ) { 00074 $watch = 'watch'; 00075 } elseif ( $params['unwatch'] ) { 00076 $watch = 'unwatch'; 00077 } else { 00078 $watch = $params['watchlist']; 00079 } 00080 $this->setWatch( $watch, $titleObj, 'watchdeletion' ); 00081 00082 $r = array( 00083 'title' => $titleObj->getPrefixedText(), 00084 'reason' => $reason, 00085 'logid' => $status->value 00086 ); 00087 $this->getResult()->addValue( null, $this->getModuleName(), $r ); 00088 } 00089 00096 private static function getPermissionsError( $title, $user, $token ) { 00097 // Check permissions 00098 return $title->getUserPermissionsErrors( 'delete', $user ); 00099 } 00100 00110 public static function delete( Page $page, User $user, $token, &$reason = null ) { 00111 $title = $page->getTitle(); 00112 $errors = self::getPermissionsError( $title, $user, $token ); 00113 if ( count( $errors ) ) { 00114 return $errors; 00115 } 00116 00117 // Auto-generate a summary, if necessary 00118 if ( is_null( $reason ) ) { 00119 // Need to pass a throwaway variable because generateReason expects 00120 // a reference 00121 $hasHistory = false; 00122 $reason = $page->getAutoDeleteReason( $hasHistory ); 00123 if ( $reason === false ) { 00124 return array( array( 'cannotdelete', $title->getPrefixedText() ) ); 00125 } 00126 } 00127 00128 $error = ''; 00129 // Luckily, Article.php provides a reusable delete function that does the hard work for us 00130 return $page->doDeleteArticleReal( $reason, false, 0, true, $error ); 00131 } 00132 00142 public static function deleteFile( Page $page, User $user, $token, $oldimage, &$reason = null, $suppress = false ) { 00143 $title = $page->getTitle(); 00144 $errors = self::getPermissionsError( $title, $user, $token ); 00145 if ( count( $errors ) ) { 00146 return $errors; 00147 } 00148 00149 $file = $page->getFile(); 00150 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) { 00151 return self::delete( $page, $user, $token, $reason ); 00152 } 00153 00154 if ( $oldimage ) { 00155 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) { 00156 return array( array( 'invalidoldimage' ) ); 00157 } 00158 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage ); 00159 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) { 00160 return array( array( 'nodeleteablefile' ) ); 00161 } 00162 } 00163 00164 if ( is_null( $reason ) ) { // Log and RC don't like null reasons 00165 $reason = ''; 00166 } 00167 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user ); 00168 } 00169 00170 public function mustBePosted() { 00171 return true; 00172 } 00173 00174 public function isWriteMode() { 00175 return true; 00176 } 00177 00178 public function getAllowedParams() { 00179 return array( 00180 'title' => null, 00181 'pageid' => array( 00182 ApiBase::PARAM_TYPE => 'integer' 00183 ), 00184 'token' => array( 00185 ApiBase::PARAM_TYPE => 'string', 00186 ApiBase::PARAM_REQUIRED => true 00187 ), 00188 'reason' => null, 00189 'watch' => array( 00190 ApiBase::PARAM_DFLT => false, 00191 ApiBase::PARAM_DEPRECATED => true, 00192 ), 00193 'watchlist' => array( 00194 ApiBase::PARAM_DFLT => 'preferences', 00195 ApiBase::PARAM_TYPE => array( 00196 'watch', 00197 'unwatch', 00198 'preferences', 00199 'nochange' 00200 ), 00201 ), 00202 'unwatch' => array( 00203 ApiBase::PARAM_DFLT => false, 00204 ApiBase::PARAM_DEPRECATED => true, 00205 ), 00206 'oldimage' => null, 00207 ); 00208 } 00209 00210 public function getParamDescription() { 00211 $p = $this->getModulePrefix(); 00212 return array( 00213 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid", 00214 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title", 00215 'token' => 'A delete token previously retrieved through prop=info', 00216 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used', 00217 'watch' => 'Add the page to your watchlist', 00218 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch', 00219 'unwatch' => 'Remove the page from your watchlist', 00220 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename' 00221 ); 00222 } 00223 00224 public function getResultProperties() { 00225 return array( 00226 '' => array( 00227 'title' => 'string', 00228 'reason' => 'string', 00229 'logid' => 'integer' 00230 ) 00231 ); 00232 } 00233 00234 public function getDescription() { 00235 return 'Delete a page'; 00236 } 00237 00238 public function getPossibleErrors() { 00239 return array_merge( parent::getPossibleErrors(), 00240 $this->getTitleOrPageIdErrorMessage(), 00241 array( 00242 array( 'notanarticle' ), 00243 array( 'hookaborted', 'error' ), 00244 array( 'delete-toobig', 'limit' ), 00245 array( 'cannotdelete', 'title' ), 00246 array( 'invalidoldimage' ), 00247 array( 'nodeleteablefile' ), 00248 ) 00249 ); 00250 } 00251 00252 public function needsToken() { 00253 return true; 00254 } 00255 00256 public function getTokenSalt() { 00257 return ''; 00258 } 00259 00260 public function getExamples() { 00261 return array( 00262 'api.php?action=delete&title=Main%20Page&token=123ABC' => 'Delete the Main Page', 00263 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move' => 'Delete the Main Page with the reason "Preparing for move"', 00264 ); 00265 } 00266 00267 public function getHelpUrls() { 00268 return 'https://www.mediawiki.org/wiki/API:Delete'; 00269 } 00270 00271 public function getVersion() { 00272 return __CLASS__ . ': $Id$'; 00273 } 00274 }