Linker.php cleanup:
authorAryeh Gregor <simetrical@users.mediawiki.org>
Tue, 29 Jul 2008 00:08:25 +0000 (00:08 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Tue, 29 Jul 2008 00:08:25 +0000 (00:08 +0000)
* Allow makeLinkObj to accept an associative array of arguments for $aprops, so Brion's eyes can be saved from melting.
* Fail fast when various methods are passed non-Titles, don't just return some garbage and hope no one notices.
* Whitespace, wfDeprecated().

includes/Linker.php

index 0661e37..eaa56f3 100644 (file)
@@ -234,16 +234,10 @@ class Linker {
         *                      the end of the link.
         * @param $prefix String: optional prefix. As trail, only before instead of after.
         */
-       function makeLinkObj( $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
+       function makeLinkObj( Title $nt, $text= '', $query = '', $trail = '', $prefix = '' ) {
                global $wgUser;
                wfProfileIn( __METHOD__ );
 
-               if ( !$nt instanceof Title ) {
-                       # Fail gracefully
-                       wfProfileOut( __METHOD__ );
-                       return "<!-- ERROR -->{$prefix}{$text}{$trail}";
-               }
-
                if ( $nt->isExternal() ) {
                        $u = $nt->getFullURL();
                        $link = $nt->getPrefixedURL();
@@ -308,19 +302,16 @@ class Linker {
         * @param $query  String: link target
         * @param $trail  String: text after link
         * @param $prefix String: text before link text
-        * @param $aprops String: extra attributes to the a-element
+        * @param $aprops Mixed: extra attributes to the a-element.  If a string,
+        *   inserted literally into the HTML, with a space prepended.  It can also
+        *   be an associative array.  In this case the keys are attributes, and
+        *   values are *unescaped* attribute values.
         * @param $style  String: style to apply - if empty, use getInternalLinkAttributesObj instead
         * @return the a-element
         */
-       function makeKnownLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
+       function makeKnownLinkObj( Title $title, $text = '', $query = '', $trail = '', $prefix = '' , $aprops = '', $style = '' ) {
                wfProfileIn( __METHOD__ );
 
-               if ( !$title instanceof Title ) {
-                       # Fail gracefully
-                       wfProfileOut( __METHOD__ );
-                       return "<!-- ERROR -->{$prefix}{$text}{$trail}";
-               }
-
                $nt = $this->normaliseSpecialPage( $title );
 
                $u = $nt->escapeLocalURL( $query );
@@ -340,7 +331,16 @@ class Linker {
                        $style = $this->getInternalLinkAttributesObj( $nt, $text );
                }
 
-               if ( $aprops !== '' ) $aprops = ' ' . $aprops;
+               if( is_string( $aprops ) && $aprops != '' ) {
+                       $aprops = " $aprops";
+               } elseif( is_array( $aprops ) ) {
+                       $attributes = $aprops;
+                       $aprops = '';
+                       foreach( $attributes as $key => $value ) {
+                               $value = htmlspecialchars( $value );
+                               $aprops .= " $key=\"$value\"";
+                       }
+               }
 
                list( $inside, $trail ) = Linker::splitTrail( $trail );
                $r = "<a href=\"{$u}\"{$style}{$aprops}>{$prefix}{$text}{$inside}</a>{$trail}";
@@ -358,15 +358,9 @@ class Linker {
         *                      be included in the link text. Other characters will be appended after
         *                      the end of the link.
         */
-       function makeBrokenLinkObj( $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
+       function makeBrokenLinkObj( Title $title, $text = '', $query = '', $trail = '', $prefix = '' ) {
                wfProfileIn( __METHOD__ );
 
-               if ( !$title instanceof Title ) {
-                       # Fail gracefully
-                       wfProfileOut( __METHOD__ );
-                       return "<!-- ERROR -->{$prefix}{$text}{$trail}";
-               }
-
                $nt = $this->normaliseSpecialPage( $title );
 
                if( $nt->getNamespace() == NS_SPECIAL ) {
@@ -406,6 +400,7 @@ class Linker {
         *                      the end of the link.
         */
        function makeStubLinkObj( $nt, $text = '', $query = '', $trail = '', $prefix = '' ) {
+               wfDeprecated( __METHOD__ );
                return $this->makeColouredLinkObj( $nt, 'stub', $text, $query, $trail, $prefix );
        }
 
@@ -421,7 +416,6 @@ class Linker {
         *                      the end of the link.
         */
        function makeColouredLinkObj( $nt, $colour, $text = '', $query = '', $trail = '', $prefix = '' ) {
-
                if($colour != ''){
                        $style = $this->getInternalLinkAttributesObj( $nt, $text, $colour );
                } else $style = '';
@@ -1304,21 +1298,22 @@ class Linker {
         * @return         string HTML to use for edit link
         */
        public function doEditSectionLink( Title $nt, $section, $tooltip='' ) {
-               $attribs = '';
-               if( $tooltip ) {
-                       $attribs = wfMsgHtml( 'editsectionhint', htmlspecialchars( $tooltip ) );
-                       $attribs = " title=\"$attribs\"";
-               }
-
                $url = $this->makeKnownLinkObj(
                        $nt,
                        htmlspecialchars(wfMsg('editsection')),
                        "action=edit&section=$section",
-                       '', '', '',  $attribs
+                       '', '', '',
+                       array( 'title' => wfMsg( 'editsectionhint', $tooltip ) )
                );
 
-               # Run the old hook
+               # Run the old hook.  This takes up most of the function . . . hopefully
+               # we can rid of it someday.
                $result = null;
+               $attribs = '';
+               if( $tooltip ) {
+                       $attribs = wfMsgHtml( 'editsectionhint', htmlspecialchars( $tooltip ) );
+                       $attribs = " title=\"$attribs\"";
+               }
                wfRunHooks( 'EditSectionLink', array( &$this, $nt, $section, $attribs, $url, &$result ) );
                if( !is_null( $result ) ) {
                        # For reverse compatibility, add the brackets *after* the hook is