comparison scripts/score_parser.php @ 1088:3705f68a38b7

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