annotate scripts/get_filtered_count.php @ 2202:61c8e13f1e2e

Merge branch 'master' of https://github.com/BrechtDeMan/WebAudioEvaluationTool
author www-data <www-data@sucuk.dcs.qmul.ac.uk>
date Fri, 08 Apr 2016 13:20:54 +0100
parents 6c819878ac85
children
rev   line source
n@1284 1 <?php
n@1284 2 //http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php
n@1284 3 function rel2abs($rel, $base)
n@1284 4 {
n@1284 5 /* return if already absolute URL */
n@1284 6 if (parse_url($rel, PHP_URL_SCHEME) != '' || substr($rel, 0, 2) == '//') return $rel;
n@1284 7
n@1284 8 /* queries and anchors */
n@1284 9 if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
n@1284 10
n@1284 11 /* parse base URL and convert to local variables:
n@1284 12 $scheme, $host, $path */
n@1284 13 extract(parse_url($base));
n@1284 14
n@1284 15 /* remove non-directory element from path */
n@1284 16 $path = preg_replace('#/[^/]*$#', '', $path);
n@1284 17
n@1284 18 /* destroy path if relative url points to root */
n@1284 19 if ($rel[0] == '/') $path = '';
n@1284 20
n@1284 21 /* dirty absolute URL */
n@1284 22 $abs = "$host$path/$rel";
n@1284 23
n@1284 24 /* replace '//' or '/./' or '/foo/../' with '/' */
n@1284 25 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
n@1284 26 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
n@1284 27
n@1284 28 /* absolute URL is ready! */
n@1284 29 return $scheme.'://'.$abs;
n@1284 30 }
n@1284 31
n@1284 32 /*
n@1284 33 This looks for files that pass the filtering response
n@1284 34 The filtering system uses key-value pairs
n@1284 35 The key is double encoded using a '-'. The first part is the ID of the item to filter,
n@1284 36 the second is the method:
n@1284 37 min - Minimum Inclusive
n@1284 38 max - Maximum Inclusive
n@1284 39 exclude-# - exclude, followed by a number to uniquely add, (will create a triple [], ignore the third as random)
n@1284 40 */
n@1284 41 $keys = [];
n@1284 42 $waet_url = null;
n@1284 43 foreach ($_GET as $key => $value) {
n@1284 44 $key = explode("-",$key);
n@1284 45 if ($key[0] == "url") {
n@1284 46 $waet_url = $value;
n@1284 47 } else {
n@1284 48 $v_pair = [$key[1],$value];
n@1284 49 if(array_key_exists($key[0],$keys)) {
n@1284 50 // We have some data
n@1284 51 array_push($keys[$key[0]],$v_pair);
n@1284 52 } else {
n@1284 53 // Create new key data
n@1284 54 $keys[$key[0]] = [$v_pair];
n@1284 55 }
n@1284 56 }
n@1284 57 }
n@1284 58
n@1284 59 $files = [];
n@1284 60 $saves = glob("../saves/*.xml");
n@1284 61 if (is_array($saves))
n@1284 62 {
n@1284 63 foreach($saves as $filename) {
n@1284 64 $xml_string = file_get_contents($filename, FILE_TEXT);
n@1284 65 $xml_object = simplexml_load_string($xml_string);
n@1284 66 if ($xml_object) {
n@1284 67 // First we must check the URLs match
n@1284 68 $waet = $xml_object->waet[0];
n@1284 69 if (urldecode($waet["url"])==$waet_url) {
n@1284 70 // It is part of the dataset, so now perform checks
n@1284 71 $continue = true;
n@1284 72 foreach($keys as $keyId => $keyArr) {
n@1284 73 $elem = $xml_object->xpath("//*[@ref='".$keyId."']");
n@1284 74 $elem = $elem[0]; // Can only be one.
n@1284 75 switch ($elem["type"]) {
n@1284 76 case "number":
n@1284 77 // Number, we must check for min/max
n@1284 78 $value = (real)$elem->response;
n@1284 79 foreach ($keyArr as $keyCheck) {
n@1284 80 if ($keyCheck[0] == 'min' && $value < $keyCheck[1]) {
n@1284 81 $continue = false;
n@1284 82 break;
n@1284 83 } else if ($keyCheck[0] == 'max' && $value > $keyCheck[1]) {
n@1284 84 $continue = false;
n@1284 85 break;
n@1284 86 }
n@1284 87 }
n@1284 88 break;
n@1284 89 case "checkbox":
n@1284 90 // Will have an array of <response>
n@1284 91 foreach ($elem->response as $response) {
n@1284 92 foreach ($keyArr as $keyCheck) {
n@1284 93 if ($response["name"] == $keyCheck[1]) {
n@1284 94 if($response["checked"] == "true" && $keyCheck[0] == "exclude") {
n@1284 95 $continue = false;
n@1284 96 break;
n@1284 97 }
n@1284 98 }
n@1284 99 }
n@1284 100 if($continue == false) {
n@1284 101 break;
n@1284 102 }
n@1284 103 }
n@1284 104 break;
n@1284 105 case "radio":
n@1284 106 foreach ($keyArr as $keyCheck) {
n@1284 107 if ($keyCheck[0] == "exclude" && $elem->response["name"] == $keyCheck[1]) {
n@1284 108
n@1284 109 $continue = false;
n@1284 110 break;
n@1284 111 }
n@1284 112 }
n@1284 113 break;
n@1284 114 default:
n@1284 115 break;
n@1284 116 }
n@1284 117 if ($continue == false) {
n@1284 118 break;
n@1284 119 }
n@1284 120 }
n@1284 121 if ($continue) {
n@1284 122 array_push($files,rel2abs($filename,"http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']));
n@1284 123 }
n@1284 124 }
n@1284 125 }
n@1284 126 }
n@1284 127 }
n@1284 128 if (count($files) == 0) {
n@1284 129 echo '{"urls": []}';
n@1284 130 } else {
n@1284 131 echo '{"urls": ["'.implode('","',$files).'"]}';
n@1284 132 }
n@1284 133
n@1284 134 ?>