* Fixed unclosed <p> tag
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index eb517f1..c0c9e0a 100644 (file)
@@ -79,6 +79,17 @@ if ( !function_exists( 'mb_substr' ) ) {
        }
 }
 
+if( !function_exists( 'floatval' ) ) {
+       /**
+        * First defined in PHP 4.2.0
+        * @param mixed $var;
+        * @return float
+        */
+       function floatval( $var ) {
+               return (float)$var;
+       }
+}
+
 /**
  * Where as we got a random seed
  * @var bool $wgTotalViews
@@ -224,7 +235,15 @@ function wfReadOnly() {
        if ( '' == $wgReadOnlyFile ) {
                return false;
        }
-       return is_file( $wgReadOnlyFile );
+       
+       // Set $wgReadOnly and unset $wgReadOnlyFile, for faster access next time
+       if ( is_file( $wgReadOnlyFile ) ) {
+               $wgReadOnly = true;
+       } else {
+               $wgReadOnly = false;
+       }
+       $wgReadOnlyFile = '';
+       return $wgReadOnly;
 }
 
 
@@ -281,14 +300,28 @@ function wfMsgNoDBForContent( $key ) {
  * Really get a message
  */
 function wfMsgReal( $key, $args, $useDB, $forContent=false ) {
-       static $replacementKeys = array( '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9' );
+       $fname = 'wfMsgReal';
+       wfProfileIn( $fname );
+       
+       $message = wfMsgGetKey( $key, $useDB, $forContent );
+       $message = wfMsgReplaceArgs( $message, $args );
+       wfProfileOut( $fname );
+       return $message;
+}
+
+/**
+ * Fetch a message string value, but don't replace any keys yet.
+ * @param string $key
+ * @param bool $useDB
+ * @param bool $forContent
+ * @return string
+ * @access private
+ */
+function wfMsgGetKey( $key, $useDB, $forContent = false ) {
        global $wgParser, $wgMsgParserOptions;
        global $wgContLang, $wgLanguageCode;
        global $wgMessageCache, $wgLang;
        
-       $fname = 'wfMsgReal';
-       wfProfileIn( $fname );
-
        if( is_object( $wgMessageCache ) ) {
                $message = $wgMessageCache->get( $key, $useDB, $forContent );
        } else {
@@ -312,6 +345,19 @@ function wfMsgReal( $key, $args, $useDB, $forContent=false ) {
                        $message = $wgParser->transformMsg($message, $wgMsgParserOptions);
                }
        }
+       return $message;
+}
+
+/**
+ * Replace message parameter keys on the given formatted output.
+ *
+ * @param string $message
+ * @param array $args
+ * @return string
+ * @access private
+ */
+function wfMsgReplaceArgs( $message, $args ) {
+       static $replacementKeys = array( '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9' );
        
        # Fix windows line-endings
        # Some messages are split with explode("\n", $msg)
@@ -321,11 +367,25 @@ function wfMsgReal( $key, $args, $useDB, $forContent=false ) {
        if( count( $args ) ) {
                $message = str_replace( $replacementKeys, $args, $message );
        }
-       wfProfileOut( $fname );
        return $message;
 }
 
-
+/**
+ * Return an HTML-escaped version of a message.
+ * Parameter replacements, if any, are done *after* the HTML-escaping,
+ * so parameters may contain HTML (eg links or form controls). Be sure
+ * to pre-escape them if you really do want plaintext, or just wrap
+ * the whole thing in htmlspecialchars().
+ *
+ * @param string $key
+ * @param string ... parameters
+ * @return string
+ */
+function wfMsgHtml( $key ) {
+       $args = func_get_args();
+       array_shift( $args );
+       return wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $key, true ) ), $args );
+}
 
 /**
  * Just like exit() but makes a note of it.
@@ -380,7 +440,8 @@ function wfDebugDieBacktrace( $msg = '' ) {
                        $msg .= "\n<p>Backtrace:</p>\n$backtrace";
                }
         }
-        die( $msg );
+        echo $msg;
+        die( -1 );
 }
 
 function wfBacktrace() {
@@ -591,7 +652,7 @@ function wfEscapeJsString( $string ) {
        $pairs = array(
                "\\" => "\\\\",
                "\"" => "\\\"",
-               "\'" => "\\\'",
+               '\'' => '\\\'',
                "\n" => "\\n",
                "\r" => "\\r",
                
@@ -1065,19 +1126,18 @@ function wfGetSiteNotice() {
        wfProfileIn( $fname );
 
        $notice = wfMsg( 'sitenotice' );
-       if($notice == '&lt;sitenotice&gt;') $notice = '';
-       # Allow individual wikis to turn it off
-       if ( $notice == '-' ) {
+       if( $notice == '&lt;sitenotice&gt;' || $notice == '-' ) {
                $notice = '';
-       } else {
-               if ($notice == '') {
-                       $notice = $wgSiteNotice;
-               }
-               if($notice != '-' && $notice != '') {
-                       $specialparser = new Parser();
-                       $parserOutput = $specialparser->parse( $notice, $wgTitle, $wgOut->mParserOptions, false );
-                       $notice = $parserOutput->getText();
-               }
+       }
+       if( $notice == '' ) {
+               # We may also need to override a message with eg downtime info
+               # FIXME: make this work!
+               $notice = $wgSiteNotice;
+       }
+       if($notice != '-' && $notice != '') {
+               $specialparser = new Parser();
+               $parserOutput = $specialparser->parse( $notice, $wgTitle, $wgOut->mParserOptions, false );
+               $notice = $parserOutput->getText();
        }
        wfProfileOut( $fname );
        return $notice;
@@ -1178,7 +1238,7 @@ function &wfGetMimeMagic() {
  */
 function wfTempDir() {
        foreach( array( 'TMPDIR', 'TMP', 'TEMP' ) as $var ) {
-               $tmp = getenv( 'TMPDIR' );
+               $tmp = getenv( $var );
                if( $tmp && file_exists( $tmp ) && is_dir( $tmp ) && is_writable( $tmp ) ) {
                        return $tmp;
                }
@@ -1205,4 +1265,25 @@ function wfMkdirParents( $fullDir, $mode ) {
        return true;
 }
 
+/**
+ * Increment a statistics counter
+ */
+function wfIncrStats( $key ) {
+       global $wgDBname, $wgMemc;
+       $key = "$wgDBname:stats:$key";
+       if ( is_null( $wgMemc->incr( $key ) ) ) {
+               $wgMemc->add( $key, 1 );
+       }
+}
+
+/**
+ * @param mixed $nr The number to format
+ * @param int $acc The number of digits after the decimal point, default 2
+ * @param bool $round Whether or not to round the value, default true
+ * @return float
+ */
+function percent( $nr, $acc = 2, $round = true ) {
+       $ret = sprintf( "%.${acc}f", $nr );
+       return $round ? round( $ret, $acc ) . '%' : "$ret%";
+}
 ?>