Localisation updates Cantonese, Chinese and Old/Late Time Chinese
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 61e9e62..a9bf015 100644 (file)
@@ -444,24 +444,36 @@ function wfMsgWeirdKey ( $key ) {
  * Fetch a message string value, but don't replace any keys yet.
  * @param string $key
  * @param bool $useDB
- * @param bool $forContent
+ * @param string $langcode Code of the language to get the message for, or
+ *                         behaves as a content language switch if it is a 
+ *                         boolean.
  * @return string
  * @private
  */
-function wfMsgGetKey( $key, $useDB, $forContent = false, $transform = true ) {
+function wfMsgGetKey( $key, $useDB, $langCode = false, $transform = true ) {
        global $wgParser, $wgContLang, $wgMessageCache, $wgLang;
 
        # If $wgMessageCache isn't initialised yet, try to return something sensible.
        if( is_object( $wgMessageCache ) ) {
-               $message = $wgMessageCache->get( $key, $useDB, $forContent );
+               $message = $wgMessageCache->get( $key, $useDB, $langCode );
                if ( $transform ) {
                        $message = $wgMessageCache->transform( $message );
                }
        } else {
-               if( $forContent ) {
+               if( $langCode === true ) {
                        $lang = &$wgContLang;
-               } else {
+               } elseif( $langCode === false ) {
                        $lang = &$wgLang;
+               } else {
+                       $validCodes = array_keys( Language::getLanguageNames() );
+                       if( in_array( $langCode, $validCodes ) ) {
+                               # $langcode corresponds to a valid language.
+                               $lang = Language::factory( $langCode );
+                       } else {
+                               # $langcode is a string, but not a valid language code; use content language.
+                               $lang =& $wgContLang;
+                               wfDebug( 'Invalid language code passed to wfMsgGetKey, falling back to content language.' );
+                       }
                }
 
                # MessageCache::get() does this already, Language::getMessage() doesn't
@@ -552,6 +564,8 @@ function wfMsgWikiHtml( $key ) {
  *  <i>replaceafter</i>: parameters are substituted after parsing or escaping
  *  <i>parsemag</i>: transform the message using magic phrases
  *  <i>content</i>: fetch message for content language instead of interface
+ *  <i>language</i>: language code to fetch message for (overriden by <i>content</i>), its behaviour
+ *                   with parser, parseinline and parsemag is undefined.
  * Behavior for conflicting options (e.g., parse+parseinline) is undefined.
  */
 function wfMsgExt( $key, $options ) {
@@ -565,12 +579,22 @@ function wfMsgExt( $key, $options ) {
                $options = array($options);
        }
 
-       $forContent = false;
        if( in_array('content', $options) ) {
                $forContent = true;
+               $langCode = true;
+       } elseif( array_key_exists('language', $options) ) {
+               $forContent = false;
+               $langCode = $options['language'];
+               $validCodes = array_keys( Language::getLanguageNames() );
+               if( !in_array($options['language'], $validCodes) ) {
+                       $langCode = false;
+               }
+       } else {
+               $forContent = false;
+               $langCode = false;
        }
 
-       $string = wfMsgGetKey( $key, /*DB*/true, $forContent, /*Transform*/false );
+       $string = wfMsgGetKey( $key, /*DB*/true, $langCode, /*Transform*/false );
 
        if( !in_array('replaceafter', $options) ) {
                $string = wfMsgReplaceArgs( $string, $args );
@@ -1443,41 +1467,35 @@ function wfTimestamp($outputtype=TS_UNIX,$ts=0) {
                $uts=time();
        } elseif (preg_match('/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D',$ts,$da)) {
                # TS_DB
-               $uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],
-                           (int)$da[2],(int)$da[3],(int)$da[1]);
        } elseif (preg_match('/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D',$ts,$da)) {
                # TS_EXIF
-               $uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],
-                       (int)$da[2],(int)$da[3],(int)$da[1]);
        } elseif (preg_match('/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D',$ts,$da)) {
                # TS_MW
-               $uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],
-                           (int)$da[2],(int)$da[3],(int)$da[1]);
-       } elseif (preg_match('/^(\d{1,13})$/D',$ts,$da)) {
+       } elseif (preg_match('/^\d{1,13}$/D',$ts)) {
                # TS_UNIX
                $uts = $ts;
-       } elseif (preg_match('/^(\d{1,2})-(...)-(\d\d(\d\d)?) (\d\d)\.(\d\d)\.(\d\d)/', $ts, $da)) {
+       } elseif (preg_match('/^\d{1,2}-...-\d\d(?:\d\d)? \d\d\.\d\d\.\d\d/', $ts)) {
                # TS_ORACLE
                $uts = strtotime(preg_replace('/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
                                str_replace("+00:00", "UTC", $ts)));
        } elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/', $ts, $da)) {
                # TS_ISO_8601
-               $uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],
-                       (int)$da[2],(int)$da[3],(int)$da[1]);
        } elseif (preg_match('/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)[\+\- ](\d\d)$/',$ts,$da)) {
                # TS_POSTGRES
-               $uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],
-               (int)$da[2],(int)$da[3],(int)$da[1]);
        } elseif (preg_match('/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d) GMT$/',$ts,$da)) {
                # TS_POSTGRES
-               $uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],
-               (int)$da[2],(int)$da[3],(int)$da[1]);
        } else {
                # Bogus value; fall back to the epoch...
                wfDebug("wfTimestamp() fed bogus time value: $outputtype; $ts\n");
                $uts = 0;
        }
 
