annotate scripts/get_filtered_count.php @ 2212:279733b3b67e

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