Insert a new line in between when adding </td><td> in tables.
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
index c2a3a42..58c5771 100644 (file)
@@ -11,7 +11,7 @@ class CoreParserFunctions {
                # Syntax for arguments (see self::setFunctionHook):
                #  "name for lookup in localized magic words array",
                #  function callback,
-               #  optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}
+               #  optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
                #    instead of {{#int:...}})
 
                $parser->setFunctionHook( 'int',              array( __CLASS__, 'intFunction'      ), SFH_NO_HASH );
@@ -67,10 +67,8 @@ class CoreParserFunctions {
                $parser->setFunctionHook( 'talkpagenamee',    array( __CLASS__, 'talkpagenamee'    ), SFH_NO_HASH );
                $parser->setFunctionHook( 'subjectpagename',  array( __CLASS__, 'subjectpagename'  ), SFH_NO_HASH );
                $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH );
-               $parser->setFunctionHook( 'pipetrick',        array( __CLASS__, 'pipetrick'        ), SFH_NO_HASH );
-               $parser->setFunctionHook( 'pipetricke',       array( __CLASS__, 'pipetricke'       ), SFH_NO_HASH );
                $parser->setFunctionHook( 'tag',              array( __CLASS__, 'tagObj'           ), SFH_OBJECT_ARGS );
-               $parser->setFunctionHook( 'formatdate',           array( __CLASS__, 'formatDate'           ) );
+               $parser->setFunctionHook( 'formatdate',       array( __CLASS__, 'formatDate'       ) );
 
                if ( $wgAllowDisplayTitle ) {
                        $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
@@ -95,16 +93,16 @@ class CoreParserFunctions {
        static function formatDate( $parser, $date, $defaultPref = null ) {
                $df = DateFormatter::getInstance();
 
-               $date = trim($date);
+               $date = trim( $date );
 
                $pref = $parser->mOptions->getDateFormat();
 
                // Specify a different default date format other than the the normal default
                // iff the user has 'default' for their setting
-               if ($pref == 'default' && $defaultPref)
+               if ( $pref == 'default' && $defaultPref )
                        $pref = $defaultPref;
 
-               $date = $df->reformat( $pref, $date, array('match-whole') );
+               $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
                return $date;
        }
 
@@ -121,13 +119,42 @@ class CoreParserFunctions {
                        return array( 'found' => false );
                }
        }
-       
+
        static function nse( $parser, $part1 = '' ) {
                return wfUrlencode( str_replace( ' ', '_', self::ns( $parser, $part1 ) ) );
        }
 
-       static function urlencode( $parser, $s = '' ) {
-               return urlencode( $s );
+       /**
+        * urlencodes a string according to one of three patterns: (bug 22474)
+        *
+        * By default (for HTTP "query" strings), spaces are encoded as '+'.
+        * 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 Parser object
+        * @param $s String: The text to encode.
+        * @param $arg String (optional): The type of encoding.
+        */
+       static function urlencode( $parser, $s = '', $arg = null ) {
+               static $magicWords = null;
+               if ( is_null( $magicWords ) ) {
+                       $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
+               }
+               switch( $magicWords->matchStartToEnd( $arg ) ) {
+
+                       // Encode as though it's a wiki page, '_' for ' '.
+                       case 'url_wiki':
+                               return wfUrlencode( str_replace( ' ', '_', $s ) );
+
+                       // Encode for an HTTP Path, '%20' for ' '.
+                       case 'url_path':
+                               return rawurlencode( $s );
+
+                       // Encode for HTTP query, '+' for ' '.
+                       case 'url_query':
+                       default:
+                               return urlencode( $s );
+               }
        }
 
        static function lcfirst( $parser, $s = '' ) {
@@ -170,7 +197,7 @@ class CoreParserFunctions {
                # and the variable will fail. If we can't get a decent title from the first
                # attempt, url-decode and try for a second.
                if( is_null( $title ) )
-                       $title = Title::newFromUrl( urldecode( $s ) );
+                       $title = Title::newFromURL( urldecode( $s ) );
                if( !is_null( $title ) ) {
                        # Convert NS_MEDIA -> NS_FILE
                        if( $title->getNamespace() == NS_MEDIA ) {
@@ -209,7 +236,7 @@ class CoreParserFunctions {
                // allow prefix.
                $title = Title::newFromText( $user );
 
-               if (is_object( $title ) && $title->getNamespace() == NS_USER)
+               if ( is_object( $title ) && $title->getNamespace() == NS_USER )
                        $user = $title->getText();
 
                // check parameter, or use $wgUser if in interface message
@@ -224,8 +251,8 @@ class CoreParserFunctions {
                wfProfileOut( __METHOD__ );
                return $ret;
        }
-       static function plural( $parser, $text = '') {
-               $forms = array_slice( func_get_args(), 2);
+       static function plural( $parser, $text = '' ) {
+               $forms = array_slice( func_get_args(), 2 );
                $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
                return $parser->getFunctionLang()->convertPlural( $text, $forms );
        }
@@ -234,9 +261,9 @@ class CoreParserFunctions {
         * Override the title of the page when viewed, provided we've been given a
         * title which will normalise to the canonical title
         *
-        * @param Parser $parser Parent parser
-        * @param string $text Desired title text
-        * @return string
+        * @param $parser Parser: parent parser
+        * @param $text String: desired title text
+        * @return String
         */
        static function displaytitle( $parser, $text = '' ) {
                global $wgRestrictDisplayTitle;
@@ -330,37 +357,37 @@ class CoreParserFunctions {
         */
        static function mwnamespace( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return str_replace( '_', ' ', $t->getNsText() );
        }
        static function namespacee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return wfUrlencode( $t->getNsText() );
        }
        static function talkspace( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) || !$t->canTalk() )
+               if ( is_null( $t ) || !$t->canTalk() )
                        return '';
                return str_replace( '_', ' ', $t->getTalkNsText() );
        }
        static function talkspacee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) || !$t->canTalk() )
+               if ( is_null( $t ) || !$t->canTalk() )
                        return '';
                return wfUrlencode( $t->getTalkNsText() );
        }
        static function subjectspace( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return str_replace( '_', ' ', $t->getSubjectNsText() );
        }
        static function subjectspacee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return wfUrlencode( $t->getSubjectNsText() );
        }
@@ -370,98 +397,77 @@ class CoreParserFunctions {
        */
        static function pagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return wfEscapeWikiText( $t->getText() );
        }
        static function pagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return $t->getPartialURL();
        }
        static function fullpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) || !$t->canTalk() )
+               if ( is_null( $t ) || !$t->canTalk() )
                        return '';
                return wfEscapeWikiText( $t->getPrefixedText() );
        }
        static function fullpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) || !$t->canTalk() )
