annotate scripts/get_filtered_count.php @ 1925:788a9af36b66

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