00001 <?php
00002
00025 if ( !defined( 'MEDIAWIKI' ) ) {
00026
00027 require_once( "ApiBase.php" );
00028 }
00029
00036 class ApiDelete extends ApiBase {
00037
00038 public function __construct( $main, $action ) {
00039 parent::__construct( $main, $action );
00040 }
00041
00049 public function execute() {
00050 global $wgUser;
00051
00052 $params = $this->extractRequestParams();
00053
00054 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
00055
00056 if ( isset( $params['title'] ) ) {
00057 $titleObj = Title::newFromText( $params['title'] );
00058 if ( !$titleObj ) {
00059 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00060 }
00061 } elseif ( isset( $params['pageid'] ) ) {
00062 $titleObj = Title::newFromID( $params['pageid'] );
00063 if ( !$titleObj ) {
00064 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
00065 }
00066 }
00067 if ( !$titleObj->exists() ) {
00068 $this->dieUsageMsg( array( 'notanarticle' ) );
00069 }
00070
00071 $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
00072 if ( $titleObj->getNamespace() == NS_FILE ) {
00073 $retval = self::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
00074 if ( count( $retval ) ) {
00075 $this->dieUsageMsg( reset( $retval ) );
00076 }
00077 } else {
00078 $articleObj = new Article( $titleObj );
00079 $retval = self::delete( $articleObj, $params['token'], $reason );
00080
00081 if ( count( $retval ) ) {
00082 $this->dieUsageMsg( reset( $retval ) );
00083 }
00084
00085 if ( $params['watch'] || $wgUser->getOption( 'watchdeletion' ) ) {
00086 $articleObj->doWatch();
00087 } elseif ( $params['unwatch'] ) {
00088 $articleObj->doUnwatch();
00089 }
00090 }
00091
00092 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
00093 $this->getResult()->addValue( null, $this->getModuleName(), $r );
00094 }
00095
00096 private static function getPermissionsError( &$title, $token ) {
00097 global $wgUser;
00098
00099
00100 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
00101 if ( count( $errors ) > 0 ) {
00102 return $errors;
00103 }
00104
00105 return array();
00106 }
00107
00116 public static function delete( &$article, $token, &$reason = null ) {
00117 global $wgUser;
00118 if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
00119 global $wgDeleteRevisionsLimit;
00120 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
00121 }
00122 $title = $article->getTitle();
00123 $errors = self::getPermissionsError( $title, $token );
00124 if ( count( $errors ) ) {
00125 return $errors;
00126 }
00127
00128
00129 if ( is_null( $reason ) ) {
00130
00131
00132 $hasHistory = false;
00133 $reason = $article->generateReason( $hasHistory );
00134 if ( $reason === false ) {
00135 return array( array( 'cannotdelete' ) );
00136 }
00137 }
00138
00139 $error = '';
00140 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) ) {
00141 $this->dieUsageMsg( array( 'hookaborted', $error ) );
00142 }
00143
00144
00145 if ( $article->doDeleteArticle( $reason ) ) {
00146 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $article->getId() ) );
00147 return array();
00148 }
00149 return array( array( 'cannotdelete', $article->mTitle->getPrefixedText() ) );
00150 }
00151
00152 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
00153 $errors = self::getPermissionsError( $title, $token );
00154 if ( count( $errors ) ) {
00155 return $errors;
00156 }
00157
00158 if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
00159 return array( array( 'invalidoldimage' ) );
00160 }
00161
00162 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
00163 $oldfile = false;
00164
00165 if ( $oldimage ) {
00166 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
00167 }
00168
00169 if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
00170 return self::delete( new Article( $title ), $token, $reason );
00171 }
00172 if ( is_null( $reason ) ) {
00173 $reason = '';
00174 }
00175 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
00176
00177 if ( !$status->isGood() ) {
00178 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
00179 }
00180
00181 return array();
00182 }
00183
00184 public function mustBePosted() {
00185 return true;
00186 }
00187
00188 public function isWriteMode() {
00189 return true;
00190 }
00191
00192 public function getAllowedParams() {
00193 return array(
00194 'title' => null,
00195 'pageid' => array(
00196 ApiBase::PARAM_TYPE => 'integer'
00197 ),
00198 'token' => null,
00199 'reason' => null,
00200 'watch' => false,
00201 'unwatch' => false,
00202 'oldimage' => null
00203 );
00204 }
00205
00206 public function getParamDescription() {
00207 return array(
00208 'title' => 'Title of the page you want to delete. Cannot be used together with pageid',
00209 'pageid' => 'Page ID of the page you want to delete. Cannot be used together with 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 'unwatch' => 'Remove the page from your watchlist',
00214 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
00215 );
00216 }
00217
00218 public function getDescription() {
00219 return array(
00220 'Delete a page.'
00221 );
00222 }
00223
00224 public function getPossibleErrors() {
00225 return array_merge( parent::getPossibleErrors(), array(
00226 array( 'invalidtitle', 'title' ),
00227 array( 'nosuchpageid', 'pageid' ),
00228 array( 'notanarticle' ),
00229 array( 'hookaborted', 'error' ),
00230 ) );
00231 }
00232
00233 public function needsToken() {
00234 return true;
00235 }
00236
00237 public function getTokenSalt() {
00238 return '';
00239 }
00240
00241 protected function getExamples() {
00242 return array(
00243 'api.php?action=delete&title=Main%20Page&token=123ABC',
00244 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
00245 );
00246 }
00247
00248 public function getVersion() {
00249 return __CLASS__ . ': $Id: ApiDelete.php 74217 2010-10-03 15:53:07Z reedy $';
00250 }
00251 }