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