Mercurial > hg > waet-hammond-1
comparison scripts/comment_parser.php @ 0:d2eb0e6ccaaf
initial commit
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Wed, 20 Apr 2016 16:26:57 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d2eb0e6ccaaf |
---|---|
1 <?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 } | |
12 | |
13 class testPage { | |
14 function __construct($id) { | |
15 $this->id = $id; | |
16 $this->elements = array(); | |
17 } | |
18 } | |
19 // XML Saves location - assumes it will be saves/ | |
20 $saves = glob("../saves/*.xml"); | |
21 $comment_struct = array(); | |
22 if (is_array($saves)) | |
23 { | |
24 foreach($saves as $filename) { | |
25 $xml_string = file_get_contents($filename, FILE_TEXT); | |
26 $xml_object = simplexml_load_string($xml_string); | |
27 if ($xml_object == false) { | |
28 echo "<h1>FATAL</h1> <span>could not parse file ".$filename.": </span>"; | |
29 foreach(libxml_get_errors() as $error) { | |
30 echo "<br>", $error->message; | |
31 } | |
32 } else { | |
33 // Iterate over each audioHolder node | |
34 foreach($xml_object->page as $pageInstance) | |
35 { | |
36 // Find the page in the comment_struct | |
37 $page_struct = null; | |
38 foreach($comment_struct as $comment_struct_page) | |
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 } | |
50 // Get the audioelements of the page | |
51 foreach($pageInstance->audioelement as $fragment) | |
52 { | |
53 // Find the page in the comment_struct | |
54 $element_struct = null; | |
55 foreach($page_struct->elements as $page_struct_element) | |
56 { | |
57 if ($fragment['id'] == $page_struct_element->id) | |
58 { | |
59 $element_struct = $page_struct_element; | |
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); | |
68 } | |
69 } | |
70 } | |
71 } | |
72 // Now we have a sub <xml> containing all comment data | |
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 } | |
143 } else { | |
144 echo "FATAL - No saved XML files discovered"; | |
145 } | |
146 ?> |