Big oops - merged to wrong branch.
[lhc/web/wiklou.git] / includes / Linker.php
index d96052f..722df3a 100644 (file)
@@ -1,4 +1,25 @@
 <?php
+/**
+ * Methods to make links and related items.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
 /**
  * Some internal bits split of from Skin.php. These functions are used
  * for primarily page content: links, embedded images, table of contents. Links
@@ -20,6 +41,7 @@ class Linker {
         *
         * @param $class String: the contents of the class attribute; if an empty
         *   string is passed, which is the default value, defaults to 'external'.
+        * @return string
         * @deprecated since 1.18 Just pass the external class directly to something using Html::expandAttributes
         */
        static function getExternalLinkAttributes( $class = 'external' ) {
@@ -36,6 +58,7 @@ class Linker {
         * @param $unused String: unused
         * @param $class String: the contents of the class attribute; if an empty
         *   string is passed, which is the default value, defaults to 'external'.
+        * @return string
         */
        static function getInterwikiLinkAttributes( $title, $unused = null, $class = 'external' ) {
                global $wgContLang;
@@ -57,6 +80,7 @@ class Linker {
         *   not HTML-escaped
         * @param $unused String: unused
         * @param $class String: the contents of the class attribute, default none
+        * @return string
         */
        static function getInternalLinkAttributes( $title, $unused = null, $class = '' ) {
                $title = urldecode( $title );
@@ -73,6 +97,7 @@ class Linker {
         * @param $class String: the contents of the class attribute, default none
         * @param $title Mixed: optional (unescaped) string to use in the title
         *   attribute; if false, default to the name of the page we're linking to
+        * @return string
         */
        static function getInternalLinkAttributesObj( $nt, $unused = null, $class = '', $title = false ) {
                if ( $title === false ) {
@@ -154,20 +179,15 @@ class Linker {
         * @param $query         array  The query string to append to the URL
         *   you're linking to, in key => value array form.  Query keys and values
         *   will be URL-encoded.
-        * @param $options string|array  String or array:
-        *   - Either with numerical index and following values:
-        *     - 'known': Page is known to exist, so don't check if it does.
-        *     - 'broken': Page is known not to exist, so don't check if it does.
-        *     - 'noclasses': Don't add any classes automatically (includes "new",
+        * @param $options string|array  String or array of strings:
+        *     'known': Page is known to exist, so don't check if it does.
+        *     'broken': Page is known not to exist, so don't check if it does.
+        *     'noclasses': Don't add any classes automatically (includes "new",
         *       "stub", "mw-redirect", "extiw").  Only use the class attribute
         *       provided, if any, so you get a simple blue link with no funny i-
         *       cons.
-        *     'forcearticlepath': Use the article path always, even with a querystring.
+        *     'forcearticlepath': Use the article path always, even with a querystring.
         *       Has compatibility issues on some setups, so avoid wherever possible.
-        *   - Or with following indexes:
-        *     - 'language': the value of that index is the language to use; currently
-        *       only used for the tooltip when linking to a page that doesn't exist
-        *       (since 1.19)
         * @return string HTML <a> attribute
         */
        public static function link(
@@ -194,7 +214,7 @@ class Linker {
 
                # If we don't know whether the page exists, let's find out.
                wfProfileIn( __METHOD__ . '-checkPageExistence' );
-               if ( !in_array( 'known', $options, true ) && !in_array( 'broken', $options, true ) ) {
+               if ( !in_array( 'known', $options ) and !in_array( 'broken', $options ) ) {
                        if ( $target->isKnown() ) {
                                $options[] = 'known';
                        } else {
@@ -204,14 +224,14 @@ class Linker {
                wfProfileOut( __METHOD__ . '-checkPageExistence' );
 
                $oldquery = array();
-               if ( in_array( 'forcearticlepath', $options, true ) && $query ) {
+               if ( in_array( "forcearticlepath", $options ) && $query ) {
                        $oldquery = $query;
                        $query = array();
                }
 
                # Note: we want the href attribute first, for prettiness.
                $attribs = array( 'href' => self::linkUrl( $target, $query, $options ) );
-               if ( in_array( 'forcearticlepath', $options, true ) && $oldquery ) {
+               if ( in_array( 'forcearticlepath', $options ) && $oldquery ) {
                        $attribs['href'] = wfAppendQuery( $attribs['href'], wfArrayToCgi( $oldquery ) );
                }
 
@@ -234,6 +254,7 @@ class Linker {
 
        /**
         * Identical to link(), except $options defaults to 'known'.
+        * @return string
         */
        public static function linkKnown(
                $target, $html = null, $customAttribs = array(),
@@ -246,12 +267,15 @@ class Linker {
         * Returns the Url used to link to a Title
         *
         * @param $target Title
+        * @param $query Array: query parameters
+        * @param $options Array
+        * @return String
         */
        private static function linkUrl( $target, $query, $options ) {
                wfProfileIn( __METHOD__ );
                # We don't want to include fragments for broken links, because they
                # generally make no sense.
-               if ( in_array( 'broken', $options, true ) && $target->mFragment !== '' ) {
+               if ( in_array( 'broken', $options ) && $target->mFragment !== '' ) {
                        $target = clone $target;
                        $target->mFragment = '';
                }
@@ -259,7 +283,7 @@ class Linker {
                # If it's a broken link, add the appropriate query pieces, unless
                # there's already an action specified, or unless 'edit' makes no sense
                # (i.e., for a nonexistent special page).
-               if ( in_array( 'broken', $options, true ) && empty( $query['action'] )
+               if ( in_array( 'broken', $options ) && empty( $query['action'] )
                        && !$target->isSpecialPage() ) {
                        $query['action'] = 'edit';
                        $query['redlink'] = '1';
@@ -279,22 +303,24 @@ class Linker {
         * @return array
         */
        private static function linkAttribs( $target, $attribs, $options ) {
-               global $wgUser;
-
                wfProfileIn( __METHOD__ );
-
+               global $wgUser;
                $defaults = array();
 
-               if ( !in_array( 'noclasses', $options, true ) ) {
+               if ( !in_array( 'noclasses', $options ) ) {
                        wfProfileIn( __METHOD__ . '-getClasses' );
                        # Now build the classes.
                        $classes = array();
 
+                       if ( in_array( 'broken', $options ) ) {
+                               $classes[] = 'new';
+                       }
+
                        if ( $target->isExternal() ) {
                                $classes[] = 'extiw';
-                       } elseif ( in_array( 'broken', $options, true ) ) {
-                               $classes[] = 'new';
-                       } else { # Avoid useless calls to LinkCache (see r50387)
+                       }
+
+                       if ( !in_array( 'broken', $options ) ) { # Avoid useless calls to LinkCache (see r50387)
                                $colour = self::getLinkColour( $target, $wgUser->getStubThreshold() );
                                if ( $colour !== '' ) {
                                        $classes[] = $colour; # mw-redirect or stub
@@ -310,14 +336,10 @@ class Linker {
                if ( $target->getPrefixedText() == '' ) {
                        # A link like [[#Foo]].  This used to mean an empty title
                        # attribute, but that's silly.  Just don't output a title.
-               } elseif ( in_array( 'known', $options, true ) ) {
+               } elseif ( in_array( 'known', $options ) ) {
                        $defaults['title'] = $target->getPrefixedText();
                } else {
-                       $msg = wfMessage( 'red-link-title', $target->getPrefixedText() );
-                       if ( isset( $options['language'] ) ) {
-                               $msg->inLanguage( $options['language'] );
-                       }
-                       $defaults['title'] = $msg->text();
+                       $defaults['title'] = wfMsg( 'red-link-title', $target->getPrefixedText() );
                }
 
                # Finally, merge the custom attribs with the default ones, and iterate
@@ -396,6 +418,31 @@ class Linker {
                return "<strong class=\"selflink\">{$prefix}{$html}{$inside}</strong>{$trail}";
        }
 
+       /**
+        * Get a message saying that an invalid title was encountered.
+        * This should be called after a method like Title::makeTitleSafe() returned
+        * a value indicating that the title object is invalid.
+        *
+        * @param $context IContextSource context to use to get the messages
+        * @param $namespace int Namespace number
+        * @param $title string Text of the title, without the namespace part
+        */
+       public static function getInvalidTitleDescription( IContextSource $context, $namespace, $title ) {
+               global $wgContLang;
+
+               // First we check whether the namespace exists or not.
+               if ( MWNamespace::exists( $namespace ) ) {
+                       if ( $namespace == NS_MAIN ) {
+                               $name = $context->msg( 'blanknamespace' )->text();
+                       } else {
+                               $name = $wgContLang->getFormattedNsText( $namespace );
+                       }
+                       return $context->msg( 'invalidtitle-knownnamespace', $namespace, $name, $title )->text();
+               } else {
+                       return $context->msg( 'invalidtitle-unknownnamespace', $namespace, $title )->text();
+               }
+       }
+
        /**
         * @param $title Title
         * @return Title
@@ -614,8 +661,9 @@ class Linker {
        /**
         * Get the link parameters for MediaTransformOutput::toHtml() from given
         * frame parameters supplied by the Parser.
-        * @param $frameParams The frame parameters
-        * @param $query An optional query string to add to description page links
+        * @param $frameParams array The frame parameters
+        * @param $query string An optional query string to add to description page links
+        * @return array
         */
        private static function getImageLinkMTOParams( $frameParams, $query = '' ) {
                $mtoParams = array();
@@ -645,6 +693,7 @@ class Linker {
         * @param $params Array
         * @param $framed Boolean
         * @param $manualthumb String
+        * @return mixed
         */
        public static function makeThumbLinkObj( Title $title, $file, $label = '', $alt,
                $align = 'right', $params = array(), $framed = false , $manualthumb = "" )
@@ -853,7 +902,7 @@ class Linker {
         * This will make a broken link if $file is false.
         *
         * @param $title Title object.
-        * @param $file File|false mixed File object or false
+        * @param $file File|bool mixed File object or false
         * @param $html String: pre-sanitized HTML
         * @return String: HTML
         *
@@ -897,6 +946,7 @@ class Linker {
         * @param $escape Boolean: do we escape the link text?
         * @param $linktype String: type of external link. Gets added to the classes
         * @param $attribs Array of extra attributes to <a>
+        * @return string
         */
        public static function makeExternalLink( $url, $text, $escape = true, $linktype = '', $attribs = array() ) {
                $class = "external";
@@ -925,16 +975,23 @@ class Linker {
        /**
         * Make user link (or user contributions for unregistered users)
         * @param $userId   Integer: user id in database.
-        * @param $userText String: user name in database
+        * @param $userName String: user name in database.
+        * @param $altUserName String: text to display instead of the user name (optional)
         * @return String: HTML fragment
+        * @since 1.19 Method exists for a long time. $displayText was added in 1.19.
         */
-       public static function userLink( $userId, $userText ) {
+       public static function userLink( $userId, $userName, $altUserName = false ) {
                if ( $userId == 0 ) {
-                       $page = SpecialPage::getTitleFor( 'Contributions', $userText );
+                       $page = SpecialPage::getTitleFor( 'Contributions', $userName );
                } else {
-                       $page = Title::makeTitle( NS_USER, $userText );
+                       $page = Title::makeTitle( NS_USER, $userName );
                }
-               return self::link( $page, htmlspecialchars( $userText ), array( 'class' => 'mw-userlink' ) );
+
+               return self::link(
+                       $page,
+                       htmlspecialchars( $altUserName !== false ? $altUserName : $userName ),
+                       array( 'class' => 'mw-userlink' )
+               );
        }
 
        /**
@@ -984,7 +1041,7 @@ class Linker {
                wfRunHooks( 'UserToolLinksEdit', array( $userId, $userText, &$items ) );
 
                if ( $items ) {
-                       return ' <span class="mw-usertoollinks">(' . $wgLang->pipeList( $items ) . ')</span>';
+                       return ' <span class="mw-usertoollinks">' . wfMessage( 'parentheses' )->rawParams( $wgLang->pipeList( $items ) )->escaped() . '</span>';
                } else {
                        return '';
                }
@@ -995,6 +1052,7 @@ class Linker {
         * @param $userId Integer: user identifier
         * @param $userText String: user name or IP address
         * @param $edits Integer: user edit count (optional, for performance)
+        * @return String
         */
        public static function userToolLinksRedContribs( $userId, $userText, $edits = null ) {
                return self::userToolLinks( $userId, $userText, true, 0, $edits );
@@ -1093,6 +1151,7 @@ class Linker {
         * @param $comment String
         * @param $title Mixed: Title object (to generate link to the section in autocomment) or null
         * @param $local Boolean: whether section links should refer to local page
+        * @return mixed|String
         */
        public static function formatComment( $comment, $title = null, $local = false ) {
                wfProfileIn( __METHOD__ );
@@ -1124,7 +1183,7 @@ class Linker {
         * Called by Linker::formatComment.
         *
         * @param $comment String: comment text
-        * @param $title An optional title object used to links to sections
+        * @param $title Title|null An optional title object used to links to sections
         * @param $local Boolean: whether section links should refer to local page
         * @return String: formatted comment
         */
@@ -1178,17 +1237,16 @@ class Linker {
                                $link = '';
                        }
                }
-               $auto = "$link$auto";
                if ( $pre ) {
                        # written summary $presep autocomment (summary /* section */)
-                       $auto = wfMsgExt( 'autocomment-prefix', array( 'escapenoentities', 'content' ) ) . $auto;
+                       $pre .= wfMsgExt( 'autocomment-prefix', array( 'escapenoentities', 'content' ) );
                }
                if ( $post ) {
                        # autocomment $postsep written summary (/* section */ summary)
                        $auto .= wfMsgExt( 'colon-separator', array( 'escapenoentities', 'content' ) );
                }
                $auto = '<span class="autocomment">' . $auto . '</span>';
-               $comment = $pre . $auto . $post;
+               $comment = $pre . $link . $wgLang->getDirMark() . '<span dir="auto">' . $auto . $post . '</span>';
                return $comment;
        }
 
@@ -1204,7 +1262,7 @@ class Linker {
         *
         * @todo FIXME: Doesn't handle sub-links as in image thumb texts like the main parser
         * @param $comment String: text to format links in
-        * @param $title An optional title object used to links to sections
+        * @param $title Title|null An optional title object used to links to sections
         * @param $local Boolean: whether section links should refer to local page
         * @return String
         */
@@ -1387,7 +1445,8 @@ class Linker {
                        return '';
                } else {
                        $formatted = self::formatComment( $comment, $title, $local );
-                       return " <span class=\"comment\" dir=\"auto\">($formatted)</span>";
+                       $formatted = wfMessage( 'parentheses' )->rawParams( $formatted )->escaped();
+                       return " <span class=\"comment\">$formatted</span>";
                }
        }
 
@@ -1428,7 +1487,7 @@ class Linker {
                } else {
                        global $wgLang;
                        $stxt = wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $size ) );
-                       $stxt = "($stxt)";
+                       $stxt = wfMessage( 'parentheses' )->rawParams( $stxt )->escaped();
                }
                $stxt = htmlspecialchars( $stxt );
                return "<span class=\"history-size\">$stxt</span>";
@@ -1472,6 +1531,7 @@ class Linker {
         * End a Table Of Contents line.
         * tocUnindent() will be used instead if we're ending a line below
         * the new level.
+        * @return string
         */
        public static function tocLineEnd() {
                return "</li>\n";
@@ -1497,7 +1557,7 @@ class Linker {
         * Generate a table of contents from a section tree
         * Currently unused.
         *
-        * @param $tree Return value of ParserOutput::getSections()
+        * @param $tree array Return value of ParserOutput::getSections()
         * @return String: HTML fragment
         */
        public static function generateTOC( $tree ) {
@@ -1550,6 +1610,7 @@ class Linker {
        /**
         * Split a link trail, return the "inside" portion and the remainder of the trail
         * as a two-element array
+        * @return array
         */
        static function splitTrail( $trail ) {
                global $wgContLang;
@@ -1577,11 +1638,12 @@ class Linker {
         * other users.
         *
         * @param $rev Revision object
+        * @return string
         */
        public static function generateRollback( $rev ) {
-               return '<span class="mw-rollback-link">['
-                       . self::buildRollbackLink( $rev )
-                       . ']</span>';
+               return '<span class="mw-rollback-link">'
+                       . wfMessage( 'brackets' )->rawParams( self::buildRollbackLink( $rev ) )->plain()
+                       . '</span>';
        }
 
        /**
@@ -1707,7 +1769,7 @@ class Linker {
         * Format a size in bytes for output, using an appropriate
         * unit (B, KB, MB or GB) according to the magnitude in question
         *
-        * @param $size Size to format
+        * @param $size int Size to format
         * @return String
         */
        public static function formatSize( $size ) {
@@ -1854,7 +1916,7 @@ class Linker {
                $html = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
                $tag = $restricted ? 'strong' : 'span';
                $link = self::link( $sp, $html, array(), $query, array( 'known', 'noclasses' ) );
-               return Xml::tags( $tag, array( 'class' => 'mw-revdelundel-link' ), "($link)" );
+               return Xml::tags( $tag, array( 'class' => 'mw-revdelundel-link' ), wfMessage( 'parentheses' )->rawParams( $link )->escaped() );
        }
 
        /**
@@ -1867,7 +1929,7 @@ class Linker {
         */
        public static function revDeleteLinkDisabled( $delete = true ) {
                $html = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
-               return Xml::tags( 'span', array( 'class' => 'mw-revdelundel-link' ), "($html)" );
+               return Xml::tags( 'span', array( 'class' => 'mw-revdelundel-link' ), wfMessage( 'parentheses' )->rawParams( $html )->escaped() );
        }
 
        /* Deprecated methods */
@@ -1884,6 +1946,7 @@ class Linker {
         * @param $trail String: Optional trail. Alphabetic characters at the start of this string will
         *               be included in the link text. Other characters will be appended after
         *               the end of the link.
+        * @return string
         */
        static function makeBrokenLink( $title, $text = '', $query = '', $trail = '' ) {
                wfDeprecated( __METHOD__, '1.16' );
@@ -1912,6 +1975,7 @@ class Linker {
         *                      be included in the link text. Other characters will be appended after
         *                      the end of the link.
         * @param $prefix String: optional prefix. As trail, only before instead of after.
+        * @return string
         */
        static function makeLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
                # wfDeprecated( __METHOD__, '1.16' ); // See r105985 and it's revert. Somewhere still used.
@@ -1943,7 +2007,7 @@ class Linker {
         * @param $prefix String: text before link text
         * @param $aprops String: extra attributes to the a-element
         * @param $style  String: style to apply - if empty, use getInternalLinkAttributesObj instead
-        * @return the a-element
+        * @return string the a-element
         */
        static function makeKnownLinkObj(
                $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = ''
@@ -1981,6 +2045,7 @@ class Linker {
         *                      be included in the link text. Other characters will be appended after
         *                      the end of the link.
         * @param $prefix String: Optional prefix
+        * @return string
         */
        static function makeBrokenLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
                wfDeprecated( __METHOD__, '1.16' );
@@ -2012,6 +2077,7 @@ class Linker {
         *                      be included in the link text. Other characters will be appended after
         *                      the end of the link.
         * @param $prefix String: Optional prefix
+        * @return string
         */
        static function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
                wfDeprecated( __METHOD__, '1.16' );
@@ -2026,6 +2092,7 @@ class Linker {
 
        /**
         * Returns the attributes for the tooltip and access key.
+        * @return array
         */
        public static function tooltipAndAccesskeyAttribs( $name ) {
                # @todo FIXME: If Sanitizer::expandAttributes() treated "false" as "output
@@ -2046,6 +2113,7 @@ class Linker {
 
        /**
         * Returns raw bits of HTML, use titleAttrib()
+        * @return null|string
         */
        public static function tooltip( $name, $options = null ) {
                # @todo FIXME: If Sanitizer::expandAttributes() treated "false" as "output
@@ -2072,6 +2140,7 @@ class DummyLinker {
         *
         * @param $fname String Name of called method
         * @param $args Array Arguments to the method
+        * @return mixed
         */
        public function __call( $fname, $args ) {
                return call_user_func_array( array( 'Linker', $fname ), $args );