annotate php/pool.php @ 2922:7fa175a37aca master

Merge branch 'master' of https://github.com/BrechtDeMan/WebAudioEvaluationTool
author www-data <www-data@sucuk.dcs.qmul.ac.uk>
date Thu, 03 Aug 2017 18:24:46 +0100
parents 464c6c6692d6
children 53d701288da5
rev   line source
nicholas@2249 1 <?php
nicholas@2457 2 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
nicholas@2457 3 header("Cache-Control: post-check=0, pre-check=0", false);
nicholas@2457 4 header("Pragma: no-cache");
nicholas@2249 5 // This works out the pool of pages to force the browser to use from the master pool set by 'modifying' the XML file
nicholas@2249 6 //
nicholas@2249 7
nicholas@2249 8 // This scripts lists all participated pages and ranks them by lowest occurence first
nicholas@2249 9 // The script then removes from the list any pages which have been completed more times than the lowest occuring test page
nicholas@2249 10 // If the number of pages left is less than the number of pages requested from the poolSize, then those that were
nicholas@2249 11 // selected will have the alwaysInclude attribute set to true to ensure the next iteration will be selected. Then the next
nicholas@2249 12 // least occuring pages are re-added. This continues until number of testPages >= poolSize.
nicholas@2249 13
nicholas@2249 14 // The reference will always point to the original master XML file.
nicholas@2249 15
nicholas@2249 16 include 'rel2abs.php';
nicholas@2249 17
nicholas@2249 18 // MODIFY THE FOLLOWING LINE TO POINT TO YOUR TEST FILE
nicholas@2249 19 $master_file = "../tests/pool.xml";
nicholas@2249 20 // Note this is relative to the PHP location
nicholas@2249 21
nicholas@2249 22 // First set up the store with all the test page key nodes
nicholas@2283 23 $pages = array();
nicholas@2249 24 $master_xml = simplexml_load_string(file_get_contents($master_file, FILE_TEXT));
nicholas@2249 25 if ($master_xml) {
nicholas@2249 26 if (!isset($master_xml->setup["poolSize"]))
nicholas@2249 27 {
nicholas@2249 28 echo file_get_contents($master_file, FILE_TEXT);
nicholas@2249 29 return;
nicholas@2249 30 }
nicholas@2249 31 $poolSize = $master_xml->setup["poolSize"];
nicholas@2249 32 foreach($master_xml->page as $pageInstance) {
nicholas@2249 33 $id = (string)$pageInstance['id'];
nicholas@2249 34 $pages[$id] = 0;
nicholas@2249 35 }
nicholas@2249 36 }
nicholas@2249 37
nicholas@2249 38 $waet_url = rel2abs("pool.php","http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
nicholas@2249 39
nicholas@2249 40 $saves = glob("../saves/*.xml");
nicholas@2249 41 if (is_array($saves))
nicholas@2249 42 {
nicholas@2249 43 foreach($saves as $filename) {
nicholas@2249 44 $xml_object = simplexml_load_string(file_get_contents($filename, FILE_TEXT));
nicholas@2249 45 if($xml_object) {
nicholas@2249 46 // First we must check the saves match the master URL
nicholas@2249 47 $waet = $xml_object->waet[0];
nicholas@2249 48 if (urldecode($waet["url"])==$waet_url) {
nicholas@2249 49 // This save is a save from the master XML
nicholas@2249 50 // Count which pages have been added
nicholas@2249 51 foreach($xml_object->page as $page) {
nicholas@2249 52 $id = (string)$page['ref'];
nicholas@2249 53 $pages[$id] = $pages[$id] + 1;
nicholas@2249 54 }
nicholas@2249 55 }
nicholas@2249 56 }
nicholas@2249 57 }
nicholas@2249 58 }
nicholas@2249 59
nicholas@2249 60 // Now we have a list of pages, sorted from low to high
nicholas@2249 61 // Create the new prototype tree
nicholas@2249 62 $orig_doc = new DOMDocument;
nicholas@2249 63 $orig_doc->loadXML(file_get_contents($master_file, FILE_TEXT));
nicholas@2249 64 $orig_doc->schemaValidate("../xml/test-schema.xsd");
nicholas@2249 65 $new_doc = new DOMDocument;
nicholas@2249 66 $new_doc->formatOutput = true;
nicholas@2249 67 //<waet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test-schema.xsd">
nicholas@2249 68 $root = $new_doc->createElement('waet');
nicholas@2249 69 $root->setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
nicholas@2249 70 $root->setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd");
nicholas@2249 71 $root = $new_doc->appendChild($root);
nicholas@2249 72
nicholas@2249 73 // Copy over the <setup> node
nicholas@2249 74 $dom_setup = $new_doc->importNode(dom_import_simplexml($master_xml->setup),true);
nicholas@2249 75 $root->appendChild($dom_setup);
nicholas@2249 76
nicholas@2249 77 // We must now extract the number which have been performed the least
nicholas@2283 78 $rot_pages = array();
nicholas@2249 79 foreach($pages as $key => $var)
nicholas@2249 80 if(array_key_exists($var,$rot_pages)) {
nicholas@2249 81 array_push($rot_pages[$var],$key);
nicholas@2249 82 } else {
nicholas@2283 83 $rot_pages[$var] = array($key);
nicholas@2249 84 }
nicholas@2249 85 ksort($rot_pages);
nicholas@2249 86 $Keys = array_keys($rot_pages);
nicholas@2249 87
nicholas@2249 88 // Pages are grouped into an array based on the number of page initiations ($rot_pages)
nicholas@2249 89 // $Keys is an array of the sorted key maps
nicholas@2249 90 $pageList = $new_doc->getElementsByTagName("page");
nicholas@2249 91 $iter = 0;
nicholas@2249 92 while ($pageList->length < $poolSize) {
nicholas@2249 93 if ($iter > 0) {
nicholas@2249 94 // We are adding more than one set of pages, make all current always include
nicholas@2249 95 foreach($pageList as $page) {
nicholas@2249 96 $page->setAttribute("alwaysInclude","true");
nicholas@2249 97 }
nicholas@2249 98 }
nicholas@2249 99 foreach($rot_pages[$Keys[$iter]] as $pageId) {
nicholas@2249 100 // We have the pages to add as a $pageId
nicholas@2249 101 // Now COPY
nicholas@2249 102 $dom_page = $orig_doc->getElementById($pageId);
nicholas@2249 103 $dom_page = $new_doc->importNode($dom_page,true);
nicholas@2249 104 $root->appendChild($dom_page);
nicholas@2249 105 }
nicholas@2249 106 $iter++;
nicholas@2249 107 $pageList = $new_doc->getElementsByTagName("page");
nicholas@2249 108 }
nicholas@2249 109
nicholas@2249 110 echo $new_doc->saveXML();
nicholas@2249 111
nicholas@2538 112 ?>