Tweak r41788 - Use findFile() to check current version if $time given
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 80e5843..0c25e05 100644 (file)
@@ -297,24 +297,28 @@ function wfErrorLog( $text, $file ) {
                        $protocol = $m[1];
                        $host = $m[2];
                        $port = $m[3];
-                       $prefix = isset( $m[4] ) ? $m[4] : '';
-               } elseif ( preg_match( '!^(tcp|udp):(?://)?([a-zA-Z0-9-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) {
+                       $prefix = isset( $m[4] ) ? $m[4] : false;
+               } elseif ( preg_match( '!^(tcp|udp):(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) {
                        $protocol = $m[1];
                        $host = $m[2];
                        $port = $m[3];
-                       $prefix = isset( $m[4] ) ? $m[4] : '';
+                       $prefix = isset( $m[4] ) ? $m[4] : false;
                } else {
                        throw new MWException( __METHOD__.": Invalid UDP specification" );
                }
-               $prefix = strval( $prefix );
-               if ( $prefix != '' ) {
-                       $prefix .= ' ';
+               // Clean it up for the multiplexer
+               if ( strval( $prefix ) !== '' ) {
+                       $text = preg_replace( '/^/m', $prefix . ' ', $text );
+                       if ( substr( $text, -1 ) != "\n" ) {
+                               $text .= "\n";
+                       }
                }
+
                $sock = fsockopen( "$protocol://$host", $port );
                if ( !$sock ) {
                        return;
                }
-               fwrite( $sock, $prefix . $text );
+               fwrite( $sock, $text );
                fclose( $sock );
        } else {
                wfSuppressWarnings();
@@ -662,16 +666,19 @@ function wfMsgWikiHtml( $key ) {
 /**
  * Returns message in the requested format
  * @param string $key Key of the message
- * @param array $options Processing rules:
- *  <i>parse</i>: parses wikitext to html
- *  <i>parseinline</i>: parses wikitext to html and removes the surrounding p's added by parser or tidy
- *  <i>escape</i>: filters message through htmlspecialchars
- *  <i>escapenoentities</i>: same, but allows entity references like &nbsp; through
- *  <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.
+ * @param array $options Processing rules.  Can take the following options:
+ *   <i>parse</i>: parses wikitext to html
+ *   <i>parseinline</i>: parses wikitext to html and removes the surrounding
+ *       p's added by parser or tidy
+ *   <i>escape</i>: filters message through htmlspecialchars
+ *   <i>escapenoentities</i>: same, but allows entity references like &nbsp; through
+ *   <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
+ * Also can accept a single associative argument, of the form 'language' => 'xx':
+ *   <i>language</i>: Language object or 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 ) {
@@ -680,9 +687,18 @@ function wfMsgExt( $key, $options ) {
        $args = func_get_args();
        array_shift( $args );
        array_shift( $args );
+       $options = (array)$options;
 
-       if( !is_array($options) ) {
-               $options = array($options);
+       foreach( $options as $arrayKey => $option ) {
+               if( !preg_match( '/^[0-9]+|language$/', $arrayKey ) ) {
+                       # An unknown index, neither numeric nor "language"
+                       trigger_error( "wfMsgExt called with incorrect parameter key $arrayKey", E_USER_WARNING );
+               } elseif( preg_match( '/^[0-9]+$/', $arrayKey ) && !in_array( $option,
+               array( 'parse', 'parseinline', 'escape', 'escapenoentities',
+               'replaceafter', 'parsemag', 'content' ) ) ) {
+                       # A numeric index with unknown value
+                       trigger_error( "wfMsgExt called with incorrect parameter $option", E_USER_WARNING );
+               }
        }
 
        if( in_array('content', $options) ) {
@@ -690,12 +706,7 @@ function wfMsgExt( $key, $options ) {
                $langCode = true;
        } elseif( array_key_exists('language', $options) ) {
                $forContent = false;
-               $langCode = $options['language'];
-               $validCodes = array_keys( Language::getLanguageNames() );
-               if( !in_array($options['language'], $validCodes) ) {
-                       # Fallback to en, instead of whatever interface language we might have
-                       $langCode = 'en';
-               }
+               $langCode = wfGetLangObj( $options['language'] );
        } else {
                $forContent = false;
                $langCode = false;
@@ -812,6 +823,9 @@ function wfHostname() {
                }
                if( is_array( $uname ) && isset( $uname['nodename'] ) ) {
                        $host = $uname['nodename'];
+               } elseif ( getenv( 'COMPUTERNAME' ) ) {
+                       # Windows computer name
+                       $host = getenv( 'COMPUTERNAME' );
                } else {
                        # This may be a virtual server.
                        $host = $_SERVER['SERVER_NAME'];
@@ -1201,9 +1215,14 @@ function wfPurgeSquidServers ($urlArr) {
 /**
  * Windows-compatible version of escapeshellarg()
  * Windows doesn't recognise single-quotes in the shell, but the escapeshellarg()
- * function puts single quotes in regardless of OS
+ * function puts single quotes in regardless of OS.
+ *
+ * Also fixes the locale problems on Linux in PHP 5.2.6+ (bug backported to 
+ * earlier distro releases of PHP)
  */
 function wfEscapeShellArg( ) {
+       wfInitShellLocale();
+
        $args = func_get_args();
        $first = true;
        $retVal = '';
@@ -1254,7 +1273,7 @@ function wfMerge( $old, $mine, $yours, &$result ){
 
        # This check may also protect against code injection in
        # case of broken installations.
-       if(! file_exists( $wgDiff3 ) ){
+       if( !$wgDiff3 || !file_exists( $wgDiff3 ) ) {
                wfDebug( "diff3 not found\n" );
                return false;
        }
@@ -1843,22 +1862,64 @@ function wfGetNamespaceNotice() {
        return $namespaceNotice;
 }
 
+/**
+ * Gets and returns the site-wide notice
+ * @return $siteNotice The site-wide notice as set by $wgSiteNotice or MediaWiki:Sitenotice interface message
+ */
 function wfGetSiteNotice() {
-       global $wgUser, $wgSiteNotice;
+       global $wgUser, $wgMajorSiteNoticeID, $wgTitle;
+       global $wgCookiePrefix, $wgCookieExpiration, $wgCookiePath;
        $fname = 'wfGetSiteNotice';
        wfProfileIn( $fname );
        $siteNotice = '';
+       $loggedIn = false;
+       $spTitle = SpecialPage::getTitleFor( 'DismissNotice' );
+       $spUrl = $spTitle->escapeFullURL( array( 'returnto' => $wgTitle->getPrefixedURL() ) );
+       
+       if( $wgUser instanceOf User && $wgUser->isLoggedIn() ) {
+               $loggedIn = true;
+               $siteNotice = wfGetCachedNotice('sitenotice');
+               if($siteNotice === false)
+                       return '';
+       } else {
+               $siteNotice = wfGetCachedNotice('anonnotice');
+               if($siteNotice === false) {
+                       $siteNotice = wfGetCachedNotice('sitenotice');
+                       if($siteNotice === false)
+                               return '';
+               }
+       }
+       
+       $msgClose = wfMsg( 'sitenotice_close' );
+       $id = intval( $wgMajorSiteNoticeID ) . "." . intval( wfMsgForContent( 'sitenotice_id' ) );
 
        if( wfRunHooks( 'SiteNoticeBefore', array( &$siteNotice ) ) ) {
-               if( is_object( $wgUser ) && $wgUser->isLoggedIn() ) {
-                       $siteNotice = wfGetCachedNotice( 'sitenotice' );
-               } else {
-                       $anonNotice = wfGetCachedNotice( 'anonnotice' );
-                       if( !$anonNotice ) {
-                               $siteNotice = wfGetCachedNotice( 'sitenotice' );
-                       } else {
-                               $siteNotice = $anonNotice;
-                       }
+               if( $loggedIn ) {
+                       //it is hidden
+                       if( isset($_COOKIE[$wgCookiePrefix . 'DismissSiteNotice']) && $_COOKIE[$wgCookiePrefix . 'DismissSiteNotice'] == $id )
+                               return '';
+                       $siteNotice = <<<EOT
+<table width="100%" id="mw-dismissable-notice"><tr><td width="80%">$siteNotice</td>
+<td width="20%" align="right">[<a id="dismissLink" href="$spUrl">$msgClose</a>]</td></tr></table>
+<script type="text/javascript" language="JavaScript">
+<!--
+var cookieName = "{$wgCookiePrefix}DismissSiteNotice=";
+var cookiePos = document.cookie.indexOf(cookieName);
+var siteNoticeID = "{$id}";
+
+var dismissLink = document.getElementById('dismissLink');
+dismissLink.href = "javascript:dismissNotice();";
+
+function dismissNotice() {
+               var date = new Date();
+               date.setTime(date.getTime() + {$wgCookieExpiration});
+               document.cookie = cookieName + siteNoticeID + "; expires="+date.toGMTString() + "; path={$wgCookiePath}";
+               var element = document.getElementById('siteNotice');
+               element.parentNode.removeChild(element);
+}
+-->
+</script>
+EOT;
                }
                if( !$siteNotice ) {
                        $siteNotice = wfGetCachedNotice( 'default' );
@@ -2130,6 +2191,7 @@ function wfShellExec( $cmd, &$retval=null ) {
                $retval = 1;
                return "Unable to run external programs in safe mode.";
        }
+       wfInitShellLocale();
 
        if ( php_uname( 's' ) == 'Linux' ) {
                $time = intval( ini_get( 'max_execution_time' ) );
@@ -2159,7 +2221,21 @@ function wfShellExec( $cmd, &$retval=null ) {
                wfDebugLog( 'exec', "Possibly missing executable file: $cmd\n" );
        }
        return $output;
+}
 
+/**
+ * Workaround for http://bugs.php.net/bug.php?id=45132
+ * escapeshellarg() destroys non-ASCII characters if LANG is not a UTF-8 locale
+ */
+function wfInitShellLocale() {
+       static $done = false;
+       if ( $done ) return;
+       $done = true;
+       global $wgShellLocale;
+       if ( !wfIniGetBool( 'safe_mode' ) ) {
+               putenv( "LC_CTYPE=$wgShellLocale" );
+               setlocale( LC_CTYPE, $wgShellLocale );
+       }
 }
 
 /**
@@ -2714,8 +2790,8 @@ function wfFindFile( $title, $time = false, $flags = 0 ) {
  * Get an object referring to a locally registered file.
  * Returns a valid placeholder object if the file does not exist.
  */
-function wfLocalFile( $title ) {
-       return RepoGroup::singleton()->getLocalRepo()->newFile( $title );
+function wfLocalFile( $title, $time = false ) {
+       return RepoGroup::singleton()->getLocalRepo()->findFile( $title, $time );
 }
 
 /**
@@ -2903,20 +2979,3 @@ function wfStripIllegalFilenameChars( $name ) {
        $name = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $name );
        return $name;
 }
-
-/**
- * Send some text to
- * @param string $line
- */
-function wfRecentChange2UDP( $line ) {
-       global $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
-       # Notify external application via UDP
-       if( $wgRC2UDPAddress ) {
-               $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
-               if( $conn ) {
-                       $line = $wgRC2UDPPrefix . $line;
-                       socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
-                       socket_close( $conn );
-               }
-       }
-}