Difference between revisions of "Extension:Treeview4.php"

From Organic Design wiki
(5.0.2 - $id not $uid for per-tree id's ($uid is used by voodoo only))
m (change $uid to $voo to clarify)
Line 36: Line 36:
  
 
var $version  = TREEVIEW5_VERSION;
 
var $version  = TREEVIEW5_VERSION;
var $uid;
+
var $voo;
 
var $baseDir;
 
var $baseDir;
 
var $baseUrl;
 
var $baseUrl;
Line 56: Line 56:
 
$wgParser->setFunctionHook($wgTreeView5Magic,array($this,'Tree'));
 
$wgParser->setFunctionHook($wgTreeView5Magic,array($this,'Tree'));
 
 
$this->uid = uniqid('TVUNIQ');
+
$this->voo = uniqid('TVUNIQ');
 
$this->baseDir = dirname(__FILE__);
 
$this->baseDir = dirname(__FILE__);
 
$this->baseUrl = preg_replace('|^.+(?=/extensions)|',$wgScriptPath,$this->baseDir);
 
$this->baseUrl = preg_replace('|^.+(?=/extensions)|',$wgScriptPath,$this->baseDir);
Line 67: Line 67:
 
function Tree(&$parser) {
 
function Tree(&$parser) {
 
global $wgParser;
 
global $wgParser;
$id = uniqid('');
+
$id = uniqid("");
 
$args = "id='$id'";
 
$args = "id='$id'";
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
 
foreach (func_get_args() as $arg) if (!is_object($arg)) {
Line 92: Line 92:
  
 
$id  = 'tv'.$argv['id']; // id of this tree (must be valid JS variable identifier)
 
$id  = 'tv'.$argv['id']; // id of this tree (must be valid JS variable identifier)
$uid = $this->uid;       // used by voodoo
+
$voo = $this->voo;     // used by voodoo
 
$root = isset($argv['root']) ? $argv['root'] : '';
 
$root = isset($argv['root']) ? $argv['root'] : '';
  
 
# Protect the asterisk structure and wiki-parse the bullet tree
 
# Protect the asterisk structure and wiki-parse the bullet tree
$text = preg_replace("/(?<=\\*)\\s*\\[\\[Image:(.+?)\\]\\]/","{$uid}3$1{$uid}4",$text);
+
$text = preg_replace("/(?<=\\*)\\s*\\[\\[Image:(.+?)\\]\\]/","{$voo}3$1{$voo}4",$text);
 
$text = preg_replace_callback("/^(\\*+)(.*?)$/m",array($this,'protectTree'),$text);
 
$text = preg_replace_callback("/^(\\*+)(.*?)$/m",array($this,'protectTree'),$text);
 
$out  = $parser->parse($text,$parser->mTitle,$parser->mOptions,false,false);
 
$out  = $parser->parse($text,$parser->mTitle,$parser->mOptions,false,false);
Line 102: Line 102:
  
 
# Extract the valid rows and process if any
 
# Extract the valid rows and process if any
if (preg_match_all("/1{$uid}([0-9]+)-({$uid}3(.+?){$uid}4)?(.+?){$uid}2/",$text,$matches,PREG_SET_ORDER)) {
+
if (preg_match_all("/1{$voo}([0-9]+)-({$voo}3(.+?){$voo}4)?(.+?){$voo}2/",$text,$matches,PREG_SET_ORDER)) {
 
 
 
# Convert the custom images in $wgTreeViewImages into JS statements
 
# Convert the custom images in $wgTreeViewImages into JS statements
Line 132: Line 132:
  
 
# Output the dTree
 
# Output the dTree
$dtree .= "document.write($uid);\n";
+
$dtree .= "document.write($id);\n";
 
}
 
}
  
Line 142: Line 142:
 
*/
 
*/
 
function protectTree($m) {
 
function protectTree($m) {
return "1{$this->uid}".strlen($m[1])."-$m[2]{$this->uid}2\n";
+
return "1{$this->voo}".strlen($m[1])."-$m[2]{$this->voo}2\n";
 
}
 
}
 
   
 
   

Revision as of 05:37, 27 February 2008

<?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.
Voodoo.svg

In computer programming, "Voodoo", or "Magic" refers to techniques that are secret or not widely known, and may be deliberately kept secret. The Jargon File makes a distinction between "deep magic", which refers to code based on esoteric theoretical knowledge; "black magic" (voodoo), which refers to code based on techniques that appear to work but which lack a theoretical explanation; and "heavy wizardry", which refers to code based on obscure or undocumented intricacies of particular hardware or software.

At Organic Design the most common of these is extending an instance's class at runtime after it has been instantiated, a technique that can be used to provide additional hooks into existing code without requiring modification of code-base files. Another is reading in a class file, declaring it under a different name, then sub-classing that with a new class of the original name - that way the environment uses the new extended class thinking it's the original one.

See also

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

define('TREEVIEW5_VERSION','5.0.2, 2008-02-27');

  1. Set any unset images to default titles

if (!isset($wgTreeViewImages) || !is_array($wgTreeViewImages)) $wgTreeViewImages = array();

  1. Keep track of JavaScript added to page to avoid doubleups

if (!isset($wgJS)) $wgJS = array();

$wgTreeView5Magic = "tree"; # the parser-function name for trees $wgTreeViewShowLines = false; # whether to render the dotted lines joining nodes $wgExtensionFunctions[] = 'wfSetupTreeView5'; $wgHooks['LanguageGetMagic'][] = 'wfTreeView5LanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'Treeview5', 'author' => 'Nad, Sven', 'url' => 'http://www.mediawiki.org/wiki/Extension:Treeview', 'description' => 'Extends the wiki parser to allow bullet and numbered lists to work with recursion and optionally allows these to be rendered as collapsible trees using the free dTree JavaScript tree menu.', 'version' => TREEVIEW5_VERSION );

class TreeView5 {

var $version = TREEVIEW5_VERSION; var $voo; var $baseDir; var $baseUrl; var $useLines;

/** * Constructor */ function TreeView5() { global $wgParser,$wgScriptPath,$wgTreeView5Magic,$wgTreeViewImages,$wgTreeViewShowLines;

# Convert image titles to file paths and obtain pixel width of items foreach ($wgTreeViewImages as $k => $v) { $title = Title::newFromText($v,NS_IMAGE); $image = Image::newFromTitle($title); if ($image && $image->exists()) $wgTreeViewImages[$k] = $image->getURL(); }

$wgParser->setFunctionHook($wgTreeView5Magic,array($this,'Tree'));

$this->voo = uniqid('TVUNIQ'); $this->baseDir = dirname(__FILE__); $this->baseUrl = preg_replace('|^.+(?=/extensions)|',$wgScriptPath,$this->baseDir); $this->useLines = $wgTreeViewShowLines ? 'true' : 'false'; }

/** * Restructure recursive trees into a single bullet list surrounded by internal treeview5 tags */ function Tree(&$parser) { global $wgParser; $id = uniqid(""); $args = "id='$id'"; foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?=.+)$/',$arg,$m)) $args .= " $arg"; else $text = $arg; }

# Black magic $text = $this->uniq[$id] = preg_replace( '/^\\*(\\**)\\s*(\\x07UNIQ.+?-treeview5(.+?)-.+?-QINU\\x07?)/me', '$this->uniq["$3"] ? preg_replace("/^(\\*+)/m","$1\$1",$this->uniq["$3"]) : "$1$2"', $text );

# And a little more voodoo here $wgParser->setHook("treeview5$id",array($this,'treeview')); return "<treeview5$id $args>$text</treeview5$id>"; }

/** * Convert a bullet list into a treeview */ function treeview($text,$argv,&$parser) { global $wgTreeViewImages,$wgJsMimeType;

$id = 'tv'.$argv['id']; // id of this tree (must be valid JS variable identifier) $voo = $this->voo; // used by voodoo $root = isset($argv['root']) ? $argv['root'] : ;

# Protect the asterisk structure and wiki-parse the bullet tree $text = preg_replace("/(?<=\\*)\\s*\\[\\[Image:(.+?)\\]\\]/","{$voo}3$1{$voo}4",$text); $text = preg_replace_callback("/^(\\*+)(.*?)$/m",array($this,'protectTree'),$text); $out = $parser->parse($text,$parser->mTitle,$parser->mOptions,false,false); $text = $out->getText();

# Extract the valid rows and process if any if (preg_match_all("/1{$voo}([0-9]+)-({$voo}3(.+?){$voo}4)?(.+?){$voo}2/",$text,$matches,PREG_SET_ORDER)) {

# Convert the custom images in $wgTreeViewImages into JS statements $images = ; foreach ($wgTreeViewImages as $k => $v) $images .= "$id.icon['$k'] = '$v';";

# Build the dTree instantiation code $dtree = "// TreeView{$this->version} $id = new dTree('$id'); for (i in $id.icon) $id.icon[i] = '{$this->baseUrl}/'+$id.icon[i]; {$images} $id.config.useLines = {$this->useLines}; $id.add(0, -1, '$rootname'); ";

# Add the content to the dTree object $counter = 1; $parent = 0; $currdepth = 0; foreach ($matches as $row) { list(,$depth,,$icon,$item) = $row; if ($depth > $cols) $lasts[$cols = $depth] = true; $parent = $currdepth < $depth-1 ? $counter-1 : $depth-1; $item = str_replace("'","\\'",$item); $dtree .= "${id}.add($counter,$parent,'$item');\n"; $currdepth = $depth-1; $counter++; }

# Output the dTree $dtree .= "document.write($id);\n"; }

return("<script type=\"$wgJsMimeType\">\n\n</script>"); }

/** * Protect asterisk bullet structure from wiki parser */ function protectTree($m) { return "1{$this->voo}".strlen($m[1])."-$m[2]{$this->voo}2\n"; }

/** * Add the javascript to the output object if not added yet and there is at least one tree */ function addJS() { global $wgOut,$wgJS,$wgJsMimeType; if (isset($wgJS['TreeView5'])) return; $wgJS['TreeView5'] = true; $wgOut->addScript("<script type=\"$wgJsMimeType\" src=\"{$this->baseUrl}/dtree.js\" />\n"); } }

/**

* Called from $wgExtensionFunctions array when initialising extensions
*/

function wfSetupTreeView5() { global $wgTreeView5; $wgTreeView5 = new TreeView5(); $wgTreeView5->addJS(); # Make code unconditional for now due to parser caching }

/**

* Needed in MediaWiki >1.8.0 for magic word hooks to work properly
*/

function wfTreeView5LanguageGetMagic(&$magicWords,$langCode = 0) { global $wgTreeView5,$wgTreeView5Magic; $magicWords[$wgTreeView5Magic] = array(0,$wgTreeView5Magic); return true; }