Difference between revisions of "Extension:Treeview5.php"

From Organic Design wiki
(Add str_replace to escape quotes in $item)
(fixed images)
Line 1: Line 1:
 
<?php
 
<?php
# MediaWiki Treeview Extension{{php}}{{Category:Extensions|Treeview5}}{{Category:Code that uses voodoo|Treeview5}}
+
/**
# - See http://www.mediawiki.org/wiki/Extension:Tree_view for installation and usage details
+
* MediaWiki Treeview Extension{{php}}{{Category:Extensions|Treeview5}}{{Category:Code that uses voodoo|Treeview5}}
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
+
* - See http://www.mediawiki.org/wiki/Extension:Tree_view for installation and usage details
# - Author:  http://www.organicdesign.co.nz/nad
+
* - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
# - Started: (Version5) 2007-10-24{{Category:Extensions in progress|Treeview5}}
+
* - Author:  http://www.organicdesign.co.nz/nad
 +
* - Started: (Version5) 2007-10-24{{Category:Extensions in progress|Treeview5}}
 +
*/
  
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
  
define('TREEVIEW5_VERSION','5.0.0, 2007-10-24');
+
define('TREEVIEW5_VERSION','5.0.0, 2008-02-26');
  
 
# Set any unset images to default titles
 
# Set any unset images to default titles
Line 35: Line 37:
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
 
'name'        => 'Treeview5',
 
'name'        => 'Treeview5',
'author'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
+
'author'      => '[http://www.organicdesign.co.nz/nad Nad], [http://www.organicdesign.co.nz/User:Sven Sven]',
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:Treeview',
 
'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',
+
'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 [http://www.destroydrop.com/javascripts/tree dTree] JavaScript tree menu.',
 
'version'    => TREEVIEW5_VERSION
 
'version'    => TREEVIEW5_VERSION
 
);
 
);
Line 44: Line 46:
  
 
var $version  = TREEVIEW5_VERSION;
 
var $version  = TREEVIEW5_VERSION;
var $uid     = '';
+
var $uid;
 +
var $baseDir;
 +
var $baseUrl;
  
 
+
/**
# Constructor
+
* Constructor
 +
*/
 
function TreeView5() {
 
function TreeView5() {
global $wgParser,$wgTreeView5Magic,$wgTreeViewImages,$wgTreeViewIndent;
+
global $wgParser,$wgScriptPath,$wgTreeView5Magic,$wgTreeViewImages,$wgTreeViewIndent;
  
 
# Convert image titles to file paths and obtain pixel width of items
 
# Convert image titles to file paths and obtain pixel width of items
Line 62: Line 67:
 
}
 
}
  
 +
$wgParser->setFunctionHook($wgTreeView5Magic,array($this,'Tree'));
 +
 
$this->uid = uniqid('TVUNIQ');
 
$this->uid = uniqid('TVUNIQ');
$wgParser->setFunctionHook($wgTreeView5Magic,array($this,'Tree'));
+
$this->baseDir = dirname(__FILE__);
 +
$this->baseUrl = preg_replace('|^.+(?=/extensions)|',$wgScriptPath,$this->baseDir);
 
}
 
}
  
# Restructure recursive trees into a single bullet list surrounded by internal treeview5 tags
+
/**
 +
* Restructure recursive trees into a single bullet list surrounded by internal treeview5 tags
 +
*/
 
function Tree(&$parser) {
 
function Tree(&$parser) {
 
global $wgParser;
 
global $wgParser;
Line 87: Line 97:
 
}
 
}
  
# Convert a bullet list into a treeview
+
/**
 +
* Convert a bullet list into a treeview
 +
*/
 
