changeset 522:d85e219a5eeb Dev_main

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 2bde74ae8396
children d753dfb040bf
files scripts/comment_parser.html scripts/comment_parser.php
diffstat 2 files changed, 187 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/comment_parser.html	Wed Feb 17 17:27:29 2016 +0000
@@ -0,0 +1,72 @@
+<html lang="en">
+    <head>
+        <meta charset="utf-8" />
+        <script type="text/javascript">
+            function getXML()
+            {
+                var XMLHttp = new XMLHttpRequest();
+                XMLHttp.open("GET","comment_parser.php?format=XML",true);
+                XMLHttp.onload = function() {
+                    // Now we have the XML data, extract
+                    var parse = new DOMParser();
+                    var ajax = parse.parseFromString(XMLHttp.response,'text/xml');
+                    
+                    var parent = document.createElement("div");
+                    parent.appendChild(ajax.children[0]);
+                    var file = [parent.innerHTML];
+                    var bb = new Blob(file,{type : 'application/xml'});
+                    generateLink(bb,".xml");
+                }
+                XMLHttp.send();
+            }
+            
+            function getJSON()
+            {
+                var XMLHttp = new XMLHttpRequest();
+                XMLHttp.open("GET","comment_parser.php?format=JSON",true);
+                XMLHttp.onload = function() {
+                    // Now we have the XML data, extract
+                    var file = [XMLHttp.response];
+                    var bb = new Blob(file,{type : 'application/json'});
+                    generateLink(bb,".json");
+                }
+                XMLHttp.send();
+            }
+            
+            function getCSV()
+            {
+                var XMLHttp = new XMLHttpRequest();
+                XMLHttp.open("GET","comment_parser.php?format=CSV",true);
+                XMLHttp.onload = function() {
+                    // Now we have the XML data, extract
+                    var file = [XMLHttp.response];
+                    var bb = new Blob(file,{type : 'text/csv'});
+                    generateLink(bb,".csv");
+                }
+                XMLHttp.send();
+            }
+            
+            function generateLink(blobfile,fmt)
+            {
+                var dnlk = window.URL.createObjectURL(blobfile);
+                var a = document.createElement("a");
+                a.hidden = '';
+                a.href = dnlk;
+                a.download = "save"+fmt;
+                a.textContent = "Save File";
+                document.getElementById("download").appendChild(a);
+            }
+        </script>
+    </head>
+    <body>
+        <h1>WAET Test Results Analysis</h1>
+        <h2>Comment Extraction</h2>
+        <p>All of the XMLs in the server 'saves/' directory are automatically parsed and downloaded, extracting only the comments. Simply select the comments you wish to extract below and your desired data format.</p>
+        <div id="download"></div>
+        <div>
+            <button onclick="getXML();">XML</button>
+            <button onclick="getJSON();">JSON</button>
+            <button onclick="getCSV();">CSV</button>
+        </div>
+    </body>
+</html>
\ No newline at end of file
--- a/scripts/comment_parser.php	Wed Feb 17 12:10:55 2016 +0000
+++ b/scripts/comment_parser.php	Wed Feb 17 17:27:29 2016 +0000
@@ -1,15 +1,29 @@
 <?php
 // Comment Parser for PHP
+class audioElement {
+    function __construct($id) {
+        $this->id = $id;
+        $this->comments = array();
+    }
+    function addComment($str) {
+        array_push($this->comments,$str);
+    }
+}
 
+class testPage {
+    function __construct($id) {
+        $this->id = $id;
+        $this->elements = array();
+    }
+}
 // XML Saves location - assumes it will be saves/
 $saves = glob("../saves/*.xml");
