CologneBlue rewrite: fix talkLink() to use generic nav links
authorMatmaRex <matma.rex@gmail.com>
Fri, 5 Oct 2012 18:13:15 +0000 (20:13 +0200)
committerAlexandre Emsenhuber <ialex.wiki@gmail.com>
Sun, 21 Oct 2012 16:39:43 +0000 (18:39 +0200)
* introduce processNavlinkForDocument() for repeated links
* remove wfFindFile() check for NS_FILE namespace: Linker::link() already
  checks this
* redo the way the message for the link is selected
* comment everything

Change-Id: Ibeb0a1bfa1f34b9fdd9f6e5f082d6973d1ba09b9

skins/CologneBlue.php

index 473ee2b..f7f08ec 100644 (file)
@@ -339,56 +339,79 @@ class CologneBlueTemplate extends BaseTemplate {
                }
        }
 
+       // @fixed
        function talkLink() {
                $title = $this->getSkin()->getTitle();
-               if ( NS_SPECIAL == $title->getNamespace() ) {
-                       # No discussion links for special pages
-                       return '';
+
+               if ( $title->getNamespace() == NS_SPECIAL ) {
+                       // No discussion links for special pages
+                       return "";
                }
 
-               $linkOptions = array();
-
-               if ( $title->isTalkPage() ) {
-                       $link = $title->getSubjectPage();
-                       switch( $link->getNamespace() ) {
-                               case NS_MAIN:
-                                       $text = wfMessage( 'articlepage' );
-                                       break;
-                               case NS_USER:
-                                       $text = wfMessage( 'userpage' );
-                                       break;
-                               case NS_PROJECT:
-                                       $text = wfMessage( 'projectpage' );
-                                       break;
-                               case NS_FILE:
-                                       $text = wfMessage( 'imagepage' );
-                                       # Make link known if image exists, even if the desc. page doesn't.
-                                       if ( wfFindFile( $link ) )
-                                               $linkOptions[] = 'known';
-                                       break;
-                               case NS_MEDIAWIKI:
-                                       $text = wfMessage( 'mediawikipage' );
-                                       break;
-                               case NS_TEMPLATE:
-                                       $text = wfMessage( 'templatepage' );
-                                       break;
-                               case NS_HELP:
-                                       $text = wfMessage( 'viewhelppage' );
-                                       break;
-                               case NS_CATEGORY:
-                                       $text = wfMessage( 'categorypage' );
-                                       break;
-                               default:
-                                       $text = wfMessage( 'articlepage' );
-                       }
+               $companionTitle = $title->isTalkPage() ? $title->getSubjectPage() : $title->getTalkPage();
+               $companionNamespace = $companionTitle->getNamespace();
+
+               // TODO these messages appear to only be used by CologneBlue and legacy skins,
+               // kill and replace with something more sensibly named?
+               $nsToMessage = array(
+                       NS_MAIN => 'articlepage',
+                       NS_USER => 'userpage',
+                       NS_PROJECT => 'projectpage',
+                       NS_FILE => 'imagepage',
+                       NS_MEDIAWIKI => 'mediawikipage',
+                       NS_TEMPLATE => 'templatepage',
+                       NS_HELP => 'viewhelppage',
+                       NS_CATEGORY => 'categorypage',
+                       NS_FILE => 'imagepage',
+               );
+
+               // Find out the message to use for link text. Use either the array above or,
+               // for non-talk pages, a generic "discuss this" message.
+               // Default is the same as for main namespace.
+               if ( isset( $nsToMessage[$companionNamespace] ) ) {
+                       $message = $nsToMessage[$companionNamespace];
                } else {
-                       $link = $title->getTalkPage();
-                       $text = wfMessage( 'talkpage' );
+                       $message = $companionTitle->isTalkPage() ? 'talkpage' : 'articlepage';
                }
 
-               $s = Linker::link( $link, $text->text(), array(), array(), $linkOptions );
+               // Obviously this can't be reasonable and just return the key for talk namespace, only for content ones.
+               // Thus we have to mangle it in exactly the same way SkinTemplate does. (bug 40805)
+               $key = $companionTitle->getNamespaceKey( '' );
+               if ( $companionTitle->isTalkPage() ) {
+                       $key = ( $key == 'main' ? 'talk' : $key . "_talk" );
+               }
 
-               return $s;
+               // Use the regular navigational link, but replace its text. Everything else stays unmodified.
+               $namespacesLinks = $this->data['content_navigation']['namespaces'];
+               $link = $this->processNavlinkForDocument( $namespacesLinks[ $key ] );
+               $link['text'] = wfMessage( $message )->text();
+
+               return $this->makeListItem( $message, $link, array( 'tag' => 'span' ) );
+       }
+
+       /**
+        * Takes a navigational link generated by SkinTemplate in whichever way
+        * and mangles attributes unsuitable for repeated use. In particular, this modifies the ids
+        * and removes the accesskeys. This is necessary to be able to use the same navlink twice,
+        * e.g. in sidebar and in footer.
+        *
+        * @param $navlink array Navigational link generated by SkinTemplate
+        * @param $idPrefix mixed Prefix to add to id of this navlink. If false, id is removed entirely. Default is 'cb-'.
+        */
+       function processNavlinkForDocument( $navlink, $idPrefix='cb-' ) {
+               if ( $navlink['id'] ) {
+                       $navlink['single-id'] = $navlink['id']; // to allow for tooltip generation
+                       $navlink['tooltiponly'] = true; // but no accesskeys
+
+                       // mangle or remove the id
+                       if ( $idPrefix === false ) {
+                               unset( $navlink['id'] );
+                       } else {
+                               $navlink['id'] =  $idPrefix . $navlink['id'];
+                       }
+               }
+
+               return $navlink;
        }
        
        /**