function treeview($text,$argv,&$parser) {
 
function treeview($text,$argv,&$parser) {
 
global $wgTreeViewImages,$wgJsMimeType;
 
global $wgTreeViewImages,$wgJsMimeType;
  
$id     = $argv['id'];
+
$id       = $argv['id'];
$uid     = $this->uid;
+
$uid     = $this->uid;
$ver     = $this->version;
+
$ver     = $this->version;
$plus   = $wgTreeViewImages['plus'];
+
$plus     = $wgTreeViewImages['plus'];
$minus   = $wgTreeViewImages['minus'];
+
$minus   = $wgTreeViewImages['minus'];
$opened = $wgTreeViewImages['opened'];
+
$opened   = $wgTreeViewImages['opened'];
$closed = $wgTreeViewImages['closed'];
+
$closed   = $wgTreeViewImages['closed'];
$spacer = $wgTreeViewImages['spacer'];
+
$spacer   = $wgTreeViewImages['spacer'];
$doc     = $wgTreeViewImages['doc'];
+
$doc     = $wgTreeViewImages['doc'];
$vert   = $wgTreeViewImages['vert'];
+
$vert     = $wgTreeViewImages['vert'];
$node   = $wgTreeViewImages['node'];
+
$node     = $wgTreeViewImages['node'];
$last   = $wgTreeViewImages['last'];
+
$last     = $wgTreeViewImages['last'];
$default = isset($argv['openlevels']) ? $argv['openlevels']+1 : 1;
+
$default = isset($argv['openlevels']) ? $argv['openlevels']+1 : 1;
$rows   = array();
+
$rows     = array();
$lasts   = array();
+
$lasts   = array();
$cols   = 0;
+
$cols     = 0;
$next   = 0;
+
$next     = 0;
$tree   = '';
+
$tree     = '';
$rootname= '';
+
$rootname = isset($argv['rootname']) ? $argv['rootname'] : '';
 
if(isset($argv['rootname'])) {
 
  $rootname = $argv['rootname'];
 
}
 
 
 
  
$dtree  = "$uid =  new dTree('$uid');\n${uid}.add(0, -1, '$rootname');\n";
 
 
# 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:(.+?)\\]\\]/","{$uid}3$1{$uid}4",$text);
Line 125: Line 131:
 
# 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{$uid}([0-9]+)-({$uid}3(.+?){$uid}4)?(.+?){$uid}2/",$text,$matches,PREG_SET_ORDER)) {
# Parse 1 - link syntax etc parsed
+
$dtree = "$uid =  new dTree('$uid');
 
+
for (i in $uid.icon) $uid.icon[i] = '{$this->baseUrl}/'+$uid.icon[i];
  $counter  =  1;
+
$uid.add(0, -1, '$rootname');
  $parent    = 0;
+
";
  $currdepth = 0;
+
$counter  =  1;
 +
$parent    = 0;
 +
$currdepth = 0;
 
foreach ($matches as $row) {
 
foreach ($matches as $row) {
  print "ITEM=>$item\n<br />";
 
 
list(,$depth,,$icon,$item) = $row;
 
list(,$depth,,$icon,$item) = $row;
 
if ($depth > $cols) $lasts[$cols = $depth] = true;
 
if ($depth > $cols) $lasts[$cols = $depth] = true;
+
$parent    = $currdepth < $depth-1 ? $counter-1 : $depth-1;
if($currdepth<($depth-1)) {
+
$item     = str_replace("'","\\'",$item);
  $parent = $counter-1;
+
$dtree   .= "${uid}.add($counter,$parent,'$item');\n";
} else {
+
$currdepth = $depth-1;
  $parent = $depth-1;
+
$counter++;
}
 
$item = str_replace("'","\\'",$item);
 
$dtree .= "${uid}.add($counter, " . ($parent) . ", '$item');\n";
 
  $counter++;
 
  $currdepth = ($depth-1);
 
 
}
 
}
 
$dtree .= "document.write($uid);\n";
 
$dtree .= "document.write($uid);\n";
 
}
 
}
print "DTREE=>$dtree<br />\n";
+
 
 
return("<script type=\"$wgJsMimeType\">\n<!--\n$dtree-->\n</script>");
 
return("<script type=\"$wgJsMimeType\">\n<!--\n$dtree-->\n</script>");
 
}
 
}
  
# Protect asterisk bullet structure from wiki parser
+
/**
 +
* Protect asterisk bullet structure from wiki parser
 +
*/
 
function protectTree($m) {
 
function protectTree($m) {
 
return "1{$this->uid}".strlen($m[1])."-$m[2]{$this->uid}2\n";
 
return "1{$this->uid}".strlen($m[1])."-$m[2]{$this->uid}2\n";
 
}
 
}
 
   
 
   
# Add the javascript to the output object if not added yet and there is at least one tree
+
/**
 +
* Add the javascript to the output object if not added yet and there is at least one tree
 +
*/
 
function addJS() {
 
function addJS() {
 
global $wgOut,$wgJS,$wgJsMimeType;
 
global $wgOut,$wgJS,$wgJsMimeType;
 
if (isset($wgJS['TreeView5'])) return;
 
if (isset($wgJS['TreeView5'])) return;
 
$wgJS['TreeView5'] = true;
 
$wgJS['TreeView5'] = true;
$wgOut->addScript("<script type=\"$wgJsMimeType\" src=\"/wiki/extensions/treeview/dtree.js\"><!-- dtree js --></script>\n");
+
$wgOut->addScript("<script type=\"$wgJsMimeType\" src=\"{$this->baseUrl}/dtree.js\" />\n");
 
}
 
}
 
}
 
}
 
   
 
   
# Called from $wgExtensionFunctions array when initialising extensions
+
/**
 +
* Called from $wgExtensionFunctions array when initialising extensions
 +
*/
 
