Difference between revisions of "Extension:SimpleForms.php"

From Organic Design wiki
m
(remove type from select and textarea)
Line 4: Line 4:
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Author: http://www.organicdesign.co.nz/nad
 
# - Author: http://www.organicdesign.co.nz/nad
 +
# - Started: 2007-04-25, see article history
 
# {{#Security:*|dev}}{{#Security:view|*}}{{php}}
 
# {{#Security:*|dev}}{{#Security:view|*}}{{php}}
  
$wgSimpleFormsVersion          = 'Version 0.01 (2007-04-25)';
+
$wgSimpleFormsVersion          = 'Version 0.01/2007-04-29';
  
 
$wgSimpleFormsFormMagic        = "form";    # the parser-function name for form containers
 
$wgSimpleFormsFormMagic        = "form";    # the parser-function name for form containers
 
$wgSimpleFormsInputMagic      = "input";  # the parser-function name for form inputs
 
$wgSimpleFormsInputMagic      = "input";  # the parser-function name for form inputs
 
$wgSimpleFormsRequestMagic    = "request"; # the parser-function name for accessing the post/get variables
 
$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';
 
$wgExtensionFunctions[]        = 'wfSetupSimpleForms';
 
$wgHooks['LanguageGetMagic'][] = 'wfSimpleFormsLanguageGetMagic';
 
$wgHooks['LanguageGetMagic'][] = 'wfSimpleFormsLanguageGetMagic';
 +
 +
