From 917587f08e849ab23b565f77b19091acebe86a8b Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Mon, 19 May 2008 21:30:03 +0000 Subject: [PATCH] API: Merge file deletion code from ApiEdit_Vodafone branch. --- RELEASE-NOTES | 1 + includes/api/ApiDelete.php | 93 +++++++++++++++++++++++++++----------- 2 files changed, 67 insertions(+), 27 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index ed6494279e..21f5a5d373 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -344,6 +344,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 14022) Added usprop=registration and auprop=blockinfo * (bug 14021) Removed titles= support from list=backlinks (has been obsolete for ages) * (bug 13829) Expose parse tree via action=expandtemplates +* (bug 13606) Allow deletion of images === Languages updated in 1.13 === diff --git a/includes/api/ApiDelete.php b/includes/api/ApiDelete.php index 3fa5b022eb..45e7025ce5 100644 --- a/includes/api/ApiDelete.php +++ b/includes/api/ApiDelete.php @@ -64,22 +64,45 @@ class ApiDelete extends ApiBase { if(!$titleObj->exists()) $this->dieUsageMsg(array('notanarticle')); - $articleObj = new Article($titleObj); $reason = (isset($params['reason']) ? $params['reason'] : NULL); - $retval = self::delete($articleObj, $params['token'], $reason); - - if(!empty($retval)) - // We don't care about multiple errors, just report one of them - $this->dieUsageMsg(current($retval)); + if ($titleObj->getNamespace() == NS_IMAGE) { + $retval = self::deletefile($params['token'], $titleObj, $params['oldimage'], $reason, false); + if(!empty($retval)) + // We don't care about multiple errors, just report one of them + $this->dieUsageMsg(current($retval)); + } else { + $articleObj = new Article($titleObj); + $retval = self::delete($articleObj, $params['token'], $reason); + + if(!empty($retval)) + // We don't care about multiple errors, just report one of them + $this->dieUsageMsg(current($retval)); + + if($params['watch'] || $wgUser->getOption('watchdeletion')) + $articleObj->doWatch(); + else if($params['unwatch']) + $articleObj->doUnwatch(); + } - if($params['watch'] || $wgUser->getOption('watchdeletion')) - $articleObj->doWatch(); - else if($params['unwatch']) - $articleObj->doUnwatch(); $r = array('title' => $titleObj->getPrefixedText(), 'reason' => $reason); $this->getResult()->addValue(null, $this->getModuleName(), $r); } + private static function getPermissionsError(&$title, $token) { + global $wgUser; + // Check wiki readonly + if (wfReadOnly()) return array(array('readonlytext')); + + // Check permissions + $errors = $title->getUserPermissionsErrors('delete', $wgUser); + if (count($errors) > 0) return $errors; + + // Check token + if(!$wgUser->matchEditToken($token)) + return array(array('sessionfailure')); + return array(); + } + /** * We have our own delete() function, since Article.php's implementation is split in two phases * @@ -90,25 +113,14 @@ class ApiDelete extends ApiBase { */ public static function delete(&$article, $token, &$reason = NULL) { - global $wgUser; - - // Check permissions - $errors = $article->mTitle->getUserPermissionsErrors('delete', $wgUser); - if(!empty($errors)) - return $errors; - if(wfReadOnly()) - return array(array('readonlytext')); - if($wgUser->isBlocked()) - return array(array('blocked')); - - // Check token - if(!$wgUser->matchEditToken($token)) - return array(array('sessionfailure')); + $errors = self::getPermissionsError($article->getTitle(), $token); + if (count($errors)) return $errors; // Auto-generate a summary, if necessary if(is_null($reason)) { - $reason = $article->generateReason($hasHistory); + // $hasHistory = false; + $reason = $article->generateReason(false); if($reason === false) return array(array('cannotdelete')); } @@ -119,6 +131,31 @@ class ApiDelete extends ApiBase { return array(array('cannotdelete', $article->mTitle->getPrefixedText())); } + public static function deleteFile($token, &$title, $oldimage, &$reason = NULL, $suppress = false) + { + $errors = self::getPermissionsError($title, $token); + if (count($errors)) return $errors; + + if( $oldimage && !FileDeleteForm::isValidOldSpec($oldimage) ) + return array(array('invalidoldimage')); + + $file = wfFindFile($title); + $oldfile = false; + + if( $oldimage ) + $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage ); + + if( !FileDeleteForm::haveDeletableFile($file, $oldfile, $oldimage) ) + return array(array('nofile')); + + $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress ); + + if( !$status->isGood() ) + return array(array('cannotdelete', $title->getPrefixedText())); + + return array(); + } + public function mustBePosted() { return true; } public function getAllowedParams() { @@ -127,7 +164,8 @@ class ApiDelete extends ApiBase { 'token' => null, 'reason' => null, 'watch' => false, - 'unwatch' => false + 'unwatch' => false, + 'oldimage' => null ); } @@ -137,7 +175,8 @@ class ApiDelete extends ApiBase { 'token' => 'A delete token previously retrieved through prop=info', 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.', 'watch' => 'Add the page to your watchlist', - 'unwatch' => 'Remove the page from your watchlist' + 'unwatch' => 'Remove the page from your watchlist', + 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename' ); } -- 2.20.1