comparison new/php/get_tests.php @ 15:853caf8cd74b

Update
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 04 May 2016 17:25:19 +0100
parents
children
comparison
equal deleted inserted replaced
14:f63604ce8f21 15:853caf8cd74b
1 <?php
2 /*
3 Get Tests
4
5 This script returns the XML test names available, plus the number of tests
6 */
7
8 include "rel2abs.php";
9
10 // XML Saves location - assumes it will be saves/
11 $data = array();
12 $saves = glob("../saves/*.xml");
13 if (is_array($saves))
14 {
15 foreach($saves as $filename) {
16 $xml_string = file_get_contents($filename, FILE_TEXT);
17 $xml_object = simplexml_load_string($xml_string);
18 if ($xml_object) {
19 $filename = rel2abs($filename,"http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
20 $waet = $xml_object->waet[0];
21 $testName = urldecode($waet["url"]);
22 if(array_key_exists($testName,$data)) {
23 // Key exists
24 array_push($data[$testName],$filename);
25 } else {
26 // Key does not exist
27 $data[$testName] = array($filename);
28 }
29 }
30 }
31 }
32
33 // Now read the format response
34 $format = "JSON";
35 if (array_key_exists("format",$_GET)) {
36 $format = $_GET["format"];
37 }
38 switch($format) {
39 case "JSON":
40 // Return JSON
41 $doc_root = '{"tests": [';
42 $keys = array_keys($data);
43 $numTests = count($data);
44 for ($testIndex = 0; $testIndex < $numTests; $testIndex++) {
45 $test_root = '{"testName": "'.$keys[$testIndex].'", "files": [';
46 $numFiles = count($data[$keys[$testIndex]]);
47 for ($countIndex=0; $countIndex < $numFiles; $countIndex++) {
48 $test_root = $test_root.'"'.$data[$keys[$testIndex]][$countIndex].'"';
49 if ($countIndex == $numFiles-1) {
50 $test_root = $test_root.']}';
51 } else {
52 $test_root = $test_root.',';
53 }
54 }
55 $doc_root = $doc_root.$test_root;
56 if ($testIndex == $numTests-1) {
57 $doc_root = $doc_root.']}';
58 } else {
59 $doc_root = $doc_root.',';
60 }
61 }
62 echo $doc_root;
63 break;
64 default:
65 echo '{"error": "format can only be JSON"}';
66 }
67
68 ?>