More skin system improvements; Create a new BaseTemplate class extended from QuickTem...
authorDaniel Friesen <dantman@users.mediawiki.org>
Mon, 6 Dec 2010 17:47:53 +0000 (17:47 +0000)
committerDaniel Friesen <dantman@users.mediawiki.org>
Mon, 6 Dec 2010 17:47:53 +0000 (17:47 +0000)
For the first helper in this class add a helper to use in generating a toolbox. Include a list item and link generating method that can be used to generate most of our sidebar links with minimal code.
Eventually this toolbox code will be used in code abstracting common sidebar generating code away into common helpers. For now make MonoBook and Vector and the skins based off of them use this method of generating toolboxes and sidebar links.

docs/hooks.txt
includes/SkinTemplate.php
skins/MonoBook.php
skins/Vector.php

index 407b7f8..ef66db0 100644 (file)
@@ -1165,6 +1165,15 @@ the SkinTemplateToolboxEnd hook instead, which works for all
 "SkinTemplate"-type skins.
 $tools: array of tools
 
+'BaseTemplateToolbox': Called by BaseTemplate when building the $toolbox array
+and returning it for the skin to output. You can add items to the toolbox while
+still letting the skin make final decisions on skin-specific markup conventions
+using this hook.
+&$sk: The BaseTemplate base skin template
+&$toolbox: An array of toolbox items, see BaseTemplate::getToolbox and
+       BaseTemplate::makeListItem for details on the format of individual
+       items inside of this array
+
 'NewRevisionFromEditComplete': called when a revision was inserted
 due to an edit
 $article: the article edited
index 7f20f66..a9324ce 100644 (file)
@@ -1183,3 +1183,178 @@ abstract class QuickTemplate {
                return ( $msg != '-' ) && ( $msg != '' ); # ????
        }
 }
+
+/**
+ * New base template for a skin's template extended from QuickTemplate
+ * this class features helper methods that provide common ways of interacting
+ * with the data stored in the QuickTemplate
+ */
+abstract class BaseTemplate extends QuickTemplate {
+       
+       /**
+        * Create an array of common toolbox items from the data in the quicktemplate
+        * stored by SkinTemplate.
+        * The resulting array is built acording to a format intended to be passed
+        * through makeListItem to generate the html.
+        */
+       function getToolbox() {
+               wfProfileIn( __METHOD__ );
+
+               $toolbox = array();
+               if ( $this->data['notspecialpage'] ) {
+                       $toolbox["whatlinkshere"] = $this->data['nav_urls']['whatlinkshere'];
+                       $toolbox["whatlinkshere"]["id"] = "t-whatlinkshere";
+                       if ( $this->data['nav_urls']['recentchangeslinked'] ) {
+                               $toolbox["recentchangeslinked"] = $this->data['nav_urls']['recentchangeslinked'];
+                               $toolbox["recentchangeslinked"]["msg"] = "recentchangeslinked-toolbox";
+                               $toolbox["recentchangeslinked"]["id"] = "t-recentchangeslinked";
+                       }
+               }
+               if( isset( $this->data['nav_urls']['trackbacklink'] ) && $this->data['nav_urls']['trackbacklink'] ) {
+                       $toolbox["trackbacklink"] = $this->data['nav_urls']['trackbacklink'];
+                       $toolbox["trackbacklink"]["id"] = "t-trackbacklink";
+               }
+               if ( $this->data['feeds'] ) {
+                       $toolbox["feeds"]["id"] = "feedlinks";
+                       $toolbox["feeds"]["links"] = array();
+                       foreach ( $this->data['feeds'] as $key => $feed ) {
+                               $toolbox["feeds"]["links"][$key] = $feed;
+                               $toolbox["feeds"]["links"][$key]["id"] = "feed-$key";
+                               $toolbox["feeds"]["links"][$key]["rel"] = "alternate";
+                               $toolbox["feeds"]["links"][$key]["type"] = "application/{$key}+xml";
+                               $toolbox["feeds"]["links"][$key]["class"] = "feedlink";
+                       }
+               }
+               foreach ( array('contributions', 'log', 'blockip', 'emailuser', 'upload', 'specialpages') as $special ) {
+                       if ( $this->data['nav_urls'][$special] ) {
+                               $toolbox[$special] = $this->data['nav_urls'][$special];
+                               $toolbox[$special]["id"] = "t-$special";
+                       }
+               }
+               if ( !empty( $this->data['nav_urls']['print']['href'] ) ) {
+                       $toolbox["print"] = $this->data['nav_urls']['print'];
+                       $toolbox["print"]["rel"] = "alternate";
+                       $toolbox["print"]["msg"] = "printableversion";
+               }
+               if ( !empty( $this->data['nav_urls']['permalink']['href'] ) ) {
+                       $toolbox["permalink"] = $this->data['nav_urls']['permalink'];
+                       $toolbox["permalink"]["id"] = "t-permalink";
+               } elseif ( $this->data['nav_urls']['permalink']['href'] === '' ) {
+                       $toolbox["permalink"] = $this->data['nav_urls']['permalink'];
+                       unset( $toolbox["permalink"]["href"] );
+                       $toolbox["ispermalink"]["tooltiponly"] = true;
+                       $toolbox["ispermalink"]["id"] = "t-ispermalink";
+                       $toolbox["ispermalink"]["msg"] = "permalink";
+               }
+               wfRunHooks( 'BaseTemplateToolbox', array( &$this, &$toolbox ) );
+               wfProfileOut( __METHOD__ );
+               return $toolbox;
+       }
+
+       /**
+        * Makes a link, usually used by makeListItem to generate a link for an item
+        * in a list used in navigation lists, portlets, portals, sidebars, etc...
+        * 
+        * $key is a string, usually a key from the list you are generating this link from
+        * $item is an array containing some of a specific set of keys.
+        * The text of the link will be generated either from the contents of the "text"
+        * key in the $item array, if a "msg" key is present a message by that name will
+        * be used, and if neither of those are set the $key will be used as a message name.
+        * If a "href" key is not present makeLink will just output htmlescaped text.
+        * The href, id, class, rel, and type keys are used as attributes for the link if present.
+        * If an "id" or "single-id" (if you don't want the actual id to be output on the link)
+        * is present it will be used to generate a tooltip and accesskey for the link.
+        * If you don't want an accesskey set $item["tooltiponly"] = true;
+        */
+       function makeLink($key, $item) {
+               if ( isset($item["text"]) ) {
+                       $text = $item["text"];
+               } else {
+                       $text = $this->translator->translate( isset($item["msg"]) ? $item["msg"] : $key );
+               }
+               
+               if ( !isset($item["href"]) ) {
+                       return htmlspecialchars($text);
+               }
+               
+               $attrs = array();
+               foreach ( array("href", "id", "class", "rel", "type") as $attr ) {
+                       if ( isset($item[$attr]) ) {
+                               $attrs[$attr] = $item[$attr];
+                       }
+               }
+               
+               if ( isset($item["id"]) ) {
+                       $item["single-id"] = $item["id"];
+               }
+               if ( isset($item["single-id"]) ) {
+                       if ( isset($item["tooltiponly"]) && $item["tooltiponly"] ) {
+                               $attrs["title"] = $this->skin->titleAttrib($item["single-id"]);
+                               if ( $attrs["title"] === false ) {
+                                       unset($attrs["title"]);
+                               }
+                       } else {
+                               $attrs = array_merge($attrs, $this->skin->tooltipAndAccesskeyAttribs($item["single-id"]));
+                       }
+               }
+               
+               return Html::element( "a", $attrs, $text );
+       }
+
+       /**
+        * Generates a list item for a navigation, portlet, portal, sidebar... etc list
+        * $key is a string, usually a key from the list you are generating this link from
+        * $item is an array of list item data containing some of a specific set of keys.
+        * The "id" and "class" keys will be used as attributes for the list item,
+        * if "active" contains a value of true a "active" class will also be appended to class.
+        * If you want something other than a <li> you can pass a tag name such as
+        * "tag" => "span" in the $options array to change the tag used.
+        * link/content data for the list item may come in one of two forms
+        * A "links" key may be used, in which case it should contain an array with
+        * a list of links to include inside the list item, see makeLink for the format
+        * of individual links array items.
+        * Otherwise the relevant keys from the list item $item array will be passed
+        * to makeLink instead. Note however that "id" and "class" are used by the
+        * list item directly so they will not be passed to makeLink
+        * (however the link will still support a tooltip and accesskey from it)
+        * If you need an id or class on a single link you should include a "links"
+        * array with just one link item inside of it.
+        */
+       function makeListItem($key, $item, $options = array()) {
+               if ( isset($item["links"]) ) {
+                       $html = '';
+                       foreach ( $item["links"] as $linkKey => $link ) {
+                               $html .= $this->makeLink($linkKey, $link);
+                       }
+               } else {
+                       $link = array();
+                       foreach ( array("text", "msg", "href", "rel", "type", "tooltiponly") as $k ) {
+                               if ( isset($item[$k]) ) {
+                                       $link[$k] = $item[$k];
+                               }
+                       }
+                       if ( isset($item["id"]) ) {
+                               // The id goes on the <li> not on the <a> for single links
+                               // but makeSidebarLink still needs to know what id to use when
+                               // generating tooltips and accesskeys.
+                               $link["single-id"] = $item["id"];
+                       }
+                       $html = $this->makeLink($key, $link);
+               }
+               
+               $attrs = array();
+               foreach ( array("id", "class") as $attr ) {
+                       if ( isset($item[$attr]) ) {
+                               $attrs[$attr] = $item[$attr];
+                       }
+               }
+               if ( isset($item["active"]) && $item["active"] ) {
+                       $attrs["class"] .= " active";
+                       $attrs["class"] = trim($attrs["class"]);
+               }
+               return Html::rawElement( isset($options["tag"]) ? $options["tag"] : "li", $attrs, $html );
+       }
+
+       
+}
+
index 8b0db5b..b57a45d 100644 (file)
@@ -51,7 +51,7 @@ class SkinMonoBook extends SkinTemplate {
  * @todo document
  * @ingroup Skins
  */
-class MonoBookTemplate extends QuickTemplate {
+class MonoBookTemplate extends BaseTemplate {
        var $skin;
        /**
         * Template filter callback for MonoBook skin.
@@ -269,49 +269,11 @@ class MonoBookTemplate extends QuickTemplate {
                <div class="pBody">
                        <ul>
 <?php
-               if($this->data['notspecialpage']) { ?>
-                               <li id="t-whatlinkshere"><a href="<?php
-                               echo htmlspecialchars($this->data['nav_urls']['whatlinkshere']['href'])
-                               ?>"<?php echo $this->skin->tooltipAndAccesskey('t-whatlinkshere') ?>><?php $this->msg('whatlinkshere') ?></a></li>
-<?php
-                       if( $this->data['nav_urls']['recentchangeslinked'] ) { ?>
-                               <li id="t-recentchangeslinked"><a href="<?php
-                               echo htmlspecialchars($this->data['nav_urls']['recentchangeslinked']['href'])
-                               ?>"<?php echo $this->skin->tooltipAndAccesskey('t-recentchangeslinked') ?>><?php $this->msg('recentchangeslinked-toolbox') ?></a></li>
-<?php          }
-               }
-               if( isset( $this->data['nav_urls']['trackbacklink'] ) && $this->data['nav_urls']['trackbacklink'] ) { ?>
-                       <li id="t-trackbacklink"><a href="<?php
-                               echo htmlspecialchars($this->data['nav_urls']['trackbacklink']['href'])
-                               ?>"<?php echo $this->skin->tooltipAndAccesskey('t-trackbacklink') ?>><?php $this->msg('trackbacklink') ?></a></li>
-<?php  }
-               if($this->data['feeds']) { ?>
-                       <li id="feedlinks"><?php foreach($this->data['feeds'] as $key => $feed) {
-                                       ?><a id="<?php echo Sanitizer::escapeId( "feed-$key" ) ?>" href="<?php
-                                       echo htmlspecialchars($feed['href']) ?>" rel="alternate" type="application/<?php echo $key ?>+xml" class="feedlink"<?php echo $this->skin->tooltipAndAccesskey('feed-'.$key) ?>><?php echo htmlspecialchars($feed['text'])?></a>&#160;
-                                       <?php } ?></li><?php
-               }
-
-               foreach( array('contributions', 'log', 'blockip', 'emailuser', 'upload', 'specialpages') as $special ) {
-
-                       if($this->data['nav_urls'][$special]) {
-                               ?><li id="t-<?php echo $special ?>"><a href="<?php echo htmlspecialchars($this->data['nav_urls'][$special]['href'])
-                               ?>"<?php echo $this->skin->tooltipAndAccesskey('t-'.$special) ?>><?php $this->msg($special) ?></a></li>
-<?php          }
-               }
+               foreach ( $this->getToolbox() as $key => $tbitem ) { ?>
+                               <?php echo $this->makeListItem($key, $tbitem); ?>
 
-               if(!empty($this->data['nav_urls']['print']['href'])) { ?>
-                               <li id="t-print"><a href="<?php echo htmlspecialchars($this->data['nav_urls']['print']['href'])
-                               ?>" rel="alternate"<?php echo $this->skin->tooltipAndAccesskey('t-print') ?>><?php $this->msg('printableversion') ?></a></li><?php
-               }
-
-               if(!empty($this->data['nav_urls']['permalink']['href'])) { ?>
-                               <li id="t-permalink"><a href="<?php echo htmlspecialchars($this->data['nav_urls']['permalink']['href'])
-                               ?>"<?php echo $this->skin->tooltipAndAccesskey('t-permalink') ?>><?php $this->msg('permalink') ?></a></li><?php
-               } elseif ($this->data['nav_urls']['permalink']['href'] === '') { ?>
-                               <li id="t-ispermalink"<?php echo $this->skin->tooltip('t-ispermalink') ?>><?php $this->msg('permalink') ?></li><?php
+<?php
                }
-
                wfRunHooks( 'MonoBookTemplateToolboxEnd', array( &$this ) );
                wfRunHooks( 'SkinTemplateToolboxEnd', array( &$this ) );
 ?>
@@ -329,10 +291,9 @@ class MonoBookTemplate extends QuickTemplate {
                <h5<?php $this->html('userlangattributes') ?>><?php $this->msg('otherlanguages') ?></h5>
                <div class="pBody">
                        <ul>
-<?php          foreach($this->data['language_urls'] as $langlink) { ?>
-                               <li class="<?php echo htmlspecialchars($langlink['class'])?>"><?php
-                               ?><a href="<?php echo htmlspecialchars($langlink['href']) ?>" title="<?php
-                               echo htmlspecialchars($langlink['title']) ?>"><?php echo $langlink['text'] ?></a></li>
+<?php          foreach($this->data['language_urls'] as $key => $langlink) { ?>
+                               <?php echo $this->makeListItem($key, $langlink); ?>
+
 <?php          } ?>
                        </ul>
                </div>
@@ -349,10 +310,9 @@ class MonoBookTemplate extends QuickTemplate {
                <div class='pBody'>
 <?php   if ( is_array( $cont ) ) { ?>
                        <ul>
-<?php                  foreach($cont as $val) { ?>
-                               <li id="<?php echo Sanitizer::escapeId($val['id']) ?>"<?php
-                                       if ( $val['active'] ) { ?> class="active" <?php }
-                               ?>><a href="<?php echo htmlspecialchars($val['href']) ?>"<?php echo $this->skin->tooltipAndAccesskey($val['id']) ?>><?php echo htmlspecialchars($val['text']) ?></a></li>
+<?php                  foreach($cont as $key => $val) { ?>
+                               <?php echo $this->makeListItem($key, $val); ?>
+
 <?php                  } ?>
                        </ul>
 <?php   } else {
index f972101..197d6bb 100644 (file)
@@ -339,7 +339,7 @@ class SkinVector extends SkinTemplate {
  * QuickTemplate class for Vector skin
  * @ingroup Skins
  */
-class VectorTemplate extends QuickTemplate {
+class VectorTemplate extends BaseTemplate {
 
        /* Members */
 
@@ -592,36 +592,13 @@ class VectorTemplate extends QuickTemplate {
        <h5<?php $this->html('userlangattributes') ?>><?php $this->msg( 'toolbox' ) ?></h5>
        <div class="body">
                <ul>
-               <?php if( $this->data['notspecialpage'] ): ?>
-                       <li id="t-whatlinkshere"><a href="<?php echo htmlspecialchars( $this->data['nav_urls']['whatlinkshere']['href'] ) ?>"<?php echo $this->skin->tooltipAndAccesskey( 't-whatlinkshere' ) ?>><?php $this->msg( 'whatlinkshere' ) ?></a></li>
-                       <?php if( $this->data['nav_urls']['recentchangeslinked'] ): ?>
-                       <li id="t-recentchangeslinked"><a href="<?php echo htmlspecialchars( $this->data['nav_urls']['recentchangeslinked']['href'] ) ?>"<?php echo $this->skin->tooltipAndAccesskey( 't-recentchangeslinked' ) ?>><?php $this->msg( 'recentchangeslinked-toolbox' ) ?></a></li>
-                       <?php endif; ?>
-               <?php endif; ?>
-               <?php if( isset( $this->data['nav_urls']['trackbacklink'] ) ): ?>
-               <li id="t-trackbacklink"><a href="<?php echo htmlspecialchars( $this->data['nav_urls']['trackbacklink']['href'] ) ?>"<?php echo $this->skin->tooltipAndAccesskey( 't-trackbacklink' ) ?>><?php $this->msg( 'trackbacklink' ) ?></a></li>
-               <?php endif; ?>
-               <?php if( $this->data['feeds']): ?>
-               <li id="feedlinks">
-                       <?php foreach( $this->data['feeds'] as $key => $feed ): ?>
-                       <a id="<?php echo Sanitizer::escapeId( "feed-$key" ) ?>" href="<?php echo htmlspecialchars( $feed['href'] ) ?>" rel="alternate" type="application/<?php echo $key ?>+xml" class="feedlink"<?php echo $this->skin->tooltipAndAccesskey( 'feed-' . $key ) ?>><?php echo htmlspecialchars( $feed['text'] ) ?></a>
-                       <?php endforeach; ?>
-               </li>
-               <?php endif; ?>
-               <?php foreach( array( 'contributions', 'log', 'blockip', 'emailuser', 'upload', 'specialpages' ) as $special ): ?>
-                       <?php if( $this->data['nav_urls'][$special]): ?>
-                       <li id="t-<?php echo $special ?>"><a href="<?php echo htmlspecialchars( $this->data['nav_urls'][$special]['href'] ) ?>"<?php echo $this->skin->tooltipAndAccesskey( 't-' . $special ) ?>><?php $this->msg( $special ) ?></a></li>
-                       <?php endif; ?>
-               <?php endforeach; ?>
-               <?php if( !empty( $this->data['nav_urls']['print']['href'] ) ): ?>
-               <li id="t-print"><a href="<?php echo htmlspecialchars( $this->data['nav_urls']['print']['href'] ) ?>" rel="alternate"<?php echo $this->skin->tooltipAndAccesskey( 't-print' ) ?>><?php $this->msg( 'printableversion' ) ?></a></li>
-               <?php endif; ?>
-               <?php if (  !empty(  $this->data['nav_urls']['permalink']['href'] ) ): ?>
-               <li id="t-permalink"><a href="<?php echo htmlspecialchars( $this->data['nav_urls']['permalink']['href'] ) ?>"<?php echo $this->skin->tooltipAndAccesskey( 't-permalink' ) ?>><?php $this->msg( 'permalink' ) ?></a></li>
-               <?php elseif ( $this->data['nav_urls']['permalink']['href'] === '' ): ?>
-               <li id="t-ispermalink"<?php echo $this->skin->tooltip( 't-ispermalink' ) ?>><?php $this->msg( 'permalink' ) ?></li>
-               <?php endif; ?>
-               <?php wfRunHooks( 'SkinTemplateToolboxEnd', array( &$this ) ); ?>
+<?php
+               foreach ( $this->getToolbox() as $key => $tbitem ): ?>
+                               <?php echo $this->makeListItem($key, $tbitem); ?>
+
+<?php
+               endforeach;
+               wfRunHooks( 'SkinTemplateToolboxEnd', array( &$this ) ); ?>
                </ul>
        </div>
 </div>
@@ -634,8 +611,9 @@ class VectorTemplate extends QuickTemplate {
        <h5<?php $this->html('userlangattributes') ?>><?php $this->msg( 'otherlanguages' ) ?></h5>
        <div class="body">
                <ul>
-               <?php foreach ( $this->data['language_urls'] as $langlink ): ?>
-                       <li class="<?php echo htmlspecialchars(  $langlink['class'] ) ?>"><a href="<?php echo htmlspecialchars( $langlink['href'] ) ?>" title="<?php echo htmlspecialchars( $langlink['title'] ) ?>"><?php echo $langlink['text'] ?></a></li>
+               <?php foreach ( $this->data['language_urls'] as $key => $langlink ): ?>
+                       <?php echo $this->makeListItem($key, $langlink); ?>
+
                <?php endforeach; ?>
                </ul>
        </div>
@@ -650,8 +628,9 @@ class VectorTemplate extends QuickTemplate {
        <div class="body">
                <?php if ( is_array( $content ) ): ?>
                <ul>
-               <?php foreach( $content as $val ): ?>
-                       <li id="<?php echo Sanitizer::escapeId( $val['id'] ) ?>"<?php if ( $val['active'] ): ?> class="active" <?php endif; ?>><a href="<?php echo htmlspecialchars( $val['href'] ) ?>"<?php echo $this->skin->tooltipAndAccesskey( $val['id'] ) ?>><?php echo htmlspecialchars( $val['text'] ) ?></a></li>
+               <?php foreach( $content as $key => $val ): ?>
+                       <?php echo $this->makeListItem($key, $val); ?>
+
                <?php endforeach; ?>
                </ul>
                <?php else: ?>