https://www.mediawiki.org/wiki/Special:Code/MediaWiki/108379
Revision: 108379
Author: nad
Date: 2012-01-09 02:06:38 +0000 (Mon, 09 Jan 2012)
Log Message:
-----------
hook tree building script into jQuery ready hook if 1.16+
Modified Paths:
--------------
trunk/extensions/TreeAndMenu/TreeAndMenu.php
Modified: trunk/extensions/TreeAndMenu/TreeAndMenu.php
===================================================================
--- trunk/extensions/TreeAndMenu/TreeAndMenu.php 2012-01-09 01:06:42 UTC
(rev 108378)
+++ trunk/extensions/TreeAndMenu/TreeAndMenu.php 2012-01-09 02:06:38 UTC
(rev 108379)
@@ -12,16 +12,16 @@
* @licence GNU General Public Licence 2.0 or later
*/
-if ( !defined( 'MEDIAWIKI' ) ) die( 'Not an entry point.' );
+if( !defined( 'MEDIAWIKI' ) ) die( 'Not an entry point.' );
-define( 'TREEANDMENU_VERSION','1.2.4, 2011-07-26' );
+define( 'TREEANDMENU_VERSION','1.3.0, 2012-01-08' );
-# Set any unset images to default titles
-if ( !isset( $wgTreeViewImages ) || !is_array( $wgTreeViewImages ) )
$wgTreeViewImages = array();
+// Set any unset images to default titles
+if( !isset( $wgTreeViewImages ) || !is_array( $wgTreeViewImages ) )
$wgTreeViewImages = array();
-$wgTreeMagic = "tree"; # the parser-function name for trees
-$wgMenuMagic = "menu"; # the parser-function name for
dropdown menus
-$wgTreeViewShowLines = false; # whether to render the dotted lines
joining nodes
+$wgTreeMagic = "tree"; // the parser-function name for trees
+$wgMenuMagic = "menu"; // the parser-function name for
dropdown menus
+$wgTreeViewShowLines = false; // whether to render the dotted lines
joining nodes
$wgExtensionFunctions[] = 'wfSetupTreeAndMenu';
$wgHooks['LanguageGetMagic'][] = 'wfTreeAndMenuLanguageGetMagic';
@@ -37,14 +37,14 @@
class TreeAndMenu {
var $version = TREEANDMENU_VERSION;
- var $uniq = ''; # uniq part of all tree id's
- var $uniqname = 'tam'; # input name for uniqid
- var $id = ''; # id for specific tree
- var $baseDir = ''; # internal absolute path to treeview
directory
- var $baseUrl = ''; # external URL to treeview directory
(relative to domain)
- var $images = ''; # internal JS to update dTree images
- var $useLines = true; # internal variable determining whether to
render connector lines
- var $args = array(); # args for each tree
+ var $uniq = ''; // uniq part of all tree id's
+ var $uniqname = 'tam'; // input name for uniqid
+ var $id = ''; // id for specific tree
+ var $baseDir = ''; // internal absolute path to treeview
directory
+ var $baseUrl = ''; // external URL to treeview directory
(relative to domain)
+ var $images = ''; // internal JS to update dTree images
+ var $useLines = true; // internal variable determining whether to
render connector lines
+ var $args = array(); // args for each tree
var $js = 0;
/**
@@ -54,19 +54,19 @@
global $wgOut, $wgHooks, $wgParser, $wgScriptPath,
$wgJsMimeType, $wgTreeMagic,
$wgMenuMagic, $wgTreeViewImages, $wgTreeViewShowLines,
$wgTreeViewBaseDir, $wgTreeViewBaseUrl;
- # Add hooks
+ // Add hooks
$wgParser->setFunctionHook( $wgTreeMagic, array(
$this,'expandTree' ) );
$wgParser->setFunctionHook( $wgMenuMagic, array(
$this,'expandMenu' ) );
$wgHooks['ParserAfterTidy'][] = array( $this,
'renderTreeAndMenu' );
-
- # Update general tree paths and properties
+
+ // Update general tree paths and properties
$this->baseDir = isset( $wgTreeViewBaseDir ) ?
$wgTreeViewBaseDir : dirname( __FILE__ );
$this->baseUrl = isset( $wgTreeViewBaseUrl ) ?
$wgTreeViewBaseUrl : preg_replace( '|^.+(?=/ext)|', $wgScriptPath,
$this->baseDir );
$this->useLines = $wgTreeViewShowLines ? 'true' : 'false';
$this->uniq = uniqid( $this->uniqname );
- # Convert image titles to file paths and store as JS to update
dTree
- foreach ( $wgTreeViewImages as $k => $v ) {
+ // Convert image titles to file paths and store as JS to update
dTree
+ foreach( $wgTreeViewImages as $k => $v ) {
$title = Title::newFromText( $v, NS_IMAGE );
$image = wfFindFile( $title );
$v = $image && $image->exists() ? $image->getURL() :
$wgTreeViewImages[$k];
@@ -74,7 +74,6 @@
}
}
-
/**
* Expand #tree parser-functions
*/
@@ -96,13 +95,13 @@
*/
private function expandTreeAndMenu( $magic, $args ) {
$parser = array_shift( $args );
-
- # Store args for this tree for later use
+
+ // Store args for this tree for later use
$text = '';
- foreach ( $args as $arg ) if ( preg_match(
'/^(\\w+?)\\s*=\\s*(.+)$/s', $arg, $m ) ) $args[$m[1]] = $m[2]; else $text =
$arg;
+ foreach( $args as $arg ) if ( preg_match(
'/^(\\w+?)\\s*=\\s*(.+)$/s', $arg, $m ) ) $args[$m[1]] = $m[2]; else $text =
$arg;
- # If root, parse as wikitext
- if ( isset( $args['root'] ) ) {
+ // If root, parse as wikitext
+ if( isset( $args['root'] ) ) {
$p = clone $parser;
$o = clone $parser->mOptions;
$o->mTidy = $o->mEnableLimitReport = false;
@@ -110,12 +109,12 @@
$args['root'] = addslashes( $html );
}
- # Create a unique id for this tree or use id supplied in args
and store args wrt id
+ // Create a unique id for this tree or use id supplied in args
and store args wrt id
$this->id = isset($args['id']) ? $args['id'] : uniqid( '' );
$args['type'] = $magic;
$this->args[$this->id] = $args;
- # Reformat tree rows for matching in ParserAfterStrip
+ // Reformat tree rows for matching in ParserAfterStrip
$text = preg_replace( '/(?<=\\*)\\s*\\[\\[Image:(.+?)\\]\\]/',
"{$this->uniq}3$1{$this->uniq}4", $text );
$text = preg_replace_callback( '/^(\\*+)(.*?)$/m', array(
$this, 'formatRow' ), $text );
@@ -139,109 +138,113 @@
* Called after parser has finished (ParserAfterTidy) so all
transcluded parts can be assembled into final trees
*/
public function renderTreeAndMenu( &$parser, &$text ) {
- global $wgJsMimeType, $wgOut;
+ global $wgJsMimeType, $wgOut, $wgVersion;
$u = $this->uniq;
- # Determine which trees are sub trees
- # - there should be a more robust way to do this,
- # it's just based on the fact that all sub-tree's have a
minus preceding their row data
- if ( !preg_match_all( "/\x7f\x7f1$u\x7f(.+?)\x7f/", $text,
$subs ) ) $subs = array( 1 => array() );
-
- # Extract all the formatted tree rows in the page and if any,
replace with dTree JavaScript
- if ( preg_match_all(
"/\x7f1$u\x7f(.+?)\x7f([0-9]+)\x7f({$u}3(.+?){$u}4)?(.*?)(?=\x7f[12]$u)/",
$text, $matches, PREG_SET_ORDER ) ) {
+ // Determine which trees are sub trees
+ // - there should be a more robust way to do this,
+ // it's just based on the fact that all sub-tree's have a
minus preceding their row data
+ if( !preg_match_all( "/\x7f\x7f1$u\x7f(.+?)\x7f/", $text, $subs
) ) $subs = array( 1 => array() );
- # PASS-1: build $rows array containing depth, and tree
start/end information
+ // Extract all the formatted tree rows in the page and if any,
replace with dTree JavaScript
+ if( preg_match_all(
"/\x7f1$u\x7f(.+?)\x7f([0-9]+)\x7f({$u}3(.+?){$u}4)?(.*?)(?=\x7f[12]$u)/",
$text, $matches, PREG_SET_ORDER ) ) {
+
+ // PASS-1: build $rows array containing depth, and tree
start/end information
$rows = array();
- $depths = array( '' => 0 ); # depth of each tree root
- $rootId = ''; # the id of the current
root-tree (used as tree id in PASS2)
+ $depths = array( '' => 0 ); // depth of each tree root
+ $rootId = ''; // the id of the current
root-tree (used as tree id in PASS2)
$lastId = '';
$lastDepth = 0;
- foreach ( $matches as $match ) {
+ foreach( $matches as $match ) {
list( , $id, $depth,, $icon, $item ) = $match;
$start = false;
- if ( $id != $lastId ) {
- if ( !isset( $depths[$id] ) )
$depths[$id] = $depths[$lastId]+$lastDepth;
- if ( $start = $rootId != $id &&
!in_array( $id, $subs[1] ) ) $depths[$rootId = $id] = 0;
+ if( $id != $lastId ) {
+ if( !isset( $depths[$id] ) )
$depths[$id] = $depths[$lastId]+$lastDepth;
+ if( $start = $rootId != $id &&
!in_array( $id, $subs[1] ) ) $depths[$rootId = $id] = 0;
}
- if ( $item ) $rows[] = array( $rootId, $depth +
$depths[$id], $icon, addslashes( $item ), $start );
+ if( $item ) $rows[] = array( $rootId, $depth +
$depths[$id], $icon, addslashes( $item ), $start );
$lastId = $id;
$lastDepth = $depth;
}
- # PASS-2: build the JavaScript and replace into $text
- $parents = array(); # parent node for each depth
- $parity = array(); # keep track of odd/even rows for
each depth
+ // PASS-2: build the JavaScript and replace into $text
+ $parents = array(); // parent node for each depth
+ $parity = array(); // keep track of odd/even rows
for each depth
$node = 0;
$last = -1;
$nodes = '';
$opennodes = array();
- foreach ( $rows as $i => $info ) {
+ foreach( $rows as $i => $info ) {
$node++;
list( $id, $depth, $icon, $item, $start ) =
$info;
$objid = $this->uniqname . preg_replace(
'/\W/', '', $id );
$args = $this->args[$id];
$type = $args['type'];
$end = $i == count( $rows )-1 ||
$rows[$i+1][4];
- if ( !isset( $args['root'] ) ) $args['root'] =
''; # tmp - need to handle rootless trees
+ if( !isset( $args['root'] ) ) $args['root'] =
''; // tmp - need to handle rootless trees
$openlevels = isset( $args['openlevels'] ) ?
$args['openlevels']+1 : 0;
- if ( $start ) $node = 1;
-
- # Append node script for this row
- if ( $depth > $last ) $parents[$depth] =
$node-1;
+ if( $start ) $node = 1;
+
+ // Append node script for this row
+ if( $depth > $last ) $parents[$depth] = $node-1;
$parent = $parents[$depth];
- if ( $type == 'tree' ) {
+ if( $type == 'tree' ) {
$nodes .= "$objid.add($node, $parent,
'$item');\n";
- if ( $depth > 0 && $openlevels > $depth
) $opennodes[$parent] = true;
+ if( $depth > 0 && $openlevels > $depth
) $opennodes[$parent] = true;
}
else {
- if ( !$start ) {
- if ( $depth < $last ) $nodes .=
str_repeat( '</ul></li>', $last - $depth );
- elseif ( $depth > $last )
$nodes .= "\n<ul>";
+ if( !$start ) {
+ if( $depth < $last ) $nodes .=
str_repeat( '</ul></li>', $last - $depth );
+ elseif( $depth > $last ) $nodes
.= "\n<ul>";
}
$parity[$depth] = isset(
$parity[$depth] ) ? $parity[$depth]^1 : 0;
$class = $parity[$depth] ? 'odd' :
'even';
$nodes .= "<li class=\"$class\">$item";
- if ( $depth >= $rows[$node][1] ) $nodes
.= "</li>\n";
+ if( $depth >= $rows[$node][1] ) $nodes
.= "</li>\n";
}
$last = $depth;
- # Last row of current root, surround nodes
dtree or menu script and div etc
- if ( $end ) {
+ // Last row of current root, surround nodes
dtree or menu script and div etc
+ if( $end ) {
$class = isset( $args['class'] ) ?
$args['class'] : "d$type";
- if ( $type == 'tree' ) {
+ if( $type == 'tree' ) {
- # Finalise a tree
+ // Finalise a tree
$add = isset( $args['root'] ) ?
"tree.add(0,-1,'".$args['root']."');" : '';
$top = $bottom = $root =
$opennodesjs = '';
- foreach ( array_keys(
$opennodes ) as $i ) $opennodesjs .= "$objid.o($i);";
- foreach ( $args as $arg => $pos
)
- if ( ( $pos == 'top' ||
$pos == 'bottom' || $pos == 'root' ) && ( $arg == 'open' || $arg == 'close' ) )
+ foreach( array_keys( $opennodes
) as $i ) $opennodesjs .= "$objid.o($i);";
+ foreach( $args as $arg => $pos )
+ if( ( $pos == 'top' ||
$pos == 'bottom' || $pos == 'root' ) && ( $arg == 'open' || $arg == 'close' ) )
$$pos .= "<a
href=\"javascript: $objid.{$arg}All();\"> {$arg} all</a> ";
- if ( $top ) $top =
"<p> $top</p>";
- if ( $bottom ) $bottom =
"<p> $bottom</p>";
+ if( $top ) $top =
"<p> $top</p>";
+ if( $bottom ) $bottom =
"<p> $bottom</p>";
- # Add the dTRee script if not
loaded yet
+ // Add the dTRee script if not
loaded yet
$dTreeScript = $this->js++ ? ""
: "<script type=\"$wgJsMimeType\" src=\"{$this->baseUrl}/dtree.js\"></script>";
- $html = "$top<div
class='$class' id='$id'>$dTreeScript
- <script
type=\"$wgJsMimeType\">/*<![CDATA[*/
- //
TreeAndMenu-{$this->version}
- tree =
new dTree('$objid');
- for (i
in tree.icon) tree.icon[i] = '{$this->baseUrl}/'+tree.icon[i];{$this->images}
-
tree.config.useLines = {$this->useLines};
- $add
- $objid
= tree;
- $nodes
-
document.getElementById('$id').innerHTML = $objid.toString();
-
$opennodesjs
- /*]]>*/</script>
- </div>$bottom";
- }
- else {
-
- # Finalise a menu
- if ( $depth > 0 ) $nodes .=
str_repeat( '</ul></li>', $depth );
- $nodes = preg_replace( "/<(a.*?
)title=\".+?\".*?>/", "<$1>", $nodes ); # IE has problems with title attribute
in suckerfish menus
+ // Define the script to build
this tree
+ $script = "tree = new
dTree('$objid');
+
for (i in tree.icon) tree.icon[i] =
'{$this->baseUrl}/'+tree.icon[i];{$this->images}
+
tree.config.useLines = {$this->useLines};
+
$add
+
$objid = tree;
+
$nodes
+
document.getElementById('$id').innerHTML = $objid.toString();
+
$opennodesjs";
+
+ // If MediaWiki version 1.16+
bind the script to jQuery's ready hook
+ if( version_compare(
$wgVersion, '1.16.0' ) >= 0 ) $script = "jQuery( function() { $script } );";
+
+ // Embed the script into the
output
+ $html = "$top<div
class='$class' id='$id'>$dTreeScript<script type=\"$wgJsMimeType\">/*<![CDATA[*/
+ //
TreeAndMenu-{$this->version}
+ $script
+
/*]]>*/</script></div>$bottom";
+ } else {
+
+ // Finalise a menu
+ if( $depth > 0 ) $nodes .=
str_repeat( '</ul></li>', $depth );
+ $nodes = preg_replace( "/<(a.*?
)title=\".+?\".*?>/", "<$1>", $nodes ); // IE has problems with title attribute
in suckerfish menus
$html = "
<ul class='$class'
id='$id'>\n$nodes</ul>
<script
type=\"$wgJsMimeType\">/*<![CDATA[*/
@@ -256,19 +259,18 @@
";
}
- $text = preg_replace(
"/\x7f1$u\x7f$id\x7f.+?$/m", $html, $text, 1 ); # replace first occurence of
this trees root-id
+ $text = preg_replace(
"/\x7f1$u\x7f$id\x7f.+?$/m", $html, $text, 1 ); // replace first occurence of
this trees root-id
$nodes = '';
$last = -1;
}
}
}
- $text = preg_replace( "/\x7f1$u\x7f.+?[\\r\\n]+/m", '', $text
); # Remove all unreplaced row information
+ $text = preg_replace( "/\x7f1$u\x7f.+?[\\r\\n]+/m", '', $text
); // Remove all unreplaced row information
return true;
}
-
+
}
-
/**
* Called from $wgExtensionFunctions array when initialising extensions
*/
@@ -277,7 +279,6 @@
$wgTreeAndMenu = new TreeAndMenu();
}
-
/**
* Reserve magic words
*/
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs