# MediaWiki - How do I install an Extension?



## chicago (Aug 10, 2007)

I need help installing the extension Treeview.php to my mediawiki. I've already created a new file called Treeview.php and uploaded the file into my wiki folder. Now I need to go into my LocalSettings.php file and make some edits or additions. What do I need to add to my LocalSettings.php file and where do I add it?:4-dontkno


----------



## Iomega0318 (Mar 12, 2007)

If you can post a link to where you got the information from I can help you better..


----------



## chicago (Aug 10, 2007)

Thank! This is the link:

http://www.mediawiki.org/wiki/Extension:Tree_view


----------



## Iomega0318 (Mar 12, 2007)

Ok I assume you created the Treeview.php which includes what is below?


```
<?php
# MediaWiki Treeview Extension
# - See http://www.mediawiki.org/wiki/Extension:Tree_view for installation and usage details
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
# - Author: http://www.organicdesign.co.nz/nad
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
define('TREEVIEW_VERSION','3.5.7, 2007-05-15');
 
if (!is_array($wgTreeViewImages)) $wgTreeViewImages = array(
	'plus'   => "$wgUploadPath/7/72/Plus.gif",
	'minus'  => "$wgUploadPath/a/a0/Minus.gif",
	'opened' => "$wgUploadPath/4/46/Folder_opn_sml_yel.gif",
	'closed' => "$wgUploadPath/6/6e/Folder_sml_yel.gif",
	'doc'    => "$wgUploadPath/e/e3/Doc-icon.gif",
	'spacer' => "$wgUploadPath/c/c0/Blank.gif"
	);
 
# Keep track of JavaScript added to page to avoid doubleups
if (!isset($wgJS)) $wgJS = array();
 
$wgTreeViewMagic               = "tree"; # the parser-function name for trees
$wgExtensionFunctions[]        = 'wfSetupTreeView';
$wgHooks['LanguageGetMagic'][] = 'wfTreeViewLanguageGetMagic';
 
$wgExtensionCredits['parserhook'][] = array(
	'name'        => 'Treeview',
	'author'      => '[http://www.organicdesign.co.nz/nad User:Nad]',
	'url'         => 'http://www.mediawiki.org/wiki/Extension:Treeview',
	'description' => 'Allows dynamic tree-views to be made with bullet-list syntax',
	'version'     => TREEVIEW_VERSION
	);
 
class TreeView {
 
	var $version = TREEVIEW_VERSION;
	var $token   = '@@@';
	var $id      = 0;
	var $js      = 0;
	var $info;
	var $args;
 
	# Constructor
	function TreeView($magic) {
		global $wgParser,$wgHooks;
		$this->info = array();
		$this->args = array();
		$wgParser->setFunctionHook($magic,array($this,'Tree'));
		$wgParser->setHook('TreeViewRowStart',array($this,'RowStart'));
		$wgParser->setHook('TreeViewRowEnd',array($this,'RowEnd'));
		}
 
	# Restructure recursive trees to single root trees surrounded by $magic tags
	function Tree(&$parser) {
		$this->addJS();
		$args = array();
		foreach (func_get_args() as $arg) if (!is_object($arg)) {
			if (preg_match('/^(.+?)=(.+)$/',$arg,$match)) $args[$match[1]] = $match[2]; else $args[] = $arg;
			}
		$text = $args[0];
		$tree = '';
		$token = $this->token;
		$ver = $this->version;
		$id = $this->id++;
		if (count($parser->mTemplatePath)) $tree = "*O$token\n$text\n*C$token";
		elseif (preg_match_all("/^(\\*+)(.$token)? *(.*?) *$/m",$text,$match)) {
			$this->args[$id] = $args;
			$this->info[$id] = array();
			$nest = array();
			$ld = '';
			$row = -1;
			foreach ($match[1] as $i => $indent) {
				if ($sub = $match[2][$i]) $sub == "O$token" ? $nest[] = substr($ld,0,-1) : array_pop($nest);
				elseif ($item = $match[3][$i]) {
					$this->info[$id][++$row] = strlen($indent.join('',$nest))-1;
					$tree .= "<TreeViewRowStart>$id,$row</TreeViewRowStart>$item<TreeViewRowEnd/>\n";
					}
				$ld = $indent;
				}
			$tree = "<table class='tree-view' id='tree-view-$id' title='Extension:Treeview (ver $ver)'>\n$tree</table>";
			}
		return $tree;
		}
 
	# Convert each row into HTML
	function RowStart($text,$argv,&$parser) {
		global $wgTreeViewImages;
		list($id,$row) = split(',',$text);
 
		$args    = $this->args[$id];
		$info    = $this->info[$id];
		$node    = $id*1000+$row;
		$depth   = $info[$row];
		$next    = isset($info[$row+1])       ? $info[$row+1] : 0;
		$plus    = isset($args['plus'])       ? $args['plus']       : $wgTreeViewImages['plus'];
		$minus   = isset($args['minus'])      ? $args['minus']      : $wgTreeViewImages['minus'];
		$opened  = isset($args['opened'])     ? $args['opened']     : $wgTreeViewImages['opened'];
		$closed  = isset($args['closed'])     ? $args['closed']     : $wgTreeViewImages['closed'];
		$spacer  = isset($args['spacer'])     ? $args['spacer']     : $wgTreeViewImages['spacer'];
		$doc     = isset($args['doc'])        ? $args['doc']        : $wgTreeViewImages['doc'];
		$default = isset($args['openlevels']) ? $args['openlevels'] : 0;
 
		$show = $depth > $default ? ' style="display:none"' : '';
		if ($depth >= $default) { $open = $plus; $icon = $closed; } else { $open = $minus; $icon = $opened; }
		if ($depth >= $next) { $open = ''; $icon = $doc; }
			else $open = "<a href='javascript:toggleTreeviewItem($node)'><img id='tree-img-$node' src='$open'/></a>";
 
		return "<tr$show class='tree-row' depth='$depth' id='$node'>"
			. "<td valign='bottom'><img src='$spacer' width='".($depth*16)."' height='1'/>$open"
			. " <img id='tree-fld-$node' src='$icon'/> ";
		}
 
	function RowEnd($text,$argv,&$parser) {
		return "</td></tr>\n";
		}
 
	# Add the javascript to the output object if not added yet and there is at least one tree
	function addJS() {
		global $wgOut,$wgTreeViewImages,$wgJS,$wgJsMimeType;
		if (isset($wgJS['TreeView'])) return;
		$wgJS['TreeView'] = true;
		$plus    = $wgTreeViewImages['plus'];
		$minus   = $wgTreeViewImages['minus'];
		$opened  = $wgTreeViewImages['opened'];
		$closed  = $wgTreeViewImages['closed'];
		$spacer  = $wgTreeViewImages['spacer'];
		$doc     = $wgTreeViewImages['doc'];
		$wgOut->addScript('<script type="'.$wgJsMimeType.'">
			function toggleTreeviewItem(id) {
			var plus   = "'.$plus.'";
			var minus  = "'.$minus.'";
			var opened = "'.$opened.'";
			var closed = "'.$closed.'";
			var doc    = "'.$doc.'";
			var item   = document.getElementById(id);
			var next   = document.getElementById(id+1);
			var depth  = 0+item.getAttribute("depth");
			var close  = next.style.display != "none";
			var img    = document.getElementById("tree-img-"+id);
			var fld    = document.getElementById("tree-fld-"+id);
			fld.setAttribute("src",close ? closed : opened);
			img.setAttribute("src",close ? plus : minus);
			while ((item = document.getElementById(++id)) && (0+item.getAttribute("depth") > depth)) {
				if (close) item.style.display = "none";
				else if (depth == item.getAttribute("depth")-1) {
					item.style.display = "";
					if (img = document.getElementById("tree-img-"+id)) img.setAttribute("src",plus);
					if (fld = document.getElementById("tree-fld-"+id))
						if (fld.getAttribute("src") == opened) fld.setAttribute("src",closed);
					}
				}
			}</script>');
		}
	}
 
# Called from $wgExtensionFunctions array when initialising extensions
function wfSetupTreeView() {
	global $wgTreeView,$wgTreeViewMagic,$wgVersion;
	$wgTreeView = new TreeView($wgTreeViewMagic);
	$wgTreeView->addJS(); # addScript doesn't seem to work from within parser
	}
 
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
function wfTreeViewLanguageGetMagic(&$magicWords,$langCode = 0) {
	global $wgTreeViewMagic;
	$magicWords[$wgTreeViewMagic] = array(0,$wgTreeViewMagic);
	return true;
	}
?>
```
If so then just continue below..
If not please make a file called "Treeview.php" CaSe SenSiTivE!
then add what is in the code tags into the file..
then upload the file to your extensions folder..



If that is done then here is what you need to do:
you can go here for more, but it is explained below..

PLEASE! PLEASE!
Make a backup!

In your mediawiki root folder where your index file is there is a file called LocalSettings.php you are going to want to download it open it with a text editor and near the top there are some lines of code that say for example:

```
require_once( "includes/DefaultSettings.php" );
or
include( "includes/DefaultSettings.php" );
```
It says on some to add the code to the bottom of your settings file but I prefer to keep them together.. you will want to add this:

```
include("$IP/extensions/Treeview.php");
#$wgExtensionFunctions[] = 'wfTreeviewAddJS';
#function wfTreeviewAddJS() { $GLOBALS['wgTreeView']->addJS($GLOBALS['wgOut']); }
```
right underneath those lines, then just re-upload your file and choose to overwrite if it asks, then just go to your wiki.. if it worked everything should do what it says it can do here if not try refreshing your page or clearing your catch before reporting any problems..


----------

