Personal tools
Make a donation
$ 




Extension:TreeNav.php

From OrganicDesign Wiki

Jump to: navigation, search
<?php
/**
 * TreeNav extension - Adds navigation to pages which are part of the sidebar tree
 * 
 * See http://www.organicdesign.co.nz/Extension:TreeNav.php for installation and usage details
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @author [http://www.mediawiki.org/wiki/User:Nad User:Nad]
 * @copyright © 2007 [http://www.mediawiki.org/wiki/User:Nad User:Nad]
 * @licence GNU General Public Licence 2.0 or later
 */
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
define('TREENAV_VERSION', '0.0.4, 2009-02-22');
 
$egTreeNavArticle = 'MediaWiki:Sidebar';
 
$wgExtensionFunctions[] = 'efSetupTreeNav';
 
$wgExtensionCredits['parserhook'][] = array(
	'name'        => 'TreeNav',
	'author'      => '[http://www.mediawiki.org/wiki/User:Nad User:Nad]',
	'description' => 'Adds navigation to pages which are part of the sidebar tree',
	'url'         => 'http://www.organicdesign.co.nz/Extension:TreeNav.php',
	'version'     => TREENAV_VERSION
);
 
class TreeNav {
 
	function __construct() {
		global $wgHooks;
		$wgHooks['OutputPageBeforeHTML'][] = $this;		
	}
 
	/**
	 * Add the features to the rendered page
	 */
	function onOutputPageBeforeHTML(&$out, &$text) {
		global $wgTitle, $wgUser, $egTreeNavArticle, $wgParser, $wgSitename;
		$tree = new Article(Title::newFromText($egTreeNavArticle));
		$tree = $tree->getContent();
		$t = false;
		preg_match('|\{\{#tree:.*?root\s*=\s*.+?\[\[\s*(.+?)\s*(\|\s*(.+?))?\]\]|m', $tree, $t);
		if (preg_match('|\{\{#tree:.*?^(\*.+)\}\}|sm', $tree, $m)) $tree = $m[1];
		$tree = $wgParser->preprocess($tree, $wgTitle, new ParserOptions());
		if (preg_match_all('|^\s*(\*+)\s*\[\[\s*(.+?)\s*(\|\s*(.+?))?\]\]|m', $tree, $m)) {
			list(, $depths, $titles,, $anchors) = $m;
			array_unshift($depths, '');
			array_unshift($titles, $t ? $t[1] : 'Main Page');
			array_unshift($anchors, $t ? $t[3] : $wgSitename);
 
			# Find first location of current title
			if ($i = array_search($wgTitle->getPrefixedText(), $titles)) {
 
				# Calculate prev and next
				$prev = $i > 0 ? $i-1 : false;
				#if ($depths[$prev] < $depths[$i]) $prev = false; # disable prev if it goes to parent
				$prev = $prev ? $wgUser->getSkin()->makeLinkObj(Title::newFromText($titles[$prev]), '< Prev') : '< Prev';
				$next = $i < count($titles)-1 ? $i+1 : false;
				$next = $next ? $wgUser->getSkin()->makeLinkObj(Title::newFromText($titles[$next]), 'Next >') : 'Next >';
 
				# Build path
				$path = array();
				$ld = 100;
				for ($j = $i; $j >= 0; $j--) {
					$depth = strlen($depths[$j]);
					if ($depth < $ld) {
						$ld = $depth;
						$k = $i == $j ? $j : $j;
						$title = Title::newFromText($titles[$k]);
						$anchor = $anchors[$k] ? $anchors[$k] : $title->getPrefixedText();
						array_unshift($path, $wgUser->getSkin()->makeLinkObj($title, $anchor));
					}
				}
 
				# Render information
				$path = join('>', $path);
				$text = "<div class='treenav-path'>$path</div><div class='treenav-prev'>$prev</div><div class='treenav-next'>$next</div>$text";
			}
		}
		return true;
	}
 
	function __toString() { return __CLASS__; }
 }
 
/**
 * Called from $wgExtensionFunctions array when initialising extensions
 */
function efSetupTreeNav() {
	global $egTreeNav;
	$egegTreeNav = new TreeNav();
}

The GNU Project Debian Linux Ubuntu Linux Wikipedia Affiliate Button MediaWiki

Content under the www.organicdesign.co.nz domain is available under the Creative Commons Attribution-ShareAlike License