annotate scripts/get_tests.php @ 2212:279733b3b67e

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