(bug 10672) Make Linker::doEditSectionLink protected, not private
[lhc/web/wiklou.git] / includes / Linker.php
index 75fd64c..56377fa 100644 (file)
@@ -228,7 +228,6 @@ class Linker {
                                }
                                wfProfileOut( $fname.'-immediate' );
                                wfProfileOut( $fname );
-print "returning $retVal;";
                                return $retVal;
                        }
 
@@ -332,7 +331,9 @@ print "returning $retVal;";
                $fname = 'Linker::makeBrokenLinkObj';
                wfProfileIn( $fname );
 
-               if ( '' == $query ) {
+               if( $nt->getNamespace() == NS_SPECIAL ) {
+                       $q = $query;
+               } else if ( '' == $query ) {
                        $q = 'action=edit';
                } else {
                        $q = 'action=edit&'.$query;
@@ -426,15 +427,27 @@ print "returning $retVal;";
                return $s;
        }
 
-       /** @todo document */
+       /** Creates the HTML source for images
+       * @param object $nt
+       * @param string $label label text
+       * @param string $alt alt text
+       * @param string $align horizontal alignment: none, left, center, right)
+       * @param array $params some format keywords: width, height, page, upright, upright_factor, frameless, border
+       * @param boolean $framed shows image in original size in a frame
+       * @param boolean $thumb shows image as thumbnail in a frame
+       * @param string $manual_thumb image name for the manual thumbnail
+       * @param string $valign vertical alignment: baseline, sub, super, top, text-top, middle, bottom, text-bottom
+       * @return string
+       */
        function makeImageLinkObj( $nt, $label, $alt, $align = '', $params = array(), $framed = false,
-         $thumb = false, $manual_thumb = '', $valign = '', $upright = false, $upright_factor = 0, $border = false )
+         $thumb = false, $manual_thumb = '', $valign = '', $time = false )
        {
                global $wgContLang, $wgUser, $wgThumbLimits, $wgThumbUpright;
 
-               $img   = new Image( $nt );
+               $img = wfFindFile( $nt, $time );
 
-               if ( !$img->allowInlineDisplay() && $img->exists() ) {
+               if ( $img && !$img->allowInlineDisplay() ) {
+                       wfDebug( __METHOD__.': '.$nt->getPrefixedDBkey()." does not allow inline display\n" );
                        return $this->makeKnownLinkObj( $nt );
                }
 
@@ -447,10 +460,9 @@ print "returning $retVal;";
                        $postfix = '</div>';
                        $align   = 'none';
                }
-
-               if ( !isset( $params['width'] ) ) {
+               if ( $img && !isset( $params['width'] ) ) {
                        $params['width'] = $img->getWidth( $page );
-                       if( $thumb || $framed ) {
+                       if( $thumb || $framed || isset( $params['frameless'] ) ) {
                                $wopt = $wgUser->getOption( 'thumbsize' );
 
                                if( !isset( $wgThumbLimits[$wopt] ) ) {
@@ -458,12 +470,12 @@ print "returning $retVal;";
                                }
 
                                // Reduce width for upright images when parameter 'upright' is used
-                               if ( $upright_factor == 0 ) {
-                                       $upright_factor = $wgThumbUpright;
+                               if ( !isset( $params['upright_factor'] ) || $params['upright_factor'] == 0 ) {
+                                       $params['upright_factor'] = $wgThumbUpright;
                                }
                                // Use width which is smaller: real image width or user preference width
                                // For caching health: If width scaled down due to upright parameter, round to full __0 pixel to avoid the creation of a lot of odd thumbs
-                               $params['width'] = min( $params['width'], $upright ? round( $wgThumbLimits[$wopt] * $upright_factor, -1 ) : $wgThumbLimits[$wopt] );
+                               $params['width'] = min( $params['width'], isset( $params['upright'] ) ? round( $wgThumbLimits[$wopt] * $params['upright_factor'], -1 ) : $wgThumbLimits[$wopt] );
                        }
                }
 
@@ -479,10 +491,10 @@ print "returning $retVal;";
                        if ( $align == '' ) {
                                $align = $wgContLang->isRTL() ? 'left' : 'right';
                        }
-                       return $prefix.$this->makeThumbLinkObj( $img, $label, $alt, $align, $params, $framed, $manual_thumb, $upright ).$postfix;
+                       return $prefix.$this->makeThumbLinkObj( $nt, $img, $label, $alt, $align, $params, $framed, $manual_thumb ).$postfix;
                }
 
-               if ( $params['width'] && $img->exists() ) {
+               if ( $img && $params['width'] ) {
                        # Create a resized image, without the additional thumbnail features
                        $thumb = $img->transform( $params );
                } else {
@@ -503,7 +515,7 @@ print "returning $retVal;";
                if ( $valign ) {
                        $imgAttribs['style'] = "vertical-align: $valign";
                }
-               if ( $border ) {
+               if ( isset( $params['border'] ) ) {
                        $imgAttribs['class'] = "thumbborder";
                }
                $linkAttribs = array(
@@ -513,7 +525,7 @@ print "returning $retVal;";
                );
 
                if ( !$thumb ) {
-                       $s = $this->makeBrokenImageLinkObj( $img->getTitle() );
+                       $s = $this->makeBrokenImageLinkObj( $nt );
                } else {
                        $s = $thumb->toHtml( $imgAttribs, $linkAttribs );
                }
@@ -525,57 +537,68 @@ print "returning $retVal;";
 
        /**
         * Make HTML for a thumbnail including image, border and caption
-        * $img is an Image object
+        * @param Title $nt 
+        * @param Image $img Image object or false if it doesn't exist
         */
-       function makeThumbLinkObj( $img, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manual_thumb = "", $upright = false ) {
+       function makeThumbLinkObj( Title $nt, $img, $label = '', $alt, $align = 'right', $params = array(), $framed=false , $manual_thumb = "" ) {
                global $wgStylePath, $wgContLang;
+               $exists = $img && $img->exists();
 
                $page = isset( $params['page'] ) ? $params['page'] : false;
 
                if ( empty( $params['width'] ) ) {
                        // Reduce width for upright images when parameter 'upright' is used 
-                       $params['width'] = $upright ? 130 : 180;
+                       $params['width'] = isset( $params['upright'] ) ? 130 : 180;
                }
                $thumb = false;
-               if ( $manual_thumb != '' ) {
-                       # Use manually specified thumbnail
-                       $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb );
-                       if( $manual_title ) {
-                               $manual_img = new Image( $manual_title );
-                               $thumb = $manual_img->getUnscaledThumb();
-                       }
-               } elseif ( $framed ) {
-                       // Use image dimensions, don't scale
-                       $thumb = $img->getUnscaledThumb( $page );
+
+               if ( !$exists ) {
+                       $outerWidth = $params['width'] + 2;
                } else {
-                       # Do not present an image bigger than the source, for bitmap-style images
-                       # This is a hack to maintain compatibility with arbitrary pre-1.10 behaviour
-                       $srcWidth = $img->getWidth( $page );
-                       if ( $srcWidth && !$img->mustRender() && $params['width'] > $srcWidth ) {
-                               $params['width'] = $srcWidth;
+                       if ( $manual_thumb != '' ) {
+                               # Use manually specified thumbnail
+                               $manual_title = Title::makeTitleSafe( NS_IMAGE, $manual_thumb );
+                               if( $manual_title ) {
+                                       $manual_img = wfFindFile( $manual_title );
+                                       if ( $manual_img ) {
+                                               $thumb = $manual_img->getUnscaledThumb();
+                                       } else {
+                                               $exists = false;
+                                       }
+                               }
+                       } elseif ( $framed ) {
+                               // Use image dimensions, don't scale
+                               $thumb = $img->getUnscaledThumb( $page );
+                       } else {
+                               # Do not present an image bigger than the source, for bitmap-style images
+                               # This is a hack to maintain compatibility with arbitrary pre-1.10 behaviour
+                               $srcWidth = $img->getWidth( $page );
+                               if ( $srcWidth && !$img->mustRender() && $params['width'] > $srcWidth ) {
+                                       $params['width'] = $srcWidth;
+                               }
+                               $thumb = $img->transform( $params );
                        }
-                       $thumb = $img->transform( $params );
-               }
 
-               if ( $thumb ) {
-                       $outerWidth = $thumb->getWidth() + 2;
-               } else {
-                       $outerWidth = $params['width'] + 2;
+                       if ( $thumb ) {
+                               $outerWidth = $thumb->getWidth() + 2;
+                       } else {
+                               $outerWidth = $params['width'] + 2;
+                       }
                }
 
                $query = $page ? 'page=' . urlencode( $page ) : '';
-               $u = $img->getTitle()->getLocalURL( $query );
+               $u = $nt->getLocalURL( $query );
 
                $more = htmlspecialchars( wfMsg( 'thumbnail-more' ) );
                $magnifyalign = $wgContLang->isRTL() ? 'left' : 'right';
                $textalign = $wgContLang->isRTL() ? ' style="text-align:right"' : '';
 
                $s = "<div class=\"thumb t{$align}\"><div class=\"thumbinner\" style=\"width:{$outerWidth}px;\">";
-               if ( !$thumb ) {
-                       $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
+               if( !$exists ) {
+                       $s .= $this->makeBrokenImageLinkObj( $nt );
                        $zoomicon = '';
-               } elseif( !$img->exists() ) {
-                       $s .= $this->makeBrokenImageLinkObj( $img->getTitle() );
+               } elseif ( !$thumb ) {
+                       $s .= htmlspecialchars( wfMsg( 'thumbnail_error', '' ) );
                        $zoomicon = '';
                } else {
                        $imgAttribs = array(
@@ -604,40 +627,44 @@ print "returning $retVal;";
        }
 
        /**
-        * Pass a title object, not a title string
+        * Make a "broken" link to an image
+        *
+        * @param Title $title Image title
+        * @param string $text Link label
+        * @param string $query Query string
+        * @param string $trail Link trail
+        * @param string $prefix Link prefix
+        * @return string
         */
-       function makeBrokenImageLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
-               # Fail gracefully
-               if ( ! isset($nt) ) {
-                       # throw new MWException();
+       public function makeBrokenImageLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
+               global $wgEnableUploads;
+               if( $title instanceof Title ) {
+                       wfProfileIn( __METHOD__ );
+                       if( $wgEnableUploads ) {
+                               $upload = SpecialPage::getTitleFor( 'Upload' );
+                               if( $text == '' )
+                                       $text = htmlspecialchars( $title->getPrefixedText() );
+                               $q = 'wpDestFile=' . $title->getPartialUrl();
+                               if( $query != '' )
+                                       $q .= '&' . $query;
+                               list( $inside, $trail ) = self::splitTrail( $trail );
+                               $style = $this->getInternalLinkAttributesObj( $title, $text, 'yes' );
+                               wfProfileOut( __METHOD__ );
+                               return '<a href="' . $upload->escapeLocalUrl( $q ) . '"'
+                                       . $style . '>' . $prefix . $text . $inside . '</a>' . $trail;
+                       } else {
+                               wfProfileOut( __METHOD__ );
+                               return $this->makeKnownLinkObj( $title, $text, $query, $trail, $prefix );
+                       }
+               } else {
                        return "<!-- ERROR -->{$prefix}{$text}{$trail}";
                }
-
-               $fname = 'Linker::makeBrokenImageLinkObj';
-               wfProfileIn( $fname );
-
-               $q = 'wpDestFile=' . urlencode( $nt->getDBkey() );
-               if ( '' != $query ) {
-                       $q .= "&$query";
-               }
-               $uploadTitle = SpecialPage::getTitleFor( 'Upload' );
-               $url = $uploadTitle->escapeLocalURL( $q );
-
-               if ( '' == $text ) {
-                       $text = htmlspecialchars( $nt->getPrefixedText() );
-               }
-               $style = $this->getInternalLinkAttributesObj( $nt, $text, "yes" );
-               list( $inside, $trail ) = Linker::splitTrail( $trail );
-               $s = "<a href=\"{$url}\"{$style}>{$prefix}{$text}{$inside}</a>{$trail}";
-
-               wfProfileOut( $fname );
-               return $s;
        }
 
-       /** @todo document */
-       function makeMediaLink( $name, /* wtf?! */ $url, $alt = '' ) {
+       /** @deprecated use Linker::makeMediaLinkObj() */
+       function makeMediaLink( $name, $unused = '', $text = '' ) {
                $nt = Title::makeTitleSafe( NS_IMAGE, $name );
-               return $this->makeMediaLinkObj( $nt, $alt );
+               return $this->makeMediaLinkObj( $nt, $text );
        }
 
        /**
@@ -655,13 +682,13 @@ print "returning $retVal;";
                        ### HOTFIX. Instead of breaking, return empty string.
                        return $text;
                } else {
-                       $img  = new Image( $title );
-                       if( $img->exists() ) {
+                       $img  = wfFindFile( $title );
+                       if( $img ) {
                                $url  = $img->getURL();
                                $class = 'internal';
                        } else {
                                $upload = SpecialPage::getTitleFor( 'Upload' );
-                               $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $img->getName() ) );
+                               $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $title->getDbKey() ) );
                                $class = 'new';
                        }
                        $alt = htmlspecialchars( $title->getText() );
@@ -734,7 +761,7 @@ print "returning $retVal;";
                if( $userId ) {
                        // check if the user has an edit
                        if( $redContribsWhenNoEdits && User::edits( $userId ) == 0 ) {
-                               $style = "class='new'";
+                               $style = " class='new'";
                        } else {
                                $style = '';
                        }
@@ -903,12 +930,13 @@ print "returning $retVal;";
        }
 
        /**
-        * Format regular and media links - all other wiki formatting is ignored
-        * Called by Linker::formatComment.
-        * @param $comment The comment text.
-        * @return Comment text with links using HTML.
+        * Formats wiki links and media links in text; all other wiki formatting
+        * is ignored
+        *
+        * @param string $comment Text to format links in
+        * @return string
         */
-       private function formatLinksInComment( $comment ) {
+       public function formatLinksInComment( $comment ) {
                global $wgContLang;
 
                $medians = '(?:' . preg_quote( Namespace::getCanonicalName( NS_MEDIA ), '/' ) . '|';
@@ -1032,31 +1060,66 @@ print "returning $retVal;";
                 . "</script>\n";
        }
 
-       /** @todo document */
+       /**
+        * Used to generate section edit links that point to "other" pages
+        * (sections that are really part of included pages).
+        *
+        * @param $title Title string.
+        * @param $section Integer: section number.
+        */
        public function editSectionLinkForOther( $title, $section ) {
-               global $wgContLang;
-
                $title = Title::newFromText( $title );
-               $editurl = '&section='.$section;
-               $url = $this->makeKnownLinkObj( $title, wfMsg('editsection'), 'action=edit'.$editurl );
-
-               return "<span class=\"editsection\">[".$url."]</span>";
-
+               return $this->doEditSectionLink( $title, $section, '', 'EditSectionLinkForOther' );
        }
 
        /**
-        * @param $title Title object.
+        * @param $nt Title object.
         * @param $section Integer: section number.
         * @param $hint Link String: title, or default if omitted or empty
         */
-       public function editSectionLink( $nt, $section, $hint='' ) {
-               global $wgContLang;
+       public function editSectionLink( Title $nt, $section, $hint='' ) {
+               if( $hint != '' ) {
+                       $hint = wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) );
+                       $hint = " title=\"$hint\"";
+               }
+               return $this->doEditSectionLink( $nt, $section, $hint, 'EditSectionLink' );
+       }
 
+       /**
+        * Implement editSectionLink and editSectionLinkForOther.
+        *
+        * @param $nt      Title object
+        * @param $section Integer, section number
+        * @param $hint    String, for HTML title attribute
+        * @param $hook    String, name of hook to run
+        * @return         String, HTML to use for edit link
+        */
+       protected function doEditSectionLink( Title $nt, $section, $hint, $hook ) {
+               global $wgContLang;
                $editurl = '&section='.$section;
-               $hint = ( $hint=='' ) ? '' : ' title="' . wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) ) . '"';
-               $url = $this->makeKnownLinkObj( $nt, wfMsg('editsection'), 'action=edit'.$editurl, '', '', '',  $hint );
+               $url = $this->makeKnownLinkObj(
+                       $nt,
+                       wfMsg('editsection'),
+                       'action=edit'.$editurl,
+                       '', '', '',  $hint
+               );
+               $result = null;
 
-               return "<span class=\"editsection\">[".$url."]</span>";
+               // The two hooks have slightly different interfaces . . .
+               if( $hook == 'EditSectionLink' ) {
+                       wfRunHooks( $hook, array( &$this, $nt, $section, $hint, $url, &$result ) );
+               } elseif( $hook == 'EditSectionLinkForOther' ) {
+                       wfRunHooks( $hook, array( &$this, $nt, $section, $url, &$result ) );
+               }
+               
+               // For reverse compatibility, add the brackets *after* the hook is run,
+               // and even add them to hook-provided text.
+               if( is_null( $result ) ) {
+                       $result = wfMsg( 'editsection-brackets', $url );
+               } else {
+                       $result = wfMsg( 'editsection-brackets', $result );
+               }
+               return "<span class=\"editsection\">$result</span>";
        }
 
        /**
@@ -1112,15 +1175,28 @@ print "returning $retVal;";
         * @param Revision $rev
         */
        function generateRollback( $rev ) {
-               global $wgUser, $wgRequest;
+               return '<span class="mw-rollback-link">['
+                       . $this->buildRollbackLink( $rev )
+                       . ']</span>';
+       }
+       
+       /**
+        * Build a raw rollback link, useful for collections of "tool" links
+        *
+        * @param Revision $rev
+        * @return string
+        */
+       public function buildRollbackLink( $rev ) {
+               global $wgRequest, $wgUser;
                $title = $rev->getTitle();
-
-               $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
-               $extraRollback .= '&token=' . urlencode(
-                       $wgUser->editToken( array( $title->getPrefixedText(), $rev->getUserText() ) ) );
-               return '<span class="mw-rollback-link">['. $this->makeKnownLinkObj( $title,
-                       wfMsg('rollbacklink'),
-                       'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extraRollback ) .']</span>';
+               $extra  = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
+               $extra .= '&token=' . urlencode( $wgUser->editToken( array( $title->getPrefixedText(),
+                       $rev->getUserText() ) ) );
+               return $this->makeKnownLinkObj(
+                       $title,
+                       wfMsgHtml( 'rollbacklink' ),
+                       'action=rollback&from=' . urlencode( $rev->getUserText() ) . $extra
+               );              
        }
 
        /**
@@ -1259,4 +1335,5 @@ print "returning $retVal;";
        }
 }
 
-?>
+
+