Fix extra space from r64084
[lhc/web/wiklou.git] / includes / Linker.php
index a25013b..c049f47 100644 (file)
@@ -110,7 +110,7 @@ class Linker {
                if ( $t->isRedirect() ) {
                        # Page is a redirect
                        $colour = 'mw-redirect';
-               } elseif ( $threshold > 0 && 
+               } elseif ( $threshold > 0 &&
                           $t->exists() && $t->getLength() < $threshold &&
                           MWNamespace::isContent( $t->getNamespace() ) ) {
                        # Page is a stub
@@ -220,6 +220,9 @@ class Linker {
                return $this->link( $target, $text, $customAttribs, $query, $options );
        }
 
+       /**
+        * Returns the Url used to link to a Title
+        */
        private function linkUrl( $target, $query, $options ) {
                wfProfileIn( __METHOD__ );
                # We don't want to include fragments for broken links, because they
@@ -242,6 +245,9 @@ class Linker {
                return $ret;
        }
 
+       /**
+        * Returns the array of attributes used when linking to the Title $target
+        */
        private function linkAttribs( $target, $attribs, $options ) {
                wfProfileIn( __METHOD__ );
                global $wgUser;
@@ -301,6 +307,9 @@ class Linker {
                return $ret;
        }
 
+       /**
+        * Default text of the links to the Title $target
+        */
        private function linkText( $target ) {
                # We might be passed a non-Title by make*LinkObj().  Fail gracefully.
                if( !$target instanceof Title ) {
@@ -341,7 +350,7 @@ class Linker {
         * despite $query not being used.
         */
        function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
-               if ( '' == $text ) {
+               if ( $text == '' ) {
                        $text = htmlspecialchars( $nt->getPrefixedText() );
                }
                list( $inside, $trail ) = Linker::splitTrail( $trail );
@@ -360,7 +369,10 @@ class Linker {
                }
        }
 
-       /** @todo document */
+       /**
+        * Returns the filename part of an url.
+        * Used as alternative text for external images.
+        */
        function fnamePart( $url ) {
                $basename = strrchr( $url, '/' );
                if ( false === $basename ) {
@@ -371,9 +383,12 @@ class Linker {
                return $basename;
        }
 
-       /** @todo document */
+       /**
+        * Return the code for images which were added via external links,
+        * via Parser::maybeMakeExternalImage().
+        */
        function makeExternalImage( $url, $alt = '' ) {
-               if ( '' == $alt ) {
+               if ( $alt == '' ) {
                        $alt = $this->fnamePart( $url );
                }
                $img = '';
@@ -453,7 +468,7 @@ class Linker {
                if ( $file && !isset( $hp['width'] ) ) {
                        $hp['width'] = $file->getWidth( $page );
 
-                       if( isset( $fp['thumbnail'] ) || isset( $fp['framed'] ) || isset( $fp['frameless'] ) || !$hp['width'] ) {
+                       if( isset( $fp['thumbnail'] ) || isset( $fp['framed'] ) || isset( $fp['frameless'] ) ) {
                                $wopt = $wgUser->getOption( 'thumbsize' );
 
                                if( !isset( $wgThumbLimits[$wopt] ) ) {
@@ -470,7 +485,9 @@ class Linker {
                                        round( $wgThumbLimits[$wopt] * $fp['upright'], -1 ) :
                                        $wgThumbLimits[$wopt];
                                if ( $hp['width'] <= 0 || $prefWidth < $hp['width'] ) {
-                                       $hp['width'] = $prefWidth;
+                                       if( !isset( $hp['height'] ) ) {
+                                               $hp['width'] = $prefWidth;
+                                       }
                                }
                        }
                }
@@ -506,7 +523,7 @@ class Linker {
                }
 
                if ( !$thumb ) {
-                       $s = $this->makeBrokenImageLinkObj( $title, '', '', '', '', $time==true );
+                       $s = $this->makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time==true );
                } else {
                        $params = array(
                                'alt' => $fp['alt'],
@@ -526,7 +543,7 @@ class Linker {
 
                        $s = $thumb->toHtml( $params );
                }
-               if ( '' != $fp['align'] ) {
+               if ( $fp['align'] != '' ) {
                        $s = "<div class=\"float{$fp['align']}\">{$s}</div>";
                }
                return str_replace("\n", ' ',$prefix.$s.$postfix);
@@ -606,7 +623,7 @@ class Linker {
                # So we don't need to pass it here in $query. However, the URL for the
                # zoom icon still needs it, so we make a unique query for it. See bug 14771
                $url = $title->getLocalURL( $query );
-               if( $page ) { 
+               if( $page ) {
                        $url = wfAppendQuery( $url, 'page=' . urlencode( $page ) );
                }
 
@@ -614,7 +631,7 @@ class Linker {
 
                $s = "<div class=\"thumb t{$fp['align']}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
                if( !$exists ) {
-                       $s .= $this->makeBrokenImageLinkObj( $title, '', '', '', '', $time==true );
+                       $s .= $this->makeBrokenImageLinkObj( $title, $fp['title'], '', '', '', $time==true );
                        $zoomicon = '';
                } elseif ( !$thumb ) {
                        $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
@@ -651,26 +668,31 @@ class Linker {
         * @return string
         */
        public function makeBrokenImageLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '', $time = false ) {
-               global $wgEnableUploads;
+               global $wgEnableUploads, $wgUploadNavigationUrl;
                if( $title instanceof Title ) {
                        wfProfileIn( __METHOD__ );
                        $currentExists = $time ? ( wfFindFile( $title ) != false ) : false;
-                       if( $wgEnableUploads && !$currentExists ) {
-                               $upload = SpecialPage::getTitleFor( 'Upload' );
+                       if( ( $wgUploadNavigationUrl || $wgEnableUploads ) && !$currentExists ) {
                                if( $text == '' )
                                        $text = htmlspecialchars( $title->getPrefixedText() );
+
                                $redir = RepoGroup::singleton()->getLocalRepo()->checkRedirect( $title );
                                if( $redir ) {
+                                       wfProfileOut( __METHOD__ );
                                        return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
                                }
-                               $q = 'wpDestFile=' . $title->getPartialUrl();
-                               if( $query != '' )
-                                       $q .= '&' . $query;
+                               
+                               $href = $this->getUploadUrl( $title, $query );
+
+
                                list( $inside, $trail ) = self::splitTrail( $trail );
-                               $style = $this->getInternalLinkAttributesObj( $title, $text, 'new' );
+
                                wfProfileOut( __METHOD__ );
-                               return '<a href="' . $upload->escapeLocalUrl( $q ) . '"'
-                                       . $style . '>' . $prefix . $text . $inside . '</a>' . $trail;
+                               return Html::element( 'a', array(
+                                       'href' => $href,
+                                       'class' => 'new',
+                                       'title' => $title->getPrefixedText()
+                               ), $prefix . $text . $inside ) . $trail;
                        } else {
                                wfProfileOut( __METHOD__ );
                                return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
@@ -679,6 +701,27 @@ class Linker {
                        return "<!-- ERROR -->{$prefix}{$text}{$trail}";
                }
        }
+       
+       /**
+        * Get the URL to upload a certain file
+        * 
+        * @param $destFile Title Title of the file to upload
+        * @param $query string Urlencoded query string to prepend
+        * @return string Urlencoded URL
+        */
+       protected function getUploadUrl( $destFile, $query = '' ) {
+               global $wgUploadNavigationUrl;
+               $q = 'wpDestFile=' . $destFile->getPartialUrl();
+               if( $query != '' )
+                       $q .= '&' . $query;
+
+               if( $wgUploadNavigationUrl ) {
+                       return wfAppendQuery( $wgUploadNavigationUrl, $q );
+               } else {
+                       $upload = SpecialPage::getTitleFor( 'Upload' );
+                       return $upload->getLocalUrl( $q );
+               }       
+       }
 
        /**
         * Create a direct link to a given uploaded file.
@@ -701,8 +744,7 @@ class Linker {
                                $url  = $img->getURL();
                                $class = 'internal';
                        } else {
-                               $upload = SpecialPage::getTitleFor( 'Upload' );
-                               $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $title->getDBkey() ) );
+                               $url = $this->getUploadUrl( $title );
                                $class = 'new';
                        }
                        $alt = htmlspecialchars( $title->getText() );
@@ -714,11 +756,15 @@ class Linker {
                }
        }
 
-       /** @todo document */
+       /**
+        *  Make a link to a special page given its name and, optionally,
+        * a message key from the link text.
+        * Usage example: $skin->specialLink( 'recentchanges' )
+        */
        function specialLink( $name, $key = '' ) {
                global $wgContLang;
 
-               if ( '' == $key ) { $key = strtolower( $name ); }
+               if ( $key == '' ) { $key = strtolower( $name ); }
                $pn = $wgContLang->ucfirst( $name );
                return $this->makeKnownLink( $wgContLang->specialPage( $pn ),
                  wfMsg( $key ) );
@@ -731,17 +777,20 @@ class Linker {
         * @param boolean $escape Do we escape the link text?
         * @param String $linktype Type of external link. Gets added to the classes
         * @param array $attribs Array of extra attributes to <a>
-        * 
-        * @TODO! @FIXME! This is a really crappy implementation. $linktype and 
+        *
+        * @todo FIXME: This is a really crappy implementation. $linktype and
         * 'external' are mashed into the class attrib for the link (which is made
-        * into a string). Then, if we've got additional params in $attribs, we 
+        * into a string). Then, if we've got additional params in $attribs, we
         * add to it. People using this might want to change the classes (or other
-        * default link attributes), but passing $attribsText is just messy. Would 
-        * make a lot more sense to make put the classes into $attribs, let the 
-        * hook play with them, *then* expand it all at once. 
+        * default link attributes), but passing $attribsText is just messy. Would
+        * make a lot more sense to make put the classes into $attribs, let the
+        * hook play with them, *then* expand it all at once.
         */
        function makeExternalLink( $url, $text, $escape = true, $linktype = '', $attribs = array() ) {
-               $attribsText = $this->getExternalLinkAttributes( 'external ' . $linktype );
+               if ( isset( $attribs[ 'class' ] ) ) $class = $attribs[ 'class' ]; # yet another hack :(
+               else $class =  'external ' . $linktype;
+
+               $attribsText = $this->getExternalLinkAttributes( $class );
                $url = htmlspecialchars( $url );
                if( $escape ) {
                        $text = htmlspecialchars( $text );
@@ -862,7 +911,7 @@ class Linker {
                if( $rev->isDeleted( Revision::DELETED_USER ) && $isPublic ) {
                        $link = wfMsgHtml( 'rev-deleted-user' );
                } else if( $rev->userCan( Revision::DELETED_USER ) ) {
-                       $link = $this->userLink( $rev->getUser( Revision::FOR_THIS_USER ), 
+                       $link = $this->userLink( $rev->getUser( Revision::FOR_THIS_USER ),
                                $rev->getUserText( Revision::FOR_THIS_USER ) );
                } else {
                        $link = wfMsgHtml( 'rev-deleted-user' );
@@ -884,7 +933,7 @@ class Linker {
                        $link = wfMsgHtml( 'rev-deleted-user' );
                } else if( $rev->userCan( Revision::DELETED_USER ) ) {
                        $userId = $rev->getUser( Revision::FOR_THIS_USER );
-                       $userText = $rev->getUserText( Revision::FOR_THIS_USER ); 
+                       $userText = $rev->getUserText( Revision::FOR_THIS_USER );
                        $link = $this->userLink( $userId, $userText ) .
                                ' ' . $this->userToolLinks( $userId, $userText );
                } else {
@@ -912,7 +961,7 @@ class Linker {
         * @param mixed $title Title object (to generate link to the section in autocomment) or null
         * @param bool $local Whether section links should refer to local page
         */
-       function formatComment($comment, $title = NULL, $local = false) {
+       function formatComment($comment, $title = null, $local = false) {
                wfProfileIn( __METHOD__ );
 
                # Sanitize text a bit:
@@ -921,7 +970,7 @@ class Linker {
                $comment = Sanitizer::escapeHtmlAllowEntities( $comment );
 
                # Render autocomments and make links:
-               $comment = $this->formatAutoComments( $comment, $title, $local );
+               $comment = $this->formatAutocomments( $comment, $title, $local );
                $comment = $this->formatLinksInComment( $comment, $title, $local );
 
                wfProfileOut( __METHOD__ );
@@ -953,16 +1002,16 @@ class Linker {
                unset( $this->autocommentLocal );
                return $comment;
        }
-       
+
        private function formatAutocommentsCallback( $match ) {
                $title = $this->autocommentTitle;
                $local = $this->autocommentLocal;
-               
-               $pre=$match[1];
-               $auto=$match[2];
-               $post=$match[3];
-               $link='';
-               if( $title ) {
+
+               $pre = $match[1];
+               $auto = $match[2];
+               $post = $match[3];
+               $link = '';
+               if ( $title ) {
                        $section = $auto;
 
                        # Generate a valid anchor name from the section title.
@@ -976,7 +1025,7 @@ class Linker {
                        if ( $local ) {
                                $sectionTitle = Title::newFromText( '#' . $section );
                        } else {
-                               $sectionTitle = Title::makeTitleSafe( $title->getNamespace(), 
+                               $sectionTitle = Title::makeTitleSafe( $title->getNamespace(),
                                        $title->getDBkey(), $section );
                        }
                        if ( $sectionTitle ) {
@@ -1005,7 +1054,7 @@ class Linker {
         * Formats wiki links and media links in text; all other wiki formatting
         * is ignored
         *
-        * @fixme doesn't handle sub-links as in image thumb texts like the main parser
+        * @todo Fixme: doesn't handle sub-links as in image thumb texts like the main parser
         * @param string $comment Text to format links in
         * @return string
         */
@@ -1035,7 +1084,7 @@ class Linker {
                }
 
                # Handle link renaming [[foo|text]] will show link as "text"
-               if( "" != $match[3] ) {
+               if( $match[3] != "" ) {
                        $text = $match[3];
                } else {
                        $text = $match[1];
@@ -1058,11 +1107,11 @@ class Linker {
                        if (isset($match[1][0]) && $match[1][0] == ':')
                                $match[1] = substr($match[1], 1);
                        list( $inside, $trail ) = Linker::splitTrail( $trail );
-                       
+
                        $linkText = $text;
                        $linkTarget = Linker::normalizeSubpageLink( $this->commentContextTitle,
                                $match[1], $linkText );
-                       
+
                        $target = Title::newFromText( $linkTarget );
                        if( $target ) {
                                if( $target->getText() == '' && !$this->commentLocal && $this->commentContextTitle ) {
@@ -1083,7 +1132,7 @@ class Linker {
 
                return $comment;
        }
-       
+
        static function normalizeSubpageLink( $contextTitle, $target, &$text ) {
                # Valid link forms:
                # Foobar -- normal
@@ -1120,7 +1169,7 @@ class Linker {
                                }
 
                                $ret = $contextTitle->getPrefixedText(). '/' . trim($noslash) . $suffix;
-                               if( '' === $text ) {
+                               if( $text === '' ) {
                                        $text = $target . $suffix;
                                } # this might be changed for ugliness reasons
                        } else {
@@ -1138,7 +1187,7 @@ class Linker {
                                                # / at the end means don't show full path
                                                if( substr( $nodotdot, -1, 1 ) === '/' ) {
                                                        $nodotdot = substr( $nodotdot, 0, -1 );
-                                                       if( '' === $text ) {
+                                                       if( $text === '' ) {
                                                                $text = $nodotdot . $suffix;
                                                        }
                                                }
@@ -1166,7 +1215,7 @@ class Linker {
         *
         * @return string
         */
-       function commentBlock( $comment, $title = NULL, $local = false ) {
+       function commentBlock( $comment, $title = null, $local = false ) {
                // '*' used to be the comment inserted by the software way back
                // in antiquity in case none was provided, here for backwards
                // compatability, acc. to brion -ævar
@@ -1215,12 +1264,16 @@ class Linker {
                return "<span class=\"history-size\">$stxt</span>";
        }
 
-       /** @todo document */
+       /**
+        * Add another level to the Table of Contents
+        */
        function tocIndent() {
                return "\n<ul>";
        }
 
-       /** @todo document */
+       /**
+        * Finish one or more sublevels on the Table of Contents
+        */
        function tocUnindent($level) {
                return "</li>\n" . str_repeat( "</ul>\n</li>\n", $level>0 ? $level : 0 );
        }
@@ -1238,12 +1291,20 @@ class Linker {
                        $tocline . '</span></a>';
        }
 
-       /** @todo document */
+       /**
+        * End a Table Of Contents line.
+        * tocUnindent() will be used instead if we're ending a line below
+        * the new level.
+        */
        function tocLineEnd() {
                return "</li>\n";
        }
 
-       /** @todo document */
+       /**
+        * Wraps the TOC in a table and provides the hide/collapse javascript.
+        * @param string $toc html of the Table Of Contents
+        * @return string Full html of the TOC
+        */
        function tocList($toc) {
                $title = wfMsgHtml('toc') ;
                return
@@ -1261,9 +1322,10 @@ class Linker {
                        . ' } ' )
                . "\n";
        }
-       
+
        /**
         * Generate a table of contents from a section tree
+        * Currently unused.
         * @param $tree Return value of ParserOutput::getSections()
         * @return string HTML
         */
@@ -1278,7 +1340,7 @@ class Linker {
                                        $lastLevel - $section['toclevel'] );
                        else
                                $toc .= $this->tocLineEnd();
-                       
+
                        $toc .= $this->tocLine( $section['anchor'],
                                $section['line'], $section['number'],
                                $section['toclevel'], $section['index'] );
@@ -1301,6 +1363,8 @@ class Linker {
         * @return         string HTML to use for edit link
         */
        public function doEditSectionLink( Title $nt, $section, $tooltip = null ) {
+               // HTML generated here should probably have userlangattributes
+               // added to it for LTR text on RTL pages
                $attribs = array();
                if( !is_null( $tooltip ) ) {
                        $attribs['title'] = wfMsg( 'editsectionhint', $tooltip );
@@ -1376,7 +1440,7 @@ class Linker {
                        $regex = $wgContLang->linkTrail();
                }
                $inside = '';
-               if ( '' != $trail ) {
+               if ( $trail != '' ) {
                        $m = array();
                        if ( preg_match( $regex, $trail, $m ) ) {
                                $inside = $m[1];
@@ -1453,11 +1517,11 @@ class Linker {
                        # Construct the HTML
                        $outText = '<div class="mw-templatesUsedExplanation">';
                        if ( $preview ) {
-                               $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ) );
+                               $outText .= wfMsgExt( 'templatesusedpreview', array( 'parse' ), count( $templates ) );
                        } elseif ( $section ) {
-                               $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ) );
+                               $outText .= wfMsgExt( 'templatesusedsection', array( 'parse' ), count( $templates ) );
                        } else {
-                               $outText .= wfMsgExt( 'templatesused', array( 'parse' ) );
+                               $outText .= wfMsgExt( 'templatesused', array( 'parse' ), count( $templates ) );
                        }
                        $outText .= "</div><ul>\n";
 
@@ -1598,24 +1662,38 @@ class Linker {
                wfProfileOut( __METHOD__ );
                return false;
        }
-       
+
        /**
         * Creates a (show/hide) link for deleting revisions/log entries
         *
         * @param array $query  Query parameters to be passed to link()
         * @param bool $restricted  Set to true to use a <strong> instead of a <span>
+        * @param bool $delete Set to true to use (show/hide) rather than (show)
         *
         * @return string HTML <a> link to Special:Revisiondelete, wrapped in a
         * span to allow for customization of appearance with CSS
         */
-       public function revDeleteLink( $query = array(), $restricted = false ) {
+       public function revDeleteLink( $query = array(), $restricted = false, $delete = true ) {
                $sp = SpecialPage::getTitleFor( 'Revisiondelete' );
-               $text = wfMsgHtml( 'rev-delundel' );
+               $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
                $tag = $restricted ? 'strong' : 'span';
                $link = $this->link( $sp, $text, array(), $query, array( 'known', 'noclasses' ) );
                return Xml::tags( $tag, array( 'class' => 'mw-revdelundel-link' ), "($link)" );
        }
 
+       /**
+        * Creates a dead (show/hide) link for deleting revisions/log entries
+        *
+        * @param bool $delete Set to true to use (show/hide) rather than (show)
+        *
+        * @return string HTML text wrapped in a span to allow for customization
+        * of appearance with CSS
+        */
+       public function revDeleteLinkDisabled( $delete = true ) {
+               $text = $delete ? wfMsgHtml( 'rev-delundel' ) : wfMsgHtml( 'rev-showdeleted' );
+               return Xml::tags( 'span', array( 'class' => 'mw-revdelundel-link' ), "($text)" );
+       }
+
        /* Deprecated methods */
 
        /**
@@ -1938,11 +2016,11 @@ class Linker {
        }
 
        /**
-        * @deprecated Returns raw bits of HTML, use titleAttrib() and accesskey()
+        * Returns the attributes for the tooltip and access key
         */
-       public function tooltipAndAccesskey( $name ) {
-               global $wgDisableTooltipsAndAccesskeys;
-               if ($wgDisableTooltipsAndAccesskeys)
+       public function tooltipAndAccesskeyAttribs( $name ) {
+               global $wgEnableTooltipsAndAccesskeys;
+               if ( !$wgEnableTooltipsAndAccesskeys )
                        return array();
                # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
                # no attribute" instead of "output '' as value for attribute", this
@@ -1957,14 +2035,21 @@ class Linker {
                if ( $attribs['accesskey'] === false ) {
                        unset( $attribs['accesskey'] );
                }
-               return Xml::expandAttributes( $attribs );
+               return $attribs;
        }
+       /**
+        * @deprecated Returns raw bits of HTML, use titleAttrib() and accesskey()
+        */
+       public function tooltipAndAccesskey( $name ) {
+               return Xml::expandAttributes( $this->tooltipAndAccesskeyAttribs( $name ) );
+       }
+
 
        /** @deprecated Returns raw bits of HTML, use titleAttrib() */
        public function tooltip( $name, $options = null ) {
-               global $wgDisableTooltipsAndAccesskeys;
-               if ($wgDisableTooltipsAndAccesskeys)
-                       return array();
+               global $wgEnableTooltipsAndAccesskeys;
+               if ( !$wgEnableTooltipsAndAccesskeys )
+                       return '';
                # FIXME: If Sanitizer::expandAttributes() treated "false" as "output
                # no attribute" instead of "output '' as value for attribute", this
                # would be two lines.