annotate php/get_tests.php @ 2913:b7347521a226

Fix Firefox not downloading new test_create pages
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Tue, 01 Aug 2017 14:13:07 +0100
parents 464c6c6692d6
children 53d701288da5
rev   line source
nicholas@2224 1 <?php
nicholas@2457 2 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
nicholas@2457 3 header("Cache-Control: post-check=0, pre-check=0", false);
nicholas@2457 4 header("Pragma: no-cache");
nicholas@2224 5 /*
nicholas@2224 6 Get Tests
nicholas@2224 7
nicholas@2224 8 This script returns the XML test names available, plus the number of tests
nicholas@2224 9 */
nicholas@2224 10
nicholas@2283 11 include "rel2abs.php";
nicholas@2224 12
nicholas@2224 13 // XML Saves location - assumes it will be saves/
nicholas@2283 14 $data = array();
nicholas@2224 15 $saves = glob("../saves/*.xml");
nicholas@2224 16 if (is_array($saves))
nicholas@2224 17 {
nicholas@2224 18 foreach($saves as $filename) {
nicholas@2224 19 $xml_string = file_get_contents($filename, FILE_TEXT);
nicholas@2224 20 $xml_object = simplexml_load_string($xml_string);
nicholas@2224 21 if ($xml_object) {
nicholas@2224 22 $filename = rel2abs($filename,"http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
nicholas@2224 23 $waet = $xml_object->waet[0];
nicholas@2224 24 $testName = urldecode($waet["url"]);
nicholas@2224 25 if(array_key_exists($testName,$data)) {
nicholas@2224 26 // Key exists
nicholas@2224 27 array_push($data[$testName],$filename);
nicholas@2224 28 } else {
nicholas@2224 29 // Key does not exist
nicholas@2283 30 $data[$testName] = array($filename);
nicholas@2224 31 }
nicholas@2224 32 }
nicholas@2224 33 }
nicholas@2224 34 }
nicholas@2224 35
nicholas@2224 36 // Now read the format response
nicholas@2224 37 $format = "JSON";
nicholas@2224 38 if (array_key_exists("format",$_GET)) {
nicholas@2224 39 $format = $_GET["format"];
nicholas@2224 40 }
nicholas@2224 41 switch($format) {
nicholas@2224 42 case "JSON":
nicholas@2224 43 // Return JSON
nicholas@2224 44 $doc_root = '{"tests": [';
nicholas@2224 45 $keys = array_keys($data);
nicholas@2224 46 $numTests = count($data);
nicholas@2224 47 for ($testIndex = 0; $testIndex < $numTests; $testIndex++) {
nicholas@2224 48 $test_root = '{"testName": "'.$keys[$testIndex].'", "files": [';
nicholas@2224 49 $numFiles = count($data[$keys[$testIndex]]);
nicholas@2224 50 for ($countIndex=0; $countIndex < $numFiles; $countIndex++) {
nicholas@2224 51 $test_root = $test_root.'"'.$data[$keys[$testIndex]][$countIndex].'"';
nicholas@2224 52 if ($countIndex == $numFiles-1) {
nicholas@2224 53 $test_root = $test_root.']}';
nicholas@2224 54 } else {
nicholas@2224 55 $test_root = $test_root.',';
nicholas@2224 56 }
nicholas@2224 57 }
nicholas@2224 58 $doc_root = $doc_root.$test_root;
nicholas@2224 59 if ($testIndex == $numTests-1) {
nicholas@2224 60 $doc_root = $doc_root.']}';
nicholas@2224 61 } else {
nicholas@2224 62 $doc_root = $doc_root.',';
nicholas@2224 63 }
nicholas@2224 64 }
nicholas@2224 65 echo $doc_root;
nicholas@2224 66 break;
nicholas@2224 67 default:
nicholas@2224 68 echo '{"error": "format can only be JSON"}';
nicholas@2224 69 }
nicholas@2224 70
nicholas@2538 71 ?>