Localisation updates for extension messages from Betawiki (2008-07-11 11:00 CEST)
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 3825f08..56a313f 100644 (file)
@@ -176,12 +176,16 @@ function wfDebug( $text, $logonly = false ) {
        global $wgOut, $wgDebugLogFile, $wgDebugComments, $wgProfileOnly, $wgDebugRawPage;
        static $recursion = 0;
 
+       static $cache = array(); // Cache of unoutputted messages
+
        # Check for raw action using $_GET not $wgRequest, since the latter might not be initialised yet
        if ( isset( $_GET['action'] ) && $_GET['action'] == 'raw' && !$wgDebugRawPage ) {
                return;
        }
 
        if ( $wgDebugComments && !$logonly ) {
+               $cache[] = $text;
+
                if ( !isset( $wgOut ) ) {
                        return;
                }
@@ -193,7 +197,10 @@ function wfDebug( $text, $logonly = false ) {
                        $wgOut->_unstub();
                        $recursion--;
                }
-               $wgOut->debug( $text );
+
+               // add the message and possible cached ones to the output
+               array_map( array( $wgOut, 'debug' ), $cache );
+               $cache = array();
        }
        if ( '' != $wgDebugLogFile && !$wgProfileOnly ) {
                # Strip unprintables; they can switch terminal modes when binary data
@@ -445,7 +452,7 @@ function wfMsgWeirdKey ( $key ) {
  * @param string $key
  * @param bool $useDB
  * @param string $langcode Code of the language to get the message for, or
- *                         behaves as a content language switch if it is a 
+ *                         behaves as a content language switch if it is a
  *                         boolean.
  * @return string
  * @private
@@ -453,6 +460,8 @@ function wfMsgWeirdKey ( $key ) {
 function wfMsgGetKey( $key, $useDB, $langCode = false, $transform = true ) {
        global $wgParser, $wgContLang, $wgMessageCache, $wgLang;
 
+       wfRunHooks('NormalizeMessageKey', array(&$key, &$useDB, &$langCode, &$transform));
+       
        # If $wgMessageCache isn't initialised yet, try to return something sensible.
        if( is_object( $wgMessageCache ) ) {
                $message = $wgMessageCache->get( $key, $useDB, $langCode );
@@ -587,7 +596,8 @@ function wfMsgExt( $key, $options ) {
                $langCode = $options['language'];
                $validCodes = array_keys( Language::getLanguageNames() );
                if( !in_array($options['language'], $validCodes) ) {
-                       $langCode = false;
+                       # Fallback to en, instead of whatever interface language we might have
+                       $langCode = 'en';
                }
        } else {
                $forContent = false;
@@ -989,100 +999,33 @@ function wfSetBit( &$dest, $bit, $state = true ) {
  */
 function wfArrayToCGI( $array1, $array2 = NULL )
 {
-    if ( !is_null( $array2 ) ) {
-        $array1 = $array1 + $array2;
-    }
-       
-    $cgi = '';
-    foreach ( $array1 as $key => $value ) {
-        if ( '' !== $value ) {
-            if ( '' != $cgi ) {
-                $cgi .= '&';
-            }
-            $cgi .= urlencode( $key ) . '=' . urlencode( $value );
-        }
-    }
-    return $cgi;
-}
-/**
- * Build a query string from complex data input and return.
- * 
- * @param mixed $query The query data to build the string from
- * @param string $prefix A prefix to add to numeric keys to make them valid
- * @param string $dataKey A key name to use on the data
- * @return string The query string
- */
-function wfBuildQuery( $query, $prefix = null, $dataKey = null ) {
-       # Passthrough strings and blank data
-       if( is_null($query) || is_string($query) ) return (string) $query;
-       
-       # Recursively build the query.
-       $data = array();
-       $keyNum = 0;
-       foreach( (array) $query as $key => $value ) {
-               if( is_int($key) && $prefix != null ) {
-                       # Prefix numeric keys when given a prefix.
-                       $key = "{$prefix}{$key}";
-               }
-               if( isset($dataKey) && $dataKey != '' ) {
-                       # Add array type data to keys when needed
-                       # but don't add the key inside the [] when they are sequential
-                       if( $keyNum === $key ) $key = "{$dataKey}[]"; 
-                       else $key = "{$dataKey}[{$key}]";
-                       
-               }
-               # Push the data onto the end of the array recursing if needed.
-               array_push($data,
-                       is_array($value) || is_object($value)
-                       ? wfBuildQuery($value,null,$key)
-                       : urlencode($key)."=".urlencode($value) );
-               $keyNum++;
-       };
-       # Implode and return.
-       return implode('&', $data);
-};
-
-/**
- * Parse a query string into an array.
- * 
- * @param mixed $query The query string to parse
- * @return array The array data for the query
- */
-function wfParseQuery( $query ) {
-       # Passthrough non-strings.
-       if( !is_string($query) ) return $query;
-       
-       $data = array();
-       # Separate all name-value pairs
-       $pairs = explode('&', $query);
-       foreach($pairs as $pair) {
-               # Pull out the names and the values
-               list($name, $value) = explode('=', $pair, 2);
-               # Decode the variable name and look for arrays
-               $m = array();
-               $name = urldecode($name);
-               if( preg_match('/^(.*?)((\[.*?\])+)$/S', $name, $m) !== 0 ) {
-                       $name  = $m[1];
-                       $braces = $m[2];
-                       $indexes = explode( '][', substr( $braces, 1, -1 ) );
-                       
-                       if(!isset($data[$name])) $data[$name] = array();
-                       $recursive =& $data[$name];
-                       foreach( $indexes as $index ) {
-                               if( $index != "" ) {
-                                       if(!isset($recursive[$index])) $recursive[$index] = array();
-                                       $recursive =& $recursive[$index];
-                               } else {
-                                       $i = array_push( $recursive, array() ) - 1;
-                                       $recursive =& $recursive[$i];
+       if ( !is_null( $array2 ) ) {
+               $array1 = $array1 + $array2;
+       }
+
+       $cgi = '';
+       foreach ( $array1 as $key => $value ) {
+               if ( '' !== $value ) {
+                       if ( '' != $cgi ) {
+                               $cgi .= '&';
+                       }
+                       if(is_array($value))
+                       {
+                               $firstTime = true;
+                               foreach($value as $v)
+                               {
+                                       $cgi .= ($firstTime ? '' : '&') .
+                                               urlencode( $key . '[]' ) . '=' .
+                                               urlencode( $v );
+                                       $firstTime = false;
                                }
                        }
-                       $recursive = urldecode($value);
-               } else {
-                       $data[$name] = urldecode($value);
+                       else
+                               $cgi .= urlencode( $key ) . '=' .
+                                       urlencode( $value );
                }
        }
-       return $data;
+       return $cgi;
 }
 
 /**
@@ -1340,7 +1283,7 @@ function wfClearOutputBuffers() {
 function wfAcceptToPrefs( $accept, $def = '*/*' ) {
        # No arg means accept anything (per HTTP spec)
        if( !$accept ) {
-               return array( $def => 1 );
+               return array( $def => 1.0 );
        }
 
        $prefs = array();
@@ -1349,12 +1292,12 @@ function wfAcceptToPrefs( $accept, $def = '*/*' ) {
 
        foreach( $parts as $part ) {
                # FIXME: doesn't deal with params like 'text/html; level=1'
-               @list( $value, $qpart ) = explode( ';', $part );
+               @list( $value, $qpart ) = explode( ';', trim( $part ) );
                $match = array();
                if( !isset( $qpart ) ) {
-                       $prefs[$value] = 1;
+                       $prefs[$value] = 1.0;
                } elseif( preg_match( '/q\s*=\s*(\d*\.\d+)/', $qpart, $match ) ) {
-                       $prefs[$value] = $match[1];
+                       $prefs[$value] = floatval($match[1]);
                }
        }
 
@@ -1772,7 +1715,54 @@ function wfMkdirParents( $fullDir, $mode = 0777 ) {
                return true;
        if( file_exists( $fullDir ) )
                return true;
-       return mkdir( str_replace( '/', DIRECTORY_SEPARATOR, $fullDir ), $mode, true );
+
+       # Go back through the paths to find the first directory that exists
+       $currentDir = $fullDir;
+       $createList = array();
+       while ( strval( $currentDir ) !== '' && !file_exists( $currentDir ) ) {
+               # Strip trailing slashes
+               $currentDir = rtrim( $currentDir, '/\\' );
+
+               # Add to create list
+               $createList[] = $currentDir;
+
+               # Find next delimiter searching from the end
+               $p = max( strrpos( $currentDir, '/' ), strrpos( $currentDir, '\\' ) );
+               if ( $p === false ) {
+                       $currentDir = false;
+               } else {
+                       $currentDir = substr( $currentDir, 0, $p );
+               }
+       }
+
+       if ( count( $createList ) == 0 ) {
+               # Directory specified already exists
+               return true;
+       } elseif ( $currentDir === false ) {
+               # Went all the way back to root and it apparently doesn't exist
+               wfDebugLog( 'mkdir', "Root doesn't exist?\n" );
+               return false;
+       }
+       # Now go forward creating directories
+       $createList = array_reverse( $createList );
+
+       # Is the parent directory writable?
+       if ( $currentDir === '' ) {
+               $currentDir = '/';
+       }
+       if ( !is_writable( $currentDir ) ) {
+               wfDebugLog( 'mkdir', "Not writable: $currentDir\n" );
+               return false;
+       }
+
+       foreach ( $createList as $dir ) {
+               # use chmod to override the umask, as suggested by the PHP manual
+               if ( !mkdir( $dir, $mode ) || !chmod( $dir, $mode ) ) {
+                       wfDebugLog( 'mkdir', "Unable to create directory $dir\n" );
+                       return false;
+               }
+       }
+       return true;
 }
 
 /**
@@ -1819,15 +1809,12 @@ function wfPercent( $nr, $acc = 2, $round = true ) {
  * @param string $userid ID of the user
  * @param string $password Password of the user
  * @return string Hashed password
+ * @deprecated Use User::crypt() or User::oldCrypt() instead
  */
 function wfEncryptPassword( $userid, $password ) {
-       global $wgPasswordSalt;
-       $p = md5( $password);
-
-       if($wgPasswordSalt)
-               return md5( "{$userid}-{$p}" );
-       else
-               return $p;
+       wfDeprecated(__FUNCTION__);
+       # Just wrap around User::oldCrypt()
+       return User::oldCrypt($password, $userid);
 }
 
 /**
@@ -2309,12 +2296,20 @@ function wfCreateObject( $name, $p ){
 }
 
 /**
- * Aliases for modularized functions
+ * Alias for modularized function
+ * @deprecated Use Http::get() instead
  */
 function wfGetHTTP( $url, $timeout = 'default' ) {
+       wfDeprecated(__FUNCTION__);
        return Http::get( $url, $timeout );
 }
+
+/**
+ * Alias for modularized function
+ * @deprecated Use Http::isLocalURL() instead
+ */
 function wfIsLocalURL( $url ) {
+       wfDeprecated(__FUNCTION__);
        return Http::isLocalURL( $url );
 }
 
@@ -2322,7 +2317,7 @@ function wfHttpOnlySafe() {
        global $wgHttpOnlyBlacklist;
        if( !version_compare("5.2", PHP_VERSION, "<") )
                return false;
-       
+
        if( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
                foreach( $wgHttpOnlyBlacklist as $regex ) {
                        if( preg_match( $regex, $_SERVER['HTTP_USER_AGENT'] ) ) {
@@ -2330,7 +2325,7 @@ function wfHttpOnlySafe() {
                        }
                }
        }
-       
+
        return true;
 }
 
@@ -2458,7 +2453,7 @@ function wfSplitWikiID( $wiki ) {
 }
 
 /*
- * Get a Database object
+ * Get a Database object.
  * @param integer $db Index of the connection to get. May be DB_MASTER for the
  *                master (for write queries), DB_SLAVE for potentially lagged
  *                read queries, or an integer >= 0 for a particular server.
@@ -2468,6 +2463,10 @@ function wfSplitWikiID( $wiki ) {
  *              in one group.
  *
  * @param string $wiki The wiki ID, or false for the current wiki
+ *
+ * Note: multiple calls to wfGetDB(DB_SLAVE) during the course of one request
+ * will always return the same object, unless the underlying connection or load
+ * balancer is manually destroyed.
  */
 function &wfGetDB( $db = DB_LAST, $groups = array(), $wiki = false ) {
        return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
@@ -2566,6 +2565,10 @@ function wfLoadExtensionMessages( $extensionName, $langcode = false ) {
                $loaded[$extensionName] = array();
        }
 
+       if ( !isset($wgExtensionMessagesFiles[$extensionName]) ) {
+               throw new MWException( "Messages file for extensions $extensionName is not defined" );
+       }
+
        if( !$langcode && !array_key_exists( '*', $loaded[$extensionName] ) ) {
                # Just do en, content language and user language.
                $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], false );
@@ -2677,8 +2680,18 @@ function wfWaitForSlaves( $maxLag ) {
 /** Generate a random 32-character hexadecimal token.
  * @param mixed $salt Some sort of salt, if necessary, to add to random characters before hashing.
  */
- function wfGenerateToken( $salt = '' ) {
+function wfGenerateToken( $salt = '' ) {
        $salt = serialize($salt);
 
        return md5( mt_rand( 0, 0x7fffffff ) . $salt );
 }
+
+/**
+ * Replace all invalid characters with -
+ * @param mixed $title Filename to process
+ */
+function wfStripIllegalFilenameChars( $name ) {
+       $name = wfBaseName( $name );
+       $name = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $name );
+       return $name;
+}