annotate php/comment_parser.php @ 3141:335bc77627e0 tip

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