TemplateData Skeleton

Produces a skeleton templatedata document for a given template. It retrieves the source from the wiki page and scans for all instances of {{{name these are used as a guess for the parameters used by a template.

Wikipedia site
Template name

Source code

Source code

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TemplateData Skeleton</title>
</head>
<body>
<?php 
$root = "../";
$page = "TemplateData Skeleton";
include("../header.php");
?>

<h1>TemplateData Skeleton</h1>

<p>Produces a skeleton templatedata document for a given template.
It retrieves the source from the wiki page and scans for all instances of <code>{{{name</code>
these are used as a guess for the parameters used by a template.
</p>
<?php
$page='Template:';
$site='en.wikipedia.org';
if(isset($_GET['PageName'])) {
  $page=$_GET['PageName'];
}
if(isset($_GET['SiteName'])) {
  $site=$_GET['SiteName'];
}
?>
<form name="LoadPage">
Wikipedia site <input name="SiteName" type="text" size="60" value="<?php echo $site; ?>"/><br/>
Template name <input name="PageName" type="text" size="60" value="<?php echo $page; ?>"/><br/>
<input type="submit" />
</form>


<?php
/** 
 * Send a GET requst using cURL 
 * @param string $url to request 
 * @param array $get values to send 
 * @param array $options for cURL 
 * @return string 
 * Taken from a comment on the php curl doc
 */ 
function curl_get($url, array $get = array(), array $options = array()) 
{    
    $defaults = array( 
        CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get), 
        CURLOPT_HEADER => 0, 
        CURLOPT_RETURNTRANSFER => TRUE, 
        CURLOPT_TIMEOUT => 4 
    ); 
    
    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    } 
    curl_close($ch); 
    return $result; 
} 

function println($str) {
  echo($str . "\n");
}


if(isset($_GET['PageName']))
{
//$url = 'http://www.example.com';
//$url = 'http://en.wikipedia.org/w/index.php?title=Template:Taxobox+name&action=raw';
$url = 'http://' . $_GET['SiteName'] . '/w/index.php?title=' . urlencode($_GET['PageName'])	;
$rawurl = $url . '&action=raw';
println('<p>'); 
println('Template &nbsp;&nbsp;&nbsp; <a href="' . $url . '">' . $_GET['PageName'] . '</a><br/>');
println('Template doc <a href="' . $url . '/doc">' . $_GET['PageName'] . '/doc</a><br/>');
println('Template raw <a href="' . $rawurl . '">' . $rawurl . '</a><br/>');
println('</p>');

$res = curl_get($rawurl );

println('<h2>Source code</h2>');
println('<pre>');
println( htmlentities($res) );
println('</pre>');

println('<h2>TemplateData</h2>');

preg_match_all ("/\{\{\{[^\|\}]+/",$res,$matches);
//array_shift($matches);
$params = array();
//foreach ($matches as $k => $v) {
$count = count($matches[0]);
for ($i = 0; $i < $count; $i++) {
  $v = substr($matches[0][$i],3);
//  println('<li>' . $v . '</li>');
  ++$params[$v];
}

println('<pre>');
println('&lt;templatedata&gt;');
println('{');
println('  "description": "insert description here",');
println('  "params": {');
$n = count($params);
$i=0;
foreach($params as $key => $val) {
//println( $key );

println('    "' . $key . '": {');
println('      "label": "' . $key . '",');
println('      "description": "",');
println('      "type": "string",');
println('      "required": false');
echo('    }');
if(++$i < $n ) echo ',';
echo "\n";

}
println('  }');
println('}');
println('&lt;/templatedata&gt;');
println('</pre>');

}
?>


<p><a href="TemplateData.php?Source=true">Source code</a></p>
<?php
if(isset($_GET['Source'])) {
println('<h2>Source code</h2>');
println('<pre>');

$handle = fopen('TemplateData.php','r');
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
		echo htmlentities($buffer);
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}


println('</pre>');
}

include("../footer.php");
?>

</body>
</html>