RawMsg

From Jimbojw.com

Jump to: navigation, search
<?php //

This is a MediaWiki Extension. For more information about it, see the documentation in RawMsg Extension.

Important! Unless otherwise specified, this extension is released under The MIT License. If you choose to install it, you do so at your own risk and discretion.

/*
 * RawMsg.php - Adds a parser function for inserting raw messages.
 * @author Jim R. Wilson
 * @version 0.1
 * @copyright Copyright (C) 2007 Jim R. Wilson
 * @license The MIT License - http://www.opensource.org/licenses/mit-license.php 
 * -----------------------------------------------------------------------
 * Requirements:
 *     MediaWiki 1.6.x, 1.9.x, 1.10.x or higher
 *     PHP 4.x, 5.x or higher.
 * Installation:
 *     1. Drop this script (RawMsg.php) in $IP/extensions
 *         Note: $IP is your MediaWiki install dir.
 *     2. Enable the extension by adding this line to your LocalSettings.php:
 *         require_once('extensions/RawMsg.php');
 * Version Notes:
 *     version 0.1:
 *         Initial release.
 * -----------------------------------------------------------------------
 * Copyright (c) 2007 Jim R. Wilson
 * 
 * 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. 
 * -----------------------------------------------------------------------
 */
 
# Confirm MW environment
if (defined('MEDIAWIKI')) {
 
# Credits
$wgExtensionCredits['parserhook'][] = array(
    'name'=>'RawMsg',
    'author'=>'Jim R. Wilson - wilson.jim.r&lt;at&gt;gmail.com',
    'url'=>'http://jimbojw.com/wiki/index.php?title=RawMsg',
    'description'=>'Adds a parser function for inserting raw messages.',
    'version'=>'0.1'
);
 
/**
 * Wrapper class for encapsulating RawMsg related parser methods
 */
class RawMsg {
 
    /**
     * Sets up parser functions.
     */
    function setup( ) {
    
        # Setup parser hook
    	global $wgParser, $wgVersion;
    	$hook = (version_compare($wgVersion, '1.7', '<')?'#rawmsg':'rawmsg');
    	$wgParser->setFunctionHook( $hook, array($this, 'parserFunction') );
    	
    	# Add system messages
    	global $wgMessageCache;
      $wgMessageCache->addMessage('rawmsg-missing-params', 'Error: <nowiki>{{#rawmsg:}}</nowiki> is missing a required parameter.');
      $wgMessageCache->addMessage('rawmsg-missing-article', 'Error: RawMsg cannot find the article [[MediaWiki:rawmsg-$1]].');
    }
    
    /**
     * Adds magic words for parser functions.
     * @param Array $magicWords
     * @param $langCode
     * @return Boolean Always true
     */
    function parserFunctionMagic( &$magicWords, $langCode='en' ) {
        $magicWords['rawmsg'] = array( 0, 'rawmsg' );
        return true;
    }
    
    /**
     * Inserts the requested raw message
     * @param Parser $parser Instance of running Parser.
     * @param String $msg Name of raw message to display.
     * @return String Receipt from parser as a result of successful insertion.
     */
    function parserFunction( $parser, $msg=null ) {
    
        # Short-circuit if no msg has been provided  
        if ($msg===null) return '<div class="errorbox">'.wfMsg('rawmsg-missing-params').'</div>';
 
        # Build title - short-circuit if doesnt exist
        $title = Title::newFromText('rawmsg-'.$msg, NS_MEDIAWIKI);
        if (!$title || !$title->exists()) return '<div class="errorbox">'.wfMsg('rawmsg-missing-article', $msg).'</div>';
        
        # Grab the content and send it through
        $article = new Article($title);
        return $parser->insertStripItem( $article->getContent(), $parser->mStripState );
    }
}
 
/**
 * Wrapper function for language magic call (hack for 1.6 
 */
 
# Create global instance and wire it up!
$wgRawMsg = new RawMsg();
$wgExtensionFunctions[] = array($wgRawMsg, 'setup');
if (version_compare($wgVersion, '1.7', '<')) {
    # Hack solution to resolve 1.6 array parameter nullification for hook args
    function wfRawMsgLanguageGetMagic( &$magicWords ) {
        global $wgRawMsg;
        $wgRawMsg->parserFunctionMagic( $magicWords );
        return true;
    }
    $wgHooks['LanguageGetMagic'][] = 'wfRawMsgLanguageGetMagic';
} else {
    $wgHooks['LanguageGetMagic'][] = array($wgRawMsg, 'parserFunctionMagic');
}
 
} # End MW Environment wrapper
//