* Changing PageArchive::undelete() and undeleteRevisions() to return false rather...
authorRoan Kattouw <catrope@users.mediawiki.org>
Fri, 18 Jan 2008 19:38:28 +0000 (19:38 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Fri, 18 Jan 2008 19:38:28 +0000 (19:38 +0000)
* Refactoring ApiUndelete to use ApiBase::dieUsageMsg()
* Adding new messages to ApiBase::$messageMap

includes/SpecialUndelete.php
includes/api/ApiBase.php
includes/api/ApiUndelete.php

index 954d8a9..c08a777 100644 (file)
@@ -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;
index 682dffa..400d0c5 100644 (file)
@@ -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"),
        );
        
        /**
index 56ce3c6..06a0ca8 100644 (file)
@@ -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];