Difference between revisions of "Extension:TreeNav.php"

From Organic Design wiki
(New page: <?php /** * TreeNav extension - Adds ...)
 
m
Line 31: Line 31:
 
function __construct() {
 
function __construct() {
 
global $wgHooks;
 
global $wgHooks;
 
+
$wgHooks['OutputPageBeforeHTML'][] = $this;
# Hook into OutputPageBeforeHTML for breadcrumbs and auto-linking
 
$wgHooks['OutputPageBeforeHTML'][] = $this;
 
 
 
}
 
}
  
Line 81: Line 78:
 
}
 
}
  
/**
 
* Needed in some versions to prevent Special:Version from breaking
 
*/
 
 
function __toString() { return __CLASS__; }
 
function __toString() { return __CLASS__; }
 
  }
 
  }

Revision as of 09:58, 15 February 2009

<?php /**

* TreeNav extension - Adds navigation to pages which are part of the sidebar tree
* Template:Php
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.
* See http://www.organicdesign.co.nz/Extension:TreeNav.php for installation and usage details
*
* @package MediaWiki
* @subpackage Extensions
* @author User:Nad
* @copyright © 2007 User:Nad
* @licence GNU General Public Licence 2.0 or later
*/

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('TREENAV_VERSION', '0.0.0, 2009-02-14');

$egTreeNavArticle = 'MediaWiki:Sidebar';

$wgExtensionFunctions[] = 'efSetupTreeNav';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'TreeNav', 'author' => '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; $tree = new Article(Title::newFromText($egTreeNavArticle)); $tree = $tree->getContent(); 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;

# 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 = "

$path
$prev
$next

$text";

} } return true; }

function __toString() { return __CLASS__; }

}

/**

* Called from $wgExtensionFunctions array when initialising extensions
*/

function efSetupTreeNav() { global $egTreeNav; $egegTreeNav = new TreeNav(); }