Extension:ExtensionRenderer

From MediaWiki.org

Jump to: navigation, search

         

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
XSL Renderer

Release status: beta

Implementation  Tag
Description Allows custom rendering of articles with extensions. (Like Some_Data.xml)
Author(s)  oakbladeTalk
Last Version  2008-09-13
License MIT
Download no link

check usage (experimental)

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

Contents

[edit] Usage

To register a custom renderer:

    global $wgExtensionRenderer;
    $wgExtensionRenderer->register(<extension>, <hook(&$parser, &$text, $ext)>;

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

[edit] Download instructions

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

[edit] Installation

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

#add configuration parameters here
#setup user rights here
require_once("$IP/extensions/ExtensionRenderer.php");


[edit] 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.


[edit] Code

<?php
<?php
/**
 *  Copyright (c) 2008 Michael Eagar
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy 
 *  of this software and associated documentation files (the "Software"), to deal 
 *  in the Software without restriction, including without limitation the rights to 
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
 *  the Software, and to permit persons to whom the Software is furnished to do 
 *  so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included in all 
 *  copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
 *  OTHER DEALINGS IN THE SOFTWARE. 
 * 
 *      MediaWikia Extension:ExtensionRenderer
 *      This extension allows articles to use a custom renderer based on their
 *      extension. (e.g. SomStuff.extension)
 * 
 *      To register a custom renderer:
 *      <code>
 *      global $wgExtensionRenderer;
 *      $wgExtensionRenderer->register(<extension>, <hook(&$parser, &$text, $ext)>;
 *      </code>
 *      register adds the hook to $wgHooks['ExtensionRenderer' . <extension>] 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
 */
 
#     This is a mediawiki extension so don't execute if mediawiki isn't available
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}
 
# Credits
$wgExtensionCredits['parserhook'][] = array(
    'name'  =>'ExtensionRenderer',
    'author'=>'Michael Eagar',
    'url'   =>'',
    'description'=>'Allows custom rendering of registered extensions',
    'version'    =>'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(<ext>, <hook>).
     * 
     *  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 .= "<div class=\"errorbox\">"
                        . "Could not process extension.</div>\n";     
        } else {
                $text .= '<div>' . $parser->mHiddenText . '</div>';
        }
 
        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
     *  <code>$wgHooks['EditPage::showEditForm:initial']</code> 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
     *  <code>$wgHooks['EditPage::showEditForm:fields']</code> to re-enable
     *  this extension after being disabled at
     *  <code>$wgHooks['EditPage::showEditForm:initial']</code>
     */
    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 = '<pre>' . htmlspecialchars($text) . '</pre>';
 
        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'));
        }    
    }
}
 
#     Insert this extension into MediaWiki
$wgExtensionRenderer = new ExtensionRenderer();
#     Change the argument to true to add custom renderers for some common
#     extensions. (Currently the default renderer is registered for .xsl and
#     .xml)
$wgExtensionRenderer->install(true);

[edit] Acknowledgments

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