(bug 10672) Make Linker::doEditSectionLink protected, not private
[lhc/web/wiklou.git] / includes / Linker.php
index 0020091..56377fa 100644 (file)
@@ -627,34 +627,38 @@ class Linker {
        }
 
        /**
-        * 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;
        }
 
        /** @deprecated use Linker::makeMediaLinkObj() */
@@ -684,7 +688,7 @@ class Linker {
                                $class = 'internal';
                        } else {
                                $upload = SpecialPage::getTitleFor( 'Upload' );
-                               $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $title->getText() ) );
+                               $url = $upload->getLocalUrl( 'wpDestFile=' . urlencode( $title->getDbKey() ) );
                                $class = 'new';
                        }
                        $alt = htmlspecialchars( $title->getText() );
@@ -757,7 +761,7 @@ class Linker {
                if( $userId ) {
                        // check if the user has an edit
                        if( $redContribsWhenNoEdits && User::edits( $userId ) == 0 ) {
-                               $style = "class='new'";
+                               $style = " class='new'";
                        } else {
                                $style = '';
                        }
@@ -926,12 +930,13 @@ class Linker {
        }
 
        /**
-        * 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 ), '/' ) . '|';
@@ -1055,34 +1060,66 @@ class Linker {
                 . "</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 );
-               $result = null;
-               wfRunHooks( 'EditSectionLinkForOther', array( &$this, $title, $section, $url, &$result ) );
-               return is_null( $result )
-                       ? "<span class=\"editsection\"[{$url}]</span>"
-                       : "<span class=\"editsection\">[{$result}]</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='' ) {
+       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;
-               wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $hint, $url, &$result ) );
-               return is_null( $result )
-                       ? "<span class=\"editsection\">[{$url}]</span>"
-                       : "<span class=\"editsection\">[{$result}]</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>";
        }
 
        /**
@@ -1138,15 +1175,28 @@ class Linker {
         * @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
+               );              
        }
 
        /**
@@ -1285,4 +1335,5 @@ class Linker {
        }
 }
 
-?>
+
+