Extension:SimpleForms.php

From Organic Design wiki
Revision as of 04:16, 10 May 2007 by Nad (talk | contribs) (update ver)

<?php

  1. MediaWiki SimpleForms Extension{{#Security:*|dev}}{{#Security:view|*}}Template:Php
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.
  1. - See http://www.mediawiki.org/wiki/Extension:Simple_Forms for installation and usage details
  2. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  3. - Author: http://www.organicdesign.co.nz/nad
  4. - Started: 2007-04-25, see article history

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('SIMPLEFORMS_VERSION','0.0.3, 2007-05-10');

$wgSimpleFormsFormMagic = "form"; # the parser-function name for form containers $wgSimpleFormsInputMagic = "input"; # the parser-function name for form inputs $wgSimpleFormsRequestMagic = "request"; # the parser-function name for accessing the post/get variables $wgSimpleFormsRequestPrefix = "sf_"; # restricts #request and GET/POST variable names to their own namespace, set to "" to disable

$wgExtensionFunctions[] = 'wfSetupSimpleForms'; $wgHooks['LanguageGetMagic'][] = 'wfSimpleFormsLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'Simple Forms', 'author' => '[http://www.organicdesign.co.nz/nad User:Nad', 'description' => 'Functions to make and process forms.', 'url' => 'http://www.mediawiki.org/wiki/Extension:Simple_Forms', 'version' => SIMPLEFORMS_VERSION );


class SimpleForms {

var $id = 0;

# Constructor function SimpleForms() { global $wgParser,$wgHooks,$wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsRequestMagic,$wgJavaScriptFunctions; $wgParser->setFunctionHook($wgSimpleFormsFormMagic,array($this,'form')); $wgParser->setFunctionHook($wgSimpleFormsInputMagic,array($this,'input')); $wgParser->setFunctionHook($wgSimpleFormsRequestMagic,array($this,'request')); }

# Renders a form and wraps it in tags for processing by tagHook function form(&$parser) { global $wgJavaScriptFunctions; $id = $this->id++; $args = ; foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $args .= " $match[1]=\"$match[2]\""; else $form = $arg; } $wgJavaScriptFunctions['SimpleFormsFunctions'] = ' function sfRequest(url,target) { sfXmlHttp = null if (window.XMLHttpRequest) sfXmlHttp = new XMLHttpRequest() else if (window.ActiveXObject) sfXmlHttp = new ActiveXObject('Microsoft.XMLHTTP') if (sfXmlHttp != null) { sfXmlHttp.onreadystatechange = sfRequestEvent sfXmlHttp.open('GET',url,true) sfXmlHttp.send(null) } else alert('Your browser does not support XMLHTTP, you can only use GET or POST requests.') sfXmlHttp.target = document.getElementById(target); } function sfRequestEvent() { if (sfXmlHttp.readyState == 4) { if (sfXmlHttp.status == 200) sfXmlHttp.target.innerHTML = sfXmlHttp.responseText else alert('Problem retrieving XML data!') sfXmlHttp.readyState = 0 } } '; return array("<form $args>$form</form>",'noparse'=>true); }

# Renders a form input function input(&$parser) { global $wgSimpleFormsRequestPrefix;

               $content = ;
               $method  = ;
               $type    = ;
               $args    = ;
               $argv    = array();

# Process args foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^([a-z0-9_]+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[strtolower(trim($match[1]))] = trim($match[2]); else $content = trim($arg); } if (isset($argv['type'])) $type = $argv['type']; else $type = ; if (isset($argv['name'])) $argv['name'] = $wgSimpleFormsRequestPrefix.$argv['name'];

# Textarea if ($type == 'textarea') { unset($argv['type']); foreach ($argv as $k => $v) $args .= " $k=\"$v\""; $input = "<textarea$args>$content</textarea>"; }

# Select list elseif ($type == 'select') {

                       unset($argv['type']);
                       if (isset($argv['value'])) {
                               $val = $argv['value'];
                               unset($argv['value']);
                               } else $val = ;
                       foreach ($argv as $k => $v) $args .= " $k=\"$v\"";
                       preg_match_all('/^\\*\\s*(.*?)\\s*$/m',$content,$m);
                       $input = "<select$args>\n";
                       foreach ($m[1] as $opt) {
                               $sel = $opt == $val ? ' selected' : ;
                               $input .= "<option$sel>$opt</option>\n";
                               }
                       $input .= "</select>\n";

}

# Submit button (changes to onClick) elseif ($type == 'submit' && $method == 'live') { unset($argv['type']); foreach ($argv as $k => $v) $args .= " $k=\"$v\""; }

# Default: render as normal input element else { foreach ($argv as $k => $v) $args .= " $k=\"$v\""; $input = "<input$args/>"; }

return $input; }

# Return value from the global $_REQUEST array (containing GET/POST variables) function request(&$parser,$key) { global $wgSimpleFormsRequestPrefix; return isset($_REQUEST["$wgSimpleFormsRequestPrefix$key"]) ? $_REQUEST["$wgSimpleFormsRequestPrefix$key"] : ; }

# Needed in some versions to prevent Special:Version from breaking function __toString() { return 'SimpleForms'; }

} $wgShowExceptionDetails = true;

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupSimpleForms() { global $wgHooks,$wgSimpleForms; $wgSimpleForms = new SimpleForms(); }

  1. Needed in MediaWiki >1.8.0 for magic word hooks to work properly

function wfSimpleFormsLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsRequestMagic; $magicWords[$wgSimpleFormsFormMagic] = array(0,$wgSimpleFormsFormMagic); $magicWords[$wgSimpleFormsInputMagic] = array(0,$wgSimpleFormsInputMagic); $magicWords[$wgSimpleFormsRequestMagic] = array(0,$wgSimpleFormsRequestMagic); return true; }

Template:AddJavaScript ?>