-$comment_struct = new SimpleXMLElement('<waetprocess/>');
+$comment_struct = array();
 if (is_array($saves))
 {
     foreach($saves as $filename) {
         $xml_string = file_get_contents($filename, FILE_TEXT);
         $xml_object = simplexml_load_string($xml_string);
-        $test_struct = $comment_struct->addChild("test");
         if ($xml_object == false) {
             echo "<h1>FATAL</h1> <span>could not parse file ".$filename.": </span>";
             foreach(libxml_get_errors() as $error) {
@@ -19,27 +33,113 @@
             // Iterate over each audioHolder node
             foreach($xml_object->page as $pageInstance)
             {
-                $page_struct = $test_struct->addChild("page");
-                // Get the page-id and attach
-                $page_struct->addAttribute("page-id",$pageInstance['id']);
-                
+                // Find the page in the comment_struct
+                $page_struct = null;
+                foreach($comment_struct as $comment_struct_page)
+                {
+                    if ($pageInstance['id'] == $comment_struct_page->id)
+                    {
+                        $page_struct = $comment_struct_page;
+                        break;
+                    }
+                }
+                if ($page_struct == null) {
+                    array_push($comment_struct,new testPage($pageInstance['id']));
+                    $page_struct = $comment_struct[count($comment_struct)-1];
+                }
                 // Get the audioelements of the page
                 foreach($pageInstance->audioelement as $fragment)
                 {
-                    $fragment_struct = $page_struct->addChild("audioelement");
-                    // Get the element-id and attach
-                    $page_struct->addAttribute("element-id",$fragment['id']);
-                    $page_struct->addAttribute("presented-id",$fragment['presentedId']);
-                    $page_struct->addAttribute("url",$fragment['url']);
-                    // Append the comment data
-                    echo "<p>Comment: ".$fragement->comment."</p>";
-                    $comment = $fragment_struct->addChild("comment");
+                    // Find the page in the comment_struct
+                    $element_struct = null;
+                    foreach($page_struct->elements as $page_struct_element)
+                    {
+                        if ($fragment['id'] == $page_struct_element->id)
+                        {
+                            $element_struct = $page_struct_element;
+                            break;
+                        }
+                    }
+                    if ($element_struct == null) {
+                        array_push($page_struct->elements,new audioElement($fragment['id']));
+                        $element_struct = $page_struct->elements[count($page_struct->elements)-1];
+                    }
+                    $element_struct->addComment($fragment->comment->response);
                 }
             }
         }
     }
     // Now we have a sub <xml> containing all comment data
-    echo $comment_struct->asXML();
+    switch($_GET['format']) {
+        case "XML":
+            // Convert to an XML
+            $doc_struct = new SimpleXMLElement('<waetprocess/>');
+            foreach($comment_struct as $page_struct)
+            {
+                $doc_page = $doc_struct->addChild("page");
+                $doc_page->addAttribute("id",$page_struct->id);
+                foreach($page_struct->elements as $element_struct)
+                {
+                    $doc_element = $doc_page->addChild("audioelement");
+                    $doc_element->addAttribute("id",$element_struct->id);
+                    foreach($element_struct->comments as $comment)
+                    {
+                        $doc_comment = $doc_element->addChild("comment",$comment);
+                    }
+                }
+            }
+            echo $doc_struct->asXML();
+            break;
+        case "JSON":
+            // Convert to JSON
+            $doc_string = '{ "pages": [';
+            for($page_index = 0; $page_index < count($comment_struct); $page_index++ )
+            {
+                $page_struct = $comment_struct[$page_index];
+                $doc_page = '{"id": "'.$page_struct->id.'", "elements": [';
+                for($element_index = 0; $element_index < count($page_struct->elements); $element_index++ )
+                {
+                    $element_struct = $page_struct->elements[$element_index];
+                    $doc_element = '{"id": "'.$element_struct->id.'", "comments": [';
+                    for($comment_index = 0; $comment_index < count($element_struct->comments); $comment_index++ )
+                    {
+                        $doc_comment = '"'.$element_struct->comments[$comment_index].'"';
+                        if ($comment_index < count($element_struct->comments)-1) {
+                            $doc_comment = $doc_comment.',';
+                        }
+                        $doc_element = $doc_element.$doc_comment;
+                    }
+                    $doc_element = $doc_element.']}';
+                    if ($element_index < count($page_struct->elements)-1) {
+                        $doc_element = $doc_element.',';
+                    }
+                    $doc_page = $doc_page.$doc_element;
+                }
+                $doc_page = $doc_page.']}';
+                if ($page_index < count($comment_struct)-1) {
+                    $doc_page = $doc_page.',';
+                }
+                $doc_string = $doc_string.$doc_page;
+            }
+            $doc_string = $doc_string."]}";
+            echo $doc_string;
+            break;
+        case "CSV":
+            // Conver to CSV
+            // The CSV has three columns: page, element, comment
+            $doc_string = "page,element,comment"."\r\n";
+            foreach($comment_struct as $page_struct)
+            {
+                foreach($page_struct->elements as $element_struct)
+                {
+                    foreach($element_struct->comments as $comment)
+                    {
+                        $doc_string = $doc_string.$page_struct->id.",".$element_struct->id.",".$comment."\r\n";
+                    }
+                }
+            }
+            echo $doc_string;
+    }
 } else {
     echo "FATAL - No saved XML files discovered";
 }