+               if ( is_null( $t ) || !$t->canTalk() )
                        return '';
                return $t->getPrefixedURL();
        }
        static function subpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return $t->getSubpageText();
        }
        static function subpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return $t->getSubpageUrlForm();
        }
        static function basepagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return $t->getBaseText();
        }
        static function basepagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
        }
        static function talkpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) || !$t->canTalk() )
+               if ( is_null( $t ) || !$t->canTalk() )
                        return '';
                return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
        }
        static function talkpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) || !$t->canTalk() )
+               if ( is_null( $t ) || !$t->canTalk() )
                        return '';
                return $t->getTalkPage()->getPrefixedUrl();
        }
        static function subjectpagename( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
        }
        static function subjectpagenamee( $parser, $title = null ) {
                $t = Title::newFromText( $title );
-               if ( is_null($t) )
+               if ( is_null( $t ) )
                        return '';
                return $t->getSubjectPage()->getPrefixedUrl();
        }
 
-       /**
-        * Performs the pipe trick in the same manner as [[title|]] or [[|title]].
-        * {{#pipetrick:title}} == {{#pipetrick:title|}} -> Parser::getPipeTrickText
-        * {{#pipetrick:|title}} -> Parser::getPipeTrickLink (rarer)
-        * See http://en.wikipedia.org/wiki/Help:Pipe_trick and the Parser documentation
-        * for more information.
-        */
-       static function pipetrick( $parser, $link = '', $text = '' ) {
-               if ($link)
-                       return $parser->getPipeTrickText( $link );
-               else
-                       return $parser->getPipeTrickLink( $text );
-       }
-
-       /**
-        * Performs the pipetrick and then url encodes the result
-        */
-       static function pipetricke( $parser, $link = '', $text = '' ) {
-               return wfUrlEncode( str_replace( ' ', '_', CoreParserFunctions::pipetrick( &$parser, $link, $text ) ) );
-       }
-
        /**
         * Return the number of pages in the given category, or 0 if it's nonexis-
         * tent.  This is an expensive parser function and can't be called too many
@@ -492,16 +498,16 @@ class CoreParserFunctions {
         * Return the size of the given page, or 0 if it's nonexistent.  This is an
         * expensive parser function and can't be called too many times per page.
         *
-        * @FIXME This doesn't work correctly on preview for getting the size of
-        *   the current page.
-        * @FIXME Title::getLength() documentation claims that it adds things 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
+        * @todo Fixme: This doesn't work correctly on preview for getting the size
+        *   of the current page.
+        * @todo Fixme: Title::getLength() documentation claims that it adds things
+        *   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?
         */
        static function pagesize( $parser, $page = '', $raw = null ) {
                static $cache = array();
-               $title = Title::newFromText($page);
+               $title = Title::newFromText( $page );
 
                if( !is_object( $title ) ) {
                        $cache[$page] = 0;
@@ -515,7 +521,7 @@ class CoreParserFunctions {
                if( isset( $cache[$page] ) ) {
                        $length = $cache[$page];
                } elseif( $parser->incrementExpensiveFunctionCount() ) {
-                       $rev = Revision::newFromTitle($title);
+                       $rev = Revision::newFromTitle( $title );
                        $id = $rev ? $rev->getPage() : 0;
                        $length = $cache[$page] = $rev ? $rev->getSize() : 0;
 
@@ -576,17 +582,14 @@ 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 ) {
-               $title = SpecialPage::getTitleForAlias( $text );
-               if ( $title ) {
-                       return $title->getPrefixedText();
+               list( $page, $subpage ) = SpecialPage::resolveAliasWithSubpage( $text );
+               if ( $page ) {
+                       $title = SpecialPage::getTitleFor( $page, $subpage );
+                       return $title;
                } else {
                        return wfMsgForContent( 'nosuchspecialpage' );
                }