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