Open main menu

Extension:PageVariable

MediaWiki extensions manual
OOjs UI icon advanced.svg
PageVariable
Release status: beta
Implementation Parser function
Description Allows for a variable to be assigned on a page and the html to be reused on another page
Author(s) Rob Challen
MediaWiki 1.12+
License GPL
Download see below
Example see below
Translate the PageVariable extension if it is available at translatewiki.net
Check usage and version matrix.

The PageVariable extension allows you to set a variable in a wiki page, re-use it in that page and in other pages. It provides 2 parser functions #setv and #getv which set and get variables. For usage see end of this page. I use it as a simple method of re-using small elements of one page in another and particularly for extracting one or two elements from templates. It can handle links but like other parser functions will struggle with wiki syntax tables. This function is similar to the labelled section transclusion function.

It needs a parser hook so that it updates the database table on importing data. Maybe one day I will get round to it.

Provided as is & at own risk under GNU general public licence

1) create a new table in your wiki database (you will have to modify this for your database):

CREATE TABLE <YOUR DB NAME>.pagevariable_data (
   pgv_entity VARCHAR(255) NOT NULL,
   pgv_attribute VARCHAR(255) NOT NULL,
   pgv_value TEXT
);

2) Put into /wiki/extensions/PageVariable as PageVariable.php the following:

<?php

$wgExtensionCredits['parserhook'][] = array(
  'name' => 'PageVariable',
  'version' => '0.2',
  'author' => 'Rob Challen',
  'url' => 'https://www.mediawiki.org/wiki/Extension:PageVariable',
  'description' => 'Allows to define variables and to retrieve them from other pages'
);

// instance of this extension class
$wgPageVarExtn = new PageVarExtn();

// register the extension
$wgExtensionFunctions[] = array( &$wgPageVarExtn, "setup" );
$wgHooks['LanguageGetMagic'][] = array( &$wgPageVarExtn, "onLanguageGetMagic" );
$wgHooks['ArticleDeleteComplete'][] = array( &$wgPageVarExtn, "onArticleDeleteComplete" );
$wgHooks['ArticleSave'][] = array( &$wgPageVarExtn, "onArticleSave" );

class PageVarExtn
{
	public $mDataBuffer = array();

	public function setup() {
		global $wgParser;
		// parser functions
		$wgParser->setFunctionHook( 'getv', array( &$this, "getv" ) );
		$wgParser->setFunctionHook( 'setv', array( &$this, "setv" ) );
	}

	public function onLanguageGetMagic( &$magicWords, $langCode ) {
		$magicWords['setv'] = array( 0, 'setv' );
		$magicWords['getv'] = array( 0, 'getv' );
		return true;
	}

	// hook to delete data when an article is deleted
	public function onArticleDeleteComplete( $article )
	{
		$pgv_entity = $article->getTitle()->getPrefixedDBkey();
		$dbr =& wfGetDB( DB_MASTER );
		// Delete all data for this page.
		$dbr->delete( 'pagevariable_data', array( 'pgv_entity' => $pgv_entity ) );
		return true;
	}

	// hook to save data when an article is saved
	public function onArticleSave( $article )
	{
		$pgv_entity = $article->getTitle()->getPrefixedDBkey();
		$dbr =& wfGetDB( DB_MASTER );

		// Delete all data for this page.
		$dbr->delete( 'pagevariable_data', array( 'pgv_entity' => $pgv_entity ) );
		return true;
	}

	// display and/or store data
	public function setv( &$parser, $attribute, $value )
	{
		$entity = $parser->getTitle()->getPrefixedDBkey();
		$dbr =& wfGetDB( DB_MASTER );
		$value = $parser->recursivetagparse($value);
		
		if( $attribute )
		{
			$record['pgv_entity'] = $entity;
			$record['pgv_attribute'] = $attribute;
			if( strpos($value, "<!--PageVariableExtn:$entity:$attribute-->") === false ) {
				$dbr->delete( 'pagevariable_data', $record );
				$record['pgv_value'] = $value;
				$dbr->insert( 'pagevariable_data', $record );
			} else {
				$value="<!--PageVariableExtn error:circular reference detected:$entity:$attribute-->".$value;
				$dbr->delete( 'pagevariable_data', $record );
				$record['pgv_value'] = "<p>Warning: circular reference</p>";
				$dbr->insert( 'pagevariable_data', $record );
			}
		}
		return $parser->insertStripItem( $value, $parser->mStripState );
	}

	// retrieve data
	function getv( &$parser, $entity, $attribute)
	{
	
		$dbr =& wfGetDB( DB_SLAVE );
		
		if ($attribute && Title::newFromText($entity) && Title::newFromText($entity)->exists()) {
			$entity = Title::newFromText($entity)->getPrefixedDBkey();
			$query = "select distinct pgv_value from pagevariable_data where pgv_entity = '$entity' and pgv_attribute='$attribute'";
			$res = $dbr->query( $query , __METHOD__ );

			$record = $dbr->fetchRow( $res );
			$result = "<!--PageVariableExtn:$entity:$attribute-->".$record['pgv_value'];
			
			$dbr->freeResult($res);
		} else {
			$result = "<!--PageVariableExtn error:$entity:$attribute-->";
		}
		return $parser->insertStripItem( $result, $parser->mStripState );
		
	}
}

3) add the following line to your wiki/LocalSettings.php:

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

4) use the following functions:

{{#setv: VARIABLENAME | VARIABLEVALUE}} returns VARIABLEVALUE

or:

{{#getv: PAGENAME | VARIABLENAME}} returns VARIABLEVALUE

The function should return html as generated when the original page was parsed. You can use the function within templates to store parsed template data items for later re-use. It has similarity to the tiddlywiki concept of a slice. I use it a lot to allow in conjunction with semantic mediawiki for multiple layouts depending on context. The rare possibility of a circular reference is prevented using a html comment.