User:Gatoatigrado/sidebarhack

From Wikipedia, the free encyclopedia

overview[edit]

This page is a quick hack to display a modified sidebar. It does not implement the more elegant solution of displaying user-defined toolbox items. Toolbox items (adding the horizontal line) is not complete yet; I would like Wikipedia's toolbox to add the cite item.

The very nice html output is thanks to Kwrite.
non-formatted pages at monobook and skin.
also a complete diff_version_1.8_2006-10-04.

input[edit]

 * navigation
 ** mainpage|mainpage
 ** Wikipedia:Featured content|Featured content
 ** currentevents-url|currentevents
 ** Portal:Browse|Portals
 ** Wikipedia:Categorical index|Categories
 ** randompage-url|randompage
 * #searchbox
 * interact
 ** helppage|help
 ** portal-url|portal
 ** Wikipedia:Questions|Questions
 ** contact-url|Contact
 ** sitesupport-url|sitesupport

output[edit]

updated toolbox edits visible at http://wiki.ntung.com

skin.php[edit]

if the skin is set to monobook, the buildSidebar function will look for MediaWiki:monobooksidebar. It will handle the '#searchbox' header by assigning its value to "special:searchbox" instead of an array of list items. --gatoatigrado 17:09, 29 September 2006 (UTC)

<?php
	/**
	 * Build an array that represents the sidebar(s), the navigation bar among them
	 *
	 * @return array
	 * @private
	 */
	function buildSidebar() {
		global $wgDBname, $parserMemc, $wgEnableSidebarCache;
		global $wgLanguageCode, $wgContLanguageCode;
		
		// detect if monobook is set as the current skin
		// may be set to false if there is no MediaWiki:monobooksidebar
		$isMonobook = $this->skinname == "monobook";

		// run any hooks
		$fname = 'SkinTemplate::buildSidebar';
		wfProfileIn( $fname );
		
		$cacheSidebar = $wgEnableSidebarCache && ($wgLanguageCode == $wgContLanguageCode);
		
		// user has enabled sidebar cache
		if ($cacheSidebar)
		{
			// if the user is using monobook
			if($isMonobook)
			{
				$key = "$wgDBname:monobooksidebar";
				// if the cache exists, return.
				if(($cachedsidebar = $parserMemc->get($key)) !== false)
				{
					wfProfileOut($fname);
					return $cachedsidebar;
				}
				// if MediaWiki:monobooksidebar exists, parse, if it doesn't exist, try the other cache
				else if(wfEmptyMsg('monobooksidebar', ($monobooksidebar = wfMsgForContent('monobooksidebar'))))
				{
					$isMonobook = false;
				}
			}
			if(!$isMonobook)
			{
				$key = "$wgDBname:sidebar";
				if(($cachedsidebar = $parserMemc->get($key)) !== false)
				{
					wfProfileOut($fname);
					return $cachedsidebar;
				}
			}
		}
		
		// initialize output array
		$result = array();
		
		// if the user disabled the sidebar cache, and $monobooksidebar was never saved
		if($isMonobook && !isset($monobooksidebar)) { $monobooksidebar = wfMsgForContent('monobooksidebar'); }
		if($isMonobook && !wfEmptyMsg('monobooksidebar', $monobooksidebar)) { $lines = explode("\n", $monobooksidebar); }
		else { $lines = explode("\n", wfMsgForContent('sidebar')); }
		
		foreach ($lines as $line)
		{
			if (strpos($line, '*') !== 0)
				continue;
			if (strpos($line, '**') !== 0)
			{
				$line = trim($line, '* ');
				$heading = $line;
				if($heading == "#searchbox") { $result[$heading] = 'special:searchbox'; }
			}
			else if (strpos($line, '|') !== false)
			{
				$line = explode( '|' , trim($line, '* '), 2 );
				$link = wfMsgForContent( $line[0] );
				if ($link == '-')
					continue;
				if (wfEmptyMsg($line[1], $text = wfMsg($line[1])))
					$text = $line[1];
				if (wfEmptyMsg($line[0], $link))
					$link = $line[0];
				$href = $this->makeInternalOrExternalUrl( $link );
				if($heading == "#searchbox") { $result[$heading] = array(); }
				$result[$heading][] = array(
					'text' => $text,
					'href' => $href,
					'id' => 'n-' . strtr($line[1], ' ', '-'),
					'active' => false
				);
			}
		}
		
		if ($cacheSidebar)
			$cachednotice = $parserMemc->set( $key, $result, 86400 );
		wfProfileOut( $fname );
		return $result;
	}
?>

monobook.php[edit]

The only modifications to monobook will make an exception for the header equal to #searchbox with content type string and value "special:searchbox". The code for writing the searchbox was placed in another function. This function will be called when the header #searchbox is reached, or after the end of the foreach loop for all static sidebar headers. --gatoatigrado 17:05, 29 September 2006 (UTC)

