Extension:ExtensionRenderer

This extension allows articles to use a custom renderer based on their extension. (e.g. SomStuff.extension)

Usage
To register a custom renderer: register adds the hook to $wgHooks['ExtensionRenderer'. ] so any value that is valid for $wgHooks is valid for register. The extension must start with a period ('.').

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

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

Configuration parameters
By default .xml and .xsl are registered with the default renderer. ($wgExtensionRenderer->defaultExtensionRenderer) The default extension renderer just escapes any special html characters and wraps the results in pre tags. This can be disabled by changing the parameter to $wgExtensionRenderer->install to false.

ExtensionRenderer.php
*     register adds the hook to $wgHooks['ExtensionRenderer'. ] so *     any value that is valid for $wgHooks is valid for register. * The extension must start with a period ('.'). * *      Rendering occurs at the 'ParserBeforeTidy' hook. * *      Currently the extension disables itself during editing because it messes *     up the edit page. (comment out the $this->mDisableRendering flag in the *     first if statement in hookParserBeforeString and use the default renderer *      to see what I mean); * *      A default renderer (defaultExtensionRenderer) is included that merely *     escapes html special characters and surrounds the result in pre tags. * *      Acknowledgements *     Extension:AlternateSyntaxParser was used to determine how to set things *     up for custom rendering. * *      @author Michael Eagar */ if ( !defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension, it is not a valid entry point' ); } $wgExtensionCredits['parserhook'][] = array(   'path' => __FILE__,    'name'  =>'ExtensionRenderer',    'author'=>'Michael Eagar',    'url'   =>'http://www.mediawiki.org/wiki/Extension:ExtensionRenderer',    'description'=>'Allows custom rendering of registered extensions',    'version'    =>'1.0 (Sep 13 2008)' ); /** *     Encapsulation of the extension */ class ExtensionRenderer { public $mExtHooks = array; public $mDisableRendering = false; /**    *  Register a hook for the given extension. * $hook is added to $wgHooks['ExtensionRenderer'. $ext] *    * @param unknown_type $ext * @param unknown_type $hook */   public function register($ext, $hook) { global $wgHooks; $this->mExtHooks[] = $ext; $wgHooks['ExtensionRenderer'. $ext][] = $hook; }   /**     *  Hides the article from the parser if the title ends in an extension * previously registered with register. *     *  This function hooks into $wgHooks['ParserBeforeStrip'] *    *  @param Parser $parser The current parser * @param String $text   The article source * @return Boolean       false if wiki source was hidden, true otherwise */   public function hideText(&$parser, &$text) { global $wgTitle; global $wgRequest; #  Make sure we're processing the main article. if (!$parser->mRevisionId || $this->mDisableRendering) { return true; }                  #   Determine the extension $ext = strrchr($wgTitle->getFullText, "."); if ($ext === false) { return false; }       if (in_array($ext, $this->mExtHooks)) { $parser->mHiddenText = $text; $parser->mExt = $ext; $text = ""; } else { unset($parser->mHiddenText); unset($parser->mExt); }       return false; }   /**     *  Renders the article using a custom hook. *    *  This function hooks into $wgHooks['ParserBeforeStrip'] *     *  @param Parser $parser The current parser * @param String $text   The article source * @return Boolean       false if an attempt was made to render the *                       article, true otherwise */   function showText(&$parser, &$text) { global $wgHooks; #  Make sure the current article is an extension article if (!isset($parser->mHiddenText) || !isset($parser->mExt)) return true; $ext = $parser->mExt; $result = wfRunHooks('ExtensionRenderer' . $ext, array( &$parser, &$parser->mHiddenText, $ext)); if ($result !== false) { $text .= "" . "Could not process extension. \n"; } else { $text .= ' '. $parser->mHiddenText. ' ';       }        unset($parser->mHiddenText); unset($parser->Ext); return false; }   /**     *  Disables custom rendering of extensions. And allows the wiki parser to    *  work. * Currently this method is attached to     *    to prevent this * extension from messing up the edit page. Note that because custom * rendering is disabled preview might not display properly. */   function disableRendering( &$editpage ) { $this->mDisableRendering = true; return true; }   /**     *  Disables custom rendering of extensions. And allows the wiki parser to    *  work. * Currently this method is attached to     *    to re-enable * this extension after being disabled at     * */   function enableRendering( &$editpage, &$out ) { $this->mDisableRendering = false; return true; }   /**     *      Setup the default renderer, which just escapes special html characters and *     wraps the results in pre tags. */   public function defaultExtensionRenderer(&$parser, &$text, $ext) { $text = ' '. htmlspecialchars($text). ' ';
 * 1)     This is a mediawiki extension so don't execute if mediawiki isn't available
 * 1) Credits

return false; }   /**     *  Insert this class into MediaWiki * If $addDefaultHooks is set some common extensions are registered. * (currently .xml and .xsl) *    *  @param Boolean $addDefaultHooks */   function install($addDefaultHooks) { global $wgHooks; $wgHooks['ParserBeforeStrip'][] = array($this, 'hideText'); $wgHooks['ParserAfterTidy'][] = array($this, 'showText'); $wgHooks['EditPage::showEditForm:initial'][] = array($this, 'disableRendering'); $wgHooks['EditPage::showEditForm:fields'][] = array($this, 'enableRendering');

if ($addDefaultHooks) { $this->register('.xml', array($this, 'defaultExtensionRenderer')); $this->register('.xsl', array($this, 'defaultExtensionRenderer')); }       } } $wgExtensionRenderer = new ExtensionRenderer; $wgExtensionRenderer->install(true); ?>
 * 1)     Insert this extension into MediaWiki
 * 1)     Change the argument to true to add custom renderers for some common
 * 2)     extensions. (Currently the default renderer is registered for .xsl and
 * 3)     .xml)

Acknowledgments
Extension:AlternateSyntaxParser was used to determine how to set things up for custom rendering.