annotate scripts/score_parser.php @ 1303:ade3acb0cee3

Added score_parse.php separators for CSV
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Tue, 23 Feb 2016 17:11:28 +0000
parents
children 124e6c702845
rev   line source
nickjillings@1303 1 <?php
nickjillings@1303 2 // Value parser for WAET XML
nickjillings@1303 3 // testPage --> axis --> element --> value
nickjillings@1303 4 class nestedObject {
nickjillings@1303 5 function __construct($id) {
nickjillings@1303 6 $this->id = $id;
nickjillings@1303 7 $this->nest = array();
nickjillings@1303 8 $this->type = null;
nickjillings@1303 9 $this->num = 0;
nickjillings@1303 10 }
nickjillings@1303 11 function addNewChild($id) {
nickjillings@1303 12 if ($this->type == null) {
nickjillings@1303 13 $this->type = "nest";
nickjillings@1303 14 }
nickjillings@1303 15 if ($this->type == "nest") {
nickjillings@1303 16 $obj = new nestedObject($id);
nickjillings@1303 17 array_push($this->nest,$obj);
nickjillings@1303 18 $this->num = count($this->nest);
nickjillings@1303 19 return $this->nest[$this->num-1];
nickjillings@1303 20 }
nickjillings@1303 21 return null;
nickjillings@1303 22 }
nickjillings@1303 23 function findChild($checkId) {
nickjillings@1303 24 if ($this->type == "nest"){
nickjillings@1303 25 foreach($this->nest as $child)
nickjillings@1303 26 {
nickjillings@1303 27 if (strcmp($checkId,$child->id) == 0) {
nickjillings@1303 28 return $child;
nickjillings@1303 29 }
nickjillings@1303 30 }
nickjillings@1303 31 }
nickjillings@1303 32 return null;
nickjillings@1303 33 }
nickjillings@1303 34 function addValue($val) {
nickjillings@1303 35 if ($this->type == null) {
nickjillings@1303 36 $this->type = "value";
nickjillings@1303 37 }
nickjillings@1303 38 if ($this->type == "value") {
nickjillings@1303 39 array_push($this->nest,$val);
nickjillings@1303 40 $this->num = count($this->nest);
nickjillings@1303 41 return $this->nest[$this->num-1];
nickjillings@1303 42 }
nickjillings@1303 43 return null;
nickjillings@1303 44 }
nickjillings@1303 45 }
nickjillings@1303 46
nickjillings@1303 47 // Build the root nest object to hold the testPages
nickjillings@1303 48 $root = new nestedObject("root");
nickjillings@1303 49
nickjillings@1303 50 // XML Saves location - assumes it will be saves/
nickjillings@1303 51 $saves = glob("../saves/*.xml");
nickjillings@1303 52 if (is_array($saves))
nickjillings@1303 53 {
nickjillings@1303 54 foreach($saves as $filename) {
nickjillings@1303 55 $xml_string = file_get_contents($filename, FILE_TEXT);
nickjillings@1303 56 $xml_object = simplexml_load_string($xml_string);
nickjillings@1303 57 if ($xml_object == false) {
nickjillings@1303 58 echo "<h1>FATAL</h1> <span>could not parse file ".$filename.": </span>";
nickjillings@1303 59 foreach(libxml_get_errors() as $error) {
nickjillings@1303 60 echo "<br>", $error->message;
nickjillings@1303 61 }
nickjillings@1303 62 } else {
nickjillings@1303 63 // Iterate over each $page node
nickjillings@1303 64 foreach($xml_object->page as $pageInstance)
nickjillings@1303 65 {
nickjillings@1303 66 // Find in the nest
nickjillings@1303 67 $pageInstanceId = $pageInstance['id'];
nickjillings@1303 68 $page_nest = $root->findChild($pageInstanceId);
nickjillings@1303 69 if ($page_nest == null) {
nickjillings@1303 70 $page_nest = $root->addNewChild($pageInstanceId);
nickjillings@1303 71 }
nickjillings@1303 72
nickjillings@1303 73 // Iterate over each $element node
nickjillings@1303 74 foreach($pageInstance->audioelement as $element) {
nickjillings@1303 75
nickjillings@1303 76 // Now get the <value> tags
nickjillings@1303 77 foreach($element->value as $value) {
nickjillings@1303 78 $axis_nest = null;
nickjillings@1303 79 $axisName = "default";
nickjillings@1303 80 if (isset($value['interface-name']))
nickjillings@1303 81 {
nickjillings@1303 82 // Find the axis nest
nickjillings@1303 83 $axisName = $value['interface-name'];
nickjillings@1303 84 }
nickjillings@1303 85
nickjillings@1303 86 $axis_nest = $page_nest->findChild($axisName);
nickjillings@1303 87 if ($axis_nest == null) {
nickjillings@1303 88 $axis_nest = $page_nest->addNewChild($axisName);
nickjillings@1303 89 }
nickjillings@1303 90
nickjillings@1303 91 // Find our specific element tag
nickjillings@1303 92 $elementId = $element['id'];
nickjillings@1303 93 $element_nest = $axis_nest->findChild($elementId);
nickjillings@1303 94 if ($element_nest == null) {
nickjillings@1303 95 $element_nest = $axis_nest->addNewChild($elementId);
nickjillings@1303 96 }
nickjillings@1303 97 // Now push our value
nickjillings@1303 98 $element_nest->addValue($value);
nickjillings@1303 99 }
nickjillings@1303 100 }
nickjillings@1303 101 }
nickjillings@1303 102 }
nickjillings@1303 103 }
nickjillings@1303 104 // We now have a structure in $root. EXPORT IT
nickjillings@1303 105 switch($_GET['format']) {
nickjillings@1303 106 case "XML":
nickjillings@1303 107 // Convert to XML
nickjillings@1303 108 $doc_root = new SimpleXMLElement('<waetprocess/>');
nickjillings@1303 109 foreach($root->nest as $page) {
nickjillings@1303 110 $doc_page = $doc_root->addChild("page");
nickjillings@1303 111 $doc_page->addAttribute("id",$page->id);
nickjillings@1303 112 foreach($page->nest as $axis) {
nickjillings@1303 113 $doc_axis = $doc_page->addChild("interface");
nickjillings@1303 114 $doc_axis->addAttribute("name",$axis->id);
nickjillings@1303 115 foreach($axis->nest as $element) {
nickjillings@1303 116 $doc_element = $doc_axis->addChild("audioelement");
nickjillings@1303 117 $doc_element->addAttribute("id",$element->id);
nickjillings@1303 118 foreach($element->nest as $value) {
nickjillings@1303 119 $doc_value = $doc_element->addChild("value",$value);
nickjillings@1303 120 }
nickjillings@1303 121 }
nickjillings@1303 122 }
nickjillings@1303 123 }
nickjillings@1303 124 echo $doc_root->asXML();
nickjillings@1303 125 break;
nickjillings@1303 126 case "JSON":
nickjillings@1303 127 // Convert to JSON
nickjillings@1303 128 $doc_root = '{ "pages": [';
nickjillings@1303 129 for ($pageIndex = 0; $pageIndex < $root->num; $pageIndex++)
nickjillings@1303 130 {
nickjillings@1303 131 $page = $root->nest[$pageIndex];
nickjillings@1303 132 $doc_page = '{ "id": "'.$page->id.'", "axis": [';
nickjillings@1303 133 for($axisIndex = 0; $axisIndex < $page->num; $axisIndex++)
nickjillings@1303 134 {
nickjillings@1303 135 $axis = $page->nest[$axisIndex];
nickjillings@1303 136 $doc_axis = '{ "name": "'.$axis->id.'", "elements": [';
nickjillings@1303 137 for($elementIndex = 0; $elementIndex < $axis->num; $elementIndex++)
nickjillings@1303 138 {
nickjillings@1303 139 $element = $axis->nest[$elementIndex];
nickjillings@1303 140 $doc_element = '{ "id": "'.$element->id.'", "values": [';
nickjillings@1303 141 for ($valueIndex = 0; $valueIndex < $element->num; $valueIndex++)
nickjillings@1303 142 {
nickjillings@1303 143 $doc_element = $doc_element."".strval($element->nest[$valueIndex]);
nickjillings@1303 144 if ($valueIndex < $element->num-1) {
nickjillings@1303 145 $doc_element = $doc_element.', ';
nickjillings@1303 146 }
nickjillings@1303 147 }
nickjillings@1303 148 $doc_element = $doc_element.']}';
nickjillings@1303 149 if ($elementIndex < $axis->num-1) {
nickjillings@1303 150 $doc_element = $doc_element.', ';
nickjillings@1303 151 }
nickjillings@1303 152 $doc_axis = $doc_axis.$doc_element;
nickjillings@1303 153 }
nickjillings@1303 154 $doc_axis = $doc_axis.']}';
nickjillings@1303 155 if ($axisIndex < $page->num-1) {
nickjillings@1303 156 $doc_axis = $doc_axis.', ';
nickjillings@1303 157 }
nickjillings@1303 158 $doc_page = $doc_page.$doc_axis;
nickjillings@1303 159 }
nickjillings@1303 160 $doc_page = $doc_page.']}';
nickjillings@1303 161 if ($pageIndex < $root->num-1) {
nickjillings@1303 162 $doc_page = $doc_page.', ';
nickjillings@1303 163 }
nickjillings@1303 164 $doc_root = $doc_root.$doc_page;
nickjillings@1303 165 }
nickjillings@1303 166 $doc_root = $doc_root.']}';
nickjillings@1303 167 echo $doc_root;
nickjillings@1303 168 break;
nickjillings@1303 169 case "CSV":
nickjillings@1303 170 // Convert to CSV
nickjillings@1303 171 // CSV Columts: page, axis, element, value
nickjillings@1303 172 $doc_string = "page,axis,element,value"."\r\n";
nickjillings@1303 173 foreach($root->nest as $page){
nickjillings@1303 174 foreach($page->nest as $axis) {
nickjillings@1303 175 foreach($axis->nest as $element) {
nickjillings@1303 176 foreach($element->nest as $value) {
nickjillings@1303 177 $doc_string = $doc_string.$page->id.',';
nickjillings@1303 178 $doc_string = $doc_string.$axis->id.',';
nickjillings@1303 179 $doc_string = $doc_string.$element->id.',';
nickjillings@1303 180 $doc_string = $doc_string.$value;
nickjillings@1303 181 $doc_string = $doc_string."\r\n";
nickjillings@1303 182 }
nickjillings@1303 183 }
nickjillings@1303 184 }
nickjillings@1303 185 }
nickjillings@1303 186 echo $doc_string;
nickjillings@1303 187 }
nickjillings@1303 188 } else {
nickjillings@1303 189 echo "FATAL - No saved XML files discovered";
nickjillings@1303 190 }
nickjillings@1303 191
nickjillings@1303 192 ?>