nicholas@2224
|
1 <?php
|
nicholas@2224
|
2 // Comment Parser for PHP
|
nicholas@2457
|
3 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
|
nicholas@2457
|
4 header("Cache-Control: post-check=0, pre-check=0", false);
|
nicholas@2457
|
5 header("Pragma: no-cache");
|
nicholas@2224
|
6 class audioElement {
|
nicholas@2224
|
7 function __construct($id) {
|
nicholas@2224
|
8 $this->id = $id;
|
nicholas@2224
|
9 $this->comments = array();
|
nicholas@2224
|
10 }
|
nicholas@2224
|
11 function addComment($str) {
|
nicholas@2224
|
12 array_push($this->comments,$str);
|
nicholas@2224
|
13 }
|
nicholas@2224
|
14 }
|
nicholas@2224
|
15
|
nicholas@2224
|
16 class testPage {
|
nicholas@2224
|
17 function __construct($id) {
|
nicholas@2224
|
18 $this->id = $id;
|
nicholas@2224
|
19 $this->elements = array();
|
nicholas@2224
|
20 }
|
nicholas@2224
|
21 }
|
nicholas@2224
|
22 // XML Saves location - assumes it will be saves/
|
nicholas@2224
|
23 $saves = glob("../saves/*.xml");
|
nicholas@2224
|
24 $comment_struct = array();
|
nicholas@2224
|
25 if (is_array($saves))
|
nicholas@2224
|
26 {
|
nicholas@2224
|
27 foreach($saves as $filename) {
|
nicholas@2224
|
28 $xml_string = file_get_contents($filename, FILE_TEXT);
|
nicholas@2224
|
29 $xml_object = simplexml_load_string($xml_string);
|
nicholas@2224
|
30 if ($xml_object == false) {
|
nicholas@2224
|
31 echo "<h1>FATAL</h1> <span>could not parse file ".$filename.": </span>";
|
nicholas@2224
|
32 foreach(libxml_get_errors() as $error) {
|
nicholas@2224
|
33 echo "<br>", $error->message;
|
nicholas@2224
|
34 }
|
nicholas@2224
|
35 } else {
|
nicholas@2224
|
36 // Iterate over each audioHolder node
|
nicholas@2224
|
37 foreach($xml_object->page as $pageInstance)
|
nicholas@2224
|
38 {
|
nicholas@2224
|
39 // Find the page in the comment_struct
|
nicholas@2224
|
40 $page_struct = null;
|
nicholas@2224
|
41 if($pageInstance['state'] == "complete") {
|
nicholas@2224
|
42 foreach($comment_struct as $comment_struct_page)
|
nicholas@2224
|
43 {
|
nicholas@2224
|
44 if ($pageInstance['ref'] == $comment_struct_page->id)
|
nicholas@2224
|
45 {
|
nicholas@2224
|
46 $page_struct = $comment_struct_page;
|
nicholas@2224
|
47 break;
|
nicholas@2224
|
48 }
|
nicholas@2224
|
49 }
|
nicholas@2224
|
50 if ($page_struct == null) {
|
nicholas@2224
|
51 array_push($comment_struct,new testPage($pageInstance['ref']));
|
nicholas@2224
|
52 $page_struct = $comment_struct[count($comment_struct)-1];
|
nicholas@2224
|
53 }
|
nicholas@2224
|
54 // Get the audioelements of the page
|
nicholas@2224
|
55 foreach($pageInstance->audioelement as $fragment)
|
nicholas@2224
|
56 {
|
nicholas@2224
|
57 // Find the page in the comment_struct
|
nicholas@2224
|
58 $element_struct = null;
|
nicholas@2224
|
59 foreach($page_struct->elements as $page_struct_element)
|
nicholas@2224
|
60 {
|
nicholas@2224
|
61 if ($fragment['name'] == $page_struct_element->id)
|
nicholas@2224
|
62 {
|
nicholas@2224
|
63 $element_struct = $page_struct_element;
|
nicholas@2224
|
64 break;
|
nicholas@2224
|
65 }
|
nicholas@2224
|
66 }
|
nicholas@2224
|
67 if ($element_struct == null) {
|
nicholas@2224
|
68 array_push($page_struct->elements,new audioElement($fragment['name']));
|
nicholas@2224
|
69 $element_struct = $page_struct->elements[count($page_struct->elements)-1];
|
nicholas@2224
|
70 }
|
nicholas@2224
|
71 $element_struct->addComment($fragment->comment->response);
|
nicholas@2224
|
72 }
|
nicholas@2224
|
73 }
|
nicholas@2224
|
74 }
|
nicholas@2224
|
75 }
|
nicholas@2224
|
76 }
|
nicholas@2224
|
77 // Now we have a structure containing all comment data
|
nicholas@2224
|
78 switch($_GET['format']) {
|
nicholas@2224
|
79 case "XML":
|
nicholas@2224
|
80 // Convert to an XML
|
nicholas@2224
|
81 $doc_struct = new SimpleXMLElement('<waetprocess/>');
|
nicholas@2224
|
82 foreach($comment_struct as $page_struct)
|
nicholas@2224
|
83 {
|
nicholas@2224
|
84 $doc_page = $doc_struct->addChild("page");
|
nicholas@2224
|
85 $doc_page->addAttribute("id",$page_struct->id);
|
nicholas@2224
|
86 foreach($page_struct->elements as $element_struct)
|
nicholas@2224
|
87 {
|
nicholas@2224
|
88 $doc_element = $doc_page->addChild("audioelement");
|
nicholas@2224
|
89 $doc_element->addAttribute("id",$element_struct->id);
|
nicholas@2224
|
90 foreach($element_struct->comments as $comment)
|
nicholas@2224
|
91 {
|
nicholas@2224
|
92 $doc_comment = $doc_element->addChild("comment",$comment);
|
nicholas@2224
|
93 }
|
nicholas@2224
|
94 }
|
nicholas@2224
|
95 }
|
nicholas@2224
|
96 echo $doc_struct->asXML();
|
nicholas@2224
|
97 break;
|
nicholas@2224
|
98 case "JSON":
|
nicholas@2224
|
99 // Convert to JSON
|
nicholas@2224
|
100 $doc_string = '{ "pages": [';
|
nicholas@2224
|
101 for($page_index = 0; $page_index < count($comment_struct); $page_index++ )
|
nicholas@2224
|
102 {
|
nicholas@2224
|
103 $page_struct = $comment_struct[$page_index];
|
nicholas@2224
|
104 $doc_page = '{"id": "'.$page_struct->id.'", "elements": [';
|
nicholas@2224
|
105 for($element_index = 0; $element_index < count($page_struct->elements); $element_index++ )
|
nicholas@2224
|
106 {
|
nicholas@2224
|
107 $element_struct = $page_struct->elements[$element_index];
|
nicholas@2224
|
108 $doc_element = '{"id": "'.$element_struct->id.'", "comments": [';
|
nicholas@2224
|
109 for($comment_index = 0; $comment_index < count($element_struct->comments); $comment_index++ )
|
nicholas@2224
|
110 {
|
nicholas@2224
|
111 $doc_comment = '"'.$element_struct->comments[$comment_index].'"';
|
nicholas@2224
|
112 if ($comment_index < count($element_struct->comments)-1) {
|
nicholas@2224
|
113 $doc_comment = $doc_comment.',';
|
nicholas@2224
|
114 }
|
nicholas@2224
|
115 $doc_element = $doc_element.$doc_comment;
|
nicholas@2224
|
116 }
|
nicholas@2224
|
117 $doc_element = $doc_element.']}';
|
nicholas@2224
|
118 if ($element_index < count($page_struct->elements)-1) {
|
nicholas@2224
|
119 $doc_element = $doc_element.',';
|
nicholas@2224
|
120 }
|
nicholas@2224
|
121 $doc_page = $doc_page.$doc_element;
|
nicholas@2224
|
122 }
|
nicholas@2224
|
123 $doc_page = $doc_page.']}';
|
nicholas@2224
|
124 if ($page_index < count($comment_struct)-1) {
|
nicholas@2224
|
125 $doc_page = $doc_page.',';
|
nicholas@2224
|
126 }
|
nicholas@2224
|
127 $doc_string = $doc_string.$doc_page;
|
nicholas@2224
|
128 }
|
nicholas@2224
|
129 $doc_string = $doc_string."]}";
|
nicholas@2224
|
130 echo $doc_string;
|
nicholas@2224
|
131 break;
|
nicholas@2224
|
132 case "CSV":
|
nicholas@2224
|
133 // Conver to CSV
|
nicholas@2224
|
134 // The CSV has three columns: page, element, comment
|
nicholas@2224
|
135 $doc_string = "page,element,comment"."\r\n";
|
nicholas@2224
|
136 foreach($comment_struct as $page_struct)
|
nicholas@2224
|
137 {
|
nicholas@2224
|
138 foreach($page_struct->elements as $element_struct)
|
nicholas@2224
|
139 {
|
nicholas@2224
|
140 foreach($element_struct->comments as $comment)
|
nicholas@2224
|
141 {
|
nicholas@2224
|
142 $doc_string = $doc_string.$page_struct->id.",".$element_struct->id.",".$comment."\r\n";
|
nicholas@2224
|
143 }
|
nicholas@2224
|
144 }
|
nicholas@2224
|
145 }
|
nicholas@2224
|
146 echo $doc_string;
|
nicholas@2224
|
147 }
|
nicholas@2224
|
148 } else {
|
nicholas@2224
|
149 echo "FATAL - No saved XML files discovered";
|
nicholas@2224
|
150 }
|
nicholas@2538
|
151 ?>
|