* (bug 28719) Do not call mLinkHolders __destruct explicitly
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
index ba13d2d..854676e 100644 (file)
@@ -1,10 +1,19 @@
 <?php
+/**
+ * Parser functions provided by MediaWiki core
+ *
+ * @file
+ */
 
 /**
  * Various core parser functions, registered in Parser::firstCallInit()
  * @ingroup Parser
  */
 class CoreParserFunctions {
+       /**
+        * @param $parser Parser
+        * @return void
+        */
        static function register( $parser ) {
                global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
 
@@ -78,24 +87,34 @@ class CoreParserFunctions {
                }
        }
 
+       /**
+        * @param $parser Parser
+        * @param string $part1
+        * @return array
+        */
        static function intFunction( $parser, $part1 = '' /*, ... */ ) {
                if ( strval( $part1 ) !== '' ) {
                        $args = array_slice( func_get_args(), 2 );
-                       $message = wfMsgGetKey( $part1, true, $parser->mOptions->getUserLang(), false );
-                       $message = wfMsgReplaceArgs( $message, $args );
-                       $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
+                       $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLang() )->plain();
+                       $message = $parser->replaceVariables( $message ); // like MessageCache::transform()
                        return $message;
                } else {
                        return array( 'found' => false );
                }
        }
 
+       /**
+        * @param $parser Parser
+        * @param  $date
+        * @param null $defaultPref
+        * @return mixed|string
+        */
        static function formatDate( $parser, $date, $defaultPref = null ) {
                $df = DateFormatter::getInstance();
 
                $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
@@ -167,6 +186,11 @@ class CoreParserFunctions {
                return $wgContLang->ucfirst( $s );
        }
 
+       /**
+        * @param $parser Parser
+        * @param string $s
+        * @return
+        */
        static function lc( $parser, $s = '' ) {
                global $wgContLang;
                if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
@@ -176,6 +200,11 @@ class CoreParserFunctions {
                }
        }
 
+       /**
+        * @param $parser Parser
+        * @param string $s
+        * @return
+        */
        static function uc( $parser, $s = '' ) {
                global $wgContLang;
                if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
@@ -214,6 +243,12 @@ class CoreParserFunctions {
                }
        }
 
+       /**
+        * @param $parser Parser
+        * @param string $num
+        * @param null $raw
+        * @return
+        */
        static function formatNum( $parser, $num = '', $raw = null) {
                if ( self::israw( $raw ) ) {
                        return $parser->getFunctionLang()->parseFormattedNumber( $num );
@@ -222,10 +257,21 @@ class CoreParserFunctions {
                }
        }
 
+       /**
+        * @param $parser Parser
+        * @param string $case
+        * @param string $word
+        * @return
+        */
        static function grammar( $parser, $case = '', $word = '' ) {
                return $parser->getFunctionLang()->convertGrammar( $word, $case );
        }
 
+       /**
+        * @param $parser Parser
+        * @param $user User
+        * @return
+        */
        static function gender( $parser, $user ) {
                wfProfileIn( __METHOD__ );
                $forms = array_slice( func_get_args(), 2);
@@ -239,18 +285,23 @@ 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__ );
                return $ret;
        }
+
+       /**
+        * @param $parser Parser
+        * @param string $text
+        * @return
+        */
        static function plural( $parser, $text = '' ) {
                $forms = array_slice( func_get_args(), 2 );
                $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
@@ -405,7 +456,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 +468,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 +504,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 +516,7 @@ class CoreParserFunctions {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) )
                        return '';
-               return $t->getSubjectPage()->getPrefixedUrl();
+               return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
        }
 
        /**
@@ -504,6 +555,7 @@ class CoreParserFunctions {
         *   to the link cache, so the local cache here should be unnecessary, but
         *   in fact calling getLength() repeatedly for the same $page does seem to
         *   run one query for each call?
+        * @param $parser Parser
         */
        static function pagesize( $parser, $page = '', $raw = null ) {
                static $cache = array();
@@ -581,12 +633,17 @@ class CoreParserFunctions {
                return self::pad( $string, $length, $padding );
        }
 
+       /**
+        * @param $parser Parser
+        * @param  $text
+        * @return string
+        */
        static function anchorencode( $parser, $text ) {
                return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
        }
 
        static function special( $parser, $text ) {
-               list( $page, $subpage ) = SpecialPage::resolveAliasWithSubpage( $text );
+               list( $page, $subpage ) = SpecialPageFactory::resolveAlias( $text );
                if ( $page ) {
                        $title = SpecialPage::getTitleFor( $page, $subpage );
                        return $title;
@@ -595,6 +652,11 @@ class CoreParserFunctions {
                }
        }
 
+       /**
+        * @param $parser Parser
+        * @param  $text
+        * @return string
+        */
        public static function defaultsort( $parser, $text ) {
                $text = trim( $text );
                if( strlen( $text ) == 0 )
@@ -611,11 +673,41 @@ class CoreParserFunctions {
                                '</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 ) {
+               $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->getFullUrl();
-                       if( $option == 'nowiki' ) {
+
+                       // 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;