$wgExtensionCredits['parserhook'][] = array(
 +
'name'  => "Simple Forms ($wgSimpleFormsVersion)",
 +
'author' => 'User:Nad',
 +
'url'    => 'http://www.mediawiki.org/Extension:Treeview',
 +
);
  
 
class SimpleForms {
 
class SimpleForms {
Line 50: Line 58:
  
 
function input(&$parser) {
 
function input(&$parser) {
$ot = $this->opentag;
+
global $wgSimpleFormsRequestPrefix;
$ct = $this->closetag;
+
 
 +
$ot     = $this->opentag;
 +
$ct     = $this->closetag;
 
$content = '';
 
$content = '';
 +
$method  = '';
 +
$type    = '';
 +
$args    = '';
  
 
# Process args
 
# Process args
Line 59: Line 72:
 
else $content = trim($arg);
 
else $content = trim($arg);
 
}
 
}
$type = $argv['type'];
+
if (isset($argv['type'])) $type = $argv['type']; else $type = '';
unset($argv['type']);
+
if (isset($argv['name'])) $argv['name'] = $wgSimpleFormsRequestPrefix.$argv['name'];
 
 
 
# Textarea
 
# Textarea
if ($argv['type'] == 'textarea') $input = $ot."textarea$args$ct$content$ot/textarea$ct";
+
if ($type == 'textarea') {
 +
unset($argv['type']);
 +
foreach ($argv as $k => $v) $args .= " $k=\"$v\"";
 +
$input = $ot."textarea$args$ct$content$ot/textarea$ct";
 +
}
  
 
# Select list
 
# Select list
elseif ($argv['type'] == 'select') {
+
elseif ($type == 'select') {
 +
unset($argv['type']);
 +
foreach ($argv as $k => $v) $args .= " $k=\"$v\"";
 
preg_match_all('/^\\*\\s*(.*?)\\s*$/m',$content,$options);
 
preg_match_all('/^\\*\\s*(.*?)\\s*$/m',$content,$options);
 
$options = "{$ot}option$ct".join("$ot/option$ct\n{$ot}option$ct",$options[1])."{$ot}/option$ct";
 
$options = "{$ot}option$ct".join("$ot/option$ct\n{$ot}option$ct",$options[1])."{$ot}/option$ct";
Line 73: Line 92:
  
 
# Submit button (changes to onClick)
 
# Submit button (changes to onClick)
elseif ($argv['type'] == 'submit' && $method == 'live') {
+
elseif ($type == 'submit' && $method == 'live') {
 +
unset($argv['type']);
 +
foreach ($argv as $k => $v) $args .= " $k=\"$v\"";
 
}
 
}
  
 
# Default: render as normal input element
 
# Default: render as normal input element
else $input = $ot."input$args/$ct";
+
else {
 +
foreach ($argv as $k => $v) $args .= " $k=\"$v\"";
 +
$input = $ot."input$args/$ct";
 +
}
  
 
return $input;
 
return $input;
Line 83: Line 107:
  
 
function request(&$parser,$key) {
 
function request(&$parser,$key) {
return isset($_REQUEST[$key]) ? $_REQUEST[$key] : '';
+
global $wgSimpleFormsRequestPrefix;
 +
return isset($_REQUEST["$wgSimpleFormsRequestPrefix$key"]) ? $_REQUEST["$wgSimpleFormsRequestPrefix$key"] : '';
 
}
 
}
  

Revision as of 23:57, 28 April 2007

<?

  1. MediaWiki SimpleForms Extension
  2. - See http://www.mediawiki.org/wiki/Extension:Simple_Forms for installation and usage details
  3. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  4. - Author: http://www.organicdesign.co.nz/nad
  5. - Started: 2007-04-25, see article history
  6. {{#Security:*|dev}}{{#Security:view|*}}Template:Php

$wgSimpleFormsVersion = 'Version 0.01/2007-04-29';

$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 ($wgSimpleFormsVersion)", 'author' => 'User:Nad', 'url' => 'http://www.mediawiki.org/Extension:Treeview', );

class SimpleForms {

var $id = 0; var $tagname; var $opentag; var $closetag;

# Constructor function SimpleForms() { global $wgParser,$wgHooks,$wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsRequestMagic; $wgParser->setFunctionHook($wgSimpleFormsFormMagic,array($this,'form')); $wgParser->setFunctionHook($wgSimpleFormsInputMagic,array($this,'input')); $wgParser->setFunctionHook($wgSimpleFormsRequestMagic,array($this,'request')); $this->tagname = 'this_should_be_random'; $this->opentag = 'this_should_be_random2'; $this->closetag = 'this_should_be_random3'; $wgParser->setHook($this->tagname,'tagHook'); }

function form(&$parser) { global $wgJavaScriptFunctions; $id = $this->id++; $tag = $this->tagname; $args = ; foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)=(.+)$/',$arg,$match)) $args .= " $match[1]=\"$match[2]\""; else $form = $arg; } $wgJavaScriptFunctions['SimpleFormsFunctions'] = ' function live() { } '; return "<$tag $args>$form</$tag>"; }

function input(&$parser) { global $wgSimpleFormsRequestPrefix;

$ot = $this->opentag; $ct = $this->closetag; $content = ; $method = ; $type = ; $args = ;

# Process args foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^([a-z0-9_]+?)=(.+)$/',$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 = $ot."textarea$args$ct$content$ot/textarea$ct"; }

# Select list elseif ($type == 'select') { unset($argv['type']); foreach ($argv as $k => $v) $args .= " $k=\"$v\""; preg_match_all('/^\\*\\s*(.*?)\\s*$/m',$content,$options); $options = "{$ot}option$ct".join("$ot/option$ct\n{$ot}option$ct",$options[1])."{$ot}/option$ct"; $input = $ot."select$args$ct$options$ot/select$ct"; }

# 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 = $ot."input$args/$ct"; }

return $input; }

function request(&$parser,$key) { global $wgSimpleFormsRequestPrefix; return isset($_REQUEST["$wgSimpleFormsRequestPrefix$key"]) ? $_REQUEST["$wgSimpleFormsRequestPrefix$key"] : ; }

function tagHook(&$text) { $text = str_replace($this->opentag,"<",str_replace($this->closetag,">",$text)); return "<form>$text</form>"; }

# Add the javascript to the output object if not added yet and there is at least one tree # - should add all items in $wgJavaScriptFunctions function onOutputPageBeforeHTML(&$out) { global $wgJavaScriptFunctions; foreach ($wgJavaScriptFunctions as $js) $out->addScript("\n<script type=\"text/javascript\">$js</script>\n"); return true; }

}

  1. Called from $wgExtensionFunctions array when initialising extensions

function wfSetupSimpleForms() { global $wgHooks,$wgSimpleForms,$wgJavaScriptFunctions; $wgSimpleForms = new TreeView($wgTreeViewMagic); if (!is_array($wgJavaScriptFunctions)) { $wgJavaScriptFunctions = array(); $wgHooks['OutputPageBeforeHTML'][] = $this; } }

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