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