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