From 304dc11190c596bcc829277862bc6ef93beca843 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Wed, 22 Nov 2017 00:15:32 +0100 Subject: [PATCH] Started actual parsing of XML into PHP objects. --- lib/TclUpdates/GotuObject.php | 26 +++++++++++++ lib/TclUpdates/XmlParser.php | 72 +++++++++++++++++++++++++++++++++++ parse_files.php | 29 ++++++++++++++ 3 files changed, 127 insertions(+) create mode 100755 lib/TclUpdates/GotuObject.php create mode 100755 parse_files.php diff --git a/lib/TclUpdates/GotuObject.php b/lib/TclUpdates/GotuObject.php new file mode 100755 index 0000000..5fb1eb7 --- /dev/null +++ b/lib/TclUpdates/GotuObject.php @@ -0,0 +1,26 @@ +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; + } +} diff --git a/lib/TclUpdates/XmlParser.php b/lib/TclUpdates/XmlParser.php index f4f4625..2fdb872 100644 --- a/lib/TclUpdates/XmlParser.php +++ b/lib/TclUpdates/XmlParser.php @@ -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; + } } diff --git a/parse_files.php b/parse_files.php new file mode 100755 index 0000000..abf266f --- /dev/null +++ b/parse_files.php @@ -0,0 +1,29 @@ +#!/usr/bin/env php +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); +}