Http::getProxy() method to get proxy configuration
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 8f2d43b..421cd90 100644 (file)
@@ -308,10 +308,15 @@ function wfMergeErrorArrays( /*...*/ ) {
        $out = array();
        foreach ( $args as $errors ) {
                foreach ( $errors as $params ) {
+                       $originalParams = $params;
+                       if ( $params[0] instanceof MessageSpecifier ) {
+                               $msg = $params[0];
+                               $params = array_merge( array( $msg->getKey() ), $msg->getParams() );
+                       }
                        # @todo FIXME: Sometimes get nested arrays for $params,
                        # which leads to E_NOTICEs
                        $spec = implode( "\t", $params );
-                       $out[$spec] = $params;
+                       $out[$spec] = $originalParams;
                }
        }
        return array_values( $out );
@@ -1355,24 +1360,14 @@ function wfReadOnlyReason() {
                return $readOnly;
        }
 
-       static $autoReadOnly = null;
-       if ( $autoReadOnly === null ) {
+       static $lbReadOnly = null;
+       if ( $lbReadOnly === null ) {
                // Callers use this method to be aware that data presented to a user
                // may be very stale and thus allowing submissions can be problematic.
-               try {
-                       if ( wfGetLB()->getLaggedSlaveMode() ) {
-                               $autoReadOnly = 'The database has been automatically locked ' .
-                                       'while the slave database servers catch up to the master';
-                       } else {
-                               $autoReadOnly = false;
-                       }
-               } catch ( DBConnectionError $e ) {
-                       $autoReadOnly = 'The database has been automatically locked ' .
-                               'until the slave database servers become available';
-               }
+               $lbReadOnly = wfGetLB()->getReadOnlyReason();
        }
 
-       return $autoReadOnly;
+       return $lbReadOnly;
 }
 
 /**
@@ -3346,97 +3341,7 @@ function wfRelativePath( $path, $from ) {
 function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1,
        $lowercase = true, $engine = 'auto'
 ) {
-       $input = (string)$input;
-       if (
-               $sourceBase < 2 ||
-               $sourceBase > 36 ||
-               $destBase < 2 ||
-               $destBase > 36 ||
-               $sourceBase != (int)$sourceBase ||
-               $destBase != (int)$destBase ||
-               $pad != (int)$pad ||
-               !preg_match(
-                       "/^[" . substr( '0123456789abcdefghijklmnopqrstuvwxyz', 0, $sourceBase ) . "]+$/i",
-                       $input
-               )
-       ) {
-               return false;
-       }
-
-       static $baseChars = array(
-               10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f',
-               16 => 'g', 17 => 'h', 18 => 'i', 19 => 'j', 20 => 'k', 21 => 'l',
-               22 => 'm', 23 => 'n', 24 => 'o', 25 => 'p', 26 => 'q', 27 => 'r',
-               28 => 's', 29 => 't', 30 => 'u', 31 => 'v', 32 => 'w', 33 => 'x',
-               34 => 'y', 35 => 'z',
-
-               '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5,
-               '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'a' => 10, 'b' => 11,
-               'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17,
-               'i' => 18, 'j' => 19, 'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23,
-               'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27, 's' => 28, 't' => 29,
-               'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35
-       );
-
-       if ( extension_loaded( 'gmp' ) && ( $engine == 'auto' || $engine == 'gmp' ) ) {
-               // Removing leading zeros works around broken base detection code in
-               // some PHP versions (see <https://bugs.php.net/bug.php?id=50175> and
-               // <https://bugs.php.net/bug.php?id=55398>).
-               $result = gmp_strval( gmp_init( ltrim( $input, '0' ) ?: '0', $sourceBase ), $destBase );
-       } elseif ( extension_loaded( 'bcmath' ) && ( $engine == 'auto' || $engine == 'bcmath' ) ) {
-               $decimal = '0';
-               foreach ( str_split( strtolower( $input ) ) as $char ) {
-                       $decimal = bcmul( $decimal, $sourceBase );
-                       $decimal = bcadd( $decimal, $baseChars[$char] );
-               }
-
-               // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
-               for ( $result = ''; bccomp( $decimal, 0 ); $decimal = bcdiv( $decimal, $destBase, 0 ) ) {
-                       $result .= $baseChars[bcmod( $decimal, $destBase )];
-               }
-               // @codingStandardsIgnoreEnd
-
-               $result = strrev( $result );
-       } else {
-               $inDigits = array();
-               foreach ( str_split( strtolower( $input ) ) as $char ) {
-                       $inDigits[] = $baseChars[$char];
-               }
-
-               // Iterate over the input, modulo-ing out an output digit
-               // at a time until input is gone.
-               $result = '';
-               while ( $inDigits ) {
-                       $work = 0;
-                       $workDigits = array();
-
-                       // Long division...
-                       foreach ( $inDigits as $digit ) {
-                               $work *= $sourceBase;
-                               $work += $digit;
-
-                               if ( $workDigits || $work >= $destBase ) {
-                                       $workDigits[] = (int)( $work / $destBase );
-                               }
-                               $work %= $destBase;
-                       }
-
-                       // All that division leaves us with a remainder,
-                       // which is conveniently our next output digit.
-                       $result .= $baseChars[$work];
-
-                       // And we continue!
-                       $inDigits = $workDigits;
-               }
-
-               $result = strrev( $result );
-       }
-
-       if ( !$lowercase ) {
-               $result = strtoupper( $result );
-       }
-
-       return str_pad( $result, $pad, '0', STR_PAD_LEFT );
+       return Wikimedia\base_convert( $input, $sourceBase, $destBase, $pad, $lowercase, $engine );
 }
 
 /**
@@ -3558,11 +3463,10 @@ function wfGetPrecompiledData( $name ) {
  * @return string
  */
 function wfMemcKey( /*...*/ ) {
-       global $wgCachePrefix;
-       $prefix = $wgCachePrefix === false ? wfWikiID() : $wgCachePrefix;
-       $args = func_get_args();
-       $key = $prefix . ':' . implode( ':', $args );
-       return strtr( $key, ' ', '_' );
+       return call_user_func_array(
+               array( ObjectCache::getLocalClusterInstance(), 'makeKey' ),
+               func_get_args()
+       );
 }
 
 /**
@@ -3577,13 +3481,11 @@ function wfMemcKey( /*...*/ ) {
  */
 function wfForeignMemcKey( $db, $prefix /*...*/ ) {
        $args = array_slice( func_get_args(), 2 );
-       if ( $prefix ) {
-               // Match wfWikiID() logic
-               $key = "$db-$prefix:" . implode( ':', $args );
-       } else {
-               $key = $db . ':' . implode( ':', $args );
-       }
-       return strtr( $key, ' ', '_' );
+       $keyspace = $prefix ? "$db-$prefix" : $db;
+       return call_user_func_array(
+               array( ObjectCache::getLocalClusterInstance(), 'makeKeyInternal' ),
+               array( $keyspace, $args )
+       );
 }
 
 /**
@@ -3598,9 +3500,10 @@ function wfForeignMemcKey( $db, $prefix /*...*/ ) {
  * @return string
  */
 function wfGlobalCacheKey( /*...*/ ) {
-       $args = func_get_args();
-       $key = 'global:' . implode( ':', $args );
-       return strtr( $key, ' ', '_' );
+       return call_user_func_array(
+               array( ObjectCache::getLocalClusterInstance(), 'makeGlobalKey' ),
+               func_get_args()
+       );
 }
 
 /**
@@ -3940,12 +3843,13 @@ function wfTransactionalTimeLimit() {
  * Converts shorthand byte notation to integer form
  *
  * @param string $string
+ * @param int $default Returned if $string is empty
  * @return int
  */
-function wfShorthandToInteger( $string = '' ) {
+function wfShorthandToInteger( $string = '', $default = -1 ) {
        $string = trim( $string );
        if ( $string === '' ) {
-               return -1;
+               return $default;
        }
        $last = $string[strlen( $string ) - 1];
        $val = intval( $string );
@@ -4111,7 +4015,7 @@ function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
                return $bad;
        }
 
-       $cache = ObjectCache::newAccelerator( 'hash' );
+       $cache = ObjectCache::getLocalServerInstance( 'hash' );
        $key = wfMemcKey( 'bad-image-list', ( $blacklist === null ) ? 'default' : md5( $blacklist ) );
        $badImages = $cache->get( $key );