annotate scripts/comment_parser.php @ 1090:c07b9e2312ba

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