annotate scripts/get_tests.php @ 650:ce3d4d6d01b8 Dev_main

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