Merge "Improve test coverage for ApiBase.php"
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index 62d73f4..7fafa1f 100644 (file)
@@ -692,7 +692,7 @@ abstract class ApiBase extends ContextSource {
         * Set the continuation manager
         * @param ApiContinuationManager|null $manager
         */
-       public function setContinuationManager( $manager ) {
+       public function setContinuationManager( ApiContinuationManager $manager = null ) {
                // Main module has setContinuationManager() method overridden
                // Safety - avoid infinite loop:
                if ( $this->isMain() ) {
@@ -897,7 +897,7 @@ abstract class ApiBase extends ContextSource {
 
                if ( $badParams ) {
                        $this->dieWithError(
-                               [ 'apierror-mustpostparams', join( ', ', $badParams ), count( $badParams ) ]
+                               [ 'apierror-mustpostparams', implode( ', ', $badParams ), count( $badParams ) ]
                        );
                }
        }
@@ -1129,8 +1129,8 @@ abstract class ApiBase extends ContextSource {
                                ) {
                                        $type = array_merge( $type, $paramSettings[self::PARAM_EXTRA_NAMESPACES] );
                                }
-                               // By default, namespace parameters allow ALL_DEFAULT_STRING to be used to specify
-                               // all namespaces.
+                               // Namespace parameters allow ALL_DEFAULT_STRING to be used to
+                               // specify all namespaces irrespective of PARAM_ALL.
                                $allowAll = true;
                        }
                        if ( isset( $value ) && $type == 'submodule' ) {
@@ -1152,7 +1152,7 @@ abstract class ApiBase extends ContextSource {
                                if ( $multi ) {
                                        // This loses the potential $wgContLang->checkTitleEncoding() transformation
                                        // done by WebRequest for $_GET. Let's call that a feature.
-                                       $value = join( "\x1f", $request->normalizeUnicode( explode( "\x1f", $rawValue ) ) );
+                                       $value = implode( "\x1f", $request->normalizeUnicode( explode( "\x1f", $rawValue ) ) );
                                } else {
                                        $this->dieWithError( 'apierror-badvalue-notmultivalue', 'badvalue_notmultivalue' );
                                }
@@ -1436,22 +1436,15 @@ abstract class ApiBase extends ContextSource {
                                return $value;
                        }
 
-                       if ( is_array( $allowedValues ) ) {
-                               $values = array_map( function ( $v ) {
-                                       return '<kbd>' . wfEscapeWikiText( $v ) . '</kbd>';
-                               }, $allowedValues );
-                               $this->dieWithError( [
-                                       'apierror-multival-only-one-of',
-                                       $valueName,
-                                       Message::listParam( $values ),
-                                       count( $values ),
-                               ], "multival_$valueName" );
-                       } else {
-                               $this->dieWithError( [
-                                       'apierror-multival-only-one',
-                                       $valueName,
-                               ], "multival_$valueName" );
-                       }
+                       $values = array_map( function ( $v ) {
+                               return '<kbd>' . wfEscapeWikiText( $v ) . '</kbd>';
+                       }, $allowedValues );
+                       $this->dieWithError( [
+                               'apierror-multival-only-one-of',
+                               $valueName,
+                               Message::listParam( $values ),
+                               count( $values ),
+                       ], "multival_$valueName" );
                }
 
                if ( is_array( $allowedValues ) ) {
@@ -1537,7 +1530,7 @@ abstract class ApiBase extends ContextSource {
        }
 
        /**
-        * Validate and normalize of parameters of type 'timestamp'
+        * Validate and normalize parameters of type 'timestamp'
         * @param string $value Parameter value
         * @param string $encParamName Parameter name
         * @return string Validated and normalized parameter
@@ -1559,15 +1552,15 @@ abstract class ApiBase extends ContextSource {
                        return wfTimestamp( TS_MW );
                }
 
-               $unixTimestamp = wfTimestamp( TS_UNIX, $value );
-               if ( $unixTimestamp === false ) {
+               $timestamp = wfTimestamp( TS_MW, $value );
+               if ( $timestamp === false ) {
                        $this->dieWithError(
                                [ 'apierror-badtimestamp', $encParamName, wfEscapeWikiText( $value ) ],
                                "badtimestamp_{$encParamName}"
                        );
                }
 
-               return wfTimestamp( TS_MW, $unixTimestamp );
+               return $timestamp;
        }
 
        /**
@@ -1609,21 +1602,42 @@ abstract class ApiBase extends ContextSource {
        }
 
        /**
-        * Validate and normalize of parameters of type 'user'
+        * Validate and normalize parameters of type 'user'
         * @param string $value Parameter value
         * @param string $encParamName Parameter name
         * @return string Validated and normalized parameter
         */
        private function validateUser( $value, $encParamName ) {
-               $title = Title::makeTitleSafe( NS_USER, $value );
-               if ( $title === null || $title->hasFragment() ) {
+               if ( ExternalUserNames::isExternal( $value ) && User::newFromName( $value, false ) ) {
+                       return $value;
+               }
+
+               $titleObj = Title::makeTitleSafe( NS_USER, $value );
+
+               if ( $titleObj ) {
+                       $value = $titleObj->getText();
+               }
+
+               if (
+                       !User::isValidUserName( $value ) &&
+                       // We allow ranges as well, for blocks.
+                       !IP::isIPAddress( $value ) &&
+                       // See comment for User::isIP.  We don't just call that function
+                       // here because it also returns true for things like
+                       // 300.300.300.300 that are neither valid usernames nor valid IP
+                       // addresses.
+                       !preg_match(
+                               '/^' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.xxx$/',
+                               $value
+                       )
+               ) {
                        $this->dieWithError(
                                [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $value ) ],
                                "baduser_{$encParamName}"
                        );
                }
 
-               return $title->getText();
+               return $value;
        }
 
        /**@}*/
@@ -1829,7 +1843,7 @@ abstract class ApiBase extends ContextSource {
                $msgs = [ $this->msg( 'api-usage-mailinglist-ref' ) ];
                Hooks::run( 'ApiDeprecationHelp', [ &$msgs ] );
                if ( count( $msgs ) > 1 ) {
-                       $key = '$' . join( ' $', range( 1, count( $msgs ) ) );
+                       $key = '$' . implode( ' $', range( 1, count( $msgs ) ) );
                        $msg = ( new RawMessage( $key ) )->params( $msgs );
                } else {
                        $msg = reset( $msgs );
@@ -2461,7 +2475,7 @@ abstract class ApiBase extends ContextSource {
                                realpath( __DIR__ ) ?: __DIR__ => [
                                        'path' => $IP,
                                        'name' => 'MediaWiki',
-                                       'license-name' => 'GPL-2.0+',
+                                       'license-name' => 'GPL-2.0-or-later',
                                ],
                                realpath( "$IP/extensions" ) ?: "$IP/extensions" => null,
                                realpath( $extDir ) ?: $extDir => null,
@@ -2585,16 +2599,6 @@ abstract class ApiBase extends ContextSource {
                return false;
        }
 
-       /**
-        * @deprecated since 1.25, always returns empty string
-        * @param IDatabase|bool $db
-        * @return string
-        */
-       public function getModuleProfileName( $db = false ) {
-               wfDeprecated( __METHOD__, '1.25' );
-               return '';
-       }
-
        /**
         * @deprecated since 1.25
         */
@@ -2618,15 +2622,6 @@ abstract class ApiBase extends ContextSource {
                wfDeprecated( __METHOD__, '1.25' );
        }
 
-       /**
-        * @deprecated since 1.25, always returns 0
-        * @return float
-        */
-       public function getProfileTime() {
-               wfDeprecated( __METHOD__, '1.25' );
-               return 0;
-       }
-
        /**
         * @deprecated since 1.25
         */
@@ -2641,15 +2636,6 @@ abstract class ApiBase extends ContextSource {
                wfDeprecated( __METHOD__, '1.25' );
        }
 
-       /**
-        * @deprecated since 1.25, always returns 0
-        * @return float
-        */
-       public function getProfileDBTime() {
-               wfDeprecated( __METHOD__, '1.25' );
-               return 0;
-       }
-
        /**
         * Call wfTransactionalTimeLimit() if this request was POSTed
         * @since 1.26