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