---
title: PHP Example
encoding: utf-8
filter:
  - erb
  - maruku
---

<pre class="brush: php">
&lt;?php
if (!defined('MEDIAWIKI'))
	exit(1);

$wgExtensionFunctions[] = "wfSyntaxHighlighterExtension";

$wgExtensionCredits['other'][] = array(
	'name' => 'SyntaxHighlighter',
	'author' => array('Alex Gorbatchev'),
	'version' => '1.0',
	'url' => 'http://alexgorbatchev.com/projects/syntaxhighlighter',
	'description' => 'Provides tight integration with SyntaxHighlighter.'
);

// Path to the SyntaxHighlighter scripts
$wgSyntaxHighlighterScriptPath = "{$wgScriptPath}/extensions/SyntaxHighlighter/scripts";

// Path to the SyntaxHighlighter styles
$wgSyntaxHighlighterStylesPath = "{$wgScriptPath}/extensions/SyntaxHighlighter/styles";

// Theme CSS file
$wgSyntaxHighlighterTheme = "shThemeDefault.css";

// Brushes to include on the page
$wgSyntaxHighlighterBrushes = array(
	'shBrushBash.js',
	'shBrushCpp.js',
	'shBrushCSharp.js',
	'shBrushCss.js',
	'shBrushDelphi.js',
	'shBrushDiff.js',
	'shBrushGroovy.js',
	'shBrushJava.js',
	'shBrushJScript.js',
	'shBrushPhp.js',
	'shBrushPlain.js',
	'shBrushPython.js',
	'shBrushRuby.js',
	'shBrushScala.js',
	'shBrushSql.js',
	'shBrushVb.js',
	'shBrushXml.js'
);

$dir = dirname(__FILE__) . '/';

function wfSyntaxHighlighterExtension()
{
	global $wgOut, $wgScriptPath, $wgParser, 
		$wgSyntaxHighlighterBrushes, $wgSyntaxHighlighterScriptPath, 
		$wgSyntaxHighlighterStylesPath, $wgSyntaxHighlighterTheme;
	
	// Make shCore.js the very first script to be included (before all the brushes)
	array_unshift($wgSyntaxHighlighterBrushes, 'shCore.js');
	
	$home = $wgSyntaxHighlighterScriptPath;
	
	// Add all scripts to the header
	foreach ($wgSyntaxHighlighterBrushes as $script)
		$wgOut->addScript("&lt;script type=\"text/javascript\" src=\"{$wgSyntaxHighlighterScriptPath}/$script\">&lt;/script>\n");

	// Add CSS links
	foreach (array('shCore.css', $wgSyntaxHighlighterTheme) as $css)
		$wgOut->addHeadItem($css, "&lt;link type=\"text/css\" rel=\"stylesheet\" href=\"{$wgSyntaxHighlighterStylesPath}/$css\"/>\n");

	// Add initialization code
	$wgOut->addScript(
		"&lt;script type=\"text/javascript\">".
			"var syntaxHighlighterConfig = { clipboardSwf: '{$wgSyntaxHighlighterScriptPath}/clipboard.swf' };".
			"SyntaxHighlighter.highlight();".
		"&lt;/script>\n"
		);
	
	// Finally, set up a MediaWiki hook to the &lt;sh /> tag
	$wgParser->setHook("sh", "wfSyntaxHighlighterExtensionRender"); 
	
	return true;
}

function wfSyntaxHighlighterExtensionRender($input, $argv, $parser)
{
	$args = "";
	
	while (list($key, $val) = each($argv))
		$args .= "$key: $val;";
	
	$input = htmlspecialchars($input);
	return "&lt;pre class=\"$args\">\n$input\n&lt;/pre>";
}
</pre>

<%= render(:partial => "/SyntaxHighlighter/partials/brushes") %> 

 
