Extension:VarsInSession

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
VarsInStatus
Release status: unmaintained
Implementation Parser extension , Parser function , Variable
Description Allows to create variables that are stored in the session object and accessible across multiple http requests
Author(s) Al Hooton (Ahootontalk)
Latest version 1.1.1 (2020-05-11)
MediaWiki 1.15+
PHP 5.2.x+
Database changes No
License GNU General Public License 2.0 or later
Download See code
Check usage and version matrix.

The VarsInSession extension allows the creation/usage of persistent variables in the Session object through three parser functions. These variables persist across multiple HTTP requests until the session cookie is removed or expires from the browser. By default this is when the user closes their browser.

Usage[edit]

{{#svarset: <variable name> | <value>}}<br>{{#svar-set: <variable name> | <value>}}

The #svarset and #svar-set parser functions are synonyms, and set a variable to the given value. The <variable name> parameter is required. The <value> is optional, and will be the null value if not provided.

{{#svarget: <variable_name> | <default value>}}<br>{{#svarget: <variable_name> | <default value>}}

The #svarget and #svar-get parser functions are synonyms, and get the value of a variable. The <variable name> parameter is required. The <default value> is optional, and is the returned value if <variable name> does not exist.

{{#svar-isset: <variable name>}}

The #svar-isset parser function returns "true" if the variable has been previously set, or returns the empty string if the variable has not previously been set.

Installation[edit]

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/VarsInSession/VarsInSession.php";
    // Parser caching must also be disabled to ensure each invocation of "#svarset" and "#svarget" are working with values from the Session object of the current user:
    $wgEnableParserCache = false;
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

VarsInSession.php
<?php
/**
 * Simple extension implementing persistent variables stored in the session
 * object. These variables persist as long as the underlying php session
 * object exists and can be accessed, normally until the user closes their
 * browser or deletes cookies.
 * 
 * @link https://www.mediawiki.org/wiki/Extension:VarsInSession Documentation
 * @link https://www.mediawiki.org/wiki/Extension_talk:VarsInSession Support
 *
 * @author Al Hooton (Ahooton) <al@hootons.org>
 *
 * @license https://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 * 
 * Usage:
 * 
 * {{#svar-set:<varname>|<value>}} OR {{#svarset:<varname>|<value>}} sets the 
 * value <value> to the variable <varname>
 * 
 * {{#svar-get:<varname>|<default value>}} OR {{#svarget:<varname>|<default value>}} 
 * retrieves the value of <varname> or returns <default value> in case that it has 
 * no previous value
 * 
 * {{#svar-isset:<varname>}} returns "true" if the var is set, empty string otherwise
 * 
 * History:
 * 1.0.0, 25-Jan-2010: Initial release
 * 1.0.1, 26-Jan-2010: Bug fix, handle $_SESSION not existing
 * 1.1.0, 28-Feb-2010: Added svar-isset query, aliases svar-set and svar-get
 * 1.1.1, 11-May-2020: Enhanced security, small tweaks
 * 
 */
 
// Ensure that the script cannot be executed outside of MediaWiki
if ( !defined( 'MEDIAWIKI' ) ) {
	die( "This is an extension to MediaWiki and cannot be run standalone." );
}

// Display extension's information on "Special:Version"
$wgExtensionCredits['parserhook'][] = array(
	'path'         => __FILE__,
	'name'         => 'VarsInSession',
	'version'      => '1.1.1',
	'description'  => 'Enables the creation of session-persistent variables',
	'author'       => 'Al Hooton',
	'url'          => 'https://www.mediawiki.org/wiki/Extension:VarsInSession'
);

// Create extension's class
$wgExtensionFunctions[] = 'VarsInSession_Setup';

// Register hook
$wgHooks['LanguageGetMagic'][] = 'VarsInSession_Magic';

// Perform actions
function VarsInSession_Setup() {
    global $wgParser;

    $wgParser->setFunctionHook( 'svarget', 'svarget_exec' );
    $wgParser->setFunctionHook( 'svar-get', 'svarget_exec' );
    $wgParser->setFunctionHook( 'svarset', 'svarset_exec' );
    $wgParser->setFunctionHook( 'svar-set', 'svarset_exec' );
    $wgParser->setFunctionHook( 'svar-isset', 'svarisset_exec' );
}

function VarsInSession_Magic( &$magicWords, $langCode ) {
    $magicWords['svarget'] = array( 0, 'svarget' );
    $magicWords['svar-get'] = array( 0, 'svar-get' );
    $magicWords['svarset'] = array( 0, 'svarset' );
    $magicWords['svar-set'] = array( 0, 'svar-set' );
    $magicWords['svar-isset'] = array( 0, 'svar-isset' );
    return true;
}

function svarget_exec(&$parser, $varname=null, $default="") {

        if ($varname == null) {
          trigger_error("#svar-get: variable name null");
          return "";
        }

        $ret = $default;
        if (isset($_SESSION) && (array_key_exists('vis_'.$varname, $_SESSION))) {
          $ret = $_SESSION['vis_'.$varname];
        }

        return $ret;
}


function svarset_exec(&$parser, $varname=null, $value=null) {

        if ($varname == null) {
          trigger_error("#svar-set: variable name null");
          return "";
        }

        if (isset($_SESSION)) {
          $_SESSION['vis_'.$varname]=$value;
        }

        return "";
}

function svarisset_exec(&$parser, $varname=null) {

        if ($varname == null) {
          trigger_error("#svar-isset: variable name null");
          return "";
        }

        $ret = "";
        if ((isset($_SESSION)) &&
            (array_key_exists('vis_'.$varname, $_SESSION))) {
          $ret = "true";
        }

        return $ret;
}