annotate scripts/get_tests.php @ 2202:61c8e13f1e2e

Merge branch 'master' of https://github.com/BrechtDeMan/WebAudioEvaluationTool
author www-data <www-data@sucuk.dcs.qmul.ac.uk>
date Fri, 08 Apr 2016 13:20:54 +0100
parents 6c819878ac85
children
rev   line source
n@1284 1 <?php
n@1284 2 /*
n@1284 3 Get Tests
n@1284 4
n@1284 5 This script returns the XML test names available, plus the number of tests
n@1284 6 */
n@1284 7
n@1284 8 //http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php
n@1284 9 function rel2abs($rel, $base)
n@1284 10 {
n@1284 11 /* return if already absolute URL */
n@1284 12 if (parse_url($rel, PHP_URL_SCHEME) != '' || substr($rel, 0, 2) == '//') return $rel;
n@1284 13
n@1284 14 /* queries and anchors */
n@1284 15 if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
n@1284 16
n@1284 17 /* parse base URL and convert to local variables:
n@1284 18 $scheme, $host, $path */
n@1284 19 extract(parse_url($base));
n@1284 20
n@1284 21 /* remove non-directory element from path */
n@1284 22 $path = preg_replace('#/[^/]*$#', '', $path);
n@1284 23
n@1284 24 /* destroy path if relative url points to root */
n@1284 25 if ($rel[0] == '/') $path = '';
n@1284 26
n@1284 27 /* dirty absolute URL */
n@1284 28 $abs = "$host$path/$rel";
n@1284 29
n@1284 30 /* replace '//' or '/./' or '/foo/../' with '/' */
n@1284 31 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
n@1284 32 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
n@1284 33
n@1284 34 /* absolute URL is ready! */
n@1284 35 return $scheme.'://'.$abs;
n@1284 36 }
n@1284 37
n@1284 38 // XML Saves location - assumes it will be saves/
n@1284 39 $data = [];
n@1284 40 $saves = glob("../saves/*.xml");
n@1284 41 if (is_array($saves))
n@1284 42 {
n@1284 43 foreach($saves as $filename) {
n@1284 44 $xml_string = file_get_contents($filename, FILE_TEXT);
n@1284 45 $xml_object = simplexml_load_string($xml_string);
n@1284 46 if ($xml_object) {
n@1284 47 $filename = rel2abs($filename,"http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
n@1284 48 $waet = $xml_object->waet[0];
n@1284 49 $testName = urldecode($waet["url"]);
n@1284 50 if(array_key_exists($testName,$data)) {
n@1284 51 // Key exists
n@1284 52 array_push($data[$testName],$filename);
n@1284 53 } else {
n@1284 54 // Key does not exist
n@1284 55 $data[$testName] = [$filename];
n@1284 56 }
n@1284 57 }
n@1284 58 }
n@1284 59 }
n@1284 60
n@1284 61 // Now read the format response
n@1284 62 $format = "JSON";
n@1284 63 if (array_key_exists("format",$_GET)) {
n@1284 64 $format = $_GET["format"];
n@1284 65 }
n@1284 66 switch($format) {
n@1284 67 case "JSON":
n@1284 68 // Return JSON
n@1284 69 $doc_root = '{"tests": [';
n@1284 70 $keys = array_keys($data);
n@1284 71 $numTests = count($data);
n@1284 72 for ($testIndex = 0; $testIndex < $numTests; $testIndex++) {
n@1284 73 $test_root = '{"testName": "'.$keys[$testIndex].'", "files": [';
n@1284 74 $numFiles = count($data[$keys[$testIndex]]);
n@1284 75 for ($countIndex=0; $countIndex < $numFiles; $countIndex++) {
n@1284 76 $test_root = $test_root.'"'.$data[$keys[$testIndex]][$countIndex].'"';
n@1284 77 if ($countIndex == $numFiles-1) {
n@1284 78 $test_root = $test_root.']}';
n@1284 79 } else {
n@1284 80 $test_root = $test_root.',';
n@1284 81 }
n@1284 82 }
n@1284 83 $doc_root = $doc_root.$test_root;
n@1284 84 if ($testIndex == $numTests-1) {
n@1284 85 $doc_root = $doc_root.']}';
n@1284 86 } else {
n@1284 87 $doc_root = $doc_root.',';
n@1284 88 }
n@1284 89 }
n@1284 90 echo $doc_root;
n@1284 91 break;
n@1284 92 default:
n@1284 93 echo '{"error": "format can only be JSON"}';
n@1284 94 }
n@1284 95
n@1284 96 ?>