* Fixed a typo that caused warnings
[lhc/web/wiklou.git] / includes / Linker.php
index c0bff50..3f35d9c 100644 (file)
@@ -20,7 +20,7 @@ class Linker {
        function Linker() {}
 
        /**
-        * OBSOLETE
+        * @deprecated
         */
        function postParseLinkColour( $s = NULL ) {
                return NULL;
@@ -81,16 +81,16 @@ class Linker {
         * otherwise the cache won't get updated properly.  See LINKCACHE.DOC.
         */
        function makeLink( $title, $text = '', $query = '', $trail = '' ) {
-               wfProfileIn( 'Skin::makeLink' );
+               wfProfileIn( 'Linker::makeLink' );
                $nt = Title::newFromText( $title );
                if ($nt) {
                        $result = $this->makeLinkObj( Title::newFromText( $title ), $text, $query, $trail );
                } else {
-                       wfDebug( 'Invalid title passed to Skin::makeLink(): "'.$title."\"\n" );
+                       wfDebug( 'Invalid title passed to Linker::makeLink(): "'.$title."\"\n" );
                        $result = $text == "" ? $title : $text;
                }
 
-               wfProfileOut( 'Skin::makeLink' );
+               wfProfileOut( 'Linker::makeLink' );
                return $result;
        }
 
@@ -100,7 +100,7 @@ class Linker {
                if ($nt) {
                        return $this->makeKnownLinkObj( Title::newFromText( $title ), $text, $query, $trail, $prefix , $aprops );
                } else {
-                       wfDebug( 'Invalid title passed to Skin::makeKnownLink(): "'.$title."\"\n" );
+                       wfDebug( 'Invalid title passed to Linker::makeKnownLink(): "'.$title."\"\n" );
                        return $text == '' ? $title : $text;
                }
        }
@@ -111,7 +111,7 @@ class Linker {
                if ($nt) {
                        return $this->makeBrokenLinkObj( Title::newFromText( $title ), $text, $query, $trail );
                } else {
-                       wfDebug( 'Invalid title passed to Skin::makeBrokenLink(): "'.$title."\"\n" );
+                       wfDebug( 'Invalid title passed to Linker::makeBrokenLink(): "'.$title."\"\n" );
                        return $text == '' ? $title : $text;
                }
        }
@@ -122,7 +122,7 @@ class Linker {
                if ($nt) {
                        return $this->makeStubLinkObj( Title::newFromText( $title ), $text, $query, $trail );
                } else {
-                       wfDebug( 'Invalid title passed to Skin::makeStubLink(): "'.$title."\"\n" );
+                       wfDebug( 'Invalid title passed to Linker::makeStubLink(): "'.$title."\"\n" );
                        return $text == '' ? $title : $text;
                }
        }
@@ -130,9 +130,9 @@ class Linker {
        /**
         * Pass a title object, not a title string
         */
-       function makeLinkObj( &$nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
-               global $wgOut, $wgUser, $wgInputEncoding;
-               $fname = 'Skin::makeLinkObj';
+       function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
+               global $wgOut, $wgUser;
+               $fname = 'Linker::makeLinkObj';
                wfProfileIn( $fname );
 
                # Fail gracefully
@@ -162,9 +162,7 @@ class Linker {
 
                        $parts = explode( '#', $u, 2 );
                        if ( count( $parts ) == 2 ) {
-                               $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $parts[1] ),
-                                                                       ENT_COMPAT,
-                                                                       $wgInputEncoding ) );
+                               $anchor = urlencode( Sanitizer::decodeCharReferences( str_replace(' ', '_', $parts[1] ) ) );
                                $replacearray = array(
                                        '%3A' => ':',
                                        '%' => '.'
@@ -177,6 +175,7 @@ class Linker {
 
                        $t = "<a href=\"{$u}\"{$style}>{$text}{$inside}</a>";
                                
+                       wfProfileOut( $fname );
                        return $t;
                } elseif ( $nt->isAlwaysKnown() ) {
                        # Image links, special page links and self-links with fragements are always known.
@@ -224,9 +223,9 @@ class Linker {
         * Pass a title object, not a title string
         */
        function makeKnownLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '' ) {
-               global $wgOut, $wgTitle, $wgInputEncoding;
+               global $wgOut, $wgTitle;
 
-               $fname = 'Skin::makeKnownLinkObj';
+               $fname = 'Linker::makeKnownLinkObj';
                wfProfileIn( $fname );
 
                if ( !is_object( $nt ) ) {
@@ -242,7 +241,7 @@ class Linker {
                                        $text = htmlspecialchars( $nt->getFragment() );
                                }
                        }
-                       $anchor = urlencode( do_html_entity_decode( str_replace(' ', '_', $nt->getFragment()), ENT_COMPAT, $wgInputEncoding ) );
+                       $anchor = urlencode( Sanitizer::decodeCharReferences( str_replace( ' ', '_', $nt->getFragment() ) ) );
                        $replacearray = array(
                                '%3A' => ':',
                                '%' => '.'
@@ -270,7 +269,7 @@ class Linker {
                        return "<!-- ERROR -->{$prefix}{$text}{$trail}";
                }
 
-               $fname = 'Skin::makeBrokenLinkObj';
+               $fname = 'Linker::makeBrokenLinkObj';
                wfProfileIn( $fname );
 
                if ( '' == $query ) {
@@ -310,6 +309,28 @@ class Linker {
                return $s;
        }
 
+       /**
+        * Generate either a normal exists-style link or a stub link, depending
+        * on the given page size.
+        *
+        * @param int $size
+        * @param Title $nt
+        * @param string $text
+        * @param string $query
+        * @param string $trail
+        * @param string $prefix
+        * @return string HTML of link
+        */
+       function makeSizeLinkObj( $size, $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
+               global $wgUser;
+               $threshold = IntVal( $wgUser->getOption( 'stubthreshold' ) );
+               if( $size < $threshold ) {
+                       return $this->makeStubLinkObj( $nt, $text, $query, $trail, $prefix );
+               } else {
+                       return $this->makeKnownLinkObj( $nt, $text, $query, $trail, $prefix );
+               }
+       }
+
        /** @todo document */
        function makeSelfLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
                $u = $nt->escapeLocalURL( $query );
@@ -347,7 +368,7 @@ class Linker {
        }
 
        /** @todo document */
-       function makeImageLinkObj( &$nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false, 
+       function makeImageLinkObj( $nt, $label, $alt, $align = '', $width = false, $height = false, $framed = false, 
          $thumb = false, $manual_thumb = '' ) 
        {
                global $wgContLang, $wgUser, $wgThumbLimits;
@@ -356,6 +377,8 @@ class Linker {
                $url   = $img->getViewURL();
                $prefix = $postfix = '';
                
+               wfDebug( "makeImageLinkObj: '$width'x'$height'\n" );
+               
                if ( 'center' == $align )
                {
                        $prefix  = '<div class="center">';
@@ -397,18 +420,38 @@ class Linker {
                        if ( $height !== false && ( $img->getHeight() * $width / $img->getWidth() > $height ) ) {
                                $width = $img->getWidth() * $height / $img->getHeight();
                        }
-                       if ( '' == $manual_thumb ) {
-                               $url = $img->createThumb( $width );
+                       if ( $manual_thumb == '') {
+                               $thumb = $img->getThumbnail( $width );
+                               if ( $thumb ) {
+                                       if( $width > $thumb->width ) {
+                                               // Requested a display size larger than the actual image;
+                                               // fake it up!
+                                               $height = floor($thumb->height * $width / $thumb->width);
+                                               wfDebug( "makeImageLinkObj: client-size height set to '$height'\n" );
+                                       } else {
+                                               $height = $thumb->height;
+                                               wfDebug( "makeImageLinkObj: thumb height set to '$height'\n" );
+                                       }
+                                       $url = $thumb->getUrl();
+                               }
                        }
+               } else {
+                       $width = $img->width;
+                       $height = $img->height;
                }
 
+               wfDebug( "makeImageLinkObj2: '$width'x'$height'\n" );
                $u = $nt->escapeLocalURL();
                if ( $url == '' ) {
                        $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
                        //$s .= "<br />{$alt}<br />{$url}<br />\n";
                } else {
                        $s = '<a href="'.$u.'" class="image" title="'.$alt.'">' .
-                                '<img src="'.$url.'" alt="'.$alt.'" longdesc="'.$u.'" /></a>';
+                                '<img src="'.$url.'" alt="'.$alt.'" ' .
+                                ( $width
+                                       ? ( 'width="'.$width.'" height="'.$height.'" ' )
+                                       : '' ) .
+                                'longdesc="'.$u.'" /></a>';
                }
                if ( '' != $align ) {
                        $s = "<div class=\"float{$align}\"><span>{$s}</span></div>";
@@ -446,7 +489,7 @@ class Linker {
                        $boxheight = $height;
                        $thumbUrl  = $url;
                } else {
-                       $h  = intval( $height/($width/$boxwidth) );
+                       $h  = round( $height/($width/$boxwidth) );
                        $oboxwidth = $boxwidth + 2;
                        if ( ( ! $boxheight === false ) &&  ( $h > $boxheight ) )
                        {
@@ -480,7 +523,7 @@ class Linker {
 
                $s = "<div class=\"thumb t{$align}\"><div style=\"width:{$oboxwidth}px;\">";
                if ( $thumbUrl == '' ) {
-                       $s .= $this->makeBrokenImageLinkObj( $img->getTitle );
+                       $s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
                        $zoomicon = '';
                } else {
                        $s .= '<a href="'.$u.'" class="internal" title="'.$alt.'">'.
@@ -503,14 +546,14 @@ class Linker {
        /**
         * Pass a title object, not a title string
         */
-       function makeBrokenImageLinkObj( &$nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
+       function makeBrokenImageLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
                # Fail gracefully
                if ( ! isset($nt) ) {
                        # wfDebugDieBacktrace();
                        return "<!-- ERROR -->{$prefix}{$text}{$trail}";
                }
 
-               $fname = 'Skin::makeBrokenImageLinkObj';
+               $fname = 'Linker::makeBrokenImageLinkObj';
                wfProfileIn( $fname );
 
                $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
@@ -556,16 +599,23 @@ class Linker {
                } else {
                        $name = $title->getDBKey();     
                        $img  = new Image( $title );
-                       $url  = $img->getURL();
-                       if( $nourl ) {
-                               $url = str_replace( "http://", "http-noparse://", $url );
+                       if( $img->exists() ) {
+                               $url  = $img->getURL();
+                               if( $nourl ) {
+                                       $url = str_replace( "http://", "http-noparse://", $url );
+                               }
+                               $class = 'internal';
+                       } else {
+                               $upload = Title::makeTitle( NS_SPECIAL, 'Upload' );
+                               $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
+                               $class = 'new';
                        }
                        $alt = htmlspecialchars( $title->getText() );
                        if( $text == '' ) {
                                $text = $alt;
                        }
                        $u = htmlspecialchars( $url );
-                       return "<a href=\"{$u}\" class='internal' title=\"{$alt}\">{$text}</a>";                        
+                       return "<a href=\"{$u}\" class='$class' title=\"{$alt}\">{$text}</a>";                  
                }
        }
 
@@ -608,7 +658,7 @@ class Linker {
         * temporarily to a value pass. Should be adjusted further. --brion
         */
        function formatComment($comment, $title = NULL) {
-               $fname = 'Skin::formatComment';
+               $fname = 'Linker::formatComment';
                wfProfileIn( $fname );
                
                global $wgContLang;
@@ -718,11 +768,12 @@ 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="text/javascript">'
+                                . '<script type="'.$wgJsMimeType.'">'
                                 . ' if (window.showTocToggle) {'
                                 . ' var tocShowText = "' . wfEscapeJsString( wfMsgForContent('showtoc') ) . '";'
                                 . ' var tocHideText = "' . wfEscapeJsString( wfMsgForContent('hidetoc') ) . '";'
@@ -753,16 +804,8 @@ class Linker {
 
        /** @todo document */
        function editSectionLink( $nt, $section ) {
-               global $wgRequest;
                global $wgContLang;
 
-               if( $wgRequest->getInt( 'oldid' ) && ( $wgRequest->getVal( 'diff' ) != '0' ) ) {
-                       # Section edit links would be out of sync on an old page.
-                       # But, if we're diffing to the current page, they'll be
-                       # correct.
-                       return '';
-               }
-
                $editurl = '&section='.$section;
                $url = $this->makeKnownLink($nt->getPrefixedText(),wfMsg('editsection'),'action=edit'.$editurl);