Linker::doEditSectionLink() and Linker::doEditSectionLinkForOther() and their respect...
[lhc/web/wiklou.git] / includes / Linker.php
index 4cb574a..2656b0d 100644 (file)
@@ -20,57 +20,94 @@ class Linker {
        /**
         * @deprecated
         */
-       function postParseLinkColour( $s = NULL ) {
-               return NULL;
+       function postParseLinkColour( $s = null ) {
+               return null;
        }
 
-       /** @todo document */
-       function getExternalLinkAttributes( $link, $text, $class='' ) {
-               $link = htmlspecialchars( $link );
-               $class = htmlspecialchars( $class );
-
-               $r = ($class != '') ? " class=\"$class\"" : " class=\"external\"";
-
-               $r .= " title=\"{$link}\"";
-               return $r;
+       /**
+        * Get the appropriate HTML attributes to add to the "a" element of an ex-
+        * ternal link, as created by [wikisyntax].
+        *
+        * @param string $title  The (unescaped) title text for the link
+        * @param string $unused Unused
+        * @param string $class  The contents of the class attribute; if an empty
+        *   string is passed, which is the default value, defaults to 'external'.
+        */
+       function getExternalLinkAttributes( $title, $unused = null, $class='' ) {
+               return $this->getLinkAttributesInternal( $title, $class, 'external' );
        }
 
-       function getInterwikiLinkAttributes( $link, $text, $class='' ) {
+       /**
+        * Get the appropriate HTML attributes to add to the "a" element of an in-
+        * terwiki link.
+        *
+        * @param string $title  The title text for the link, URL-encoded (???) but
+        *   not HTML-escaped
+        * @param string $unused Unused
+        * @param string $class  The contents of the class attribute; if an empty
+        *   string is passed, which is the default value, defaults to 'external'.
+        */
+       function getInterwikiLinkAttributes( $title, $unused = null, $class='' ) {
                global $wgContLang;
 
-               $link = urldecode( $link );
-               $link = $wgContLang->checkTitleEncoding( $link );
-               $link = preg_replace( '/[\\x00-\\x1f]/', ' ', $link );
-               $link = htmlspecialchars( $link );
+               # FIXME: We have a whole bunch of handling here that doesn't happen in
+               # getExternalLinkAttributes, why?
+               $title = urldecode( $title );
+               $title = $wgContLang->checkTitleEncoding( $title );
+               $title = preg_replace( '/[\\x00-\\x1f]/', ' ', $title );
 
-               $r = ($class != '') ? " class=\"$class\"" : " class=\"external\"";
+               return $this->getLinkAttributesInternal( $title, $class, 'external' );
+       }
 
-               $r .= " title=\"{$link}\"";
-               return $r;
+       /**
+        * Get the appropriate HTML attributes to add to the "a" element of an in-
+        * ternal link.
+        *
+        * @param string $title  The title text for the link, URL-encoded (???) but
+        *   not HTML-escaped
+        * @param string $unused Unused
+        * @param string $class  The contents of the class attribute, default none
+        */
+       function getInternalLinkAttributes( $title, $unused = null, $class='' ) {
+               $title = urldecode( $title );
+               $title = str_replace( '_', ' ', $title );
+               return $this->getLinkAttributesInternal( $title, $class );
        }
 
-       /** @todo document */
-       function getInternalLinkAttributes( $link, $text, $class='' ) {
-               $link = urldecode( $link );
-               $link = str_replace( '_', ' ', $link );
-               $link = htmlspecialchars( $link );
-               $r = ($class != '') ? ' class="' . htmlspecialchars( $class ) . '"' : '';
-               $r .= " title=\"{$link}\"";
-               return $r;
+       /**
+        * Get the appropriate HTML attributes to add to the "a" element of an in-
+        * ternal link, given the Title object for the page we want to link to.
+        *
+        * @param Title  $nt     The Title object
+        * @param string $unused Unused
+        * @param string $class  The contents of the class attribute, default none
+        * @param mixed  $title  Optional (unescaped) string to use in the title
+        *   attribute; if false, default to the name of the page we're linking to
+        */
+       function getInternalLinkAttributesObj( $nt, $unused = null, $class = '', $title = false ) {
+               if( $title === false ) {
+                       $title = $nt->getPrefixedText();
+               }
+               return $this->getLinkAttributesInternal( $title, $class );
        }
 
        /**
-        * @param $nt Title object.
-        * @param $text String: FIXME
-        * @param $class String: CSS class of the link, default ''.
+        * Common code for getLinkAttributesX functions
         */
-       function getInternalLinkAttributesObj( &$nt, $text, $class = '', $titleAttr = false ) {
-               $r = ($class != '') ? ' class="' . htmlspecialchars( $class ) . '"' : '';
-               if ( $titleAttr === false ) {
-                       $r .= ' title="' . $nt->getEscapedText() . '"';
-               } else {
-                       $r .= ' title="' . htmlspecialchars( $titleAttr ) . '"';
+       private function getLinkAttributesInternal( $title, $class, $classDefault = false ) {
+               $title = htmlspecialchars( $title );
+               if( $class === '' and $classDefault !== false ) {
+                       # FIXME: Parameter defaults the hard way!  We should just have
+                       # $class = 'external' or whatever as the default in the externally-
+                       # exposed functions, not $class = ''.
+                       $class = $classDefault;
                }
+               $class = htmlspecialchars( $class );
+               $r = '';
+               if( $class !== '' ) {
+                       $r .= " class=\"$class\"";
+               }
+               $r .= " title=\"$title\"";
                return $r;
        }
 
@@ -441,7 +478,7 @@ class Linker {
                } else {
                        $basename = substr( $basename, 1 );
                }
-               return htmlspecialchars( $basename );
+               return $basename;
        }
 
        /** Obsolete alias */
@@ -460,8 +497,10 @@ class Linker {
                        wfDebug("Hook LinkerMakeExternalImage changed the output of external image with url {$url} and alt text {$alt} to {$img}", true);
                        return $img;
                }
-               $s = '<img src="'.$url.'" alt="'.$alt.'" />';
-               return $s;
+               return Xml::element( 'img',
+                       array(
+                               'src' => $url,
+                               'alt' => $alt ) );
        }
 
        /**
@@ -1172,6 +1211,18 @@ class Linker {
                return $block;
        }
 
+       public function formatRevisionSize( $size ) {
+               if ( $size == 0 ) {
+                       $stxt = wfMsgExt( 'historyempty', 'parsemag' );
+               } else {
+                       global $wgLang;
+                       $stxt = wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $size ) );
+                       $stxt = "($stxt)";
+               }
+               $stxt = htmlspecialchars( $stxt );
+               return "<span class=\"history-size\">$stxt</span>";
+       }
+
        /** @todo document */
        function tocIndent() {
                return "\n<ul>";
@@ -1225,6 +1276,7 @@ class Linker {
         * @param $section Integer: section number.
         */
        public function editSectionLinkForOther( $title, $section ) {
+               wfDeprecated( __METHOD__ );
                $title = Title::newFromText( $title );
                return $this->doEditSectionLink( $title, $section, '', 'EditSectionLinkForOther' );
        }
@@ -1235,48 +1287,61 @@ class Linker {
         * @param $hint Link String: title, or default if omitted or empty
         */
        public function editSectionLink( Title $nt, $section, $hint='' ) {
-               if( $hint != '' ) {
-                       $hint = wfMsgHtml( 'editsectionhint', htmlspecialchars( $hint ) );
-                       $hint = " title=\"$hint\"";
-               }
+               wfDeprecated( __METHOD__ );
                return $this->doEditSectionLink( $nt, $section, $hint, 'EditSectionLink' );
        }
 
        /**
-        * Implement editSectionLink and editSectionLinkForOther.
+        * Create a section edit link.  This supersedes 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
+        * @param $nt      Title  The title being linked to (may not be the same as
+        *   $wgTitle, if the section is included from a template)
+        * @param $section string The designation of the section being pointed to,
+        *   to be included in the link, like "&section=$section"
+        * @param $tooltip string The tooltip to use for the link: will be escaped
+        *   and wrapped in the 'editsectionhint' message
+        * @return         string HTML to use for edit link
         */
-       protected function doEditSectionLink( Title $nt, $section, $hint, $hook ) {
-               global $wgContLang;
-               $editurl = '&section='.$section;
+       public function doEditSectionLink( Title $nt, $section, $tooltip='' ) {
+               global $wgTitle;
+               $attribs = '';
+               if( $tooltip ) {
+                       $attribs = wfMsgHtml( 'editsectionhint', htmlspecialchars( $tooltip ) );
+                       $attribs = " title=\"$attribs\"";
+               }
+
                $url = $this->makeKnownLinkObj(
                        $nt,
-                       wfMsg('editsection'),
-                       'action=edit'.$editurl,
-                       '', '', '',  $hint
+                       htmlspecialchars(wfMsg('editsection')),
+                       "action=edit&section=$section",
+                       '', '', '',  $attribs
                );
-               $result = null;
 
-               // The two hooks have slightly different interfaces . . .
-               if( $hook == 'EditSectionLink' ) {
-                       wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $hint, $url, &$result ) );
-               } elseif( $hook == 'EditSectionLinkForOther' ) {
+               # Run the old hooks
+               $result = null;
+               if( $nt->equals( $wgTitle ) ) {
+                       wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $url, &$result ) );
+               } else {
                        wfRunHooks( 'EditSectionLinkForOther', 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 );
+               if( !is_null( $result ) ) {
+                       # For reverse compatibility, add the brackets *after* the hook is
+                       # run, and even add them to hook-provided text.  These hooks have
+                       # inconsistent and redundant interfaces, which is why they should
+                       # no longer be used.  Use DoEditSectionLink instead.
+                       $result = wfMsgHtml( 'editsection-brackets', $url );
+                       return "<span class=\"editsection\">$result</span>";
                }
-               return "<span class=\"editsection\">$result</span>";
+
+               # Add the brackets and the span, and *then* run the nice new hook, with
+               # consistent and non-redundant arguments.
+               $result = wfMsgHtml( 'editsection-brackets', $url );
+               $result = "<span class=\"editsection\">$result</span>";
+
+               wfRunHooks( 'DoEditSectionLink', array( $this, $nt, $section, $tooltip, &$result ) );
+               return $result;
        }
 
        /**