comparison php/get_tests.php @ 2224:760719986df3

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