annotate scripts/get_tests.php @ 1925:788a9af36b66

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