"$something ? false : true" is silly.
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 6be53ad..81705d3 100644 (file)
@@ -1,13 +1,13 @@
 <?php
+/**
+ * Global functions used everywhere
+ * @file
+ */
 
 if ( !defined( 'MEDIAWIKI' ) ) {
        die( "This file is part of MediaWiki, it is not a valid entry point" );
 }
 
-/**
- * Global functions used everywhere
- */
-
 require_once dirname(__FILE__) . '/normal/UtfNormalUtil.php';
 
 // Hide compatibility functions from Doxygen
@@ -25,6 +25,7 @@ if( !function_exists('iconv') ) {
        # Assume will only ever use utf-8 and iso-8859-1.
        # This will *not* work in all circumstances.
        function iconv( $from, $to, $string ) {
+               if ( substr( $to, -8 ) == '//IGNORE' ) $to = substr( $to, 0, strlen( $to ) - 8 );
                if(strcasecmp( $from, $to ) == 0) return $string;
                if(strcasecmp( $from, 'utf-8' ) == 0) return utf8_decode( $string );
                if(strcasecmp( $to, 'utf-8' ) == 0) return utf8_encode( $string );
@@ -48,22 +49,22 @@ if ( !function_exists( 'mb_substr' ) ) {
                        $split = mb_substr_split_unicode( $str, intval( $start ) );
                        $str = substr( $str, $split );
                }
-               
+
                if( $count !== 'end' ) {
                        $split = mb_substr_split_unicode( $str, intval( $count ) );
                        $str = substr( $str, 0, $split );
                }
-               
+
                return $str;
        }
-       
+
        function mb_substr_split_unicode( $str, $splitPos ) {
                if( $splitPos == 0 ) {
                        return 0;
                }
-               
+
                $byteLen = strlen( $str );
-               
+
                if( $splitPos > 0 ) {
                        if( $splitPos > 256 ) {
                                // Optimize large string offsets by skipping ahead N bytes.
@@ -77,7 +78,7 @@ if ( !function_exists( 'mb_substr' ) ) {
                                $charPos = 0;
                                $bytePos = 0;
                        }
-                       
+
                        while( $charPos++ < $splitPos ) {
                                ++$bytePos;
                                // Move past any tail bytes
@@ -95,7 +96,7 @@ if ( !function_exists( 'mb_substr' ) ) {
                                        --$bytePos;
                        }
                }
-               
+
                return $bytePos;
        }
 }
@@ -163,12 +164,12 @@ if( !function_exists( 'mb_strrpos' ) ) {
                $ar = array();
                preg_match_all( '/'.$needle.'/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
 
-               if( isset( $ar[0] ) && count( $ar[0] ) > 0 && 
+               if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
                    isset( $ar[0][count($ar[0])-1][1] ) ) {
                        return $ar[0][count($ar[0])-1][1];
                } else {
                        return false;
-               } 
+               }
        }
 }
 
@@ -250,9 +251,9 @@ function wfRandom() {
  *
  * ;:@$!*(),/
  *
- * However, IIS7 redirects fail when the url contains a colon (Bug 22709), 
+ * However, IIS7 redirects fail when the url contains a colon (Bug 22709),
  * so no fancy : for IIS7.
- * 
+ *
  * %2F in the page titles seems to fatally break for some reason.
  *
  * @param $s String:
@@ -265,8 +266,8 @@ function wfUrlencode( $s ) {
                if (! isset($_SERVER['SERVER_SOFTWARE']) || ( strpos($_SERVER['SERVER_SOFTWARE'], "Microsoft-IIS/7") === false)) {
                        $needle[] = '%3A';
                }
-       }               
-       
+       }
+
        $s = urlencode( $s );
        $s = str_ireplace(
                $needle,
@@ -401,12 +402,13 @@ function wfLogDBError( $text ) {
 
 /**
  * Log to a file without getting "file size exceeded" signals.
- * 
- * Can also log to TCP or UDP with the syntax udp://host:port/prefix. This will 
+ *
+ * Can also log to TCP or UDP with the syntax udp://host:port/prefix. This will
  * send lines to the specified port, prefixed by the specified prefix and a space.
  */
 function wfErrorLog( $text, $file ) {
        if ( substr( $file, 0, 4 ) == 'udp:' ) {
+               # Needs the sockets extension
                if ( preg_match( '!^(tcp|udp):(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) {
                        // IPv6 bracketed host
                        $protocol = $m[1];
@@ -535,14 +537,14 @@ function wfGetLangObj( $langcode = false ) {
                # Great, we already have the object (hopefully)!
                return $langcode;
        }
-               
+
        global $wgContLang, $wgLanguageCode;
        if( $langcode === true || $langcode === $wgLanguageCode ) {
                # $langcode is the language code of the wikis content language object.
                # or it is a boolean and value is true
                return $wgContLang;
        }
-       
+
        global $wgLang;
        if( $langcode === false || $langcode === $wgLang->getCode() ) {
                # $langcode is the language code of user language object.
@@ -561,9 +563,16 @@ function wfGetLangObj( $langcode = false ) {
        return $wgContLang;
 }
 
+/**
+ * Use this instead of $wgContLang, when working with user interface.
+ * User interface is currently hard coded according to wiki content language
+ * in many ways, especially regarding to text direction. There is lots stuff
+ * to fix, hence this function to keep the old behaviour unless the global
+ * $wgBetterDirectionality is enabled (or removed when everything works).
+ */
 function wfUILang() {
        global $wgBetterDirectionality;
-       return wfGetLangObj( $wgBetterDirectionality ? false: true );
+       return wfGetLangObj( !$wgBetterDirectionality );
 }
 
 /**
@@ -576,7 +585,7 @@ function wfUILang() {
  *    defined in languages/Language.php
  *
  * This function also takes extra optional parameters (not
- * shown in the function definition), which can by used to
+ * shown in the function definition), which can be used to
  * insert variable text into the predefined message.
  */
 function wfMsg( $key ) {
@@ -704,10 +713,10 @@ function wfMsgWeirdKey( $key ) {
  * @return string
  */
 function wfMsgGetKey( $key, $useDB, $langCode = false, $transform = true ) {
-       global $wgContLang, $wgMessageCache;
+       global $wgMessageCache;
 
        wfRunHooks('NormalizeMessageKey', array(&$key, &$useDB, &$langCode, &$transform));
-       
+
        if ( !is_object( $wgMessageCache ) ) {
                throw new MWException( "Trying to get message before message cache is initialised" );
        }
@@ -1073,7 +1082,7 @@ function wfShowingResults( $offset, $limit ) {
  */
 function wfShowingResultsNum( $offset, $limit, $num ) {
        global $wgLang;
-       return wfMsgExt( 'showingresultsnum', array( 'parseinline' ), $wgLang->formatNum( $limit ), 
+       return wfMsgExt( 'showingresultsnum', array( 'parseinline' ), $wgLang->formatNum( $limit ),
                $wgLang->formatNum( $offset+1 ), $wgLang->formatNum( $num ) );
 }
 
@@ -1113,7 +1122,7 @@ function wfViewPrevNext( $offset, $limit, $link, $query = '', $atend = false ) {
                        $q .= '&'.$query;
                }
                $plink = '<a href="' . $title->escapeLocalUrl( $q ) . "\" title=\"{$pTitle}\" class=\"mw-prevlink\">{$prev}</a>";
-       } else { 
+       } else {
                $plink = $prev;
        }
        # Make 'next' link
@@ -1128,7 +1137,7 @@ function wfViewPrevNext( $offset, $limit, $link, $query = '', $atend = false ) {
                $nlink = '<a href="' . $title->escapeLocalUrl( $q ) . "\" title=\"{$nTitle}\" class=\"mw-nextlink\">{$next}</a>";
        }
        # Make links to set number of items per page
-       $nums = $wgLang->pipeList( array( 
+       $nums = $wgLang->pipeList( array(
                wfNumLink( $offset, 20, $title, $query ),
                wfNumLink( $offset, 50, $title, $query ),
                wfNumLink( $offset, 100, $title, $query ),
@@ -1147,9 +1156,9 @@ function wfViewPrevNext( $offset, $limit, $link, $query = '', $atend = false ) {
  */
 function wfNumLink( $offset, $limit, $title, $query = '' ) {
        global $wgLang;
-       if( $query == '' ) { 
+       if( $query == '' ) {
                $q = '';
-       } else { 
+       } else {
                $q = $query.'&';
        }
        $q .= "limit={$limit}&offset={$offset}";
@@ -1393,7 +1402,7 @@ function wfPurgeSquidServers ($urlArr) {
  * Windows doesn't recognise single-quotes in the shell, but the escapeshellarg()
  * function puts single quotes in regardless of OS.
  *
- * Also fixes the locale problems on Linux in PHP 5.2.6+ (bug backported to 
+ * Also fixes the locale problems on Linux in PHP 5.2.6+ (bug backported to
  * earlier distro releases of PHP)
  */
 function wfEscapeShellArg( ) {
@@ -1415,14 +1424,19 @@ function wfEscapeShellArg( ) {
                        // Double the backslashes before any double quotes. Escape the double quotes.
                        $tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
                        $arg = '';
-                       $delim = false;
+                       $iteration = 0;
                        foreach ( $tokens as $token ) {
-                               if ( $delim ) {
+                               if ( $iteration % 2 == 1 ) {
+                                       // Delimiter, a double quote preceded by zero or more slashes
                                        $arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
-                               } else {
+                               } else if ( $iteration % 4 == 2 ) {
+                                       // ^ in $token will be outside quotes, need to be escaped
+                                       $arg .= str_replace( '^', '^^', $token );
+                               } else { // $iteration % 4 == 0
+                                       // ^ in $token will appear inside double quotes, so leave as is
                                        $arg .= $token;
                                }
-                               $delim = !$delim;
+                               $iteration++;
                        }
                        // Double the backslashes before the end of the string, because
                        // we will soon add a quote
@@ -1512,7 +1526,7 @@ function wfDiff( $before, $after, $params = '-u' ) {
        if ($before == $after) {
                return '';
        }
-       
+
        global $wgDiff;
 
        # This check may also protect against code injection in
@@ -1531,14 +1545,14 @@ function wfDiff( $before, $after, $params = '-u' ) {
 
        fwrite( $oldtextFile, $before ); fclose( $oldtextFile );
        fwrite( $newtextFile, $after ); fclose( $newtextFile );
-       
+
        // Get the diff of the two files
        $cmd = "$wgDiff " . $params . ' ' .wfEscapeShellArg( $oldtextName, $newtextName );
-       
+
        $h = popen( $cmd, 'r' );
-       
+
        $diff = '';
-       
+
        do {
                $data = fread( $h, 8192 );
                if ( strlen( $data ) == 0 ) {
@@ -1546,12 +1560,12 @@ function wfDiff( $before, $after, $params = '-u' ) {
                }
                $diff .= $data;
        } while ( true );
-       
+
        // Clean up
        pclose( $h );
        unlink( $oldtextName );
        unlink( $newtextName );
-       
+
        // Kill the --- and +++ lines. They're not useful.
        $diff_lines = explode( "\n", $diff );
        if (strpos( $diff_lines[0], '---' ) === 0) {
@@ -1560,9 +1574,9 @@ function wfDiff( $before, $after, $params = '-u' ) {
        if (strpos( $diff_lines[1], '+++' ) === 0) {
                unset($diff_lines[1]);
        }
-       
+
        $diff = implode( "\n", $diff_lines );
-       
+
        return $diff;
 }
 
@@ -2055,7 +2069,7 @@ function wfGetNamespaceNotice() {
 }
 
 function wfGetSiteNotice() {
-       global $wgUser, $wgSiteNotice;
+       global $wgUser;
        $fname = 'wfGetSiteNotice';
        wfProfileIn( $fname );
        $siteNotice = '';
@@ -2095,9 +2109,10 @@ function &wfGetMimeMagic() {
  * we'll use sys_get_temp_dir(). The TMPDIR, TMP, and TEMP environment
  * variables are then checked in sequence, and if none are set /tmp is
  * returned as the generic Unix default.
+ * It is common to call it with tempnam().
  *
- * NOTE: When possible, use the tempfile() function to create temporary
- * files to avoid race conditions on file creation, etc.
+ * NOTE: When possible, use instead the tmpfile() function to create
+ * temporary files to avoid race conditions on file creation, etc.
  *
  * @return String
  */
@@ -2117,7 +2132,7 @@ function wfTempDir() {
 
 /**
  * Make directory, and make all parent directories if they don't exist
- * 
+ *
  * @param $dir String: full path to directory to create
  * @param $mode Integer: chmod value to use, default is $wgDirectoryMode
  * @param $caller String: optional caller param for debugging.
@@ -2370,8 +2385,8 @@ function wfShellExec( $cmd, &$retval=null ) {
                                $cmd = escapeshellarg( $script ) . " $time $mem $filesize " . escapeshellarg( $cmd );
                        }
                }
-       } elseif ( php_uname( 's' ) == 'Windows NT' && 
-               version_compare( PHP_VERSION, '5.3.0', '<' ) ) 
+       } elseif ( php_uname( 's' ) == 'Windows NT' &&
+               version_compare( PHP_VERSION, '5.3.0', '<' ) )
        {
                # This is a hack to work around PHP's flawed invocation of cmd.exe
                # http://news.php.net/php.internals/21796
@@ -2539,14 +2554,14 @@ function wfArrayMerge( $array1/* ... */ ) {
 /**
  * Merge arrays in the style of getUserPermissionsErrors, with duplicate removal
  * e.g.
- *     wfMergeErrorArrays( 
- *             array( array( 'x' ) ), 
- *             array( array( 'x', '2' ) ), 
- *             array( array( 'x' ) ), 
+ *     wfMergeErrorArrays(
+ *             array( array( 'x' ) ),
+ *             array( array( 'x', '2' ) ),
+ *             array( array( 'x' ) ),
  *             array( array( 'y') )
  *     );
  * returns:
- *             array( 
+ *             array(
  *             array( 'x', '2' ),
  *             array( 'x' ),
  *             array( 'y' )
@@ -2794,7 +2809,7 @@ function wfHttpOnlySafe() {
  * Initialise php session
  */
 function wfSetupSession() {
-       global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain, 
+       global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain,
                        $wgCookieSecure, $wgCookieHttpOnly, $wgSessionHandler;
        if( $wgSessionsInMemcached ) {
                require_once( 'MemcachedSessions.php' );
@@ -2972,7 +2987,7 @@ function &wfGetLBFactory() {
  *
  *     ignoreRedirect: If true, do not follow file redirects
  *
- *     private:        If true, return restricted (deleted) files if the current 
+ *     private:        If true, return restricted (deleted) files if the current
  *                     user is allowed to view them. Otherwise, such files will not
  *                     be found.
  *
@@ -3185,14 +3200,14 @@ function wfOut( $s ) {
 }
 
 /**
- * Count down from $n to zero on the terminal, with a one-second pause 
+ * Count down from $n to zero on the terminal, with a one-second pause
  * between showing each number. For use in command-line scripts.
  */
 function wfCountDown( $n ) {
        for ( $i = $n; $i >= 0; $i-- ) {
                if ( $i != $n ) {
                        echo str_repeat( "\x08", strlen( $i + 1 ) );
-               } 
+               }
                echo $i;
                flush();
                if ( $i ) {
@@ -3233,29 +3248,29 @@ function wfArrayInsertAfter( $array, $insert, $after ) {
        // Find the offset of the element to insert after.
        $keys = array_keys($array);
        $offsetByKey = array_flip( $keys );
-       
+
        $offset = $offsetByKey[$after];
-       
+
        // Insert at the specified offset
        $before = array_slice( $array, 0, $offset + 1, true );
        $after = array_slice( $array, $offset + 1, count($array)-$offset, true );
-       
+
        $output = $before + $insert + $after;
-       
+
        return $output;
 }
 
 /* Recursively converts the parameter (an object) to an array with the same data */
 function wfObjectToArray( $object, $recursive = true ) {
        $array = array();
-       foreach ( get_object_vars($object) as $key => $value ) {
-               if ( is_object($value) && $recursive ) {
+       foreach ( get_object_vars( $object ) as $key => $value ) {
+               if ( is_object( $value ) && $recursive ) {
                        $value = wfObjectToArray( $value );
                }
-               
+
                $array[$key] = $value;
        }
-       
+
        return $array;
 }
 
@@ -3263,8 +3278,7 @@ function wfObjectToArray( $object, $recursive = true ) {
  * Set PHP's memory limit to the larger of php.ini or $wgMemoryLimit;
  * @return Integer value memory was set to.
  */
-function wfMemoryLimit () {
+function wfMemoryLimit() {
        global $wgMemoryLimit;
        $memlimit = wfShorthandToInteger( ini_get( "memory_limit" ) );
        $conflimit = wfShorthandToInteger( $wgMemoryLimit );