* Use WikiPage instead of Article
* Create the WikiPage object directly in execute() and pass it to both delete() and deleteFile() * Reordered parameters of delete() and deleteFile() to be consistent and take both on the WikiPage and User objects (instead of relying on $wgUser); no callers outside of this class * Fixed deleteFile() to match the behaviour of FileDeleteForm with oldimage parameter * Pass the User object to getPermissionsError() * Factorised duplicated code * Added missing error code to ApiBase (and also a new one) * Added missing possible error codes (with a new one too)
This commit is contained in:
parent
ac1879e4d7
commit
2bb8c2ea4d
2 changed files with 55 additions and 46 deletions
|
|
@ -1148,6 +1148,8 @@ abstract class ApiBase extends ContextSource {
|
|||
'mustbeposted' => array( 'code' => 'mustbeposted', 'info' => "The \$1 module requires a POST request" ),
|
||||
'show' => array( 'code' => 'show', 'info' => 'Incorrect parameter - mutually exclusive values may not be supplied' ),
|
||||
'specialpage-cantexecute' => array( 'code' => 'specialpage-cantexecute', 'info' => "You don't have permission to view the results of this special page" ),
|
||||
'invalidoldimage' => array( 'code' => 'invalidoldimage', 'info' => 'The oldid parameter has invalid format' ),
|
||||
'nodeleteablefile' => array( 'code' => 'nodeleteablefile', 'info' => 'No such old version of the file' ),
|
||||
|
||||
// ApiEditPage messages
|
||||
'noimageredirect-anon' => array( 'code' => 'noimageredirect-anon', 'info' => "Anonymous users can't create image redirects" ),
|
||||
|
|
|
|||
|
|
@ -64,62 +64,61 @@ class ApiDelete extends ApiBase {
|
|||
}
|
||||
|
||||
$reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
|
||||
$pageObj = WikiPage::factory( $titleObj );
|
||||
$user = $this->getUser();
|
||||
|
||||
if ( $titleObj->getNamespace() == NS_FILE ) {
|
||||
$retval = self::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
|
||||
if ( count( $retval ) ) {
|
||||
$this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
|
||||
}
|
||||
$retval = self::deleteFile( $pageObj, $user, $params['token'], $params['oldimage'], $reason, false );
|
||||
} else {
|
||||
$articleObj = new Article( $titleObj );
|
||||
$retval = self::delete( $articleObj, $params['token'], $reason );
|
||||
|
||||
if ( count( $retval ) ) {
|
||||
$this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
|
||||
}
|
||||
|
||||
// Deprecated parameters
|
||||
if ( $params['watch'] ) {
|
||||
$watch = 'watch';
|
||||
} elseif ( $params['unwatch'] ) {
|
||||
$watch = 'unwatch';
|
||||
} else {
|
||||
$watch = $params['watchlist'];
|
||||
}
|
||||
$this->setWatch( $watch, $titleObj, 'watchdeletion' );
|
||||
$retval = self::delete( $pageObj, $user, $params['token'], $reason );
|
||||
}
|
||||
|
||||
if ( count( $retval ) ) {
|
||||
$this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
|
||||
}
|
||||
|
||||
// Deprecated parameters
|
||||
if ( $params['watch'] ) {
|
||||
$watch = 'watch';
|
||||
} elseif ( $params['unwatch'] ) {
|
||||
$watch = 'unwatch';
|
||||
} else {
|
||||
$watch = $params['watchlist'];
|
||||
}
|
||||
$this->setWatch( $watch, $titleObj, 'watchdeletion' );
|
||||
|
||||
$r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
|
||||
$this->getResult()->addValue( null, $this->getModuleName(), $r );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title Title
|
||||
* @param $user User doing the action
|
||||
* @param $token String
|
||||
* @return array
|
||||
*/
|
||||
private static function getPermissionsError( &$title, $token ) {
|
||||
global $wgUser;
|
||||
|
||||
private static function getPermissionsError( $title, $user, $token ) {
|
||||
// Check permissions
|
||||
return $title->getUserPermissionsErrors( 'delete', $wgUser );
|
||||
return $title->getUserPermissionsErrors( 'delete', $user );
|
||||
}
|
||||
|
||||
/**
|
||||
* We have our own delete() function, since Article.php's implementation is split in two phases
|
||||
*
|
||||
* @param $article Article object to work on
|
||||
* @param $page WikiPage object to work on
|
||||
* @param $user User doing the action
|
||||
* @param $token String: delete token (same as edit token)
|
||||
* @param $reason String: reason for the deletion. Autogenerated if NULL
|
||||
* @return Title::getUserPermissionsErrors()-like array
|
||||
*/
|
||||
public static function delete( &$article, $token, &$reason = null ) {
|
||||
global $wgUser;
|
||||
if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
|
||||
public static function delete( Page $page, User $user, $token, &$reason = null ) {
|
||||
if ( $page->isBigDeletion() && !$user->isAllowed( 'bigdelete' ) ) {
|
||||
global $wgDeleteRevisionsLimit;
|
||||
return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
|
||||
}
|
||||
$title = $article->getTitle();
|
||||
$errors = self::getPermissionsError( $title, $token );
|
||||
|
||||
$title = $page->getTitle();
|
||||
$errors = self::getPermissionsError( $title, $user, $token );
|
||||
if ( count( $errors ) ) {
|
||||
return $errors;
|
||||
}
|
||||
|
|
@ -129,54 +128,58 @@ class ApiDelete extends ApiBase {
|
|||
// Need to pass a throwaway variable because generateReason expects
|
||||
// a reference
|
||||
$hasHistory = false;
|
||||
$reason = $article->generateReason( $hasHistory );
|
||||
$reason = $page->getAutoDeleteReason( $hasHistory );
|
||||
if ( $reason === false ) {
|
||||
return array( array( 'cannotdelete' ) );
|
||||
return array( array( 'cannotdelete', $title->getPrefixedText() ) );
|
||||
}
|
||||
}
|
||||
|
||||
$error = '';
|
||||
// Luckily, Article.php provides a reusable delete function that does the hard work for us
|
||||
if ( $article->doDeleteArticle( $reason, false, 0, true, $error ) ) {
|
||||
if ( $page->doDeleteArticle( $reason, false, 0, true, $error ) ) {
|
||||
return array();
|
||||
} else {
|
||||
return array( array( 'cannotdelete', $article->getTitle()->getPrefixedText() ) );
|
||||
return array( array( 'cannotdelete', $title->getPrefixedText() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $page WikiPage object to work on
|
||||
* @param $user User doing the action
|
||||
* @param $token
|
||||
* @param $title Title
|
||||
* @param $oldimage
|
||||
* @param $reason
|
||||
* @param $suppress bool
|
||||
* @return \type|array|Title
|
||||
*/
|
||||
public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
|
||||
$errors = self::getPermissionsError( $title, $token );
|
||||
public static function deleteFile( Page $page, User $user, $token, $oldimage, &$reason = null, $suppress = false ) {
|
||||
$title = $page->getTitle();
|
||||
$errors = self::getPermissionsError( $title, $user, $token );
|
||||
if ( count( $errors ) ) {
|
||||
return $errors;
|
||||
}
|
||||
|
||||
if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
|
||||
return array( array( 'invalidoldimage' ) );
|
||||
$file = $page->getFile();
|
||||
if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
|
||||
return self::delete( $page, $user, $token, $reason );
|
||||
}
|
||||
|
||||
$file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
|
||||
$oldfile = false;
|
||||
|
||||
if ( $oldimage ) {
|
||||
if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
|
||||
return array( array( 'invalidoldimage' ) );
|
||||
}
|
||||
$oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
|
||||
if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
|
||||
return array( array( 'nodeleteablefile' ) );
|
||||
}
|
||||
} else {
|
||||
$oldfile = false;
|
||||
}
|
||||
|
||||
if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
|
||||
return self::delete( new Article( $title ), $token, $reason );
|
||||
}
|
||||
if ( is_null( $reason ) ) { // Log and RC don't like null reasons
|
||||
$reason = '';
|
||||
}
|
||||
$status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
|
||||
|
||||
if ( !$status->isGood() ) {
|
||||
return array( array( 'cannotdelete', $title->getPrefixedText() ) );
|
||||
}
|
||||
|
|
@ -247,6 +250,10 @@ class ApiDelete extends ApiBase {
|
|||
array( 'nosuchpageid', 'pageid' ),
|
||||
array( 'notanarticle' ),
|
||||
array( 'hookaborted', 'error' ),
|
||||
array( 'delete-toobig', 'limit' ),
|
||||
array( 'cannotdelete', 'title' ),
|
||||
array( 'invalidoldimage' ),
|
||||
array( 'nodeleteablefile' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue