* (bug 4167) Fix regression caused by patch for bug 153
[lhc/web/wiklou.git] / includes / Linker.php
index 3f35d9c..4ffca4f 100644 (file)
@@ -131,7 +131,7 @@ class Linker {
         * Pass a title object, not a title string
         */
        function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
-               global $wgOut, $wgUser;
+               global $wgUser;
                $fname = 'Linker::makeLinkObj';
                wfProfileIn( $fname );
 
@@ -221,9 +221,15 @@ class Linker {
 
        /**
         * Pass a title object, not a title string
+        * @param object Title of target page
+        * @param string Text to replace the title
+        * @param string Link target
+        * @param string Text after link
+        * @param string Text before link text
+        * @param string Extra attributes to the a-element
+        * @return the a-element
         */
        function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
-               global $wgOut, $wgTitle;
 
                $fname = 'Linker::makeKnownLinkObj';
                wfProfileIn( $fname );
@@ -252,6 +258,8 @@ class Linker {
                        $text = htmlspecialchars( $nt->getPrefixedText() );
                }
                $style = $this->getInternalLinkAttributesObj( $nt, $text );
+
+               if ( $aprops !== '' ) $aprops = ' ' . $aprops;
                
                list( $inside, $trail ) = Linker::splitTrail( $trail );
                $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
@@ -323,7 +331,7 @@ class Linker {
         */
        function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
                global $wgUser;
-               $threshold = IntVal( $wgUser->getOption( 'stubthreshold' ) );
+               $threshold = intval( $wgUser->getOption( 'stubthreshold' ) );
                if( $size < $threshold ) {
                        return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
                } else {
@@ -359,7 +367,6 @@ class Linker {
 
        /** @todo document */
        function makeExternalImage( $url, $alt = '' ) {
-               global $wgOut;
                if ( '' == $alt ) {
                        $alt = $this->fnamePart( $url );
                }
@@ -407,28 +414,29 @@ class Linker {
                                         $wopt = User::getDefaultOption( 'thumbsize' );
                                }
                                
-                               $width = $wgThumbLimits[$wopt];
+                               $width = min( $img->getWidth(), $wgThumbLimits[$wopt] );
                        }
                        
                        return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $width, $height, $framed, $manual_thumb ).$postfix;
+               }
 
-               } elseif ( $width ) {
+               if ( $width && $img->exists() ) {
 
                        # Create a resized image, without the additional thumbnail
                        # features
 
-                       if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
-                               $width = $img->getWidth() * $height / $img->getHeight();
-                       }
+                       if ( $height == false )
+                               $height = -1;
                        if ( $manual_thumb == '') {
-                               $thumb = $img->getThumbnail( $width );
+                               $thumb = $img->getThumbnail( $width, $height );
                                if ( $thumb ) {
-                                       if( $width > $thumb->width ) {
+                                       if( $width > $img->width && ( $height == -1 || $height > $img->height )) {
                                                // Requested a display size larger than the actual image;
                                                // fake it up!
-                                               $height = floor($thumb->height * $width / $thumb->width);
+                                               $height = round($thumb->height * $width / $thumb->width);
                                                wfDebug( "makeImageLinkObj: client-size height set to '$height'\n" );
                                        } else {
+                                               $width = $thumb->width;
                                                $height = $thumb->height;
                                                wfDebug( "makeImageLinkObj: thumb height set to '$height'\n" );
                                        }
@@ -485,20 +493,21 @@ class Linker {
                {
                        // Use image dimensions, don't scale
                        $boxwidth  = $width;
-                       $oboxwidth = $boxwidth + 2;
                        $boxheight = $height;
                        $thumbUrl  = $url;
                } else {
-                       $h  = round( $height/($width/$boxwidth) );
-                       $oboxwidth = $boxwidth + 2;
-                       if ( ( ! $boxheight === false ) &&  ( $h > $boxheight ) )
-                       {
-                               $boxwidth *= $boxheight/$h;
-                       } else {
-                               $boxheight = $h;
+                       if ( $boxheight === false )
+                               $boxheight = -1;
+                       if ( '' == $manual_thumb ) {
+                               $thumb = $img->getThumbnail( $boxwidth, $boxheight );
+                               if ( $thumb ) {
+                                       $thumbUrl = $thumb->getUrl();
+                                       $boxwidth = $thumb->width;
+                                       $boxheight = $thumb->height;
+                               }
                        }
-                       if ( '' == $manual_thumb ) $thumbUrl = $img->createThumb( $boxwidth );
                }
+               $oboxwidth = $boxwidth + 2;
 
                if ( $manual_thumb != '' ) # Use manually specified thumbnail
                {
@@ -522,7 +531,11 @@ class Linker {
                $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
 
                $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
-               if ( $thumbUrl == '' ) {
+               if( $thumbUrl == '' ) {
+                       // Couldn't generate thumbnail? Scale the image client-side.
+                       $thumbUrl = $url;
+               }
+               if( !$img->exists() ) {
                        $s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
                        $zoomicon = '';
                } else {
@@ -575,7 +588,7 @@ class Linker {
        }
        
        /** @todo document */
-       function makeMediaLink( $name, $url, $alt = '' ) {
+       function makeMediaLink( $name, /* wtf?! */ $url, $alt = '' ) {
                $nt = Title::makeTitleSafe( NS_IMAGE, $name );
                return $this->makeMediaLinkObj( $nt, $alt );
        }
@@ -602,7 +615,7 @@ class Linker {
                        if( $img->exists() ) {
                                $url  = $img->getURL();
                                if( $nourl ) {
-                                       $url = str_replace( "http://", "http-noparse://", $url );
+                                       $url = str_replace( "http://", UNIQ_PREFIX . "NOPARSEhttp://", $url );
                                }
                                $class = 'internal';
                        } else {
@@ -707,7 +720,7 @@ class Linker {
                                $thelink = $this->makeMediaLink( $submatch[1], "", $text );
                        } else {
                                # Other kind of link
-                               if( preg_match( wfMsgForContent( "linktrail" ), $match[4], $submatch ) ) {
+                               if( preg_match( $wgContLang->linkTrail(), $match[4], $submatch ) ) {
                                        $trail = $submatch[1];
                                } else {
                                        $trail = "";
@@ -717,6 +730,8 @@ class Linker {
                                        $match[1] = substr($match[1], 1);
                                $thelink = $this->makeLink( $match[1], $text, "", $trail );
                        }
+                       # Quote backreferences, then run preg_replace
+                       $thelink = strtr( $thelink, array( "\\" => "\\\\", '$' => "\\$" ) );
                        $comment = preg_replace( $linkRegexp, $thelink, $comment, 1 );
                }
                wfProfileOut( $fname );
@@ -769,22 +784,23 @@ class Linker {
        /** @todo document */
        function tocList($toc) {
                global $wgJsMimeType;
-               return "<table id='toc' class='toc'><tr><td>" 
-                          . "<div id='toctitle'><h2>" . wfMsgForContent('toc') . "</h2></div>\n"
-                    . $toc
-                                . "</ul>\n</td></tr></table>\n"
-                                . '<script type="'.$wgJsMimeType.'">'
-                                . ' if (window.showTocToggle) {'
-                                . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
-                                . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
-                                . ' showTocToggle();'
-                                . ' } '
-                                . "</script>\n";
+               $title =  wfMsgForContent('toc') ;
+               return
+                  '<table id="toc" class="toc" summary="' . $title .'"><tr><td>' 
+                . '<div id="toctitle"><h2>' . $title . "</h2></div>\n"
+                . $toc
+                . "</ul>\n</td></tr></table>\n"
+                . '<script type="' . $wgJsMimeType . '">'
+                . ' if (window.showTocToggle) {'
+                . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
+                . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
+                . ' showTocToggle();'
+                . ' } '
+                . "</script>\n";
        }
 
        /** @todo document */
        function editSectionLinkForOther( $title, $section ) {
-               global $wgRequest;
                global $wgContLang;
 
                $title = Title::newFromText($title);