Open main menu

Extension:CreateTpl

MediaWiki extensions manual
OOjs UI icon advanced.svg
CreateTpl
Release status: beta
Implementation Tag
Description Creates articles with template
Author(s) RUNA (Runa_cgtalk)
Latest version 0.1 (March 2010)
MediaWiki tested on 1.10.2, 1.15.1
License GPL
Download See the section labeled code below
Translate the CreateTpl extension if it is available at translatewiki.net
Check usage and version matrix.

Contents

Creating articles with templateEdit

InstallationEdit

To install this extension

  • Create file in your /extension directory: CreateTpl.php (see #Code section)
  • Add the following to LocalSettings.php :
require_once("$IP/extensions/CreateTpl.php");

SyntaxEdit

<CreateTpl>{{Template_name}}</CreateTpl>

OverviewEdit

Displays a form that creates a page with a specified template. There is a check for the existence of the article. If an article exists, then a choice: either to continue (the contents of articles will be replaced by a template), or go back.

If the template parameter types are specified, then the entry form under the appropriate entry field in parentheses indicate its type. In the presence of input values for these parameters will also be made test for compliance specified in the template type. When discrepancies such as the transition to the creation of articles will be stopped, and will display a diagnostic message.

The parameter types are described in the text of the template as follows:

<noinclude><paramtype> 
Param_name1: type 
Param_name2: type 
</paramtype></noinclude>

Possible types: number - the number date - date in format dd.mm.yyyy

I appreciated this library, but had some trouble getting it working just right.  The changes are posted below the original code. ~Juan

I just wanted to preload a template into a page and this extension got me started. I cut out a lot of it using the following after POST: header("Location:http://lc.".$wgSitename.".com/wiki/".$wgSitename.":".$wgRequest->getVal("articletitle")."?action=edit&preload=Template:".$tpl_name); If the file exists then there is no overwrite, it just pulls up existing page. (Note, I'm not handling any parameters.) ~Lynn

CodeEdit

<?php
// MediaWiki CreateTpl Extension Ver 0.1
// set up MediaWiki to react to the "<CreateTpl>" tag
$wgExtensionFunctions[] = "wfCreateTpl";
function wfCreateTpl() {
	global $wgParser;
	$wgParser->setHook( "CreateTpl", "RenderCreateTpl" );
	$wgParser->setHook( "paramtype", "NoRender" );
	return true;
}

$wgHooks['EditPage::showEditForm:initial'][] = 'preloadCreateTemplate_initial';
$wgHooks['EditPage::showEditForm:fields'][] = 'preloadCreateTemplate';

function NoRender ( $input, $argv, &$parser ) {
	#It's just blind to not display a block template <paramtype>
}

function RenderCreateTpl( $input, $argv, $parser ) {
	$parser->disableCache();
	global $wgScriptPath, $wgRequest, $wgHooks;;
	preg_match("/^{{(.*)}}/i", $input, $matches);
	$tpl_name = $matches[1];
	$output = "";
	if ($wgRequest->wasPosted()
		&& ($wgRequest->getVal("create") == "createtpl") 
		&& ($wgRequest->getVal("tplname") == urlencode($tpl_name))) {
		if (strlen($wgRequest->getVal("articletitle"))) {
			$output .= '
			<form id="'.urlencode($tpl_name).'" action="'.$wgScriptPath.'/index.php?title='.ucfirst($wgRequest->getVal("articletitle")).'&action=edit" method="POST">';
			foreach ($_POST as $key=>$value) {
				$output .= '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
			}
			$nt = Title::newFromText( $wgRequest->getVal("articletitle") );
			if ($nt->exists()) {
				$output .= "<div id=\"CreateTpl_".urlencode($tpl_name)."\"/><b><font color=\"red\">Article </font><a href=\"".$nt->getFullURL()."\">".ucfirst($wgRequest->getVal("articletitle"))."</a><font color=\"red\"> already exists.</font></b><BR/><BR/>
				Continuation will lead to the replacement text of the article.<BR/><BR/>
				<input type=\"button\" value=\"Назад\" onclick=\"history.back()\"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Continue\"/>
				";
				$output .= '</form>';
				$wgHooks['ParserAfterTidy'][] = array('fnCreateTplPositioning', urlencode($tpl_name));
				return $output;
			}
			else {
				$output .= '</form>';
				$continue = true;
				foreach ($_POST as $key=>$value) {
					if (strpos($key, "type_")===0) {
						$param = urldecode(substr($key, 5));
						switch ($value) {
							case "number":
								if ((strlen($_POST["form_$param"])) && (!is_numeric($_POST["form_$param"]))) {
									$continue = false;
									$output .= "<b><font color=\"red\">Param </font>$param<font color=\"red\"> must be a number.</font></b><BR/><BR/>";
								}
								break;
							case "date":
								if ((strlen($_POST["form_$param"])) && (!is_date($_POST["form_$param"]))) {
									$continue = false;
									$output .= "<b><font color=\"red\">Param </font>$param<font color=\"red\"> must be a date in format</font> dd.mm.yyyy</b><BR/><BR/>";
								}
								break;
						}
					}
				}
				if ($continue) {
					$output .= '<script type="text/javascript">document.getElementById("'.urlencode($tpl_name).'").submit()</script>';
					return $output;
				}
				else {
					$wgHooks['ParserAfterTidy'][] = array('fnCreateTplPositioning', urlencode($tpl_name));
				}
			}
		}
		else {
			$output .= "<b><font color=\"red\">Specify the name of article!</font></b><BR/><BR/>";
			$wgHooks['ParserAfterTidy'][] = array('fnCreateTplPositioning', urlencode($tpl_name));
		}
	}
	$dbr =& wfGetDB( DB_SLAVE );
	$res = $dbr->query( "
		select distinct t.`old_text` as page_text
		from `page` p
		inner join `revision` r on p.`page_latest`=r.`rev_id`
		inner join `text` t on r.`rev_text_id`=t.`old_id`
		where p.`page_title` = '".$tpl_name."'
		and p.`page_namespace` = 10
		limit 1
	" );
	$row = $dbr->fetchObject( $res );
	$tpl_text = $row->page_text;
	$dbr->freeResult( $res );
	
	preg_match_all("/(?<=\{{3})[^\}]*[^\}]*/", $tpl_text, $matches);
	
	$params_array = array();
	foreach ($matches[0] as $params) {
		 $expl_params = explode("|", $params, 2);
		 $params_array[] = $expl_params[0];
	}
	$params_array = array_unique($params_array);
	
	preg_match("/(?<=<paramtype>)[^<>]+(?=<\/paramtype>)/", $tpl_text, $matches);
	#print_r($matches);
	$param_types_array = explode( chr(10), $matches[0]);
	#print_r($param_types_array);
	$param_types = array();
	foreach ($param_types_array as $param_value) {
		if (strlen($param_value)) {
			$param_name_type = explode( ":", $param_value, 2 );
			$key = array_search(trim($param_name_type[0]), $params_array);
			if (($key === 0) || ($key > 0)) {
				$param_types[trim($param_name_type[0])] = trim($param_name_type[1]);
			}
		}
	}
	#print_r($param_types);
	
	
	$form_inputs = "";
	if (count($params_array)) {
		$form_inputs .= "<table class=\"standard\" style=\"width: auto\">
		<tr><td>Name or number</td><td>Value</td></tr>
		";
		foreach ($params_array as $param_name) {
			$form_inputs .= "<tr><td align=right>$param_name</td>";
			$form_inputs .= "<td><input type=text name=\"form_".urlencode($param_name)."\" style=\"width:200px\"";
			if (strlen($wgRequest->getVal("form_".urlencode($param_name)))) {
				$form_inputs .= " value=\"".$wgRequest->getVal("form_".urlencode($param_name))."\"";
			}
			$form_inputs .= "/>";
			if (array_key_exists($param_name, $param_types)) {
				$type_text = "";
				switch ($param_types[$param_name]) {
					case "number":
						$type_text = "number";
						break;
					case "date":
						$type_text = "date in format dd.mm.yyyy";
						break;
				}
				$form_inputs .= "<font style=\"font-size: 8pt\"><BR/>($type_text)</font>";
				$form_inputs .= "<input type=\"hidden\" name=\"type_".urlencode($param_name)."\" value=\"".$param_types[$param_name]."\"/>";
			}
			$form_inputs .= "</td></tr>";
		}
		$form_inputs .= "<tr><td colspan=2><center><input type=submit value=\"Create\"/></center></td></tr>
		</table>
		";
	}
	$tt = Title::makeTitle( 10, $tpl_name );
	$output .= 'Create a page with a template : <B><a href="'.$tt->getFullURL().'">'.$tpl_name.'</a></B><BR/>
<form action="" method="POST">
<input type="hidden" name="create" value="createtpl"/>
<input type="hidden" name="tplname" value="'.urlencode($tpl_name).'"/>
Article title: <input type="text" name="articletitle" value="'.$wgRequest->getVal("articletitle").'" style="width: 200px"/>
<br/>Params:
'.$form_inputs.'
</form>
';
	$tpl_name_enc = urlencode($tpl_name);
	$output = "<DIV id=\"CreateTpl_$tpl_name_enc\" style=\"padding: 10px; border: 1px solid black;\">".str_replace("\n", "", $output)."</DIV>";
	return $output;
}

function preloadCreateTemplate(&$editpage, &$output) {
	global $wgRequest;
	if ( ($wgRequest->getVal("action") == "edit")
	&& ($wgRequest->getVal("create") == "createtpl") ) {
	$tplname = urldecode($wgRequest->getVal("tplname"));
	$post_array = $wgRequest->getValues();
	
	$editpage->textbox1 = '{{'.$tplname;
	foreach($post_array as $key=>$value) {
		if ((strpos($key, "form_") === 0) && strlen(strval($value))) {
			$editpage->textbox1 .= '|'.urldecode(substr($key, 5)).'='.$value;
		}
	}
	$editpage->textbox1 .= '}}';
	}
	return true;
}

function preloadCreateTemplate_initial(&$editpage) {
	global $wgRequest;
	if ( ($wgRequest->getVal("action") == "edit")
	&& ($wgRequest->getVal("create") == "createtpl") ) {
		$editpage->formtype = "initial";
	}
	return true;
}

function fnCreateTplPositioning ( $tpl_name_enc, &$parser, &$text) {
	$label = "CreateTpl_$tpl_name_enc";
	$text .= "<script type=\"text/javascript\">document.getElementById(\"$label\").scrollIntoView(true);</script>";	
	return true;
}


function is_date($i_sDate)
{
  $blnValid = TRUE;
   // check the format first (may not be necessary as we use checkdate() below)
   if(!ereg ("^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$", $i_sDate))
   {
    $blnValid = FALSE;
   }
   else //format is okay, check that days, months, years are okay
   {
      $arrDate = explode(".", $i_sDate); // break up date by slash
      $intDay = $arrDate[0];
      $intMonth = $arrDate[1];
      $intYear = $arrDate[2];
 
      $intIsDate = checkdate($intMonth, $intDay, $intYear);
   
     if(!$intIsDate)
     {
        $blnValid = FALSE;
     }
 
   }//end else
   
   return ($blnValid);
}
?>

Some Possible ChangesEdit

  • Went through and commented the code.
  • Changed the parameter parsing so that it would use spaces: <paramtype> Param_name1:type Param_name2:type </paramtype>.
  • Also, I've gone ahead and made it so the input type could be changed. Now for example, it recognizes a textarea type. Look in the switch statement below and add whatever you might need in your form.
  • Fixed the tooltips and changed the form to use CSS instead of Tables.
  • Changed the version to .2
  • The following are valid paramtypes:
    • number
    • category
    • namespace
    • select
    • date
    • textarea

In particular the select tag functions in the following way:

<paramtype>variablename:select-option1-option2-some_other_option</paramtype>

Note that your options can't have spaces at this point... (This would mean switching back to using newlines so that the parser doesn't break... when I get around to it.)

date and number have typechecking on them.

~Juan
<?php
// MediaWiki CreateTpl Extension Ver 0.2
// set up MediaWiki to react to the "<CreateTpl>" tag
$wgExtensionFunctions[] = "wfCreateTpl";
function wfCreateTpl() {
	global $wgParser;
        # When MediaWiki sees <createTpl> or <paramtype> it runs these functions: 
        # RenderCreateTpl($text, $params, $parser), and NoRender($text, $params, $parser) respectively
	$wgParser->setHook( "CreateTpl", "RenderCreateTpl" );
	$wgParser->setHook( "paramtype", "NoRender" );
	return true;
}
 
#Register an event called 'EditPage::...'; so that it runs the function 'preload...'
#In particular, these events allow you to take the POST data from forms and create the Template wiki code in the edit window.
$wgHooks['EditPage::showEditForm:initial'][] = 'preloadCreateTemplate_initial';
$wgHooks['EditPage::showEditForm:fields'][] = 'preloadCreateTemplate';
 
# $input is the variable you change in order to display something different... don't change it for a hidden tag.
# $argv is relevant arguments
# $parser is used for contextual elements
function NoRender ( $input, $argv, $parser ) {
	#It's just blind to not display a block template <paramtype>
}

#########################
# RenderCreateTpl: When MediaWiki sees a <createTpl> tag, it replaces that tag with whatever this function returns.
#
# $input is the variable you change in order to display something different
# $argv is relevant arguments
# $parser is used for contextual elements

function RenderCreateTpl( $input, $argv, $parser ) {
        #Set a flag noting that the output object is dynamic and should not be cached
	$parser->disableCache();
	global $wgScriptPath, $wgRequest, $wgHooks;
        #Within the createTpl tag look for anything surrounded with double curly brackets that starts at the beginning of the line and is case insensitive
        #put the full text into $matches[0], put the string that was found in $matches[1].  This should be the name of a template.
	preg_match("/^{{(.*)}}/i", $input, $matches);
	$tpl_name = $matches[1];
        #This function will return $output which should be our form html.
	$output = "";
        #If this request was the result of a POST operation Do the following: ???why?rename?
	if ($wgRequest->wasPosted()
                # And it was POST-ed as both a create=createtpl and with the tplname= the template name found in the {{*}} regular expression
		&& ($wgRequest->getVal("create") == "createtpl") 
		&& ($wgRequest->getVal("tplname") == urlencode($tpl_name))) {
                # If there exists an articleTitle where the stringlength is greater than 0 
		if (strlen($wgRequest->getVal("articletitle"))) {


			#    TODO     TODO   ->  if article title is bad, complain and don't runt the following. Add namespace check? hook?


                        #start writing the form HTML that this function will eventual spit out.
                        # the form id is the template's name, the action is the URL of our site's index with
                        # the title being the article's title (with first char capitalized), action being edit, and method being POST.
			$output .= '
			<form id="'.urlencode($tpl_name).'" action="'.$wgScriptPath.'/index.php?title='.ucfirst($wgRequest->getVal("articletitle")).'&action=edit" method="POST">';
			foreach ($_POST as $key=>$value) {
                                #For each POST-ed argument, create the proper input field
				$output .= '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
			}
                        # Create a new MediaWiki Title object from the POST-ed "articletitle"
			$nt = Title::newFromText( $wgRequest->getVal("articletitle") );
                        # If the article title already exists, then output a warning. And give people the option of continuing or going back.
			if ($nt->exists()) {
				$output .= "<div id=\"CreateTpl_".urlencode($tpl_name)."\"/><b><font color=\"red\">Article </font><a href=\"".$nt->getFullURL()."\">".ucfirst($wgRequest->getVal("articletitle"))."</a><font color=\"red\"> already exists.</font></b><BR/><BR/>
				Continuation will lead to the replacement text of the article.<BR/><BR/>
				<input type=\"button\" value=\"Cancel\" onclick=\"history.back()\"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Continue\"/>
				";
				$output .= '</form>'; #finish the form
                                # ???Create a new hook now? - calls function fnCreate... with argument urlencode...
                                # the ParserAfterTidy Event lets you touch up the final page code.
                                # would call: fnCreateTplPositioning ( $tpl_name_enc, &$parser, &$text)
                                # where $tpl_name_enc is the urlencode($tpl_name), $parser lets you access contextual elements, $text is the text of the page
				$wgHooks['ParserAfterTidy'][] = array('fnCreateTplPositioning', urlencode($tpl_name));
                                #All done!
				return $output;
			}
                        # If the article title does not exist, then continue saving.
			else {  #Do error checking on various fields TODO
				$output .= '</form>';
                                #$output .= $value; # debug
				$continue = true;
				foreach ($_POST as $key=>$value) {
					if (strpos($key, "type_")===0) {
						$param = urldecode(substr($key, 5));
						switch ($value) {
							case "number": #if it is not a number, stop!
								if ((strlen($_POST["form_$param"])) && (!is_numeric($_POST["form_$param"]))) {
									$continue = false;
									$output .= "<b><font color=\"red\">Param </font>$param<font color=\"red\"> must be a number.</font></b><BR/><BR/>";
								}
								break;
							case "date": #if it is not a date, stop!
								if ((strlen($_POST["form_$param"])) && (!is_date($_POST["form_$param"]))) {
									$continue = false;
									$output .= "<b><font color=\"red\">Param </font>$param<font color=\"red\"> must be a date in format</font> dd.mm.yyyy</b><BR/><BR/>";
								}
								break;
						}
					}
				}
				if ($continue) { #everything is ok, finish submission using JavaScript
					$output .= '<script type="text/javascript">document.getElementById("'.urlencode($tpl_name).'").submit()</script>';
					return $output;
				}
				else {# error checking on fields was not ok, register the hook the displays the form, don't return.
					$wgHooks['ParserAfterTidy'][] = array('fnCreateTplPositioning', urlencode($tpl_name));
				}
			} #finish error checking
		} #finish if it was POST-ed
		else { # if no article title was POST-ed, then append this error: (and register the hook that displays the form)
			$output .= "<b><font color=\"red\">Specify the name of article!</font></b><BR/><BR/>";
			$wgHooks['ParserAfterTidy'][] = array('fnCreateTplPositioning', urlencode($tpl_name));
		}
	} 
 
        #If there was an error with the POST data, or if there was no POST, Then do the following to create a form:
        #Get the template object
	$dbr =& wfGetDB( DB_SLAVE );
	$res = $dbr->query( "
		select distinct t.`old_text` as page_text
		from `page` p
		inner join `revision` r on p.`page_latest`=r.`rev_id`
		inner join `text` t on r.`rev_text_id`=t.`old_id`
		where p.`page_title` = '".$tpl_name."'
		and p.`page_namespace` = 10
		limit 1
	" );
	$row = $dbr->fetchObject( $res );
	$tpl_text = $row->page_text;#Pull out the template text
	$dbr->freeResult( $res ); # release the DB info
 
	preg_match_all("/(?<=\{{3})[^\}]*[^\}]*/", $tpl_text, $matches); #Find all of the variables in the template
 
	$params_array = array();
	foreach ($matches[0] as $params) {#for each match,
		 $expl_params = explode("|", $params, 2);#split at "|" left should be var name, right should be default value
		 $params_array[] = $expl_params[0];#params gets all the variable names from the template
	}
	$params_array = array_unique($params_array); #get rid of all duplicate values in the array
 
	preg_match("/(?<=<paramtype>)[^<>]+(?=<\/paramtype>)/", $tpl_text, $matches);#find user defined types from the template
	#print_r($matches);
	$param_types_array = explode( chr(32), $matches[0]); #changed this to space instead of newline TODO, use space AND newline
	#print_r($param_types_array); #-> Array([0]=>"var1:type1" [1]=>"var2:type2")
	$param_types = array();
	foreach ($param_types_array as $param_value) { #foreach key/value pair, get value
		if (strlen($param_value)) { #If it is actually there...
			$param_name_type = explode( ":", $param_value, 2 );#Split the string at Colon, then get the key, 0,1,2,3 etc.
			$key = array_search(trim($param_name_type[0]), $params_array);#get the key of the template value that matches the <param_type> key
			if (($key === 0) || ($key > 0)) { #If it is a valid key
				$param_types[trim($param_name_type[0])] = trim($param_name_type[1]);#Add to the param_types array for parsing later.
			}
		}
	}
	#print_r($param_types); Array([var1]=>"type1" [var2]=>"type2")
	
        #make a table from all the parameters
	$form_inputs = "";
        $button_style = "background:#C00000;padding:4px 6px 0px 0px;border=0;float:right;margin:0px 20px 20px 0px;font-size:1.2em;border:1px double #660000;border-left:1px double #FF0033;border-right:1px double #FF0033;color:#ffffff;";
	if (count($params_array)) {#Do we really want a table? Should this use CSS tables? Yes TODO pull out style into variables
		$form_inputs .= "<br /><br /><hr /><br /><div style='margin:0 auto;width:600px;background:#232323;border:1px solid #242424;'>";
                $form_inputs .= "<h3 style='color:#ffffff;font-size:1.5em;padding:10px 10px 10px 10px;margin:0 0 0 0;border-bottom:3px solid #444444;border-top:1px solid #181818;'>Form Fields:</h3>";
		foreach ($params_array as $param_name) {
			$field_width = "550px";
                        $input_style = "width:$field_width;color:#ffffff;padding:5px 5px 5px 5px;margin:5px 5px 5px 20px;background:#333333;border-top:1px solid #181818;border-bottom:1px solid #181818;border-left:1px solid #444444;border-right:1px solid #444444;";
			if (array_key_exists($param_name, $param_types)) {
				$type_text = "";
                                
                                
				$form_inputs .= "<label style='background:#1e1e1e;width:100%;display:block;border-top:1px solid #262626;border-botton:1px solid #171717;padding:10px 0 10px 0;'><span style='color:#eeeeee;font-size:1.2em;padding:0px 0px 0px 20px;text-align:right;'>$param_name:</span><br />";

				switch ($param_types[$param_name]) {
					case "number": #This is the paramtype
						$type_text = "number";
						$form_inputs .= "<input type=text name=\"form_".urlencode($param_name)."\" style='$input_style'";
						#If there is an error on POST, the form gets reloaded.  
						#This if tries to reinsert the posted data so that you don't have to fill it in again.
                                                if (strlen($wgRequest->getVal("form_".urlencode($param_name)))) { 
							$form_inputs .= " value=\"".$wgRequest->getVal("form_".urlencode($param_name))."\"";
						}
						$form_inputs .= "/>";

                                                $form_inputs .= "<br /><span style='font-size:.85em;color:#aaaaaa;padding:4px 10px 10px 20px;text-align:right;'>($type_text)<BR/></span>";
				                $form_inputs .= "<input type=\"hidden\" name=\"type_".urlencode($param_name)."\" value=\"".$param_types[$param_name]."\"/>";
						break;
					case "category":#This is the paramtype
						$type_text = "categories";
						$form_inputs .= "<select name=\"form_".urlencode($param_name)."\" style='$input_style'>";
						#If there is an error on POST, the form gets reloaded.  
						#This if tries to reinsert the posted data so that you don't have to fill it in again.
						if (strlen($wgRequest->getVal("form_".urlencode($param_name)))) {
							$cat_selected_Value .= $wgRequest->getVal("form_".urlencode($param_name)); #take care of this later
						}

						#modified from CategoryLink Extension... Thank You!
						global $wgDBuser,$wgDBpassword, $wgDBname,$wgDBserver,$IP, $wgDBprefix;
                				$link = mysql_connect($wgDBserver, $wgDBuser, $wgDBpassword)
				                	or die('Could not connect: ' . mysql_error());
				                mysql_select_db($wgDBname) or die('Could not select database');
				                $query = 'SELECT cl_to FROM '.$wgDBprefix.'categorylinks group by cl_to';
				                $result = mysql_query($query) or die('Query failed: ' . mysql_error());
				                $num_rows = mysql_num_rows($result);
				                while($num_row = mysql_fetch_array($result)) {
				                        $data[] = $num_row['cl_to'];
						}

						foreach ($data as $option) {
							if ($option != "MenuCategory") { #Use this as a filter to not list any option.  In this case if the category is MenuCategory, it will not get listed.
	                                                	$form_inputs .= "<option ";
								if ($cat_selected_Value == $option) {
									$form_inputs .= "selected='selected'"; #took care of it here
								}
 								$form_inputs .= ">$option</option>";
							}
						}
                                                $form_inputs .= "</select>";

                                                $form_inputs .= "<br /><span style='font-size:.85em;color:#aaaaaa;padding:4px 10px 10px 20px;text-align:right;'>($type_text)<BR/></span>";
				                $form_inputs .= "<input type=\"hidden\" name=\"type_".urlencode($param_name)."\" value=\"".$param_types[$param_name]."\"/>";
						break;
                                        case "namespace":#This is the paramtype
						$type_text = "namespaces";
						$form_inputs .= "<select name=\"form_".urlencode($param_name)."\" style='$input_style'>";
						#If there is an error on POST, the form gets reloaded.  
						#This if tries to reinsert the posted data so that you don't have to fill it in again.
						if (strlen($wgRequest->getVal("form_".urlencode($param_name)))) {
							$ns_selected_Value .= $wgRequest->getVal("form_".urlencode($param_name)); #take care of this later
						}

						$namespaces = SearchEngine::searchableNamespaces();
						foreach ($namespaces as $option) {
							$form_inputs .= "<option ";
							if ($ns_selected_Value == $option) {
								$form_inputs .= "selected='selected'"; #took care of it here
							}
 							$form_inputs .= ">$option</option>";
						}
                                                $form_inputs .= "</select>";

                                                $form_inputs .= "<br /><span style='font-size:.85em;color:#aaaaaa;padding:4px 10px 10px 20px;text-align:right;'>($type_text)<BR/></span>";
				                $form_inputs .= "<input type=\"hidden\" name=\"type_".urlencode($param_name)."\" value=\"".$param_types[$param_name]."\"/>";
						break;
                                        case substr($param_types[$param_name], 0, 6) == "select":#This is the paramtype
						$type_text = "drop-down";
                                                $form_inputs .= "<select name=\"form_".urlencode($param_name)."\" style='$input_style'>";
						#If there is an error on POST, the form gets reloaded.  
						#This if tries to reinsert the posted data so that you don't have to fill it in again.
						if (strlen($wgRequest->getVal("form_".urlencode($param_name)))) {
							$selected_Value .= $wgRequest->getVal("form_".urlencode($param_name)); #take care of this later
						}
						$select_options = array();
           					$select_options = explode("-", $param_types[$param_name]);
                    				$initial = true;
						foreach ($select_options as $option) {
							if (!$initial) {
                                                		$form_inputs .= "<option ";
								if ($selected_Value == $option) {
									$form_inputs .= "selected='selected'"; #took care of it here
								}
 								$form_inputs .= ">$option</option>";
							}
							$initial = false;
						}
                                                $form_inputs .= "</select>";

                                                $form_inputs .= "<br /><span style='font-size:.85em;color:#aaaaaa;padding:4px 10px 10px 20px;text-align:right;'>($type_text)<BR/></span>";
				                $form_inputs .= "<input type=\"hidden\" name=\"type_".urlencode($param_name)."\" value=\"".$param_types[$param_name]."\"/>";
                                                break;
					case "date":#This is the paramtype
						$type_text = "date in format dd.mm.yyyy";
						$form_inputs .= "<input type=text name=\"form_".urlencode($param_name)."\" style='$input_style'";
						#If there is an error on POST, the form gets reloaded.  
						#This if tries to reinsert the posted data so that you don't have to fill it in again.
                                                if (strlen($wgRequest->getVal("form_".urlencode($param_name)))) {
							$form_inputs .= " value=\"".$wgRequest->getVal("form_".urlencode($param_name))."\"";
						}
						$form_inputs .= "/>";

                                                $form_inputs .= "<br /><span style='font-size:.85em;color:#aaaaaa;padding:4px 10px 10px 20px;text-align:right;'>($type_text)<BR/></span>";
				                $form_inputs .= "<input type=\"hidden\" name=\"type_".urlencode($param_name)."\" value=\"".$param_types[$param_name]."\"/>";
						break;
					case "textarea":#This is the paramtype
						$type_text = "textual blurb";
						$form_inputs .= "<input type=textarea name=\"form_".urlencode($param_name)."\" wrap=\"soft\" style='height:200px;$input_style' ";
						#If there is an error on POST, the form gets reloaded.  
						#This if tries to reinsert the posted data so that you don't have to fill it in again.
                                                if (strlen($wgRequest->getVal("form_".urlencode($param_name)))) {
							$form_inputs .= " value=\"".$wgRequest->getVal("form_".urlencode($param_name))."\"";
						}
						$form_inputs .= "/>";

                                                $form_inputs .= "<br /><span style='font-size:.85em;color:#aaaaaa;padding:4px 10px 10px 20px;text-align:right;'>($type_text) - This must be unformatted text.  You can edit the wiki code after submission!<BR/></span>";

				                $form_inputs .= "<input type=\"hidden\" name=\"type_".urlencode($param_name)."\" value=\"".$param_types[$param_name]."\"/>";
						break;
					default:
						$type_text = "text";
						$form_inputs .= "<input type=text name=\"form_".urlencode($param_name)."\" style='$input_style'";
                                                if (strlen($wgRequest->getVal("form_".urlencode($param_name)))) {
							$form_inputs .= " value=\"".$wgRequest->getVal("form_".urlencode($param_name))."\"";
						}
						$form_inputs .= "/>";

                                                $form_inputs .= "<br /><span style='font-size:.85em;color:#aaaaaa;padding:4px 10px 10px 20px;text-align:right;'>($type_text)<BR/></span>";
				                $form_inputs .= "<input type=\"hidden\" name=\"type_".urlencode($param_name)."\" value=\"".$param_types[$param_name]."\"/>";
				} #end switch
				
			} else { #This is no longer necessary since default: case is above... ???Is that true? nope
                                $form_inputs .= "<label style='background:#1e1e1e;width:100%;display:block;border-top:1px solid #262626;border-botton:1px solid #171717;padding:10px 0 10px 0;'><span style='color:#eeeeee;font-size:1.2em;padding:0px 0px 0px 20px;text-align:right;'>$param_name:</span><br />";
				$type_text = "text";
                                $form_inputs .= "<input type=text name=\"form_".urlencode($param_name)."\" style='$input_style'";
                                if (strlen($wgRequest->getVal("form_".urlencode($param_name)))) {
							$form_inputs .= " value=\"".$wgRequest->getVal("form_".urlencode($param_name))."\"";
				}
				$form_inputs .= "/>";
				$form_inputs .= "<br /><span style='font-size:.85em;color:#aaaaaa;padding:4px 10px 10px 20px;text-align:right;'>($type_text)<BR/></span>";
				$form_inputs .= "<input type=\"hidden\" name=\"type_".urlencode($param_name)."\" value=\"".$param_types[$param_name]."\"/>";
			}
			$form_inputs .= "</label>";
		}	
		$form_inputs .= "<br /><input type=submit value=\"Create\" style='$button_style'/>
		";
	}
	$tt = Title::makeTitle( 10, $tpl_name );
        #When you finish the form, Put a couple of hidden fields, so that you can process what happened later, 
        #Label this POST as a createtpl post and as a post of tplname templateName.
        #Stick the article Title input box at the beginning of the form....???Why not do it sooner?
        #TODO - Allow choosing of namespaces and Categories!
	$output .= 'Create a page with a template : <B><a href="'.$tt->getFullURL().'">'.$tpl_name.'</a></B><BR/>
<form action="" method="POST">
<input type="hidden" name="create" value="createtpl"/>
<input type="hidden" name="tplname" value="'.urlencode($tpl_name).'"/>
Article title: <input type="text" name="articletitle" value="'.$wgRequest->getVal("articletitle").'" style="width: 300px"/>
'.$form_inputs.'
</div></form>
';
	$tpl_name_enc = urlencode($tpl_name);
        #Make sure the formatting is ok and stick the whole thing in a div.
	$output = "<DIV id=\"CreateTpl_$tpl_name_enc\" style=\"padding: 10px; border: 1px solid black;\">".str_replace("\n", "", $output)."</DIV>";
	return $output; #finally done!
}
 #Event EditPage::showEditForm:fields allows injection of form fields into edit form... the event calls this function
# In other words, the data that was posted via a form is then grabbed by this function, and used to populate the main edit box on the edit page.
function preloadCreateTemplate(&$editpage, &$output) {
	global $wgRequest; #a web request object useful for getting data passed via a POST-ed form
	if ( ($wgRequest->getVal("action") == "edit")
	&& ($wgRequest->getVal("create") == "createtpl") ) {
        #The template name is decoded from the tplname that has been passed via POST
	$tplname = urldecode($wgRequest->getVal("tplname"));
        #get all the values from POST
	$post_array = $wgRequest->getValues();
        #Grabthe main editing textbox from the edit page and put the template code in there.
	$editpage->textbox1 = '{{'.$tplname;
	foreach($post_array as $key=>$value) {
		if ((strpos($key, "form_") === 0) && strlen(strval($value))) {
			$editpage->textbox1 .= '|'.urldecode(substr($key, 5)).'='.$value;
		}
	}
	$editpage->textbox1 .= '}}';
	}
	return true;
}
#Event EditPage::showEditForm:initial allows injection of html into edit form... the event calls this function
#It really does very little except let the editpage know that this is an "initial" event... you are creating a page, not editing a long standing one.
function preloadCreateTemplate_initial(&$editpage) {
        #Check to see if the user is both editing and createtpl-ing a page.  If so change formtype to initial.
	global $wgRequest; #a web request object useful for getting data passed via a POST-ed form
        #Get particular values from POST, namely "action" and "create" if their values match, wala!
	if ( ($wgRequest->getVal("action") == "edit")
	&& ($wgRequest->getVal("create") == "createtpl") ) {
                #??? Change the type so that editing instructions appear? or *so that MW knows this is an initial run* on an edit/create
		$editpage->formtype = "initial";
	}
	return true;
}
 #After it's all said and done, make some final edits to the html.  In particular add JavaScript to show the form.
function fnCreateTplPositioning ( $tpl_name_enc, &$parser, &$text) {
	$label = "CreateTpl_$tpl_name_enc";
	$text .= "<script type=\"text/javascript\">document.getElementById(\"$label\").scrollIntoView(true);</script>";	
	return true;
}
 
 
function is_date($i_sDate)
{
  $blnValid = TRUE;
   // check the format first (may not be necessary as we use checkdate() below)
   if(!ereg ("^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$", $i_sDate))
   {
    $blnValid = FALSE;
   }
   else //format is okay, check that days, months, years are okay
   {
      $arrDate = explode(".", $i_sDate); // break up date by slash
      $intDay = $arrDate[0];
      $intMonth = $arrDate[1];
      $intYear = $arrDate[2];
 
      $intIsDate = checkdate($intMonth, $intDay, $intYear);
 
     if(!$intIsDate)
     {
        $blnValid = FALSE;
     }
 
   }//end else
 
   return ($blnValid);
}
?>