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.

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' => 'TBD',  '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.1"; 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->getFullText; $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->getFullText; $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->getFullText; $dbr =& wfGetDB( DB_MASTER ); if( $attribute ) {			$record['pgv_entity'] = $entity; $record['pgv_attribute'] = $attribute; $dbr->delete( 'pagevariable_data', $record ); $record['pgv_value'] = $value; $dbr->insert( 'pagevariable_data', $record ); }		return $value; }

// retrieve data function getv( &$parser, $entity, $attribute) {		$dbr =& wfGetDB( DB_SLAVE ); $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); return $result; } }

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

4) use the following functions:

or: