Extension:PageVariable

This function 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 you database):

CREATE TABLE .pagevariable_data (  pgv_entity VARCHAR(255) NOT NULL,   pgv_attribute VARCHAR(255) NOT NULL,   pgv_value TEXT );

2) Put into /wiki/extensions/PageVariableExtension as PageVariableExtension.php the following:  'PageVariableExtension',  'version' => PageVarExtn::$mVersion,  'author' => 'Rob Challen',  'url' => 'http://www.mediawiki.org/w/index.php?title=Extension:PageVariableExtension',  'description' => 'define variables and 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 static $mVersion = "0.2"; 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, "") === false ) { $dbr->delete( 'pagevariable_data', $record ); $record['pgv_value'] = $value; $dbr->insert( 'pagevariable_data', $record ); } else { $value="".$value; $dbr->delete( 'pagevariable_data', $record ); $record['pgv_value'] = " Warning: circular reference "; $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 = "".$record['pgv_value']; $dbr->freeResult($res); } else { $result = ""; }		return $parser->insertStripItem( $result, $parser->mStripState ); } }

3) add the following line to your wiki/LocalSettings.php: require_once("$IP/extensions/PageVariableExtension/PageVariableExtension.php");

4) use the following functions: returns VARIABLEVALUE

or:

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.