Move ApiQueryRecentChanges::parseRCType to static method on RecentChange
authorkishanio <thobhanikishan@gmail.com>
Thu, 15 May 2014 10:18:28 +0000 (10:18 +0000)
committerkishanio <thobhanikishan@gmail.com>
Sat, 17 May 2014 15:07:01 +0000 (15:07 +0000)
Prior parseRCType was an private method in includes/api/ApiQueryRecentChanges and also
duplicated in includes/api/ApiQueryWatchList. This method has been made static and moved
to includes/changes/RecentChange.php.

Bug: 65071
Change-Id: Ic911fbcf9411c782685c4f956f8522051f2517f0

includes/api/ApiQueryRecentChanges.php
includes/api/ApiQueryWatchlist.php
includes/changes/RecentChange.php

index f962ba0..6428d15 100644 (file)
@@ -174,7 +174,11 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
                $this->addWhereFld( 'rc_namespace', $params['namespace'] );
 
                if ( !is_null( $params['type'] ) ) {
-                       $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
+                       try {
+                               $this->addWhereFld( 'rc_type', RecentChange::parseToRCType( $params['type'] ) );
+                       } catch ( MWException $e ) {
+                               ApiBase::dieDebug( __METHOD__, $e->getMessage() );
+                       }
                }
 
                if ( !is_null( $params['show'] ) ) {
@@ -414,30 +418,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
                $vals = array();
 
                $type = intval( $row->rc_type );
-
-               /* Determine what kind of change this was. */
-               switch ( $type ) {
-                       case RC_EDIT:
-                               $vals['type'] = 'edit';
-                               break;
-                       case RC_NEW:
-                               $vals['type'] = 'new';
-                               break;
-                       case RC_MOVE:
-                               $vals['type'] = 'move';
-                               break;
-                       case RC_LOG:
-                               $vals['type'] = 'log';
-                               break;
-                       case RC_EXTERNAL:
-                               $vals['type'] = 'external';
-                               break;
-                       case RC_MOVE_OVER_REDIRECT:
-                               $vals['type'] = 'move over redirect';
-                               break;
-                       default:
-                               $vals['type'] = $type;
-               }
+               $vals['type'] = RecentChange::parseFromRCType( $type );
 
                $anyHidden = false;
 
@@ -607,30 +588,6 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
                return $vals;
        }
 
-       private function parseRCType( $type ) {
-               if ( is_array( $type ) ) {
-                       $retval = array();
-                       foreach ( $type as $t ) {
-                               $retval[] = $this->parseRCType( $t );
-                       }
-
-                       return $retval;
-               }
-
-               switch ( $type ) {
-                       case 'edit':
-                               return RC_EDIT;
-                       case 'new':
-                               return RC_NEW;
-                       case 'log':
-                               return RC_LOG;
-                       case 'external':
-                               return RC_EXTERNAL;
-                       default:
-                               ApiBase::dieDebug( __METHOD__, "Unknown type '$type'" );
-               }
-       }
-
        public function getCacheMode( $params ) {
                if ( isset( $params['show'] ) ) {
                        foreach ( $params['show'] as $show ) {
index 869faea..d15ccba 100644 (file)
@@ -194,7 +194,11 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                }
 
                if ( !is_null( $params['type'] ) ) {
-                       $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
+                       try {
+                               $this->addWhereFld( 'rc_type', RecentChange::parseToRCType( $params['type'] ) );
+                       } catch ( MWException $e ) {
+                               ApiBase::dieDebug( __METHOD__, $e->getMessage() );
+                       }
                }
 
                if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
@@ -292,33 +296,8 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
 
                /* Our output data. */
                $vals = array();
-
                $type = intval( $row->rc_type );
-
-               /* Determine what kind of change this was. */
-               switch ( $type ) {
-                       case RC_EDIT:
-                               $vals['type'] = 'edit';
-                               break;
-                       case RC_NEW:
-                               $vals['type'] = 'new';
-                               break;
-                       case RC_MOVE:
-                               $vals['type'] = 'move';
-                               break;
-                       case RC_LOG:
-                               $vals['type'] = 'log';
-                               break;
-                       case RC_EXTERNAL:
-                               $vals['type'] = 'external';
-                               break;
-                       case RC_MOVE_OVER_REDIRECT:
-                               $vals['type'] = 'move over redirect';
-                               break;
-                       default:
-                               $vals['type'] = $type;
-               }
-
+               $vals['type'] = RecentChange::parseFromRCType( $type );
                $anyHidden = false;
 
                /* Create a new entry in the result for the title. */
@@ -449,35 +428,6 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                return $vals;
        }
 
-       /** Copied from ApiQueryRecentChanges.
-        *
-        * @param string $type
-        * @return string
-        */
-       private function parseRCType( $type ) {
-               if ( is_array( $type ) ) {
-                       $retval = array();
-                       foreach ( $type as $t ) {
-                               $retval[] = $this->parseRCType( $t );
-                       }
-
-                       return $retval;
-               }
-
-               switch ( $type ) {
-                       case 'edit':
-                               return RC_EDIT;
-                       case 'new':
-                               return RC_NEW;
-                       case 'log':
-                               return RC_LOG;
-                       case 'external':
-                               return RC_EXTERNAL;
-                       default:
-                               ApiBase::dieDebug( __METHOD__, "Unknown type '$type'" );
-               }
-       }
-
        public function getAllowedParams() {
                return array(
                        'allrev' => false,
index f7beb0c..3a5a869 100644 (file)
@@ -102,6 +102,70 @@ class RecentChange {
                return $rc;
        }
 
+       /**
+        * Parsing text to RC_* constants
+        * @since 1.24
+        * @param string|array $type
+        * @throws MWException
+        * @return int|array RC_TYPE
+        */
+       public static function parseToRCType( $type ) {
+               if ( is_array( $type ) ) {
+                       $retval = array();
+                       foreach ( $type as $t ) {
+                               $retval[] = RecentChange::parseToRCType( $t );
+                       }
+
+                       return $retval;
+               }
+
+               switch ( $type ) {
+                       case 'edit':
+                               return RC_EDIT;
+                       case 'new':
+                               return RC_NEW;
+                       case 'log':
+                               return RC_LOG;
+                       case 'external':
+                               return RC_EXTERNAL;
+                       default:
+                               throw new MWException( "Unknown type '$type'" );
+               }
+       }
+
+       /**
+        * Parsing RC_* constants to human-readable test
+        * @since 1.24
+        * @param int $rc_type
+        * @return string $type
+        */
+       public static function parseFromRCType( $rcType ) {
+               switch ( $rcType ) {
+                       case RC_EDIT:
+                               $type = 'edit';
+                               break;
+                       case RC_NEW:
+                               $type = 'new';
+                               break;
+                       case RC_MOVE:
+                               $type = 'move';
+                               break;
+                       case RC_LOG:
+                               $type = 'log';
+                               break;
+                       case RC_EXTERNAL:
+                               $type = 'external';
+                               break;
+                       case RC_MOVE_OVER_REDIRECT:
+                               $type = 'move over redirect';
+                               break;
+                       default:
+                               $type = "$rcType";
+               }
+
+               return $type;
+       }
+
        /**
         * No uses left in Gerrit on 2013-11-19.
         * @deprecated since 1.22