Extension:StructuredInput/code

Code from Extension:StructuredInput

/extensions/structuredInput.php
<?php

set_include_path(get_include_path . PATH_SEPARATOR . $IP.'/extensions/structuredInput/special' );

$StructuredInput = new StructuredInput; $wgHooks['SpecialPage_initList'][] = array($StructuredInput, 'addToSpecialPages'); $wgHooks['EditPage::showEditForm:initial'][] = array($StructuredInput, 'editForm');

// Bootleg php namespace class StructuredInput {

public static $inputList = array;

function getInputList { return StructuredInput::$inputList; }

function addToSpecialPages(&$list) { global $wgAllMessagesEn; foreach (StructuredInput::getInputList as $val) { $key = str_replace (' ', '', strtolower($val)); if (!array_key_exists($key, $wgAllMessagesEn)) { $wgAllMessagesEn[$key] = $val; $pageName = str_replace (' ', '', $val); $list[$pageName] = array('SpecialPage', $pageName); }   }    $wgAllMessagesEn['structuredinput'] = 'StructuredInput forms'; $list['StructuredInput'] = array('SpecialPage', 'StructuredInput'); return true; }

function editForm(&$form) { if (!empty($_POST['_type'])) { $post = $_POST; require_once('structuredInput/transformations/'.$_POST['_type'].'.php'); $form->textbox1 = $output; $form->starttime = wfTimestampNow; $form->edittime = wfTimestampNow; } elseif (empty($_GET['viewsource']) && strpos($form->mArticle->mContent, '') !== False){ global $wgServerName, $wgScriptPath;

$inputType = str_replace(array('<','!','-','>'),			      array(,,,),			       $this->extractStructuredValue('StructuredInput', $form->mArticle->mContent) 			      );

header('Location: http://'.$wgServerName.$wgScriptPath.'/index.php?title=Special:'.$inputType.'&id='.$_GET['title']); die; } }

function extractStructuredValue($key, $haystack) { preg_match('/(.*)/', $haystack, $matches);

if (!empty($matches)) { return $matches[1]; } else { return ''; } }

function extractStructuredValues($haystack) { preg_match_all('/(.*)/', $haystack, $matches);

return array_combine($matches[1], $matches[2]); }

function makeRadios($key, $data, $selectedValue) { $output = '';

foreach ($data as $item) { $id = $key.str_replace(' ', '', $item['value']);

if ($item['value'] == $selectedValue) { $selected = 'checked="checked"'; } else { $selected = ''; }

$output .= <<  {$item['value']} : {$item['caption']}

TEXT;

}

return $output; }

function makeSelect($key, $data, $selectedValue) { $output = ''; foreach ($data as $value) { if ($value == $selectedValue) { $selected = ' selected="selected"'; } else { $selected = ''; }

$output .= ''.$value.' '; }   $output .= ' '; return $output; }

function getStructuredData($id) { $pageContent = Revision::newFromTitle( Title::newFromText($id) )->getText;

$data = StructuredInput::extractStructuredValues($pageContent); $data['_title'] = $id;

return $data; }

}

?>

/extensions/structuredInput/special/SpecialStructuredInput.php
'.$input.''; }

if (!empty($structuredInputs)) $structuredInputs = ''.$structuredInputs.'';

$wgOut->addHTML($structuredInputs);

}

?>

StructuredInput form


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

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

$html = <<   Activity name: 

Goal: {$data['goal']}

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

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

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

TEMPLATE;

$wgOut->addHTML($html); }

?>

Transformation code


$output = <<<EOT

Goal
{$post['goal']}

What to do
{$post['instructions']}

EOT;

?>