+       if (count( $da ) ) {
+               // Warning! gmmktime() acts oddly if the month or day is set to 0
+               // We may want to handle that explicitly at some point
+               $uts=gmmktime((int)$da[4],(int)$da[5],(int)$da[6],
+                       (int)$da[2],(int)$da[3],(int)$da[1]);
+       }
 
        switch($outputtype) {
                case TS_UNIX:
@@ -2220,11 +2238,27 @@ function wfIsLocalURL( $url ) {
        return Http::isLocalURL( $url );
 }
 
+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'] ) ) {
+                               return false;
+                       }
+               }
+       }
+       
+       return true;
+}
+
 /**
  * Initialise php session
  */
 function wfSetupSession() {
-       global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain, $wgCookieSecure;
+       global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookieHttpOnly;
        if( $wgSessionsInMemcached ) {
                require_once( 'MemcachedSessions.php' );
        } elseif( 'files' != ini_get( 'session.save_handler' ) ) {
@@ -2232,7 +2266,21 @@ function wfSetupSession() {
                # application, it will end up failing. Try to recover.
                ini_set ( 'session.save_handler', 'files' );
        }
-       session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain, $wgCookieSecure);
+       $httpOnlySafe = wfHttpOnlySafe();
+       wfDebugLog( 'cookie',
+               'session_set_cookie_params: "' . implode( '", "',
+                       array(
+                               0,
+                               $wgCookiePath,
+                               $wgCookieDomain,
+                               $wgCookieSecure,
+                               $httpOnlySafe && $wgCookieHttpOnly ) ) . '"' );
+       if( $httpOnlySafe && $wgCookieHttpOnly ) {
+               session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $wgCookieHttpOnly );
+       } else {
+               // PHP 5.1 throws warnings if you pass the HttpOnly parameter for 5.2.
+               session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
+       }
        session_cache_limiter( 'private, must-revalidate' );
        wfSuppressWarnings();
        session_start();
