annotate scripts/get_filtered_count.php @ 650:ce3d4d6d01b8 Dev_main

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