MediaWiki Sidebar

Upgrade for the existing extension.

Installation

Simply CustomNavBlocks.php and make the following adjustments to your LocalSettings.php:

require_once( "$IP/extensions/trunk/CustomNavBlocks/CustomNavBlocks.php" );
$wgCustomNavBlocksEnable = true;

Configuration

After the extension is enabled, MediaWiki will look for a page called MediaWiki:CustomNavBlocks. This file defines the blocks used in the sidebar and has the following format:

Pagename|title
Pagename2|title2

In this example, the first block will be the content of MediaWiki:pagename with the title 'title' and the second block will be the content of MediaWiki:pagename2 with the title 'title2'. Please note that each line must not have more than one pipe (“|”).

The default Navigation block is inserted by adding navigation to the MediaWiki:CustomNavBlocks, for example:

Pagename|title
navigation
Pagename2|title2

When Pagename does not exist, a link appears to allow you to create the page.

CustomNavBlocks.php

<?php
# Alert the user that this is not a valid entry point to MediaWiki if they try to access the special pages file directly.
if (!defined('MEDIAWIKI')) {
  echo <<<EOT
INSTRUCTIONS
EOT;
  exit( 1 );
}

$wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'addCustomNavBlocks';
$wgExtensionCredits['other'][] = array (
  'name' => 'CustomNavBlocks',
  'description' => 'Better customization of your sidebar',
  'version' => '2.1.0-1.13.0',
  'author' => 'Luuk Peters (2.0.0 by Mathias Ertl)',
  'url' => 'http://www.luukpeters.nl/devel_mediawiki/CustomNavBlocks',
);

function addCustomNavBlocks( $skin, $tpl ) {
  global $wgParser, $wgCustomNavBlocksEnable;
  if ( ! $wgCustomNavBlocksEnable )
    return true;

  $parserOptions = new ParserOptions();
  
  $CustomNavBlocksRaw = $tpl->translator->translate( 'CustomNavBlocks' );
  # remove comments
  $CustomNavBlocksClean = trim( preg_replace( array('/<!--(.*)-->/s'), array(''), $CustomNavBlocksRaw ) );
  $blocks = explode( "\n", $CustomNavBlocksClean );
  $sidebar = array();

  foreach ($blocks as $block) {

    $tmp = explode( '|', $block );

    if ( count( $tmp ) == 1 && isset($tpl->data['sidebar'][$block])) {
      # try to find default sidebar item  
      $sidebar[$block] = $tpl->data['sidebar'][$block];
    } else {
      
      # break if a line in MediaWiki:CustomNavBlocks has more than one "|"
      if ( count( $tmp ) != 2 ) {
      
        $editBlockTitle = '';
        $html = "unknown: " . $block .  "" ;
        
      } else {
  
        # some shortcuts
        $definition = $tmp[0];
        $blockTitle = $tmp[1];
        
        # first, we need a title object:
        $title = Title::newFromText( $definition, NS_MEDIAWIKI );
  
        # return false if a page defined by MediaWiki:CustomNavBlocks doesn't exist:
        if (!is_null($title) && ! $title->exists() ) {
          if(!is_null($title) && $title->quickUserCan('edit')){
            $editBlockTitle = '<span class="small">(<a href="'.$GLOBALS['wgScript'].'?title='.$title->getNsText().':'.$definition.'&action=edit">'.$tpl->translator->translate('create').'</a>)</span>';
          } else {
            $editBlockTitle = '';
          }
          $html = '';
        } else {
          # get article and content:
          $content = $tpl->translator->translate( "$definition" );
      
          # parse the mediawiki-syntax into html:
          $content = $wgParser->preprocess( $content, $title, $parserOptions );
          $parserOutput = $wgParser->parse( $content, $title, $parserOptions );
          $html = $parserOutput->getText();
      
          if(!is_null($title) && $title->quickUserCan('edit')){
            $editBlockTitle = '<span class="small">(<a href="'.$GLOBALS['wgScript'].'?title='.$title->getNsText().':'.$definition.'&action=edit">'.$tpl->translator->translate('edit').'</a>)</span>';
          } else {
            $editBlockTitle = '';
          }
        }
      }
      
      # make a sidebar block:
      $sidebar["$blockTitle $editBlockTitle"] = $html;
    }
  }

  $title = Title::newFromText( "MediaWiki:CustomNavBlocks", NS_MEDIAWIKI );
  if($title->quickUserCan('edit')){
    $editBlockTitle = '<span class="small">('.'<a href="'.$GLOBALS['wgScript'].'?title='.$title->getNsText().':CustomNavBlocks&action=edit">'.('sidebar')." ".$tpl->translator->translate('edit').'</a>)</span>';
    $sidebar[$editBlockTitle] = null;
  }
  # set sidebar to new thing:
  $tpl->set( 'sidebar', $sidebar );
  return true;
}

?>
webdev/mediawiki/customnavblocks.txt · Last modified: 2008/12/18 12:53 by luuk
Recent changes RSS feed Creative Commons License Donate Driven by DokuWiki