Localisation updates for extension messages from Betawiki (2008-07-11 11:00 CEST)
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 2f1b60b..56a313f 100644 (file)
@@ -11,7 +11,6 @@ if ( !defined( 'MEDIAWIKI' ) ) {
 require_once dirname(__FILE__) . '/LogPage.php';
 require_once dirname(__FILE__) . '/normal/UtfNormalUtil.php';
 require_once dirname(__FILE__) . '/XmlFunctions.php';
-require_once dirname(__FILE__) . '/MessageFunctions.php';
 
 /**
  * Compatibility functions
@@ -320,6 +319,328 @@ function wfReadOnlyReason() {
        return $wgReadOnly;
 }
 
+/**
+ * Get a message from anywhere, for the current user language.
+ *
+ * Use wfMsgForContent() instead if the message should NOT
+ * change depending on the user preferences.
+ *
+ * @param $key String: lookup key for the message, usually
+ *    defined in languages/Language.php
+ *
+ * This function also takes extra optional parameters (not
+ * shown in the function definition), which can by used to
+ * insert variable text into the predefined message.
+ */
+function wfMsg( $key ) {
+       $args = func_get_args();
+       array_shift( $args );
+       return wfMsgReal( $key, $args, true );
+}
+
+/**
+ * Same as above except doesn't transform the message
+ */
+function wfMsgNoTrans( $key ) {
+       $args = func_get_args();
+       array_shift( $args );
+       return wfMsgReal( $key, $args, true, false, false );
+}
+
+/**
+ * Get a message from anywhere, for the current global language
+ * set with $wgLanguageCode.
+ *
+ * Use this if the message should NOT change  dependent on the
+ * language set in the user's preferences. This is the case for
+ * most text written into logs, as well as link targets (such as
+ * the name of the copyright policy page). Link titles, on the
+ * other hand, should be shown in the UI language.
+ *
+ * Note that MediaWiki allows users to change the user interface
+ * language in their preferences, but a single installation
+ * typically only contains content in one language.
+ *
+ * Be wary of this distinction: If you use wfMsg() where you should
+ * use wfMsgForContent(), a user of the software may have to
+ * customize over 70 messages in order to, e.g., fix a link in every
+ * possible language.
+ *
+ * @param $key String: lookup key for the message, usually
+ *    defined in languages/Language.php
+ */
+function wfMsgForContent( $key ) {
+       global $wgForceUIMsgAsContentMsg;
+       $args = func_get_args();
+       array_shift( $args );
+       $forcontent = true;
+       if( is_array( $wgForceUIMsgAsContentMsg ) &&
+               in_array( $key, $wgForceUIMsgAsContentMsg ) )
+               $forcontent = false;
+       return wfMsgReal( $key, $args, true, $forcontent );
+}
+
+/**
+ * Same as above except doesn't transform the message
+ */
+function wfMsgForContentNoTrans( $key ) {
+       global $wgForceUIMsgAsContentMsg;
+       $args = func_get_args();
+       array_shift( $args );
+       $forcontent = true;
+       if( is_array( $wgForceUIMsgAsContentMsg ) &&
+               in_array( $key, $wgForceUIMsgAsContentMsg ) )
+               $forcontent = false;
+       return wfMsgReal( $key, $args, true, $forcontent, false );
+}
+
+/**
+ * Get a message from the language file, for the UI elements
+ */
+function wfMsgNoDB( $key ) {
+       $args = func_get_args();
+       array_shift( $args );
+       return wfMsgReal( $key, $args, false );
+}
+
+/**
+ * Get a message from the language file, for the content
+ */
+function wfMsgNoDBForContent( $key ) {
+       global $wgForceUIMsgAsContentMsg;
+       $args = func_get_args();
+       array_shift( $args );
+       $forcontent = true;
+       if( is_array( $wgForceUIMsgAsContentMsg ) &&
+               in_array( $key, $wgForceUIMsgAsContentMsg ) )
+               $forcontent = false;
+       return wfMsgReal( $key, $args, false, $forcontent );
+}
+
+
+/**
+ * Really get a message
+ * @param $key String: key to get.
+ * @param $args
+ * @param $useDB Boolean
+ * @param $transform Boolean: Whether or not to transform the message.
+ * @param $forContent Boolean
+ * @return String: the requested message.
+ */
+function wfMsgReal( $key, $args, $useDB = true, $forContent=false, $transform = true ) {
+       wfProfileIn( __METHOD__ );
+       $message = wfMsgGetKey( $key, $useDB, $forContent, $transform );
+       $message = wfMsgReplaceArgs( $message, $args );
+       wfProfileOut( __METHOD__ );
+       return $message;
+}
+
+/**
+ * This function provides the message source for messages to be edited which are *not* stored in the database.
+ * @param $key String:
+ */
+function wfMsgWeirdKey ( $key ) {
+       $source = wfMsgGetKey( $key, false, true, false );
+       if ( wfEmptyMsg( $key, $source ) )
+               return "";
+       else
+               return $source;
+}
+
+/**
+ * Fetch a message string value, but don't replace any keys yet.
+ * @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
+ *                         boolean.
+ * @return string
+ * @private
+ */
+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 );
+               if ( $transform ) {
+                       $message = $wgMessageCache->transform( $message );
+               }
+       } else {
+               if( $langCode === true ) {
+                       $lang = &$wgContLang;
+               } 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
+               # ISSUE: Should we try to handle "message/lang" here too?
+               $key = str_replace( ' ' , '_' , $wgContLang->lcfirst( $key ) );
+
+               if( is_object( $lang ) ) {
+                       $message = $lang->getMessage( $key );
+               } else {
+                       $message = false;
+               }
+       }
+
+       return $message;
+}
+
+/**
+ * Replace message parameter keys on the given formatted output.
+ *
+ * @param string $message
+ * @param array $args
+ * @return string
+ * @private
+ */
+function wfMsgReplaceArgs( $message, $args ) {
+       # Fix windows line-endings
+       # Some messages are split with explode("\n", $msg)
+       $message = str_replace( "\r", '', $message );
+
+       // Replace arguments
+       if ( count( $args ) ) {
+               if ( is_array( $args[0] ) ) {
+                       $args = array_values( $args[0] );
+               }
+               $replacementKeys = array();
+               foreach( $args as $n => $param ) {
+                       $replacementKeys['$' . ($n + 1)] = $param;
+               }
+               $message = strtr( $message, $replacementKeys );
+       }
+
+       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 );
+}
+
+/**
+ * Return an HTML version of message
+ * Parameter replacements, if any, are done *after* parsing the wiki-text message,
+ * 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 wfMsgWikiHtml( $key ) {
+       global $wgOut;
+       $args = func_get_args();
+       array_shift( $args );
+       return wfMsgReplaceArgs( $wgOut->parse( wfMsgGetKey( $key, true ), /* can't be set to false */ true ), $args );
+}
+
+/**
+ * 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.
+ * Behavior for conflicting options (e.g., parse+parseinline) is undefined.
+ */
+function wfMsgExt( $key, $options ) {
+       global $wgOut, $wgParser;
+
+       $args = func_get_args();
+       array_shift( $args );
+       array_shift( $args );
+
+       if( !is_array($options) ) {
+               $options = array($options);
+       }
+
+       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) ) {
+                       # Fallback to en, instead of whatever interface language we might have
+                       $langCode = 'en';
+               }
+       } else {
+               $forContent = false;
+               $langCode = false;
+       }
+
+       $string = wfMsgGetKey( $key, /*DB*/true, $langCode, /*Transform*/false );
+
+       if( !in_array('replaceafter', $options) ) {
+               $string = wfMsgReplaceArgs( $string, $args );
+       }
+
+       if( in_array('parse', $options) ) {
+               $string = $wgOut->parse( $string, true, !$forContent );
+       } elseif ( in_array('parseinline', $options) ) {
+               $string = $wgOut->parse( $string, true, !$forContent );
+               $m = array();
+               if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
+                       $string = $m[1];
+               }
+       } elseif ( in_array('parsemag', $options) ) {
+               global $wgMessageCache;
+               if ( isset( $wgMessageCache ) ) {
+                       $string = $wgMessageCache->transform( $string, !$forContent );
+               }
+       }
+
+       if ( in_array('escape', $options) ) {
+               $string = htmlspecialchars ( $string );
+       } elseif ( in_array( 'escapenoentities', $options ) ) {
+               $string = htmlspecialchars( $string );
+               $string = str_replace( '&amp;', '&', $string );
+               $string = Sanitizer::normalizeCharReferences( $string );
+       }
+
+       if( in_array('replaceafter', $options) ) {
+               $string = wfMsgReplaceArgs( $string, $args );
+       }
+
+       return $string;
+}
+
+
 /**
  * Just like exit() but makes a note of it.
  * Commits open transactions except if the error parameter is set
@@ -862,10 +1183,7 @@ function wfMerge( $old, $mine, $yours, &$result ){
  */
 function wfVarDump( $var ) {
        global $wgOut;
-       ob_start();
-       var_dump( $var );
-       $s = str_replace("\n","<br />\n", ob_get_contents() . "\n");
-       ob_end_clean();
+       $s = str_replace("\n","<br />\n", var_export( $var, true ) . "\n");
        if ( headers_sent() || !@is_object( $wgOut ) ) {
                print $s;
        } else {
@@ -2369,22 +2687,11 @@ function wfGenerateToken( $salt = '' ) {
 }
 
 /**
- * Checks filename for validity
- * @param mixed $title Filename or title to check
+ * Replace all invalid characters with -
+ * @param mixed $title Filename to process
  */
-function wfIsValidFileName( $name ) {
-       if( !$name instanceof Title ) 
-               if( !Title::makeTitleSafe( NS_IMAGE, $name ) )
-                       return false;
-       else
-               $name = $name->getText();
-
-       if( in_string( ':', $name ) )
-               return false;
-       elseif( wfBaseName( $name ) != $name )
-               return false;
-       elseif( wfIsWindows() && ( in_string( '*', $name ) || in_string( '?', $name ) ) )
-               return false;
-       else
-               return true;
+function wfStripIllegalFilenameChars( $name ) {
+       $name = wfBaseName( $name );
+       $name = preg_replace ( "/[^".Title::legalChars()."]|:/", '-', $name );
+       return $name;
 }