annotate php/pool.php @ 3141:335bc77627e0 tip

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