Extension:VarsInSession

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
VarsInStatus
Release status: unmaintained
Implementation Parser extension, Parser function, Variable
Description 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 (2010-03-01)
MediaWiki 1.15+ (probably earlier as well)
PHP 5.2.x+
License GPL
Download Extension:VarsInSession#Code
Translate the VarsInSession extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?[edit]

This is a simple extension that 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.

NOTE: Some versions of Mediawiki do not create a $_SESSION global object variable for users that are not logged in, or it is created very late in the startup sequence. If you find you are having trouble with variables you set with this extension not persisting, it may be due to the lack of $_SESSION. In order to ensure this object exists prior to use, add this code to your LocalSettings.php:

global $wgSessionStarted;
if (! $wgSessionStarted) {
  wfSetupSession();
}

Download instructions[edit]

Please cut and paste the code found below and place it in $IP/extensions/VarsInSession/VarsInSession.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/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. Add this line to your LocalSettings.php file:

$wgEnableParserCache = false;

Code[edit]

<?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.

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

**/

$wgExtensionCredits['parserhook'][] = array(
'path'         => __FILE__,
'name'         => 'Session-persistent Variables Mediawiki Extension',
'version'      => '1.1.0',
'description'  => 'Enables the creation of session-persistent variables.',
'descriptionmsg' => '',
'author'       => '[mailto:al@hootons.org (Al Hooton)]',
'url'          => 'http://www.mediawiki.org/wiki/Extension:VarsInSession',
);

$wgExtensionFunctions[] = 'VarsInSession_Setup';
$wgHooks['LanguageGetMagic'][] = 'VarsInSession_Magic';

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;
}