annotate scripts/comment_parser.php @ 630:9dcfd654abad Dev_main

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