Skip to content

Commit

Permalink
Merge pull request #32 from nearwood/master
Browse files Browse the repository at this point in the history
Improve parse errors #3, and avoid cache if it fails #31
  • Loading branch information
nearwood authored Nov 5, 2019
2 parents 89ebda0 + e632ddf commit 931aedf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
32 changes: 23 additions & 9 deletions src/ini.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
<?php

/* @brief Custom MSQ Parse Exceptions
*/
class MSQ_ParseException extends Exception {
protected $htmlMessage;

public function __construct($message = null, $html = '', $code = 0, Exception $previous = null) {
parent::__construct($message, $code, $previous);
$this->htmlMessage = $html;
}

public function getHTMLMessage() {
return $this->htmlMessage;
}
}

class MSQ_ConfigException extends MSQ_ParseException { }

/*
* @brief INI parsing
*
*/
class INI
{
/**
* @brief Given a signature string, finds and parses the respective INI file.
* @brief Given a signature string, calculates the respective INI file.
*
* Returns an array of the config file contents.
* @param $signature The signature string which will be modified into a firmware/version array.
Expand Down Expand Up @@ -60,7 +78,7 @@ public static function getConfig(&$signature)
break;

default:
$msDir = "unknown";
throw new MSQ_ConfigException("Unknown/Invalid MSQ signature: $msVersion/$fwVersion");
}

//Setup firmware version for matching.
Expand All @@ -76,9 +94,7 @@ public static function getConfig(&$signature)

debug("INI File: $iniFile");

$msqMap = INI::parse($iniFile, TRUE);

return $msqMap;
return $iniFile;
}

/**
Expand All @@ -87,10 +103,9 @@ public static function getConfig(&$signature)
* Based on code from: [email protected] (php.net comments) http://php.net/manual/en/function.parse-ini-file.php#78815
*
* @param $file The path to the INI file that will be loaded and parsed.
* @param $something Unused
* @returns A huge array of arrays, starting with sections.
*/
public static function parse($file, $something)
public static function parse($file)
{
try
{
Expand All @@ -107,9 +122,8 @@ public static function parse($file, $something)

if ($ini == FALSE || count($ini) == 0)
{
echo "<div class=\"error\">Error opening file: $file</div>";
error("Error or empty file: $file");
return null;
throw new MSQ_ConfigException("Could not open MSQ config file: $file");
}
else if (DEBUG) debug("Opened: $file");

Expand Down
17 changes: 8 additions & 9 deletions src/msq.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,20 @@ public function parseMSQ($xml, &$engine, &$metadata)
$msqHeader .= "<div>Tuning SW: " . $msq->bibliography['author'] . "</div>";
$msqHeader .= "<div>Date: " . $msq->bibliography['writeDate'] . "</div>";
$msqHeader .= '</div>';
$html['msqHeader'] = $msqHeader;

$sig = $msq->versionInfo['signature'];
$sigString = $sig;
$msqMap = INI::getConfig($sig);

if ($msqMap == null)
{
try {
$iniFile = INI::getConfig($sig);
$msqMap = INI::parse($iniFile);
} catch (MSQ_ConfigException $e) {
error('Error parsing config file: ' . $e->getMessage());
$issueTitle = urlencode("INI Request: $sigString");
$msqHeader .= '<div class="error">Unable to load the corresponding configuration file for that MSQ. Please <a href="https://github.com/nearwood/msqur/issues/new?title=' . $issueTitle . '">file a bug!</a></div>';
$html['msqHeader'] = $msqHeader;
return $html; //TODO Signal caller to skip engine/metadata updates
$htmlMessage = $msqHeader . '<div class="error">Unable to load the corresponding configuration file for that MSQ. Please <a href="https://github.com/nearwood/msqur/issues/new?title=' . $issueTitle . '">file a bug!</a></div>';
throw new MSQ_ParseException("Could not load configuration file for MSQ: " . $e->getMessage(), $htmlMessage, 100, $e);
}

$html['msqHeader'] = $msqHeader;

//Calling function will update
$metadata['fileFormat'] = $msq->versionInfo['fileFormat'];
$metadata['signature'] = $sig[1];
Expand Down
30 changes: 17 additions & 13 deletions src/msqur.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,24 @@ public function view($id)
$metadata = array();
$xml = $this->db->getXML($id);
if ($xml !== null) {
$groupedHtml = $msq->parseMSQ($xml, $engine, $metadata);
$this->db->updateMetadata($id, $metadata);
$this->db->updateEngine($id, $engine);

$html = "";
foreach($groupedHtml as $group => $v)
{
//TODO Group name as fieldset legend or sth
$html .= "<div class=\"group-$group\">";
$html .= $v;
$html .= '</div>';
try {
$groupedHtml = $msq->parseMSQ($xml, $engine, $metadata);
$this->db->updateMetadata($id, $metadata);
$this->db->updateEngine($id, $engine);

$html = "";
foreach($groupedHtml as $group => $v)
{
//TODO Group name as fieldset legend or sth
$html .= "<div class=\"group-$group\">";
$html .= $v;
$html .= '</div>';
}

$this->db->updateCache($id, $html);
} catch (MSQ_ParseException $e) {
$html = $e->getHTMLMessage();
}

$this->db->updateCache($id, $html);
}
}
}
Expand Down

0 comments on commit 931aedf

Please sign in to comment.