User:Gizmhail/RawContent

Purpose

edit

This extension adds the rawcontent parser function. It returns a page content unmodified (unlike msgnw wich transforms "---" symbols or bulleted lists). It can be seen as an improved (or fixed) msgnw.

It can be used, for instance, to include the true content of a page in a textarea form, using an extension like SimpleForm (trying to do this with msgnw parser function works, until the included page contains lists, includeonly sections or "----" symbols ).

It also adds the normalizedfullpagename parser function (see below).

Usage

edit
{{#rawcontent:<Page title>}}

This will return <Page title> page content.

{{#normalizedfullpagename:<Full page title>}}

This will return namespace:pagetitle for <Full page title>, with namespace translated to english (for instance, "Modèle" namespace from a french wiki will be translated to "Template). It is useful, for instance, if you have to synchronize technical templates (like Template:Ambox of wikipedia) having the same name between several wikis in different languages.

Code

edit
<?
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * @author Sebastien Poivre <gizmhail@gmail.com>
 * @copyright Copyright (C) 2008 Sebastien Poivre
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */

$wgExtensionCredits['parserhook'][] = array(
        'name' => 'RawContent',
        'author' =>  'Orange Labs (Sebastien Poivre)',
        'url' => 'http://www.mediawiki.org/wiki/User:Gizmhail/RawContent',
        'description' => 'Add (mainly) the rawcontent parser function. It returns a page content unmodified (unlike msgnw wich transforms among others "---" symbols, includeonly sections or bulleted lists).',
        'version' => 0.3
  );

# Define a setup function
$wgExtensionFunctions[] = 'wfRawContentParserFunction_Setup';
# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][]       = 'wfRawContentParserFunction_Magic';
$wgHooks['ParserAfterTidy'][] = 'wfRawContentParserAfterTidy';

function wfRawContentParserFunction_Setup() {
        global $wgParser;
        # Set a function hook associating the "rawcontent" magic word with our function
        $wgParser->setFunctionHook( 'rawcontent', 'wfRawContentParserFunction_Render' );
        $wgParser->setFunctionHook( 'normalizedfullpagename', 'wfRawContentParserFunctionNormalizedPageName_Render' );
}

function wfRawContentParserFunction_Magic( &$magicWords, $langCode ) {
        # Add the magic word
        # The first array element is case sensitive, in this case it is not case sensitive
        # All remaining elements are synonyms for our parser function
        $magicWords['rawcontent'] = array( 0, 'rawcontent' );
        $magicWords['normalizedfullpagename'] = array( 0, 'normalizedfullpagename' );
        # unless we return true, other parser functions extensions won't get loaded.
        return true;
}

function wfRawContentParserFunction_Render( &$parser, $param1 = '', $param2 = '' ) {
        # The parser function itself
        # The input parameters are wikitext with templates expanded
        # The output should be wikitext too
	global $rawContentMarkerList;
	$title = Title::newFromText( $param1 );
	$articleContent = "";
	if($title){
		list($articleContent,$final_title) = $parser->fetchTemplateAndtitle( $title );
		if(!$final_title||$final_title->getText()!=$title->getText()||$title->mNamespace!=$final_title->mNamespace){
			$articleContent = '';
			#Redirection
			#TODO : check that it is the proper way to do this
			$rev = Revision::newFromTitle( $title );
			if($rev){
				$articleContent =  $rev->getText();
			}
		}
	}
	$makercount =  count($rawContentMarkerList);
	$marker = "xx-start-raw-content---".$makercount."-raw-content-end-xx";
	$rawContentMarkerList[$makercount] = $articleContent;
	return $marker;
}


function wfRawContentParserFunctionNormalizedPageName_Render(&$parser,$param1 = ''){
        $title = Title::newFromText( $param1 );
        $namespace = '';
        $pagename = '';
        if($title){
                $namespace = Language::factory( 'en' )->getNsText($title->mNamespace);
                $pagename = $title->getText();
        }
        if($namespace != ''){
                $pagename = "$namespace:$pagename";
        }
        return $pagename;
}

function wfRawContentParserAfterTidy(&$parser, &$text){
        global $rawContentMarkerList;
        for ($i=0;$i<count($rawContentMarkerList);$i++)
                $text = preg_replace(
                        '/xx-start-raw-content---'.$i.'-raw-content-end-xx/',
                        $rawContentMarkerList[$i],
                        $text);
        return true;
}