Difference between revisions of "Extension:TreeNav.php"

From Organic Design wiki
(include root node in breadcrumbs)
(oops, wrong root)
 
Line 13: Line 13:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
  
define('TREENAV_VERSION', '0.0.3, 2009-02-22');
+
define('TREENAV_VERSION', '0.0.4, 2009-02-22');
  
 
$egTreeNavArticle = 'MediaWiki:Sidebar';
 
$egTreeNavArticle = 'MediaWiki:Sidebar';
Line 38: Line 38:
 
*/
 
*/
 
function onOutputPageBeforeHTML(&$out, &$text) {
 
function onOutputPageBeforeHTML(&$out, &$text) {
global $wgTitle, $wgUser, $egTreeNavArticle, $wgParser;
+
global $wgTitle, $wgUser, $egTreeNavArticle, $wgParser, $wgSitename;
 
$tree = new Article(Title::newFromText($egTreeNavArticle));
 
$tree = new Article(Title::newFromText($egTreeNavArticle));
 
$tree = $tree->getContent();
 
$tree = $tree->getContent();
if (preg_match('|\{\{#tree:.+?^(\*.+)\}\}|sm', $tree, $m)) $tree = $m[1];
+
$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());
 
$tree = $wgParser->preprocess($tree, $wgTitle, new ParserOptions());
 
if (preg_match_all('|^\s*(\*+)\s*\[\[\s*(.+?)\s*(\|\s*(.+?))?\]\]|m', $tree, $m)) {
 
if (preg_match_all('|^\s*(\*+)\s*\[\[\s*(.+?)\s*(\|\s*(.+?))?\]\]|m', $tree, $m)) {
 
list(, $depths, $titles,, $anchors) = $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
 
# Find first location of current title
Line 61: Line 66:
 
for ($j = $i; $j >= 0; $j--) {
 
for ($j = $i; $j >= 0; $j--) {
 
$depth = strlen($depths[$j]);
 
$depth = strlen($depths[$j]);
if ($depth < $ld || $j == 0) {
+
if ($depth < $ld) {
 
$ld = $depth;
 
$ld = $depth;
 
$k = $i == $j ? $j : $j;
 
$k = $i == $j ? $j : $j;

Latest revision as of 06:28, 22 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.4, 2009-02-22');

$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, $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 = "

$path
$prev
$next

$text";

} } return true; }

function __toString() { return __CLASS__; }

}

/**

* Called from $wgExtensionFunctions array when initialising extensions
*/

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