annotate php/get_tests.php @ 3141:335bc77627e0 tip

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