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