1
0
mirror of https://github.com/mbirth/tcl_update_db.git synced 2024-11-09 23:06:45 +00:00

Started actual parsing of XML into PHP objects.

This commit is contained in:
Markus Birth 2017-11-22 00:15:32 +01:00
parent 2fe04b0edd
commit 304dc11190
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
3 changed files with 127 additions and 0 deletions

26
lib/TclUpdates/GotuObject.php Executable file
View File

@ -0,0 +1,26 @@
<?php
namespace TclUpdates;
class GotuObject
{
private $attrs = array();
public function __construct()
{
}
public static function fromXmlParser(XmlParser $xp)
{
if (!$xp->validateGOTU()) {
return false;
}
$g = new self();
$g->attrs['type'] = $xp->getAttr('type');
$g->attrs['fv'] = $xp->getAttr('fv');
$g->attrs['tv'] = $xp->getAttr('tv');
$g->attrs['time'] = $xp->getReleaseTime();
return $g;
}
}

View File

@ -5,6 +5,27 @@ namespace TclUpdates;
class XmlParser
{
private $dom;
private $xp;
private $attr_map = array(
'update_desc' => '//UPDATE_DESC',
'encoding_error' => '//ENCODING_ERROR',
'curef' => '//CUREF',
'type' => '//VERSION/TYPE',
'fv' => '//VERSION/FV',
'tv' => '//VERSION/TV',
'svn' => '//VERSION/SVN',
'publisher' => '//VERSION/RELEASE_INFO/publisher',
'fw_id' => '//FIRMWARE/FW_ID',
'fileset_count' => '//FIRMWARE/FILESET_COUNT',
'filename' => '//FILESET/FILE[0]/FILENAME',
'file_id' => '//FILESET/FILE[0]/FILE_ID',
'file_size' => '//FILESET/FILE[0]/SIZE',
'file_chksum' => '//FILESET/FILE[0]/CHECKSUM',
'file_version' => '//FILESET/FILE[0]/FILE_VERSION',
'description_en' => '//DESCRIPTION/en',
'description_ja' => '//DESCRIPTION/ja',
'description_zh' => '//DESCRIPTION/zh',
);
public function __construct()
{
@ -28,4 +49,55 @@ class XmlParser
}
return true;
}
public function getAttr($attr)
{
if (!isset($this->attr_map[$attr])) {
return false;
}
$xpath = $this->attr_map[$attr];
$node = $this->getXPathValue($xpath);
return $node;
}
public function getReleaseTime()
{
$yr = $this->getXPathValue('//VERSION/RELEASE_INFO/year');
$mo = $this->getXPathValue('//VERSION/RELEASE_INFO/month');
$dy = $this->getXPathValue('//VERSION/RELEASE_INFO/day');
$hr = $this->getXPathValue('//VERSION/RELEASE_INFO/hour');
$mn = $this->getXPathValue('//VERSION/RELEASE_INFO/minute');
$se = $this->getXPathValue('//VERSION/RELEASE_INFO/second');
$tz = $this->getXPathValue('//VERSION/RELEASE_INFO/timezone');
$tz = intval(str_replace('GMT', '', $tz)); // returns hours from GMT (e.g. "8" or "-8")
$stamp = sprintf('%04u-%02u-%02uT%02u:%02u:%02u%+03d:00', $yr, $mo, $dy, $hr, $mn, $se, $tz);
//$unix = strtotime($stamp);
return $stamp;
}
private function getXPath($path, $context = null)
{
if (is_null($this->xp)) {
$this->xp = new \DOMXPath($this->dom);
}
$result = $this->xp->query($path, $context);
//var_dump($result);
if ($result->length == 0) {
return null;
}
return $result;
}
private function getXPathValue($path, $context = null)
{
$node = $this->getXPath($path, $context);
if (is_null($node)) {
return null;
}
if ($node->length == 1) {
return $node->item(0)->nodeValue;
}
return $node;
}
}

29
parse_files.php Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env php
<?php
require_once __DIR__ . '/lib/autoloader.php';
use \TclUpdates\GotuObject;
use \TclUpdates\XmlParser;
$bkup_dir = __DIR__ . '/data/';
$file_list = glob($bkup_dir . '*.xml');
foreach ($file_list as $file) {
$filename = basename($file);
$data = file_get_contents($file);
$xp = new XmlParser();
$load_ok = $xp->loadXmlFromString($data);
if (!$load_ok) {
echo 'Could not load ' . $filename . '!' . PHP_EOL;
continue;
}
if (!$xp->validateGOTU()) {
echo 'XML not valid in ' . $filename . '!' . PHP_EOL;
continue;
}
echo 'Processing ' . $filename . PHP_EOL;
$g = GotuObject::fromXmlParser($xp);
print_r($g);
}