The toolbox was cleaned up. It now displays a horizontal line between page content and usual toolbox links, such as "recent changes", "special pages", and "upload file". The line will not be present if there is no content above it. prior to cleanup. --gatoatigrado 20:33, 4 October 2006 (UTC)

<?php
/**
 * @todo document
 * @package MediaWiki
 * @subpackage Skins
 */
class MonoBookTemplate extends QuickTemplate {
	var $wrote_searchbox;
	// write the searchbox
	function write_searchbox() {
		if(!isset($this->wrote_searchbox)) { $this->wrote_searchbox = true; }
		else { return; } ?>
	<div id="p-search" class="portlet">
		<h5><label for="searchInput"><?php $this->msg('search') ?></label></h5>
		<div id="searchBody" class="pBody">
			<form action="<?php $this->text('searchaction') ?>" id="searchform"><div>
				<input id="searchInput" name="search" type="text" <?php
					if($this->haveMsg('accesskey-search')) {
						?>accesskey="<?php $this->msg('accesskey-search') ?>"<?php }
					if( isset( $this->data['search'] ) ) {
						?> value="<?php $this->text('search') ?>"<?php } ?> />
				<input type='submit' name="go" class="searchButton" id="searchGoButton"	value="<?php $this->msg('go') ?>" />&nbsp;
				<input type='submit' name="fulltext" class="searchButton" value="<?php $this->msg('search') ?>" />
			</div></form>
		</div>
	</div><?php }
	/**
	 * Template filter callback for MonoBook skin.
	 * Takes an associative array of data set from a SkinTemplate-based
	 * class, and a wrapper for MediaWiki's localization database, and
	 * outputs a formatted page.
	 *
	 * @access private
	 */
	function execute() {
?>
<!-- ... -->
	<?php
		foreach ($this->data['sidebar'] as $bar => $cont) {
			if($bar == '#searchbox' && $cont === 'special:searchbox')
			{
				$this->write_searchbox();
				continue;
			} ?>
	<div class='portlet' id='p-<?php echo htmlspecialchars($bar) ?>'>
		<h5><?php $out = wfMsg( $bar ); if (wfEmptyMsg($bar, $out)) echo $bar; else echo $out; ?></h5>
		<div class='pBody'>
			<ul>
<?php 			foreach($cont as $key => $val) { ?>
				<li id="<?php echo htmlspecialchars($val['id']) ?>"<?php
					if ( $val['active'] ) { ?> class="active" <?php }
				?>><a href="<?php echo htmlspecialchars($val['href']) ?>"><?php echo htmlspecialchars($val['text']) ?></a></li>
<?php			} ?>
			</ul>
		</div>
	</div>
	<?php } $this->write_searchbox(); ?>
<!-- ... -->
	<div class="portlet" id="p-tb">
		<h5><?php $this->msg('toolbox') ?></h5>
		<div class="pBody">
			<ul>
<?php
		$text_before_line = false;
		if($this->data['feeds']) {  $text_before_line = true; ?>
			<li id="feedlinks"><?php foreach($this->data['feeds'] as $key => $feed) {
					?><span id="feed-<?php echo htmlspecialchars($key) ?>"><a href="<?php
					echo htmlspecialchars($feed['href']) ?>"><?php echo htmlspecialchars($feed['text'])?></a>&nbsp;</span>
					<?php } ?></li><?php
		}

		foreach( array('permalink', 'print', 'recentchangeslinked', 'whatlinkshere',
				'trackbacklink', 'contributions', 'blockip', 'emailuser', '-', 'recentchanges', 'specialpages', 'upload') as $special ) {
			if($special == '-' && $text_before_line) { echo("\t\t\t</ul>\n\t\t\t\t<hr />\n\t\t\t<ul>\n"); }			
			else if($this->data['nav_urls'][$special]) {
				$text_before_line = true;
				$link = $this->data['nav_urls'][$special]['href'];
				?>
				<li id="t-<?php if($link == '') { echo "is"; } echo $special ?>"><?php if($link !== '') { ?><a href="<?php echo htmlspecialchars($link)
				?>"><?php } ?><?php if($special == 'print') { $special = 'printableversion'; } $this->msg($special) ?><?php if($link !== '') { ?></a><?php } ?></li>
<?php		}
		}

		wfRunHooks( 'MonoBookTemplateToolboxEnd', array( &$this ) );
?>
			</ul>
		</div>
	</div>

skintemplate.php[edit]

This adds a variable for the "recent changes" link in the toolbox.

<?php
	function buildNavUrls () {
		// ...
		$nav_urls['recentchanges'] = array('href' => $this->makeSpecialUrl('Recentchanges'));
?>