Extension:PageEval

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.svg
PageEval

Release status:Extension status beta

ImplementationTemplate:Extension#type Parser function
DescriptionTemplate:Extension#description Load a wiki page into another.
Author(s)Template:Extension#username Linus Sundqvist
Latest versionTemplate:Extension#version 1.3d (2009-03-26)
MediaWikiTemplate:Extension#mediawiki >= 1.8
LicenseTemplate:Extension#license GPL
Download #Code

Translate the PageEval extension if it is available at translatewiki.net

Check usage and version matrix.

What can this extension do?[edit]

Loads a wiki page into another. Custom and language namespaces are supported.

Usage[edit]

Usage: {{ #pageeval: page | namespace }}
	page 		- Title of page to load, case-insensitive.
	namespace 	- (OPTIONAL) In what namespace the page exists, case-insensitive. Default is Main.

Example[edit]

Load MediaWiki:Sidebar page.

{{#pageeval:sidebar|mediawiki}}

Load the Mainpage.

{{#pageeval:mainpage}}

Download instructions[edit]

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

Installation[edit]

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

require_once("$IP/extensions/PageEval/PageEval.php");

Configuration parameters[edit]

$wgPageEvalConfig['noarticletext'] = true; // true/false - show message that the page doesn't exist
$wgPageEvalConfig['defaultns']	   = 0; // numeric - default namespace to use, if entered doesn't exist
$wgPageEvalConfig['showedit']      = true; // true/false - show edit link

Code[edit]

<?
/*
 * PageEval
 *     Load a wiki page into another. Custom and language namespaces are supported.
 *
 * Version 1.3d beta (2009-03-26)
 *     http://www.mediawiki.org/wiki/Extension:PageEval
 *     Copyright (c) Linus Sundqvist 2009
 *
 * Usage: {{ #pageeval: page | namespace }}
 *     page          - Title of page to load, case-insensitive.
 *     namespace     - (OPTIONAL) In what namespace the page exists, case-insensitive. Default is Main.
 *
 * Parameters:
 *     $wgPageEvalConfig['noarticletext'] = true; // true/false - show message that the page doesn't exist
 *     $wgPageEvalConfig['defaultns']     = 0; // numeric - default namespace to use, if entered doesn't exist
 *     $wgPageEvalConfig['showedit']      = true; // true/false - show edit link
 * 
 * Released under GPL licence
 *
 * Whats new
 *     1.3d beta
 *         Fixed a few edit link bugs.
 *     1.3c beta
 *         Added edit link and true check for edit rights.
 *
 * Todo
 *     
*/

$wgExtensionFunctions[]		= 'wfPageEvalSetup';
$wgHooks['LanguageGetMagic'][]		= 'wfPageEvalGetMagic';
$wgExtensionCredits['parserhook'][]	= array(
	'name' => 'PageEval',
	'author' =>'Linus Sundqvist', 
	'url' => 'http://www.mediawiki.org/wiki/Extension:PageEval', 
	'description' => 'Load a wiki page into another.',
	'version' => '1.3d beta'
);

function wfPageEvalSetup() {
	global $wgParser;
	$wgParser->setFunctionHook( 'pageeval', 'wfPageEvalRender' ); // {{#pageeval}}
}

function wfPageEvalGetMagic( &$magicWords, $langCode ) {
	$magicWords['pageeval'] = array( 0, 'pageeval' );
	return true;
}

function wfPageEvalRender( &$parser, $evalpage = '', $namespace = '' ) {
	$parser->disableCache();
	global $wgPageEvalConfig, $wgTitle, $wgContLang, $wgScriptPath, $mediaWiki, $wgUser;

	if ($evalpage <> "")
	{
		if ($namespace <> "" && defined("NS_".strtoupper($namespace)))
		{
			// Mediawiki or custom namespace
			$ns = constant("NS_".strtoupper($namespace));
		}
		else if ($namespace <> "")
		{
			// Language namespace
			foreach($wgContLang->namespaceNames as $key => $val) {
				if(strcasecmp($namespace, $val) == 0) $ns = $key;
			}
		}

		// Default namespace
		if (!isset($ns))
		{
			if (isset($wgPageEvalConfig['defaultns']))
				$ns = $wgPageEvalConfig['defaultns'];
			else
				$ns = 0;
		}

		// Media or Special namespace not allowed
		if ($ns < 0)
			return "";

		$namespace = $wgContLang->namespaceNames[$ns];

		$dbw =& wfGetDB( DB_MASTER );

		extract($dbw->tableNames('text', 'revision', 'page'));

		$res = $dbw->query("SELECT old_text FROM $text WHERE old_id =
			(SELECT rev_text_id FROM $revision r WHERE r.rev_id =
			(SELECT page_latest FROM $page WHERE UCASE(page_title)='".strtoupper($evalpage)."'
			AND page_namespace='".$ns."' ORDER BY page_latest DESC LIMIT 1) LIMIT 1)
			LIMIT 1");

		if (stristr($evalpage, "/") != false)
		{
			for ($i=0; $i<strlen($evalpage); $i++)
			{
				if ($evalpage[$i] == "/")
				{
					$evalpage[$i+1] = ucfirst($evalpage[$i+1]);
				}
			}
		}

		if($dbw->numRows($res) > 0)
		{
			$row = $dbw->fetchRow($res);
			$dbw->freeResult($res);

			if (!isset($wgPageEvalConfig['showedit']))
				$wgPageEvalConfig['showedit'] = true;

			$title = Title::newFromUrl($mediaWiki->params['server']."$wgScriptPath/index.php?title=$evalpage");

			if ($wgPageEvalConfig['showedit'] == true && $title->quickuserCan('edit'))
			{
				$row['old_text'] .= "\n\n<small>[[".$mediaWiki->params['server']."$wgScriptPath/index.php?title=";

				if ($namespace <> "")
					$row['old_text'] .= ucfirst($namespace).":";

				$row['old_text'] .= ucfirst($evalpage)."&action=edit ".wfMsg('edit')."]]</small>";
			}

			return $row['old_text'];
		}
		else
		{
			if (!isset($wgPageEvalConfig['noarticletext']))
				$wgPageEvalConfig['noarticletext'] = true;

			if ($wgPageEvalConfig['noarticletext'] == true)
			{
				if ($namespace <> "")
					return str_replace($wgTitle->mTextform, str_replace(" ", "_", ucfirst($namespace).":".ucfirst($evalpage)), wfMsg('noarticletext'));
				else
					return str_replace($wgTitle->mTextform, str_replace(" ", "_", ucfirst($evalpage)), wfMsg('noarticletext'));					
			}
		}
	}
}
?>