@@ -2283,14 +2331,17 @@ function wfFormatStackFrame($frame) {
  * Get a cache key
  */
 function wfMemcKey( /*... */ ) {
-       global $wgDBprefix, $wgDBname;
        $args = func_get_args();
-       if ( $wgDBprefix ) {
-               $key = "$wgDBname-$wgDBprefix:" . implode( ':', $args );
+       $key = wfWikiID() . ':' . implode( ':', $args );
+       return $key;
+}
+
+function wfMemcKeyLang( $key, $code ) {
+       if ( !is_string($code) ) {
+               return $key;
        } else {
-               $key = $wgDBname . ':' . implode( ':', $args );
+               return $key . ';L:' . $code;
        }
-       return $key;
 }
 
 /**
@@ -2310,12 +2361,16 @@ function wfForeignMemcKey( $db, $prefix /*, ... */ ) {
  * Get an ASCII string identifying this wiki
  * This is used as a prefix in memcached keys
  */
-function wfWikiID() {
-       global $wgDBprefix, $wgDBname;
-       if ( $wgDBprefix ) {
-               return "$wgDBname-$wgDBprefix";
+function wfWikiID( $db = null ) {
+       if( $db instanceof Database ) {
+               return $db->getWikiID();
        } else {
-               return $wgDBname;
+       global $wgDBprefix, $wgDBname;
+               if ( $wgDBprefix ) {
+                       return "$wgDBname-$wgDBprefix";
+               } else {
+                       return $wgDBname;
+               }
        }
 }
 
@@ -2331,7 +2386,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.
@@ -2341,6 +2396,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 );
@@ -2371,10 +2430,11 @@ function &wfGetLBFactory() {
  * @param mixed $time Requested time for an archived image, or false for the
  *                    current version. An image object will be returned which
  *                    was created at the specified time.
+ * @param mixed $flags FileRepo::FIND_ flags
  * @return File, or false if the file does not exist
  */
-function wfFindFile( $title, $time = false ) {
-       return RepoGroup::singleton()->findFile( $title, $time );
+function wfFindFile( $title, $time = false, $flags = 0 ) {
+       return RepoGroup::singleton()->findFile( $title, $time, $flags );
 }
 
 /**
@@ -2423,13 +2483,35 @@ function wfBoolToStr( $value ) {
 
 /**
  * Load an extension messages file
+ *
+ * @param string $extensionName Name of extension to load messages from\for.
+ * @param string $langcode Language to load messages for, or false for default
+ *                         behvaiour (en, content language and user language).
  */
-function wfLoadExtensionMessages( $extensionName ) {
-       global $wgExtensionMessagesFiles, $wgMessageCache;
-       if ( !empty( $wgExtensionMessagesFiles[$extensionName] ) ) {
-               $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName] );
-               // Prevent double-loading
-               $wgExtensionMessagesFiles[$extensionName] = false;
+function wfLoadExtensionMessages( $extensionName, $langcode = false ) {
+       global $wgExtensionMessagesFiles, $wgMessageCache, $wgLang, $wgContLang;
+
+       #For recording whether extension message files have been loaded in a given language.
+       static $loaded = array();
+
+       if( !array_key_exists( $extensionName, $loaded ) ) {
+               $loaded[$extensionName] = array();
+       }
+
+       if( !$langcode && !array_key_exists( '*', $loaded[$extensionName] ) ) {
+               # Just do en, content language and user language.
+               $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], false );
+               # Mark that they have been loaded.
+               $loaded[$extensionName]['en'] = true;
+               $loaded[$extensionName][$wgLang->getCode()] = true;
+               $loaded[$extensionName][$wgContLang->getCode()] = true;
+               # Mark that this part has been done to avoid weird if statements.
+               $loaded[$extensionName]['*'] = true;
+       } elseif( is_string( $langcode ) && !array_key_exists( $langcode, $loaded[$extensionName] ) ) {
+               # Load messages for specified language.
+               $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], $langcode );
+               # Mark that they have been loaded.
+               $loaded[$extensionName][$langcode] = true;
        }
 }
 
@@ -2471,7 +2553,28 @@ function wfMaxlagError( $host, $lag, $maxLag ) {
  * @return null
  */
 function wfDeprecated( $function ) {
-       trigger_error( "Use of $function is deprecated", E_USER_NOTICE );
+       global $wgDebugLogFile;
+       if ( !$wgDebugLogFile ) {
+               return;
+       }
+       $callers = wfDebugBacktrace();
+       if( isset( $callers[2] ) ){
+               $callerfunc = $callers[2];
+               $callerfile = $callers[1];
+               if( isset( $callerfile['file'] ) && isset( $callerfile['line'] ) ){
+                       $file = $callerfile['file'] . ' at line ' . $callerfile['line'];
+               } else {
+                       $file = '(internal function)';
+               }
+               $func = '';
+               if( isset( $callerfunc['class'] ) )
+                       $func .= $callerfunc['class'] . '::';
+               $func .= @$callerfunc['function'];
+               $msg = "Use of $function is deprecated. Called from $func in $file";
+       } else {
+               $msg = "Use of $function is deprecated.";
+       }
+       wfDebug( "$msg\n" );
 }
 
 /**
@@ -2506,7 +2609,7 @@ 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 );