# HG changeset patch # User Nicholas Jillings # Date 1461074320 -3600 # Node ID 30c012132427a85030fd74a106ebab687470f181 # Parent ae2bf6a1693eca7cab1e3f5d7b0c29ca6f283d08 Implementation for #21 on PHP diff -r ae2bf6a1693e -r 30c012132427 php/pool.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/php/pool.php Tue Apr 19 14:58:40 2016 +0100 @@ -0,0 +1,109 @@ += poolSize. + +// The reference will always point to the original master XML file. + +include 'rel2abs.php'; + +// MODIFY THE FOLLOWING LINE TO POINT TO YOUR TEST FILE +$master_file = "../tests/pool.xml"; +// Note this is relative to the PHP location + +// First set up the store with all the test page key nodes +$pages = []; +$master_xml = simplexml_load_string(file_get_contents($master_file, FILE_TEXT)); +if ($master_xml) { + if (!isset($master_xml->setup["poolSize"])) + { + echo file_get_contents($master_file, FILE_TEXT); + return; + } + $poolSize = $master_xml->setup["poolSize"]; + foreach($master_xml->page as $pageInstance) { + $id = (string)$pageInstance['id']; + $pages[$id] = 0; + } +} + +$waet_url = rel2abs("pool.php","http://".$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']); + +$saves = glob("../saves/*.xml"); +if (is_array($saves)) +{ + foreach($saves as $filename) { + $xml_object = simplexml_load_string(file_get_contents($filename, FILE_TEXT)); + if($xml_object) { + // First we must check the saves match the master URL + $waet = $xml_object->waet[0]; + if (urldecode($waet["url"])==$waet_url) { + // This save is a save from the master XML + // Count which pages have been added + foreach($xml_object->page as $page) { + $id = (string)$page['ref']; + $pages[$id] = $pages[$id] + 1; + } + } + } + } +} + +// Now we have a list of pages, sorted from low to high +// Create the new prototype tree +$orig_doc = new DOMDocument; +$orig_doc->loadXML(file_get_contents($master_file, FILE_TEXT)); +$orig_doc->schemaValidate("../xml/test-schema.xsd"); +$new_doc = new DOMDocument; +$new_doc->formatOutput = true; +// +$root = $new_doc->createElement('waet'); +$root->setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance"); +$root->setAttribute("xsi:noNamespaceSchemaLocation","test-schema.xsd"); +$root = $new_doc->appendChild($root); + +// Copy over the node +$dom_setup = $new_doc->importNode(dom_import_simplexml($master_xml->setup),true); +$root->appendChild($dom_setup); + +// We must now extract the number which have been performed the least +$rot_pages = []; +foreach($pages as $key => $var) + if(array_key_exists($var,$rot_pages)) { + array_push($rot_pages[$var],$key); + } else { + $rot_pages[$var] = [$key]; + } +ksort($rot_pages); +$Keys = array_keys($rot_pages); + +// Pages are grouped into an array based on the number of page initiations ($rot_pages) +// $Keys is an array of the sorted key maps +$pageList = $new_doc->getElementsByTagName("page"); +$iter = 0; +while ($pageList->length < $poolSize) { + if ($iter > 0) { + // We are adding more than one set of pages, make all current always include + foreach($pageList as $page) { + $page->setAttribute("alwaysInclude","true"); + } + } + foreach($rot_pages[$Keys[$iter]] as $pageId) { + // We have the pages to add as a $pageId + // Now COPY + $dom_page = $orig_doc->getElementById($pageId); + $dom_page = $new_doc->importNode($dom_page,true); + $root->appendChild($dom_page); + } + $iter++; + $pageList = $new_doc->getElementsByTagName("page"); +} + +echo $new_doc->saveXML(); + +?> \ No newline at end of file diff -r ae2bf6a1693e -r 30c012132427 php/rel2abs.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/php/rel2abs.php Tue Apr 19 14:58:40 2016 +0100 @@ -0,0 +1,32 @@ +0; $abs=preg_replace($re, '/', $abs, -1, $n)) {} + + /* absolute URL is ready! */ + return $scheme.'://'.$abs; +} +?> \ No newline at end of file diff -r ae2bf6a1693e -r 30c012132427 tests/pool.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/pool.xml Tue Apr 19 14:58:40 2016 +0100 @@ -0,0 +1,69 @@ + + + + + testTimer + elementTimer + elementInitialPosition + elementTracker + elementFlagListenedTo + elementFlagMoved + elementListenTracker + + + + + + + + + (1) Very Annoying + (2) Annoying + (3) Slightly Annoying + (4) Audible but not Annoying + (5) Inaudible + + + + + + + + + (1) Very Annoying + (2) Annoying + (3) Slightly Annoying + (4) Audible but not Annoying + (5) Inaudible + + + + + + + + + (1) Very Annoying + (2) Annoying + (3) Slightly Annoying + (4) Audible but not Annoying + (5) Inaudible + + + + + + + + + (1) Very Annoying + (2) Annoying + (3) Slightly Annoying + (4) Audible but not Annoying + (5) Inaudible + + + + + +