Fixes for r88113 and some realted changes:
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 141bef8..0ff0f98 100644 (file)
@@ -50,7 +50,7 @@ if( !function_exists( 'mb_strpos' ) ) {
        function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
                return Fallback::mb_strpos( $haystack, $needle, $offset, $encoding );
        }
-       
+
 }
 
 if( !function_exists( 'mb_strrpos' ) ) {
@@ -316,11 +316,12 @@ function wfUrlencode( $s ) {
  * "days=7&limit=100". Options in the first array override options in the second.
  * Options set to "" will not be output.
  *
- * @param $array1 Array( String|Array )
- * @param $array2 Array( String|Array )
+ * @param $array1 Array ( String|Array )
+ * @param $array2 Array ( String|Array )
+ * @param $prefix String
  * @return String
  */
-function wfArrayToCGI( $array1, $array2 = null ) {
+function wfArrayToCGI( $array1, $array2 = null, $prefix = '' ) {
        if ( !is_null( $array2 ) ) {
                $array1 = $array1 + $array2;
        }
@@ -331,20 +332,25 @@ function wfArrayToCGI( $array1, $array2 = null ) {
                        if ( $cgi != '' ) {
                                $cgi .= '&';
                        }
+                       if ( $prefix !== '' ) {
+                               $key = $prefix . "[$key]";
+                       }
                        if ( is_array( $value ) ) {
                                $firstTime = true;
-                               foreach ( $value as $v ) {
-                                       $cgi .= ( $firstTime ? '' : '&') .
-                                               urlencode( $key . '[]' ) . '=' .
-                                               urlencode( $v );
+                               foreach ( $value as $k => $v ) {
+                                       $cgi .= $firstTime ? '' : '&';
+                                       if ( is_array( $v ) ) {
+                                               $cgi .= wfArrayToCGI( $v, null, $key . "[$k]" );
+                                       } else {
+                                               $cgi .= urlencode( $key . "[$k]" ) . '=' . urlencode( $v );
+                                       }
                                        $firstTime = false;
                                }
                        } else {
                                if ( is_object( $value ) ) {
                                        $value = $value->__toString();
                                }
-                               $cgi .= urlencode( $key ) . '=' .
-                                       urlencode( $value );
+                               $cgi .= urlencode( $key ) . '=' . urlencode( $value );
                        }
                }
        }
@@ -362,19 +368,34 @@ function wfArrayToCGI( $array1, $array2 = null ) {
  * @return array Array version of input
  */
 function wfCgiToArray( $query ) {
-       if( isset( $query[0] ) && $query[0] == '?' ) {
+       if ( isset( $query[0] ) && $query[0] == '?' ) {
                $query = substr( $query, 1 );
        }
        $bits = explode( '&', $query );
        $ret = array();
-       foreach( $bits as $bit ) {
-               if( $bit === '' ) {
+       foreach ( $bits as $bit ) {
+               if ( $bit === '' ) {
                        continue;
                }
                list( $key, $value ) = explode( '=', $bit );
                $key = urldecode( $key );
                $value = urldecode( $value );
-               $ret[$key] = $value;
+               if ( strpos( $key, '[' ) !== false ) {
+                       $keys = array_reverse( explode( '[', $key ) );
+                       $key = array_pop( $keys );
+                       $temp = $value;
+                       foreach ( $keys as $k ) {
+                               $k = substr( $k, 0, -1 );
+                               $temp = array( $k => $temp );
+                       }
+                       if ( isset( $ret[$key] ) ) {
+                               $ret[$key] = array_merge( $ret[$key], $temp );
+                       } else {
+                               $ret[$key] = $temp;
+                       }
+               } else {
+                       $ret[$key] = $value;
+               }
        }
        return $ret;
 }
@@ -490,7 +511,7 @@ function wfParseUrl( $url ) {
        /* Provide an empty host for eg. file:/// urls (see bug 28627) */
        if ( !isset( $bits['host'] ) ) {
                $bits['host'] = '';
-               
+
                /* parse_url loses the third / for file:///c:/ urls (but not on variants) */
                if ( substr( $bits['path'], 0, 1 ) !== '/' ) {
                        $bits['path'] = '/' . $bits['path'];
@@ -602,10 +623,10 @@ function wfIsDebugRawPage() {
        }
        # Check for raw action using $_GET not $wgRequest, since the latter might not be initialised yet
        if ( ( isset( $_GET['action'] ) && $_GET['action'] == 'raw' )
-               || ( 
-                       isset( $_SERVER['SCRIPT_NAME'] ) 
-                       && substr( $_SERVER['SCRIPT_NAME'], -8 ) == 'load.php' 
-               ) )     
+               || (
+                       isset( $_SERVER['SCRIPT_NAME'] )
+                       && substr( $_SERVER['SCRIPT_NAME'], -8 ) == 'load.php'
+               ) )
        {
                $cache = true;
        } else {
@@ -616,7 +637,7 @@ function wfIsDebugRawPage() {
 
 /**
  * Get microsecond timestamps for debug logs
- * 
+ *
  * @return string
  */
 function wfDebugTimer() {
@@ -953,7 +974,7 @@ function wfMessageFallback( /*...*/ ) {
 function wfMsg( $key ) {
        $args = func_get_args();
        array_shift( $args );
-       return wfMsgReal( $key, $args, true );
+       return wfMsgReal( $key, $args );
 }
 
 /**
@@ -1051,7 +1072,7 @@ function wfMsgReal( $key, $args, $useDB = true, $forContent = false, $transform
  * @param $transform Boolean: whether to parse magic words, etc.
  * @return string
  */
-function wfMsgGetKey( $key, $useDB, $langCode = false, $transform = true ) {
+function wfMsgGetKey( $key, $useDB = true, $langCode = false, $transform = true ) {
        wfRunHooks( 'NormalizeMessageKey', array( &$key, &$useDB, &$langCode, &$transform ) );
 
        $cache = MessageCache::singleton();
@@ -1106,7 +1127,7 @@ function wfMsgReplaceArgs( $message, $args ) {
 function wfMsgHtml( $key ) {
        $args = func_get_args();
        array_shift( $args );
-       return wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $key, true ) ), $args );
+       return wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $key ) ), $args );
 }
 
 /**
@@ -1124,7 +1145,7 @@ function wfMsgWikiHtml( $key ) {
        $args = func_get_args();
        array_shift( $args );
        return wfMsgReplaceArgs(
-               MessageCache::singleton()->parse( wfMsgGetKey( $key, true ), null, /* can't be set to false */ true )->getText(),
+               MessageCache::singleton()->parse( wfMsgGetKey( $key ), null, /* can't be set to false */ true )->getText(),
                $args );
 }
 
@@ -1256,7 +1277,7 @@ function wfDebugDieBacktrace( $msg = '' ) {
  * Fetch server name for use in error reporting etc.
  * Use real server name if available, so we know which machine
  * in a server farm generated the current page.
- * 
+ *
  * @return string
  */
 function wfHostname() {
@@ -2517,13 +2538,12 @@ function wfPercent( $nr, $acc = 2, $round = true ) {
 function in_string( $needle, $str, $insensitive = false ) {
        $func = 'strpos';
        if( $insensitive ) $func = 'stripos';
-       
+
        return $func( $str, $needle ) !== false;
 }
 
 function wfSpecialList( $page, $details ) {
-       global $wgContLang;
-       $details = $details ? ' ' . $wgContLang->getDirMark() . "($details)" : '';
+       $details = $details ? ' ' . wfUILang()->getDirMark() . "($details)" : '';
        return $page . $details;
 }
 
@@ -2976,11 +2996,11 @@ function wfSetupSession( $sessionId = false ) {
                        global $IP;
                        require_once( "$IP/includes/cache/MemcachedSessions.php" );
                }
-               session_set_save_handler( 'memsess_open', 'memsess_close', 'memsess_read', 
+               session_set_save_handler( 'memsess_open', 'memsess_close', 'memsess_read',
                        'memsess_write', 'memsess_destroy', 'memsess_gc' );
 
-               // It's necessary to register a shutdown function to call session_write_close(), 
-               // because by the time the request shutdown function for the session module is 
+               // It's necessary to register a shutdown function to call session_write_close(),
+               // because by the time the request shutdown function for the session module is
                // called, $wgMemc has already been destroyed. Shutdown functions registered
                // this way are called before object destruction.
                register_shutdown_function( 'memsess_write_close' );
@@ -3307,7 +3327,7 @@ function wfWarn( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) {
  * master position. Use this when updating very large numbers of rows, as
  * in maintenance scripts, to avoid causing too much lag.  Of course, this is
  * a no-op if there are no slaves.
- * 
+ *
  * @param $maxLag Integer (deprecated)
  * @param $wiki mixed Wiki identifier accepted by wfGetLB
  * @return null
@@ -3330,7 +3350,7 @@ function wfWaitForSlaves( $maxLag = false, $wiki = false ) {
 function wfOut( $s ) {
        wfDeprecated( __METHOD__ );
        global $wgCommandLineMode;
-       if ( $wgCommandLineMode && !defined( 'MEDIAWIKI_INSTALL' ) ) {
+       if ( $wgCommandLineMode ) {
                echo $s;
        } else {
                echo htmlspecialchars( $s );
@@ -3457,7 +3477,7 @@ function wfBCP47( $code ) {
        $codeBCP = array();
        foreach ( $codeSegment as $segNo => $seg ) {
                if ( count( $codeSegment ) > 0 ) {
-                       // when previous segment is x, it is a private segment and should be lc 
+                       // when previous segment is x, it is a private segment and should be lc
                        if( $segNo > 0 && strtolower( $codeSegment[($segNo - 1)] ) == 'x') {
                                $codeBCP[$segNo] = strtolower( $seg );
                        // ISO 3166 country code
@@ -3529,3 +3549,17 @@ function wfGetParserCacheStorage() {
 function wfRunHooks( $event, $args = array() ) {
        return Hooks::run( $event, $args );
 }
+
+/**
+ * Unserialize a string to a PHP value without throwing E_NOTICE. Simply a
+ * wrapper around unserialize()
+ *
+ * @param $data string The serialized string
+ * @return mixed
+ */
+function wfUnserialize( $data ) {
+       wfSuppressWarnings();
+       $result = unserialize( $data );
+       wfRestoreWarnings();
+       return $result;
+}