Difference between revisions of "Extension:TreeNav.php"
m |
(oops, wrong root) |
||
| (2 intermediate revisions by the same user not shown) | |||
| 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. | + | 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:. | + | $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 52: | Line 57: | ||
$prev = $i > 0 ? $i-1 : false; | $prev = $i > 0 ? $i-1 : false; | ||
#if ($depths[$prev] < $depths[$i]) $prev = false; # disable prev if it goes to parent | #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'; | + | $prev = $prev ? $wgUser->getSkin()->makeLinkObj(Title::newFromText($titles[$prev]), '< Prev') : '< Prev'; |
$next = $i < count($titles)-1 ? $i+1 : false; | $next = $i < count($titles)-1 ? $i+1 : false; | ||
| − | $next = $next ? $wgUser->getSkin()->makeLinkObj(Title::newFromText($titles[$next]), 'Next') : 'Next'; | + | $next = $next ? $wgUser->getSkin()->makeLinkObj(Title::newFromText($titles[$next]), 'Next >') : 'Next >'; |
# Build path | # Build path | ||
$path = array(); | $path = array(); | ||
$ld = 100; | $ld = 100; | ||
| − | for ($j = $i; $j > 0; $j--) { | + | for ($j = $i; $j >= 0; $j--) { |
$depth = strlen($depths[$j]); | $depth = strlen($depths[$j]); | ||
if ($depth < $ld) { | if ($depth < $ld) { | ||
| Line 70: | Line 75: | ||
} | } | ||
| − | + | # 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"; | |
} | } | ||
} | } | ||
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
* 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 = "
$text";
} } return true; }
function __toString() { return __CLASS__; }
}
/**
* Called from $wgExtensionFunctions array when initialising extensions */
function efSetupTreeNav() { global $egTreeNav; $egegTreeNav = new TreeNav(); }



