* Replaced $wgMessageCache by MessageCache::singleton(); since we only use one instan...
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
index c3e9884..b76c798 100644 (file)
@@ -1,4 +1,9 @@
 <?php
+/**
+ * Parser functions provided by MediaWiki core
+ *
+ * @file
+ */
 
 /**
  * Various core parser functions, registered in Parser::firstCallInit()
@@ -81,9 +86,9 @@ class CoreParserFunctions {
        static function intFunction( $parser, $part1 = '' /*, ... */ ) {
                if ( strval( $part1 ) !== '' ) {
                        $args = array_slice( func_get_args(), 2 );
-                       $message = wfMsgGetKey( $part1, true, false, false );
+                       $message = wfMsgGetKey( $part1, true, $parser->getOptions()->getUserLang(), false );
                        $message = wfMsgReplaceArgs( $message, $args );
-                       $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
+                       $message = $parser->replaceVariables( $message ); // like MessageCache::transform()
                        return $message;
                } else {
                        return array( 'found' => false );
@@ -95,7 +100,7 @@ class CoreParserFunctions {
 
                $date = trim( $date );
 
-               $pref = $parser->mOptions->getDateFormat();
+               $pref = $parser->getOptions()->getDateFormat();
 
                // Specify a different default date format other than the the normal default
                // iff the user has 'default' for their setting
@@ -131,7 +136,7 @@ class CoreParserFunctions {
         * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
         * For links to "wiki"s, or similar software, spaces are encoded as '_',
         *
-        * @param $parser.
+        * @param $parser Parser object
         * @param $s String: The text to encode.
         * @param $arg String (optional): The type of encoding.
         */
@@ -239,13 +244,12 @@ class CoreParserFunctions {
                if ( is_object( $title ) && $title->getNamespace() == NS_USER )
                        $user = $title->getText();
 
-               // check parameter, or use $wgUser if in interface message
+               // check parameter, or use the ParserOptions if in interface message
                $user = User::newFromName( $user );
                if ( $user ) {
                        $gender = $user->getOption( 'gender' );
-               } elseif ( $parser->mOptions->getInterfaceMessage() ) {
-                       global $wgUser;
-                       $gender = $wgUser->getOption( 'gender' );
+               } elseif ( $parser->getOptions()->getInterfaceMessage() ) {
+                       $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
                }
                $ret = $parser->getFunctionLang()->gender( $gender, $forms );
                wfProfileOut( __METHOD__ );
@@ -405,7 +409,7 @@ class CoreParserFunctions {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) )
                        return '';
-               return $t->getPartialURL();
+               return wfEscapeWikiText( $t->getPartialURL() );
        }
        static function fullpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
@@ -417,31 +421,31 @@ class CoreParserFunctions {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canTalk() )
                        return '';
-               return $t->getPrefixedURL();
+               return wfEscapeWikiText( $t->getPrefixedURL() );
        }
        static function subpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) )
                        return '';
-               return $t->getSubpageText();
+               return wfEscapeWikiText( $t->getSubpageText() );
        }
        static function subpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) )
                        return '';
-               return $t->getSubpageUrlForm();
+               return wfEscapeWikiText( $t->getSubpageUrlForm() );
        }
        static function basepagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) )
                        return '';
-               return $t->getBaseText();
+               return wfEscapeWikiText( $t->getBaseText() );
        }
        static function basepagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) )
                        return '';
-               return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
+               return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
        }
        static function talkpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
@@ -453,7 +457,7 @@ class CoreParserFunctions {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canTalk() )
                        return '';
-               return $t->getTalkPage()->getPrefixedUrl();
+               return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
        }
        static function subjectpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
@@ -465,7 +469,7 @@ class CoreParserFunctions {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) )
                        return '';
-               return $t->getSubjectPage()->getPrefixedUrl();
+               return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
        }
 
        /**
@@ -582,11 +586,7 @@ class CoreParserFunctions {
        }
 
        static function anchorencode( $parser, $text ) {
-               $a = urlencode( $text );
-               $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
-               # leave colons alone, however
-               $a = str_replace( '.3A', ':', $a );
-               return $a;
+               return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
        }
 
        static function special( $parser, $text ) {
@@ -609,17 +609,47 @@ class CoreParserFunctions {
                        return '';
                else
                        return( '<span class="error">' .
-                               wfMsg( 'duplicate-defaultsort',
+                               wfMsgForContent( 'duplicate-defaultsort',
                                                 htmlspecialchars( $old ),
                                                 htmlspecialchars( $text ) ) .
                                '</span>' );
        }
 
-       public static function filepath( $parser, $name='', $option='' ) {
+       // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
+       public static function filepath( $parser, $name='', $argA='', $argB='' ) {
                $file = wfFindFile( $name );
-               if( $file ) {
-                       $url = $file->getFullUrl();
-                       if( $option == 'nowiki' ) {
+               $size = '';
+               $argA_int = intval( $argA );
+               $argB_int = intval( $argB );
+
+               if ( $argB_int > 0 ) {
+                       // {{filepath: | option | size }}
+                       $size = $argB_int;
+                       $option = $argA;
+
+               } elseif ( $argA_int > 0 ) {
+                       // {{filepath: | size [|option] }}
+                       $size = $argA_int;
+                       $option = $argB;
+
+               } else {
+                       // {{filepath: [|option] }}
+                       $option = $argA;
+               }
+
+               if ( $file ) {
+                       $url = $file->getUrl();
+
+                       // If a size is requested...                    
+                       if ( is_integer( $size ) ) {
+                               $mto = $file->transform( array( 'width' => $size ) );
+                               // ... and we can
+                               if ( $mto && !$mto->isError() ) {
+                                       // ... change the URL to point to a thumbnail.
+                                       $url = wfExpandUrl( $mto->getUrl() );
+                               }
+                       }
+                       if ( $option == 'nowiki' ) {
                                return array( $url, 'nowiki' => true );
                        }
                        return $url;
@@ -632,7 +662,6 @@ class CoreParserFunctions {
         * Parser function to extension tag adaptor
         */
        public static function tagObj( $parser, $frame, $args ) {
-               $xpath = false;
                if ( !count( $args ) ) {
                        return '';
                }
@@ -647,7 +676,7 @@ class CoreParserFunctions {
                $stripList = $parser->getStripList();
                if ( !in_array( $tagName, $stripList ) ) {
                        return '<span class="error">' .
-                               wfMsg( 'unknown_extension_tag', $tagName ) .
+                               wfMsgForContent( 'unknown_extension_tag', $tagName ) .
                                '</span>';
                }