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