Normalize "LEFT OUTER JOIN" to "LEFT JOIN" in SQL queries
[lhc/web/wiklou.git] / includes / api / ApiResult.php
index 6734740..c4a31c7 100644 (file)
@@ -18,6 +18,8 @@
  * @file
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * This class represents the result of the API operations.
  * It simply wraps a nested array structure, adding some functions to simplify
@@ -61,7 +63,7 @@ class ApiResult implements ApiSerializable {
         * probably wrong.
         * @since 1.25
         */
-       const NO_VALIDATE = 12;
+       const NO_VALIDATE = self::NO_SIZE_CHECK | 8;
 
        /**
         * Key for the 'indexed tag name' metadata item. Value is string.
@@ -287,12 +289,12 @@ class ApiResult implements ApiSerializable {
         * @param int $flags Zero or more OR-ed flags like OVERRIDE | ADD_ON_TOP.
         */
        public static function setValue( array &$arr, $name, $value, $flags = 0 ) {
-               if ( ( $flags & ApiResult::NO_VALIDATE ) !== ApiResult::NO_VALIDATE ) {
+               if ( ( $flags & self::NO_VALIDATE ) !== self::NO_VALIDATE ) {
                        $value = self::validateValue( $value );
                }
 
                if ( $name === null ) {
-                       if ( $flags & ApiResult::ADD_ON_TOP ) {
+                       if ( $flags & self::ADD_ON_TOP ) {
                                array_unshift( $arr, $value );
                        } else {
                                array_push( $arr, $value );
@@ -301,8 +303,8 @@ class ApiResult implements ApiSerializable {
                }
 
                $exists = isset( $arr[$name] );
-               if ( !$exists || ( $flags & ApiResult::OVERRIDE ) ) {
-                       if ( !$exists && ( $flags & ApiResult::ADD_ON_TOP ) ) {
+               if ( !$exists || ( $flags & self::OVERRIDE ) ) {
+                       if ( !$exists && ( $flags & self::ADD_ON_TOP ) ) {
                                $arr = [ $name => $value ] + $arr;
                        } else {
                                $arr[$name] = $value;
@@ -330,8 +332,6 @@ class ApiResult implements ApiSerializable {
         * @return array|mixed|string
         */
        private static function validateValue( $value ) {
-               global $wgContLang;
-
                if ( is_object( $value ) ) {
                        // Note we use is_callable() here instead of instanceof because
                        // ApiSerializable is an informal protocol (see docs there for details).
@@ -374,7 +374,7 @@ class ApiResult implements ApiSerializable {
                } elseif ( is_float( $value ) && !is_finite( $value ) ) {
                        throw new InvalidArgumentException( 'Cannot add non-finite floats to ApiResult' );
                } elseif ( is_string( $value ) ) {
-                       $value = $wgContLang->normalize( $value );
+                       $value = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $value );
                } elseif ( $value !== null && !is_scalar( $value ) ) {
                        $type = gettype( $value );
                        if ( is_resource( $value ) ) {
@@ -403,13 +403,13 @@ class ApiResult implements ApiSerializable {
         * @since 1.21 int $flags replaced boolean $override
         */
        public function addValue( $path, $name, $value, $flags = 0 ) {
-               $arr = &$this->path( $path, ( $flags & ApiResult::ADD_ON_TOP ) ? 'prepend' : 'append' );
+               $arr = &$this->path( $path, ( $flags & self::ADD_ON_TOP ) ? 'prepend' : 'append' );
 
-               if ( $this->checkingSize && !( $flags & ApiResult::NO_SIZE_CHECK ) ) {
+               if ( $this->checkingSize && !( $flags & self::NO_SIZE_CHECK ) ) {
                        // self::size needs the validated value. Then flag
                        // to not re-validate later.
                        $value = self::validateValue( $value );
-                       $flags |= ApiResult::NO_VALIDATE;
+                       $flags |= self::NO_VALIDATE;
 
                        $newsize = $this->size + self::size( $value );
                        if ( $this->maxSize !== false && $newsize > $this->maxSize ) {
@@ -459,7 +459,7 @@ class ApiResult implements ApiSerializable {
                        $name = array_pop( $path );
                }
                $ret = self::unsetValue( $this->path( $path, 'dummy' ), $name );
-               if ( $this->checkingSize && !( $flags & ApiResult::NO_SIZE_CHECK ) ) {
+               if ( $this->checkingSize && !( $flags & self::NO_SIZE_CHECK ) ) {
                        $newsize = $this->size - self::size( $ret );
                        $this->size = max( $newsize, 0 );
                }
@@ -511,7 +511,7 @@ class ApiResult implements ApiSerializable {
        public function addParsedLimit( $moduleName, $limit ) {
                // Add value, allowing overwriting
                $this->addValue( 'limits', $moduleName, $limit,
-                       ApiResult::OVERRIDE | ApiResult::NO_SIZE_CHECK );
+                       self::OVERRIDE | self::NO_SIZE_CHECK );
        }
 
        /**@}*/
@@ -551,7 +551,7 @@ class ApiResult implements ApiSerializable {
         * @param int $flags Zero or more OR-ed flags like OVERRIDE | ADD_ON_TOP.
         */
        public function addContentField( $path, $name, $flags = 0 ) {
-               $arr = &$this->path( $path, ( $flags & ApiResult::ADD_ON_TOP ) ? 'prepend' : 'append' );
+               $arr = &$this->path( $path, ( $flags & self::ADD_ON_TOP ) ? 'prepend' : 'append' );
                self::setContentField( $arr, $name, $flags );
        }
 
@@ -723,7 +723,7 @@ class ApiResult implements ApiSerializable {
         * @since 1.25
         * @param array &$arr
         * @param string $type See ApiResult::META_TYPE
-        * @param string $kvpKeyName See ApiResult::META_KVP_KEY_NAME
+        * @param string|null $kvpKeyName See ApiResult::META_KVP_KEY_NAME
         */
        public static function setArrayType( array &$arr, $type, $kvpKeyName = null ) {
                if ( !in_array( $type, [
@@ -742,7 +742,7 @@ class ApiResult implements ApiSerializable {
         * @since 1.25
         * @param array|string|null $path See ApiResult::addValue()
         * @param string $tag See ApiResult::META_TYPE
-        * @param string $kvpKeyName See ApiResult::META_KVP_KEY_NAME
+        * @param string|null $kvpKeyName See ApiResult::META_KVP_KEY_NAME
         */
        public function addArrayType( $path, $tag, $kvpKeyName = null ) {
                $arr = &$this->path( $path );
@@ -754,7 +754,7 @@ class ApiResult implements ApiSerializable {
         * @since 1.25
         * @param array &$arr
         * @param string $type See ApiResult::META_TYPE
-        * @param string $kvpKeyName See ApiResult::META_KVP_KEY_NAME
+        * @param string|null $kvpKeyName See ApiResult::META_KVP_KEY_NAME
         */
        public static function setArrayTypeRecursive( array &$arr, $type, $kvpKeyName = null ) {
                self::setArrayType( $arr, $type, $kvpKeyName );
@@ -770,7 +770,7 @@ class ApiResult implements ApiSerializable {
         * @since 1.25
         * @param array|string|null $path See ApiResult::addValue()
         * @param string $tag See ApiResult::META_TYPE
-        * @param string $kvpKeyName See ApiResult::META_KVP_KEY_NAME
+        * @param string|null $kvpKeyName See ApiResult::META_KVP_KEY_NAME
         */
        public function addArrayTypeRecursive( $path, $tag, $kvpKeyName = null ) {
                $arr = &$this->path( $path );
@@ -804,11 +804,11 @@ class ApiResult implements ApiSerializable {
         * @return array|object
         */
        protected static function applyTransformations( array $dataIn, array $transforms ) {
-               $strip = isset( $transforms['Strip'] ) ? $transforms['Strip'] : 'none';
+               $strip = $transforms['Strip'] ?? 'none';
                if ( $strip === 'base' ) {
                        $transforms['Strip'] = 'none';
                }
-               $transformTypes = isset( $transforms['Types'] ) ? $transforms['Types'] : null;
+               $transformTypes = $transforms['Types'] ?? null;
                if ( $transformTypes !== null && !is_array( $transformTypes ) ) {
                        throw new InvalidArgumentException( __METHOD__ . ':Value for "Types" must be an array' );
                }
@@ -954,9 +954,7 @@ class ApiResult implements ApiSerializable {
 
                                case 'kvp':
                                case 'BCkvp':
-                                       $key = isset( $metadata[self::META_KVP_KEY_NAME] )
-                                               ? $metadata[self::META_KVP_KEY_NAME]
-                                               : $transformTypes['ArmorKVP'];
+                                       $key = $metadata[self::META_KVP_KEY_NAME] ?? $transformTypes['ArmorKVP'];
                                        $valKey = isset( $transforms['BC'] ) ? '*' : 'value';
                                        $assocAsObject = !empty( $transformTypes['AssocAsObject'] );
                                        $merge = !empty( $metadata[self::META_KVP_MERGE] );
@@ -1052,7 +1050,7 @@ class ApiResult implements ApiSerializable {
         *
         * @since 1.25
         * @param array|object $data
-        * @param array &$metadata Store metadata here, if provided
+        * @param array|null &$metadata Store metadata here, if provided
         * @return array|object
         */
        public static function stripMetadataNonRecursive( $data, &$metadata = null ) {
@@ -1156,7 +1154,7 @@ class ApiResult implements ApiSerializable {
                $bools = [];
                foreach ( $vars as $k => $v ) {
                        if ( is_array( $v ) || is_object( $v ) ) {
-                               $vars[$k] = ApiResult::addMetadataToResultVars( (array)$v, is_object( $v ) );
+                               $vars[$k] = self::addMetadataToResultVars( (array)$v, is_object( $v ) );
                        } elseif ( is_bool( $v ) ) {
                                // Better here to use real bools even in BC formats
                                $bools[] = $k;
@@ -1176,22 +1174,22 @@ class ApiResult implements ApiSerializable {
                        // Get the list of keys we actually care about. Unfortunately, we can't support
                        // certain keys that conflict with ApiResult metadata.
                        $keys = array_diff( array_keys( $vars ), [
-                               ApiResult::META_TYPE, ApiResult::META_PRESERVE_KEYS, ApiResult::META_KVP_KEY_NAME,
-                               ApiResult::META_INDEXED_TAG_NAME, ApiResult::META_BC_BOOLS
+                               self::META_TYPE, self::META_PRESERVE_KEYS, self::META_KVP_KEY_NAME,
+                               self::META_INDEXED_TAG_NAME, self::META_BC_BOOLS
                        ] );
 
                        return [
-                               ApiResult::META_TYPE => 'kvp',
-                               ApiResult::META_KVP_KEY_NAME => 'key',
-                               ApiResult::META_PRESERVE_KEYS => $keys,
-                               ApiResult::META_BC_BOOLS => $bools,
-                               ApiResult::META_INDEXED_TAG_NAME => 'var',
+                               self::META_TYPE => 'kvp',
+                               self::META_KVP_KEY_NAME => 'key',
+                               self::META_PRESERVE_KEYS => $keys,
+                               self::META_BC_BOOLS => $bools,
+                               self::META_INDEXED_TAG_NAME => 'var',
                        ] + $vars;
                } else {
                        return [
-                               ApiResult::META_TYPE => 'array',
-                               ApiResult::META_BC_BOOLS => $bools,
-                               ApiResult::META_INDEXED_TAG_NAME => 'value',
+                               self::META_TYPE => 'array',
+                               self::META_BC_BOOLS => $bools,
+                               self::META_INDEXED_TAG_NAME => 'value',
                        ] + $vars;
                }
        }