Skip to content

Instantly share code, notes, and snippets.

@Nall-chan
Last active May 13, 2023 19:21
Show Gist options
  • Save Nall-chan/13e478f0d76708b057973d669932c98f to your computer and use it in GitHub Desktop.
Save Nall-chan/13e478f0d76708b057973d669932c98f to your computer and use it in GitHub Desktop.
Auto-generate ipsfunctions DocComment
<?php
ini_set('max_execution_time', 300); // kann schon mal dauern :)
$IPSDoku = array();
$urls=[];
$urls['Befehlsreferenz'] = "https://www.symcon.de/service/dokumentation/befehlsreferenz/";
$urls['Modulreferenz'] = "https://www.symcon.de/service/dokumentation/modulreferenz/";
foreach ($urls as $Name => $url)
{
$dom = new DOMDocument();
@$dom->loadHTMLFile($url);
if ($dom->documentURI == null)
{
echo "Die $Name von www.symcon.de konnte nicht geladen werden.\n";
continue;
}
$finder = new DomXPath($dom);
$classname = "tocTable";
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
foreach ($nodes as $node)
{
foreach ($node->childNodes as $Child)
{
if ($Child->childNodes->length == 2)
{
$linknode = $Child->childNodes->item(0)->childNodes->item(0);
if ($linknode->tagName == "a")
{
$f = $Child->childNodes->item(0)->nodeValue;
$link = "https://www.symcon.de" . $linknode->attributes->item(0)->value;
$result = loadDokuPage($link);
if ($result !== false)
{
$IPSDoku[$f]['desc'] = $Child->childNodes->item(1)->nodeValue;
$IPSDoku[$f]['link'] = $link;
if (isset($result['params']))
$IPSDoku[$f]['params'] = $result['params'];
if (isset($result['result']))
$IPSDoku[$f]['result'] = $result['result'];
if (isset($result['description']))
$IPSDoku[$f]['description'] = $result['description'];
}
}
} elseif ($Child->childNodes->length == 1)
{
foreach ($Child->childNodes->item(0)->childNodes->item(0)->childNodes as $Line)
{
$linknode = $Line->childNodes->item(0);
if ($linknode->childNodes->item(0)->tagName == "a")
{
$f = $Line->childNodes->item(0)->childNodes->item(0)->childNodes->item(0)->nodeValue;
$link = "https://www.symcon.de" . $linknode->childNodes->item(0)->attributes->item(0)->value;
$result = loadDokuPage($link);
if ($result !== false)
{
$IPSDoku[$f]['desc'] = $Line->childNodes->item(1)->nodeValue;
$IPSDoku[$f]['link'] = $link;
if (isset($result['params']))
$IPSDoku[$f]['params'] = $result['params'];
if (isset($result['result']))
$IPSDoku[$f]['result'] = $result['result'];
if (isset($result['description']))
$IPSDoku[$f]['description'] = $result['description'];
}
}
}
}
}
}
}
$log = fopen('test.log', "w");
fwrite($log, print_r($IPSDoku,true));
fclose($log);
$fs = IPS_GetFunctionList(0);
$version = IPS_GetKernelVersion();
asort($fs);
$typestr = Array("bool", "int", "float", "string", "mixed", "array");
$logfile = "__ipsfunctionsnew.php";
$log = fopen($logfile, "w");
if (!$log)
{
die("Cannot write to $logfile\n");
}
$date = date('Y-m-d');
$header = "
/** @mainpage Reference list of available IPSymcon functions
*
* This file has been generated using IPSFunktionsDoku.php
*/
/**
* @file
* @brief generated ipsymcon function reference using IPSFunktionsDoku.php
*/
";
fwrite($log, "<?php\r\n");
fwrite($log, $header . "\r\n");
foreach ($fs as $f)
{
$f = IPS_GetFunction($f);
fwrite($log, "\r\n/**\r\n");
if (array_key_exists($f['FunctionName'], $IPSDoku))
{
fwrite($log, "* " . $IPSDoku[$f['FunctionName']]['desc'] . "\r\n");
fwrite($log, "* \r\n");
if (isset($IPSDoku[$f['FunctionName']]['description']))
fwrite($log, "* " . $IPSDoku[$f['FunctionName']]['description'] . "\r\n");
fwrite($log, "* \r\n");
fwrite($log, "* @link " . $IPSDoku[$f['FunctionName']]['link'] . "\r\n");
} else
{
fwrite($log, "* " . $f['FunctionName'] . "\r\n");
fwrite($log, "* \r\n");
}
if (isset($IPSDoku[$f['FunctionName']]['result']))
fwrite($log, "* @returns " . $typestr[$f['Result']['Type_']] . " " . $IPSDoku[$f['FunctionName']]['result'] . "\r\n");
else
fwrite($log, "* @returns " . $typestr[$f['Result']['Type_']] . "\r\n");
$proto = "function " . $f['FunctionName'] . "(";
$param = array();
foreach ($f['Parameters'] as $id => $p)
{
$enum = null;
if (isset($p['Enumeration']) && sizeof($p['Enumeration']) > 0)
{
$b = Array();
foreach ($p['Enumeration'] as $k => $v)
{
$b[] = $k . "=" . $v;
}
$type = "int";
$enum = "enum[" . implode(", ", $b) . "]";
}
else
{
$type = $typestr[$p['Type_']];
}
$pname = $p['Description'];
$pname = preg_replace("/[\(\)]+/", "", $pname);
$param[] = $type . " $" . $pname;
if (isset($IPSDoku[$f['FunctionName']]['params'][$id]))
fwrite($log, "* @param " . $type . ' $' . $pname . " " . $IPSDoku[$f['FunctionName']]['params'][$id] . "\r\n");
else
fwrite($log, "* @param " . $type . ' $' . $pname . "\r\n");
if ($enum)
fwrite($log, "* " . $enum . "\r\n");
}
fwrite($log, "*/\r\n\r\n");
$proto .= implode(', ', $param);
$proto .=") {\r\n\r\n}\r\n";
fwrite($log, $proto);
}
fwrite($log, "\r\n");
fflush($log);
fclose($log);
echo "finished\n";
function converttags($Data)
{
$Data = preg_replace("/(\n)/", "", $Data);
$Data = preg_replace("#<table>(.*)<\/table>#iusU", "\\1", $Data);
$Data = preg_replace("#<thead>(.*)<\/thead>#iusU", "", $Data);
$Data = preg_replace("#<tbody>(.*)<\/tbody>#iusU", "\\1", $Data);
$Data = preg_replace("#<tr>(.*)<\/tr>#iusU", "<li>\\1</li>", $Data);
$Data = preg_replace("#<strong(.*)>(.*)<\/strong>#iusU", "<b>\\2</b>", $Data);
$Data = preg_replace("#<a (.*)>(.*)<\/a>#iusU", "\\2", $Data);
$Data = preg_replace("#<td>(.*)<\/td>#iusU", "\\1", $Data);
$Data = preg_replace("#<div (.*)>(.*)<\/div>#iusU", "\\2", $Data);
$Data = preg_replace("/<\/td>/", "", $Data);
$Data = preg_replace("/<td>/", "", $Data);
$Data = preg_replace("#<div (.*)>(.*)<\/div>#iusU", "\\2", $Data);
return trim($Data);
}
function loadDokuPage($url)
{
$result = array();
$dom = new DOMDocument();
@$dom->loadHTMLFile($url);
if ($dom->documentURI == null)
{
echo " Fehler beim laden der Seite: $url\n";
return false;
}
$finder = new DomXPath($dom);
$classname = "highlight-code language-php";
$testnode = $finder->query("//pre[contains(@class, '$classname')]");
if ($testnode->length ==0){
return false;
}
$finder = new DomXPath($dom);
$classname = "paramList";
$paramnode = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
if ($paramnode->length == 1)
{
$id=0;
foreach ($paramnode->item(0)->childNodes->item(1)->childNodes as $paramitem)
{
if ($paramitem->nodeType == 3)
continue;
$result['params'][$id] = $paramitem->childNodes->item(3)->ownerDocument->saveHTML($paramitem->childNodes->item(3));
$result['params'][$id] = converttags($result['params'][$id]);
$id++;
}
}
$finder = new DomXPath($dom);
$classname = "function";
$function = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
if ($function->length == 1)
{
$result['result'] = $function->item(0)->nextSibling->ownerDocument->saveHTML($function->item(0)->nextSibling);
$result['result'] = converttags($result['result']);
}
$finder = new DomXPath($dom);
$classname = "Beschreibung";
$description = $finder->query("//*[contains(concat(' ', normalize-space(@id), ' '), '$classname')]");
if ($description->length > 0)
{
$result['description'] = $description->item(0)->nextSibling->nextSibling->ownerDocument->saveHTML($description->item(0)->nextSibling->nextSibling);
$result['description'] = converttags($result['description']);
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment