Extension:StructuredInput

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
StructuredInput

Release status: beta

Implementation Special page
Description Creates a framework to build forms within Mediawiki that get converted into wiki text.
Author(s) Carlo Cabanilla
Last Version 1.0
MediaWiki 1.7.1
License No license specified
Download /code

Contents

[edit] What can this extension do?

The goal of the StructuredInput extension is to provide a framework for taking form input and transforming it into a wiki page, which can be edited either with the form or in raw wiki text.

[edit] Installation

Copy the core StructuredInput files into your Mediawiki installation, creating directories as necessary:

  • extensions/structuredInput.php
  • extensions/structuredInput/
  • extensions/structuredInput/special/
  • extensions/structuredInput/special/SpecialStructuredInput.php
  • extensions/structuredInput/transformations/

Then add the following line to your LocalSettings.php:

require_once("extensions/structuredInput.php");

[edit] Creating a new StructuredInput

[edit] StructuredInput form

In LocalSettings.php, after the require_once line to include structuredInput.php, add the name of your form to the StructuredInput::$inputList array. For example, this is what your code should like if your form were called "Add activity":

require_once("extensions/structuredInput.php");
StructuredInput::$inputList[] = 'Add activity';

To create the form page for "Add activity", create the file /extensions/structuredInput/special/SpecialAddactivity.php:

function wfSpecialAddactivity() {
    global $wgOut, $wgScriptPath;
    $action = $wgScriptPath.'/index.php?action=submit';

    if (!empty($_GET['id'])) {
      $data = StructuredInput::getStructuredData($_GET['id']);
    } else {
      $data = array();
    }

    $html = <<<TEMPLATE

<h2>Add activity</h2>

<script>
    function setAction(formEl) {
      if (formEl['_title'].value) {
	formEl.action += '&title=' + formEl['_title'].value;
	return true;
      } else {
	return false;
      }
    }
</script>

<form method="post" action="$action" onsubmit="return setAction(this)">
    <input type="hidden" name="_type" value="addactivity" />
    <input type="hidden" name="wpPreview" value="Show preview" />
    
    <label for="_title">Activity name:</label>
    <input id="_title" name="_title" value="{$data['_title']}" />

    <br /><br />

    <label for="goal">Goal:</label>
    <textarea id="goal" name="goal">{$data['goal']}</textarea>

    <br /><br />

    <label for="items">Content Covered:</label>
    <input id="items" name="items" value="{$data['items']}" />

    <br /><br />

    <label for="steps">Steps to Follow:</label>
    <textarea id="steps" name="instructions">{$data['instructions']}</textarea>

    <br /><br />

    <input type="submit" value="Save">

</form>

TEMPLATE;

    $wgOut->addHTML($html);
}

By creating this file, you will enable a new special page, http://url/to/wiki/index.php/Special:Addactivity

Some important things to note:

  • The form input _title determines what the page you're creating will be called. The onsubmit Javascript takes the input from the text file and incorporates it into the form action.
  • The hidden input _type specifies what transformation to use. In this example /extensions/structuredInput/transformations/addactivity.php will be used.

[edit] Transformation code

A transformation file gets included when processing the StructuredInput request. There are two variables that you should be concerned about:

  • $post - A copy of the data from $_POST. Feel free to modify as needed, as it doesn't affect anything else
  • $output - You should set this variable to what you want to appear in the original wiki edit form. In order to mark this page as a StructuredInput page, you must include the following as part of $output:
<!--|StructuredInput|--><!--Nameofstructuredinput--><!--|StructuredInput|-->

So for our example it would be:

<!--|StructuredInput|--><!--Addactivity--><!--|StructuredInput|-->

This tag won't appear on the wiki page itself. It just tells mediawiki to edit this page using the Addactivity StructuredInput form.

Additionally, to properly fill in the StructuredInput form, you must enclose the form variables in tags of the following form:

<!--|key|-->value<!--|key|-->

Here is what /extensions/structuredInput/transformations/addactivity.php might look like:

<?php

$output = <<<EOT

==Goal==
<!--|goal|-->{$post['goal']}<!--|goal|-->

==What to do==
<!--|instructions|-->{$post['instructions']}<!--|instructions|-->

<!--|StructuredInput|--><!--Addactivity--><!--|StructuredInput|-->

EOT;

?>

[edit] Code

Extension:StructuredInput/code

Warning: Default sort key "StructuredInput" overrides earlier default sort key "STRUCTUREDINPUT".

Personal tools