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