Localisation updates for extension messages from Betawiki
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index aa90d41..c0d7fe9 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,355 @@ function wfReadOnlyReason() {
        return $wgReadOnly;
 }
 
+/**
+ * Return a Language object from $langcode
+ * @param $langcode Mixed: either:
+ *                  - a Language object
+ *                  - code of the language to get the message for, if it is
+ *                    a valid code create a language for that language, if
+ *                    it is a string but not a valid code then make a basic
+ *                    language object
+ *                  - a boolean: if it's false then use the current users
+ *                    language (as a fallback for the old parameter
+ *                    functionality), or if it is true then use the wikis
+ * @return Language object
+ */
+function wfGetLangObj( $langcode = false ){
+       # Identify which language to get or create a language object for.
+       if( $langcode instanceof Language )
+               # Great, we already have the object!
+               return $langcode;
+               
+       global $wgContLang;
+       if( $langcode === $wgContLang->getCode() || $langcode === true )
+               # $langcode is the language code of the wikis content language object.
+               # or it is a boolean and value is true
+               return $wgContLang;
+       
+       global $wgLang;
+       if( $langcode === $wgLang->getCode() || $langcode === false )
+               # $langcode is the language code of user language object.
+               # or it was a boolean and value is false
+               return $wgLang;
+
+       $validCodes = array_keys( Language::getLanguageNames() );
+       if( in_array( $langcode, $validCodes ) )
+               # $langcode corresponds to a valid language.
+               return Language::factory( $langcode );
+
+       # $langcode is a string, but not a valid language code; use content language.
+       wfDebug( 'Invalid language code passed to wfGetLangObj, falling back to content language.' );
+       return $wgContLang;
+}
+
+/**
+ * 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 $wgContLang, $wgMessageCache;
+
+       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 {
+               $lang = wfGetLangObj( $langCode );
+
+               # 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
@@ -857,15 +1205,74 @@ function wfMerge( $old, $mine, $yours, &$result ){
        return ! $conflict;
 }
 
+/**
+ * Returns unified plain-text diff of two texts.
+ * Useful for machine processing of diffs.
+ * @param $before string The text before the changes.
+ * @param $after string The text after the changes.
+ * @param $params string Command-line options for the diff command.
+ * @return string Unified diff of $before and $after
+ */
+function wfDiff( $before, $after, $params = '-u' ) {
+       global $wgDiff;
+
+       # This check may also protect against code injection in
+       # case of broken installations.
+       if( !file_exists( $wgDiff ) ){
+               wfDebug( "diff executable not found\n" );
+               $diffs = new Diff( explode( "\n", $before ), explode( "\n", $after ) );
+               $format = new UnifiedDiffFormatter();
+               return $format->format( $diffs );
+       }
+
+       # Make temporary files
+       $td = wfTempDir();
+       $oldtextFile = fopen( $oldtextName = tempnam( $td, 'merge-old-' ), 'w' );
+       $newtextFile = fopen( $newtextName = tempnam( $td, 'merge-your-' ), 'w' );
+
+       fwrite( $oldtextFile, $before ); fclose( $oldtextFile );
+       fwrite( $newtextFile, $after ); fclose( $newtextFile );
+       
+       // Get the diff of the two files
+       $cmd = "$wgDiff " . $params . ' ' .wfEscapeShellArg( $oldtextName, $newtextName );
+       
+       $h = popen( $cmd, 'r' );
+       
+       $diff = '';
+       
+       do {
+               $data = fread( $h, 8192 );
+               if ( strlen( $data ) == 0 ) {
+                       break;
+               }
+               $diff .= $data;
+       } while ( true );
+       
+       // Clean up
+       pclose( $h );
+       unlink( $oldtextName );
+       unlink( $newtextName );
+       
+       // Kill the --- and +++ lines. They're not useful.
+       $diff_lines = explode( "\n", $diff );
+       if (strpos( $diff_lines[0], '---' ) === 0) {
+               unset($diff_lines[0]);
+       }
+       if (strpos( $diff_lines[1], '+++' ) === 0) {
+               unset($diff_lines[1]);
+       }
+       
+       $diff = implode( "\n", $diff_lines );
+       
+       return $diff;
+}
+
 /**
  * @todo document
  */
 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 {
@@ -1391,12 +1798,22 @@ function wfTempDir() {
 
 /**
  * Make directory, and make all parent directories if they don't exist
+ * 
+ * @param string $fullDir Full path to directory to create
+ * @param int $mode Chmod value to use, default is $wgDirectoryMode
+ * @return bool
  */
-function wfMkdirParents( $fullDir, $mode = 0777 ) {
+function wfMkdirParents( $fullDir, $mode = null ) {
+       global $wgDirectoryMode;
        if( strval( $fullDir ) === '' )
                return true;
        if( file_exists( $fullDir ) )
                return true;
+       // If not defined or isn't an int, set to default
+       if ( is_null( $mode ) ) {
+               $mode = $wgDirectoryMode;
+       }
+
 
        # Go back through the paths to find the first directory that exists
        $currentDir = $fullDir;
@@ -2236,6 +2653,8 @@ function wfBoolToStr( $value ) {
  * @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).
+ * @since r24808 (v1.11) Using this method of loading extension messages will not work
+ * on MediaWiki prior to that
  */
 function wfLoadExtensionMessages( $extensionName, $langcode = false ) {
        global $wgExtensionMessagesFiles, $wgMessageCache, $wgLang, $wgContLang;