Difference between revisions of "Extension:SimpleForms.php"
m |
(remove JS (making separate now), add edit-form pf) |
||
| Line 11: | Line 11: | ||
$wgSimpleFormsFormMagic = "form"; # the parser-function name for form containers | $wgSimpleFormsFormMagic = "form"; # the parser-function name for form containers | ||
| + | $wgSimpleFormsEditFormMagic = "edit"; # the parser-function name for form which are for editing/creating articles | ||
$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 | ||
| Line 70: | Line 71: | ||
var $id = 0; | var $id = 0; | ||
| + | var $edit = false; | ||
# Constructor | # Constructor | ||
function SimpleForms() { | function SimpleForms() { | ||
| − | global $wgParser,$wgHooks,$wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsRequestMagic; | + | global $wgParser,$wgHooks,$wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsRequestMagic,$wgSimpleFormsEditFormMagic; |
| − | $wgParser->setFunctionHook($wgSimpleFormsFormMagic,array($this,' | + | $wgParser->setFunctionHook($wgSimpleFormsFormMagic, array($this,'normalForm')); |
| − | $wgParser->setFunctionHook($wgSimpleFormsInputMagic,array($this,'input')); | + | $wgParser->setFunctionHook($$wgSimpleFormsEditFormMagic, array($this,'editForm')); |
| − | $wgParser->setFunctionHook($wgSimpleFormsRequestMagic,array($this,'request')); | + | $wgParser->setFunctionHook($wgSimpleFormsInputMagic, array($this,'input')); |
| + | $wgParser->setFunctionHook($wgSimpleFormsRequestMagic, array($this,'request')); | ||
$wgHooks['SkinTemplateTabs'][] = $this; | $wgHooks['SkinTemplateTabs'][] = $this; | ||
| + | } | ||
| + | |||
| + | function normalForm(&$parser) { | ||
| + | $argv = func_get_args(); | ||
| + | return $this->form($argv); | ||
| + | } | ||
| + | |||
| + | function editForm(&$parser) { | ||
| + | $argv = func_get_args(); | ||
| + | return $this->form($argv,true); | ||
} | } | ||
# Renders a form and wraps it in tags for processing by tagHook | # Renders a form and wraps it in tags for processing by tagHook | ||
| − | function form(&$ | + | # - if it's an edit-form it will return empty-string unless $this->edit is true |
| − | + | # i.e. $this->edit would be set by the edit-hook or create-specialpage parsing it | |
| − | $id = $this->id++; | + | function form(&$argv,$editform = false) { |
| + | if ($editform && !$this->edit) return ''; | ||
| + | $id = $this->id++; | ||
| + | $form = ''; | ||
$args = ''; | $args = ''; | ||
| − | foreach ( | + | foreach ($argv as $arg) if (!is_object($arg)) { |
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $args .= " $match[1]=\"$match[2]\""; else $form = $arg; | if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $args .= " $match[1]=\"$match[2]\""; else $form = $arg; | ||
} | } | ||
| − | + | return array("<form {$args}id="$id">$form</form>",'noparse'=>true); | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | return array("<form $args>$form</form>",'noparse'=>true); | ||
} | } | ||
| Line 237: | Line 232: | ||
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly | # Needed in MediaWiki >1.8.0 for magic word hooks to work properly | ||
function wfSimpleFormsLanguageGetMagic(&$magicWords,$langCode = 0) { | function wfSimpleFormsLanguageGetMagic(&$magicWords,$langCode = 0) { | ||
| − | global $wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsRequestMagic; | + | global $wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsEditFormMagic,$wgSimpleFormsRequestMagic; |
| − | $magicWords[$wgSimpleFormsFormMagic] | + | $magicWords[$wgSimpleFormsFormMagic] = array(0,$wgSimpleFormsFormMagic); |
| − | $magicWords[$wgSimpleFormsInputMagic] | + | $magicWords[$wgSimpleFormsEditFormMagic] = array(0,$wgSimpleFormsEditFormMagic); |
| − | $magicWords[$wgSimpleFormsRequestMagic] = array(0,$wgSimpleFormsRequestMagic); | + | $magicWords[$wgSimpleFormsInputMagic] = array(0,$wgSimpleFormsInputMagic); |
| + | $magicWords[$wgSimpleFormsRequestMagic] = array(0,$wgSimpleFormsRequestMagic); | ||
return true; | return true; | ||
} | } | ||
Revision as of 07:59, 2 June 2007
<?php
- MediaWiki SimpleForms ExtensionTemplate:Php
- - See http://www.mediawiki.org/wiki/Extension:Simple_Forms for installation and usage details
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: http://www.organicdesign.co.nz/nad
- - Started: 2007-04-25, see article history
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('SIMPLEFORMS_VERSION','0.0.5, 2007-06-01');
$wgSimpleFormsFormMagic = "form"; # the parser-function name for form containers $wgSimpleFormsEditFormMagic = "edit"; # the parser-function name for form which are for editing/creating articles $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 $wgSimpleFormsCreateAction = true; # whether or not to add an action button for article-creation
$wgExtensionFunctions[] = 'wfSetupSimpleForms'; $wgHooks['LanguageGetMagic'][] = 'wfSimpleFormsLanguageGetMagic';
$wgExtensionCredits['parserhook'][] = array( 'name' => 'Simple Forms', 'author' => 'User:Nad', 'description' => 'Functions to make and process forms.', 'url' => 'http://www.mediawiki.org/wiki/Extension:Simple_Forms', 'version' => SIMPLEFORMS_VERSION );
- Process content item if in request data
if (isset($_REQUEST['sf_content'])) { $content = $_REQUEST['sf_content']; if (isset($_REQUEST['title'])) { $t = Title::newFromText($_REQUEST['title']); if ($t->exists()) { # Title exists, append content $a = new Article($t); $c = $a->getContent(); $a->updateArticle("$c\n$content","Updated",false,false); } else { # No such title, create from content $a = new Article($t); $a->insertNewArticle( 'Dummy article used by Extension:SimpleForms', 'Dummy article created for Extension:SimpleForms', true, false ); } } else { # No title specified, render content with no title $t = Title::newFromText($_REQUEST['title'] = 'UNTITLED'); if (!$t->exists()) { # The UNTITLED article doesn't exist, create it now $a = new Article($t); $a->insertNewArticle( 'Dummy article used by Extension:SimpleForms', 'Dummy article created for Extension:SimpleForms', true, false ); } $wgHooks['ArticleAfterFetchContent'][] = 'wfSimpleFormsAAFC'; # Makes it seem as if the article contains the content from the request } }
- Define a singleton for SimpleForms operations
class SimpleForms {
var $id = 0; var $edit = false;
# Constructor function SimpleForms() { global $wgParser,$wgHooks,$wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsRequestMagic,$wgSimpleFormsEditFormMagic; $wgParser->setFunctionHook($wgSimpleFormsFormMagic, array($this,'normalForm')); $wgParser->setFunctionHook($$wgSimpleFormsEditFormMagic, array($this,'editForm')); $wgParser->setFunctionHook($wgSimpleFormsInputMagic, array($this,'input')); $wgParser->setFunctionHook($wgSimpleFormsRequestMagic, array($this,'request')); $wgHooks['SkinTemplateTabs'][] = $this; }
function normalForm(&$parser) { $argv = func_get_args(); return $this->form($argv); }
function editForm(&$parser) { $argv = func_get_args(); return $this->form($argv,true); }
# Renders a form and wraps it in tags for processing by tagHook # - if it's an edit-form it will return empty-string unless $this->edit is true # i.e. $this->edit would be set by the edit-hook or create-specialpage parsing it function form(&$argv,$editform = false) { if ($editform && !$this->edit) return ; $id = $this->id++; $form = ; $args = ; foreach ($argv as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $args .= " $match[1]=\"$match[2]\""; else $form = $arg; } return array("<form {$args}id="$id">$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 array($input,'noparse'=>true); }
# Return value from the global $_REQUEST array (containing GET/POST variables) function request(&$parser,$key) { global $wgRequest,$wgSimpleFormsRequestPrefix; return $wgRequest->getText($wgSimpleFormsRequestPrefix.$key); }
# Add a "create" tab for creating new articles from creation forms function onSkinTemplateTabs(&$skin,&$actions) { global $wgRequest; $action = $wgRequest->getText('action'); $tmp = $actions; $actions = array(); foreach ($tmp as $k => $v) { if (strtolower($k) == 'delete') { $actions['sf_create'] = array( 'class' => ($action == 'sf_create') ? 'selected' : false, 'text' => 'create', 'href' => Title::makeTitle(NS_SPECIAL,'CreateArticle') ); } $actions[$k] = $v; } return true; }
# Needed in some versions to prevent Special:Version from breaking function __toString() { return 'SimpleForms'; }
}
- Define a new class based on the SpecialPage class
- if title is special require_once "$IP/includes/SpecialPage.php";
class SpecialCreateArticle extends SpecialPage {
# Constructor function SpecialCreateArticle() { SpecialPage::SpecialPage('CreateArticle'); }
# Override SpecialPage::execute() execute($param) { global $wgOut; $title = Title::makeTitle(NS_SPECIAL,'CreateArticle'); }
}
- Called from $wgExtensionFunctions array when initialising extensions
function wfSetupSimpleForms() { global $wgHooks,$wgSimpleForms;
# Add messages if ($wgLanguageCode == 'en') { $wgMessageCache->addMessages(array( 'CreateArticle' => 'CreateArticle', # The friendly page title 'sf_create' => "create", # title of create action button )); }
# Add the specialpage to the environment SpecialPage::addPage(new SpecialCreateArticle());
# Instantiate a singleton for the extension $wgSimpleForms = new SimpleForms(); }
- Needed in MediaWiki >1.8.0 for magic word hooks to work properly
function wfSimpleFormsLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgSimpleFormsFormMagic,$wgSimpleFormsInputMagic,$wgSimpleFormsEditFormMagic,$wgSimpleFormsRequestMagic; $magicWords[$wgSimpleFormsFormMagic] = array(0,$wgSimpleFormsFormMagic); $magicWords[$wgSimpleFormsEditFormMagic] = array(0,$wgSimpleFormsEditFormMagic); $magicWords[$wgSimpleFormsInputMagic] = array(0,$wgSimpleFormsInputMagic); $magicWords[$wgSimpleFormsRequestMagic] = array(0,$wgSimpleFormsRequestMagic); return true; }
- Assigns the content of the UNTITLED article with
function wfSimpleFormsAAFC(&$article,&$text) { global $wgRequest; $text = $wgRequest->getText('sf_content'); return true; } ?>



