From 0ffedad161f316283c9958262a29eeace15e5082 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Fri, 18 Jan 2008 19:38:28 +0000 Subject: [PATCH] * Changing PageArchive::undelete() and undeleteRevisions() to return false rather than an error code * Refactoring ApiUndelete to use ApiBase::dieUsageMsg() * Adding new messages to ApiBase::$messageMap --- includes/SpecialUndelete.php | 19 +++++++++---------- includes/api/ApiBase.php | 2 ++ includes/api/ApiUndelete.php | 26 +++++++++----------------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/includes/SpecialUndelete.php b/includes/SpecialUndelete.php index 954d8a9635..c08a77701a 100644 --- a/includes/SpecialUndelete.php +++ b/includes/SpecialUndelete.php @@ -303,9 +303,6 @@ class PageArchive { return ($n > 0); } - const UNDELETE_NOTHINGRESTORED = 0; // No revisions could be restored - const UNDELETE_NOTAVAIL = -1; // Not all requested revisions are available - const UNDELETE_UNKNOWNERR = -2; // Unknown error /** * Restore the given (or all) text and file revisions for the page. * Once restored, the items will be removed from the archive tables. @@ -315,7 +312,8 @@ class PageArchive { * @param string $comment * @param array $fileVersions * - * @return array(number of revisions restored, number of file versions restored, log reason) on success or UNDELETE_* on failure + * @return array(number of file revisions restored, number of image revisions restored, log message) + * on success, false on failure */ function undelete( $timestamps, $comment = '', $fileVersions = array() ) { // If both the set of text revisions and file revisions are empty, @@ -335,8 +333,8 @@ class PageArchive { if( $restoreText ) { $textRestored = $this->undeleteRevisions( $timestamps ); - if($textRestored < 0) // It must be one of UNDELETE_* - return $textRestored; + if($textRestored === false) // It must be one of UNDELETE_* + return false; } else { $textRestored = 0; } @@ -357,7 +355,7 @@ class PageArchive { $wgContLang->formatNum( $filesRestored ) ); } else { wfDebug( "Undelete: nothing undeleted...\n" ); - return self::UNDELETE_NOTHINGRESTORED; + return false; } if( trim( $comment ) != '' ) @@ -376,10 +374,11 @@ class PageArchive { * @param string $comment * @param array $fileVersions * - * @return int number of revisions restored on success or UNDELETE_* on failure + * @return mixed number of revisions restored or false on failure */ private function undeleteRevisions( $timestamps ) { - if ( wfReadOnly() ) return 0; + if ( wfReadOnly() ) + return false; $restoreAll = empty( $timestamps ); @@ -444,7 +443,7 @@ class PageArchive { ); if( $dbw->numRows( $result ) < count( $timestamps ) ) { wfDebug( __METHOD__.": couldn't find all requested rows\n" ); - return self::UNDELETE_NOTAVAIL; + return false; } $revision = null; diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 682dffa518..400d0c51ed 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -616,6 +616,8 @@ abstract class ApiBase { 'unblock-notarget' => array('code' => 'notarget', 'info' => "Either the id or the user parameter must be set"), 'unblock-idanduser' => array('code' => 'idanduser', 'info' => "The id and user parameters can\'t be used together"), 'cantunblock' => array('code' => 'permissiondenied', 'info' => "You don't have permission to unblock users"), + 'cannotundelete' => array('code' => 'cantundelete', 'info' => "Couldn't undelete: the requested revisions may not exist, or may have been undeleted already"), + 'permdenied-undelete' => array('code' => 'permissiondenied', 'info' => "You don't have permission to restore deleted revisions"), ); /** diff --git a/includes/api/ApiUndelete.php b/includes/api/ApiUndelete.php index 56ce3c6668..06a0ca8cf3 100644 --- a/includes/api/ApiUndelete.php +++ b/includes/api/ApiUndelete.php @@ -43,22 +43,22 @@ class ApiUndelete extends ApiBase { $titleObj = NULL; if(!isset($params['title'])) - $this->dieUsage('The title parameter must be set', 'notitle'); + $this->dieUsageMsg(array('missingparam', 'title')); if(!isset($params['token'])) - $this->dieUsage('The token parameter must be set', 'notoken'); + $this->dieUsageMsg(array('missingparam', 'token')); if(!$wgUser->isAllowed('undelete')) - $this->dieUsage('You don\'t have permission to restore deleted revisions', 'permissiondenied'); + $this->dieUsageMsg(array('permdenied-undelete')); if($wgUser->isBlocked()) - $this->dieUsage('You have been blocked from editing', 'blocked'); + $this->dieUsageMsg(array('blockedtext')); if(wfReadOnly()) - $this->dieUsage('The wiki is in read-only mode', 'readonly'); + $this->dieUsageMsg(array('readonlytext')); if(!$wgUser->matchEditToken($params['token'])) - $this->dieUsage('Invalid token', 'badtoken'); + $this->dieUsageMsg(array('sessionfailure')); $titleObj = Title::newFromText($params['title']); if(!$titleObj) - $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle'); + $this->dieUsageMsg(array('invalidtitle', $params['title'])); // Convert timestamps if(!is_array($params['timestamps'])) @@ -71,17 +71,9 @@ class ApiUndelete extends ApiBase { $dbw->begin(); $retval = $pa->undelete((isset($params['timestamps']) ? $params['timestamps'] : array()), $params['reason']); if(!is_array($retval)) - switch($retval) - { - case PageArchive::UNDELETE_NOTHINGRESTORED: - $this->dieUsage('No revisions could be restored', 'norevs'); - case PageArchive::UNDELETE_NOTAVAIL: - $this->dieUsage('Not all requested revisions could be found', 'revsnotfound'); - case PageArchive::UNDELETE_UNKNOWNERR: - $this->dieUsage('Undeletion failed with unknown error', 'unknownerror'); - } + $this->dieUsageMsg(array('cannotundelete')); + $dbw->commit(); - $info['title'] = $titleObj->getPrefixedText(); $info['revisions'] = $retval[0]; $info['fileversions'] = $retval[1]; -- 2.20.1