comparison scripts/comment_parser.php @ 1108:a6cd19323345

PHP Comment Parser exports to XML, JSON and CSV notations. Included HTML file interface.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 17 Feb 2016 17:27:29 +0000
parents 6ac36f0fb2cd
children b09a9f38fc76 9ee921c8cdd3
comparison
equal deleted inserted replaced
1107:6ac36f0fb2cd 1108:a6cd19323345
1 <?php 1 <?php
2 // Comment Parser for PHP 2 // Comment Parser for PHP
3 class audioElement {
4 function __construct($id) {
5 $this->id = $id;
6 $this->comments = array();
7 }
8 function addComment($str) {
9 array_push($this->comments,$str);
10 }
11 }
3 12
13 class testPage {
14 function __construct($id) {
15 $this->id = $id;
16 $this->elements = array();
17 }
18 }
4 // XML Saves location - assumes it will be saves/ 19 // XML Saves location - assumes it will be saves/
5 $saves = glob("../saves/*.xml"); 20 $saves = glob("../saves/*.xml");
6 $comment_struct = new SimpleXMLElement('<waetprocess/>'); 21 $comment_struct = array();
7 if (is_array($saves)) 22 if (is_array($saves))
8 { 23 {
9 foreach($saves as $filename) { 24 foreach($saves as $filename) {
10 $xml_string = file_get_contents($filename, FILE_TEXT); 25 $xml_string = file_get_contents($filename, FILE_TEXT);
11 $xml_object = simplexml_load_string($xml_string); 26 $xml_object = simplexml_load_string($xml_string);
12 $test_struct = $comment_struct->addChild("test");
13 if ($xml_object == false) { 27 if ($xml_object == false) {
14 echo "<h1>FATAL</h1> <span>could not parse file ".$filename.": </span>"; 28 echo "<h1>FATAL</h1> <span>could not parse file ".$filename.": </span>";
15 foreach(libxml_get_errors() as $error) { 29 foreach(libxml_get_errors() as $error) {
16 echo "<br>", $error->message; 30 echo "<br>", $error->message;
17 } 31 }
18 } else { 32 } else {
19 // Iterate over each audioHolder node 33 // Iterate over each audioHolder node
20 foreach($xml_object->page as $pageInstance) 34 foreach($xml_object->page as $pageInstance)
21 { 35 {
22 $page_struct = $test_struct->addChild("page"); 36 // Find the page in the comment_struct
23 // Get the page-id and attach 37 $page_struct = null;
24 $page_struct->addAttribute("page-id",$pageInstance['id']); 38 foreach($comment_struct as $comment_struct_page)
25 39 {
40 if ($pageInstance['id'] == $comment_struct_page->id)
41 {
42 $page_struct = $comment_struct_page;
43 break;
44 }
45 }
46 if ($page_struct == null) {
47 array_push($comment_struct,new testPage($pageInstance['id']));
48 $page_struct = $comment_struct[count($comment_struct)-1];
49 }
26 // Get the audioelements of the page 50 // Get the audioelements of the page
27 foreach($pageInstance->audioelement as $fragment) 51 foreach($pageInstance->audioelement as $fragment)
28 { 52 {
29 $fragment_struct = $page_struct->addChild("audioelement"); 53 // Find the page in the comment_struct
30 // Get the element-id and attach 54 $element_struct = null;
31 $page_struct->addAttribute("element-id",$fragment['id']); 55 foreach($page_struct->elements as $page_struct_element)
32 $page_struct->addAttribute("presented-id",$fragment['presentedId']); 56 {
33 $page_struct->addAttribute("url",$fragment['url']); 57 if ($fragment['id'] == $page_struct_element->id)
34 // Append the comment data 58 {
35 echo "<p>Comment: ".$fragement->comment."</p>"; 59 $element_struct = $page_struct_element;
36 $comment = $fragment_struct->addChild("comment"); 60 break;
61 }
62 }
63 if ($element_struct == null) {
64 array_push($page_struct->elements,new audioElement($fragment['id']));
65 $element_struct = $page_struct->elements[count($page_struct->elements)-1];
66 }
67 $element_struct->addComment($fragment->comment->response);
37 } 68 }
38 } 69 }
39 } 70 }
40 } 71 }
41 // Now we have a sub <xml> containing all comment data 72 // Now we have a sub <xml> containing all comment data
42 echo $comment_struct->asXML(); 73 switch($_GET['format']) {
74 case "XML":
75 // Convert to an XML
76 $doc_struct = new SimpleXMLElement('<waetprocess/>');
77 foreach($comment_struct as $page_struct)
78 {
79 $doc_page = $doc_struct->addChild("page");
80 $doc_page->addAttribute("id",$page_struct->id);
81 foreach($page_struct->elements as $element_struct)
82 {
83 $doc_element = $doc_page->addChild("audioelement");
84 $doc_element->addAttribute("id",$element_struct->id);
85 foreach($element_struct->comments as $comment)
86 {
87 $doc_comment = $doc_element->addChild("comment",$comment);
88 }
89 }
90 }
91 echo $doc_struct->asXML();
92 break;
93 case "JSON":
94 // Convert to JSON
95 $doc_string = '{ "pages": [';
96 for($page_index = 0; $page_index < count($comment_struct); $page_index++ )
97 {
98 $page_struct = $comment_struct[$page_index];
99 $doc_page = '{"id": "'.$page_struct->id.'", "elements": [';
100 for($element_index = 0; $element_index < count($page_struct->elements); $element_index++ )
101 {
102 $element_struct = $page_struct->elements[$element_index];
103 $doc_element = '{"id": "'.$element_struct->id.'", "comments": [';
104 for($comment_index = 0; $comment_index < count($element_struct->comments); $comment_index++ )
105 {
106 $doc_comment = '"'.$element_struct->comments[$comment_index].'"';
107 if ($comment_index < count($element_struct->comments)-1) {
108 $doc_comment = $doc_comment.',';
109 }
110 $doc_element = $doc_element.$doc_comment;
111 }
112 $doc_element = $doc_element.']}';
113 if ($element_index < count($page_struct->elements)-1) {
114 $doc_element = $doc_element.',';
115 }
116 $doc_page = $doc_page.$doc_element;
117 }
118 $doc_page = $doc_page.']}';
119 if ($page_index < count($comment_struct)-1) {
120 $doc_page = $doc_page.',';
121 }
122 $doc_string = $doc_string.$doc_page;
123 }
124 $doc_string = $doc_string."]}";
125 echo $doc_string;
126 break;
127 case "CSV":
128 // Conver to CSV
129 // The CSV has three columns: page, element, comment
130 $doc_string = "page,element,comment"."\r\n";
131 foreach($comment_struct as $page_struct)
132 {
133 foreach($page_struct->elements as $element_struct)
134 {
135 foreach($element_struct->comments as $comment)
136 {
137 $doc_string = $doc_string.$page_struct->id.",".$element_struct->id.",".$comment."\r\n";
138 }
139 }
140 }
141 echo $doc_string;
142 }
43 } else { 143 } else {
44 echo "FATAL - No saved XML files discovered"; 144 echo "FATAL - No saved XML files discovered";
45 } 145 }
46 ?> 146 ?>