function wfSetupTreeView5() {
 
function wfSetupTreeView5() {
 
global $wgTreeView5;
 
global $wgTreeView5;
Line 172: Line 180:
 
}
 
}
 
   
 
   
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
+
/**
 +
* Needed in MediaWiki >1.8.0 for magic word hooks to work properly
 +
*/
 
function wfTreeView5LanguageGetMagic(&$magicWords,$langCode = 0) {
 
function wfTreeView5LanguageGetMagic(&$magicWords,$langCode = 0) {
 
global $wgTreeView5,$wgTreeView5Magic;
 
global $wgTreeView5,$wgTreeView5Magic;

Revision as of 05:00, 26 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.0, 2008-02-26');

  1. Set any unset images to default titles

if (!isset($wgTreeViewImages) || !is_array($wgTreeViewImages)) $wgTreeViewImages = array(); if (!isset($wgTreeViewImages['plus'])) $wgTreeViewImages['plus'] = 'Plus.gif'; if (!isset($wgTreeViewImages['minus'])) $wgTreeViewImages['minus'] = 'Minus.gif'; if (!isset($wgTreeViewImages['opened'])) $wgTreeViewImages['opened'] = 'Folder_opn_sml_yel.gif'; if (!isset($wgTreeViewImages['closed'])) $wgTreeViewImages['closed'] = 'Folder_sml_yel.gif'; if (!isset($wgTreeViewImages['doc'])) $wgTreeViewImages['doc'] = 'Doc-icon.gif'; if (!isset($wgTreeViewImages['spacer'])) $wgTreeViewImages['spacer'] = 'Blank.gif';

  1. These images are needed if you have $wgTreeViewShowLines set

if (!isset($wgTreeViewImages['vert'])) $wgTreeViewImages['vert'] = 'Vertline.gif'; if (!isset($wgTreeViewImages['node'])) $wgTreeViewImages['node'] = 'Node.gif'; if (!isset($wgTreeViewImages['last'])) $wgTreeViewImages['last'] = 'Lastnode.gif';

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

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

$wgTreeView5Magic = "tree"; # the parser-function name for trees $wgTreeViewIndent = 0; # the number of pixels each level indents by (0 means doc-icon width) $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 $uid; var $baseDir; var $baseUrl;

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

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

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

$this->uid = uniqid('TVUNIQ'); $this->baseDir = dirname(__FILE__); $this->baseUrl = preg_replace('|^.+(?=/extensions)|',$wgScriptPath,$this->baseDir); }

/** * 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 = $argv['id']; $uid = $this->uid; $ver = $this->version; $plus = $wgTreeViewImages['plus']; $minus = $wgTreeViewImages['minus']; $opened = $wgTreeViewImages['opened']; $closed = $wgTreeViewImages['closed']; $spacer = $wgTreeViewImages['spacer']; $doc = $wgTreeViewImages['doc']; $vert = $wgTreeViewImages['vert']; $node = $wgTreeViewImages['node']; $last = $wgTreeViewImages['last']; $default = isset($argv['openlevels']) ? $argv['openlevels']+1 : 1; $rows = array(); $lasts = array(); $cols = 0; $next = 0; $tree = ; $rootname = isset($argv['rootname']) ? $argv['rootname'] : ;

# Protect the asterisk structure and wiki-parse the bullet tree $text = preg_replace("/(?<=\\*)\\s*\\[\\[Image:(.+?)\\]\\]/","{$uid}3$1{$uid}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{$uid}([0-9]+)-({$uid}3(.+?){$uid}4)?(.+?){$uid}2/",$text,$matches,PREG_SET_ORDER)) { $dtree = "$uid = new dTree('$uid'); for (i in $uid.icon) $uid.icon[i] = '{$this->baseUrl}/'+$uid.icon[i]; $uid.add(0, -1, '$rootname'); "; $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 .= "${uid}.add($counter,$parent,'$item');\n"; $currdepth = $depth-1; $counter++; } $dtree .= "document.write($uid);\n"; }

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

/** * Protect asterisk bullet structure from wiki parser */ function protectTree($m) { return "1{$this->uid}".strlen($m[1])."-$m[2]{$this->uid}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; }