}
}
+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
* @return bool
*/
function wfReadOnly() {
- global $wgReadOnlyFile;
+ global $wgReadOnlyFile, $wgReadOnly;
+ if ( $wgReadOnly ) {
+ return true;
+ }
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;
}
* 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 {
$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)
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.
$msg .= "\n<p>Backtrace:</p>\n$backtrace";
}
}
- die( $msg );
+ echo $msg;
+ die( -1 );
}
function wfBacktrace() {
$pairs = array(
"\\" => "\\\\",
"\"" => "\\\"",
- "\'" => "\\\'",
+ '\'' => '\\\'',
"\n" => "\\n",
"\r" => "\\r",
wfProfileIn( $fname );
$notice = wfMsg( 'sitenotice' );
- if($notice == '<sitenotice>') $notice = '';
- # Allow individual wikis to turn it off
- if ( $notice == '-' ) {
+ if( $notice == '<sitenotice>' || $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;
* @param bool $contents NULL to make an open tag only; '' for a contentless closed tag (default)
* @return string
*/
-function wfElement( $element, $attribs = array(), $contents = '') {
+function wfElement( $element, $attribs = null, $contents = '') {
$out = '<' . $element;
- foreach( $attribs as $name => $val ) {
- $out .= ' ' . $name . '="' . htmlspecialchars( $val ) . '"';
+ if( !is_null( $attribs ) ) {
+ foreach( $attribs as $name => $val ) {
+ $out .= ' ' . $name . '="' . htmlspecialchars( $val ) . '"';
+ }
}
if( is_null( $contents ) ) {
$out .= '>';
if( $attribs ) {
$attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
}
- return wfElement( $element, $attribs, UtfNormal::cleanUp( $contents ) );
+ if( $contents ) {
+ $contents = UtfNormal::cleanUp( $contents );
+ }
+ return wfElement( $element, $attribs, $contents );
}
/** Global singleton instance of MimeMagic. This is initialized on demand,
*/
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;
}
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%";
+}
?>