annotate php/comment_parser.php @ 2226:43ab418ba4b8

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