From mediawiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
Release status: unmaintained
Implementation Tag
Description embed source code from a file on a page, auto syntax highlighted
Author(s) (groovehuntertalk)
Latest version 0.2 (2009/09/03)
MediaWiki 1.11, 1.13
MediaWiki 1.36 Not formally tested
MediaWiki 1.34 Not formally tested
MediaWiki 1.32 Not formally tested
License No license specified
Download see below
Example http://florian-konnertz.de/wiki/ArrayView.php

Check usage and version matrix.

What can this extension do?[edit]

embed source code from a file on a page, auto syntax highlighted. It's copied partially from Extension:Embed Document and requires geshi, so I just install Extension:SyntaxHighlight_GeSHi


use on your page:


in one line. The path starts from the root of your public code repository (see installation). The language for highlighting is found from file suffix automatically by GeSHi

Download instructions[edit]

Please cut and paste the code found below and place it in $IP/extensions/EmbedHighlightedCodeFromFile.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.


Install Extension:SyntaxHighlight_GeSHi

To install this extension, add the following to LocalSettings.php:


create a symlink code/ inside your MW root dir pointing to your public code repository. Ensure that everything below your defined root is okay for public viewing, ie. check for forgotten symlinks inside! Otherwise people may read access your operating system settings!


$wgExtensionFunctions[] = "register_embed_code_handler";

$dir = dirname(__FILE__) . '/';
$geshidir = $dir . 'SyntaxHighlight_GeSHi/geshi/';

function register_embed_code_handler() {
    global $wgParser;
    $wgParser->setHook( "embed_code", "embed_code_handler" );
    $wgParser->setHook( "ec",         "embed_code_handler" );

function embed_code_handler( $input, $argv ) {
    $allowedchars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                          'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                          'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                          'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                          '_', '/', '.');

    // test for allowed chars
    if( ! str_replace($allowedchars, '', $input) == '' ) {
        $errormsg = "<font color=#aa0000>Error: invalid character sequence between <code>&lt;embed_code&gt;...&lt;/embed_code&gt;</code> markers, ";
        $errormsg .= "allowed are only<ul><li>a...z</li><li>A...Z</li><li>_</li><li>/</li><li>.</li></ul></font>";
        return $errormsg;

    // test for ../ filesystem going up - dangerous!
    $pos = strstr( $input, '..' );
    if (! $pos ===false ) {
        $errormsg = "<font color=#aa0000>Error: ../ path elements are not allowed, sorry! </font>";
        return $errormsg;

    $input = 'code/'.$input;
    if ( file_exists($input) ) {
        $path_parts = pathinfo($input);
        $ext = $path_parts["extension"];

        $language = '';
        $source = file_get_contents($input);
        $geshi =& new GeSHi($source, $language);
        $lang = $geshi->get_language_name_from_extension( $ext );
        $geshi->set_language( $lang );

        return $geshi->parse_code();