annotate new/php/pool.php @ 15:853caf8cd74b

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