PageRenderTime 21ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/php/main/metadata/doc/testMiniDom.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 241 lines | 156 code | 72 blank | 13 comment | 1 complexity | 32f98de452a78a5f74bac04497d13e06 MD5 | raw file
  1. <?php
  2. /**
  3. * testMiniDom.php
  4. * 2005/03/16
  5. * for XML MiniDom, 2005/03/16
  6. * @copyright (C) 2005 rene.haentjens@UGent.be
  7. * @package chamilo.metadata
  8. */
  9. /**
  10. * Chamilo Metadata: XMD test and demo
  11. */
  12. function file_get_contents_n($filename) // normalize \r and \r\n to \n
  13. {
  14. $fp = fopen($filename, 'rb');
  15. $buffer = fread($fp, filesize($filename));
  16. fclose($fp); // note file_get_contents is >= PHP 4.3.0
  17. return str_replace("\r", "\n", str_replace("\r\n", "\n", $buffer));
  18. }
  19. require("../../inc/lib/xmd.lib.php");
  20. $testdoc = new xmddoc('<docroot/>'); // docroot is element 0
  21. function showDoc($title, $morestuff = '')
  22. {
  23. global $testdoc; echo '<h4>', $title, '</h4>', '<pre>',
  24. htmlspecialchars($morestuff ? $morestuff : $testdoc->xmd_xml()), '</pre>';
  25. }
  26. $sometag1 = $testdoc->xmd_add_element('sometag');
  27. $testdoc->xmd_set_attribute(0, 'owner', 'rene');
  28. $testdoc->xmd_add_text('text in my first child element', $sometag1);
  29. showDoc('Small XML document');
  30. $sometag2 = $testdoc->xmd_add_element('sometag', 0, array('x' => 'somevalue'));
  31. $testdoc->xmd_add_text('bizarre <text> in "my& 2nd child', $sometag2);
  32. $testdoc->xmd_add_text(' + more text in first one', $sometag1);
  33. $testdoc->xmd_set_attribute($sometag2, 'owner', '<c&a">');
  34. $testdoc->xmd_add_element('innertag', $sometag2);
  35. showDoc('Slightly changed');
  36. showDoc('All text', $testdoc->xmd_text());
  37. $stuff = '';
  38. foreach ($testdoc->xmd_get_element($sometag2) as $key => $value)
  39. $stuff .= $key . ': ' . $value . "\n";
  40. showDoc('Children, attributes, name and parent of 2nd sometag', $stuff);
  41. $testdoc->xmd_remove_nodes('text in my first child element', $sometag1);
  42. // note: remove text may remove more than one node...
  43. $testdoc->xmd_set_attribute(0, 'owner', 'haentjens'); // new value
  44. showDoc('Text removed from 1st sometag, docroot owner changed');
  45. $testdoc->xmd_remove_element($sometag2);
  46. $sometag2 = $testdoc->xmd_add_text_element('��', 'alors!');
  47. showDoc('2nd sometag replaced by new subelement with French name');
  48. $testdoc->name[$sometag2] = 'sometag'; // properties are read/write
  49. $testdoc->xmd_set_attribute($sometag2, 'xmlns:tn', 'urn:ugent-be'); // namesp def
  50. $subtag = $testdoc->xmd_add_element('urn:ugent-be:subtag', $sometag2);
  51. $testdoc->xmd_set_attribute($sometag2, 'urn:ugent-be:owner', 'FTW');
  52. showDoc('French name replaced, namespace definition added and used');
  53. $testdoc->xmd_set_attribute($sometag1, 'urn:ugent-be:owner', 'FTW');
  54. $testdoc->xmd_set_attribute($sometag1, 'urn:rug-ac-be:owner2', 'FLA');
  55. // restriction: cannot add attribute 'urn:rug-ac-be:owner' (same name)
  56. showDoc('Attributes with namespaces added, ns def is auto-generated');
  57. $stuff = 'subtag => ' . $testdoc->xmd_get_ns_uri($subtag) . "\n";
  58. foreach ($testdoc->attributes[$sometag1] as $name => $value)
  59. $stuff .= $name . ' => ' . $testdoc->xmd_get_ns_uri($sometag1, $name) . "\n";
  60. showDoc('Namespace-URI of subtag, of 1st sometag attributes', $stuff);
  61. $subsub = $testdoc->xmd_add_element('urn:sample-default:subsub', $subtag,
  62. array('xmlns' => 'urn:sample-default', 'someatt' => 'somevalue'));
  63. $subsubsub = $testdoc->xmd_add_element('urn:sample-default:subsubsub', $subsub);
  64. showDoc('Subsub element has default namespace');
  65. $stuff = 'subsub => ' . $testdoc->xmd_get_ns_uri($subsub) . "\n";
  66. $stuff .= 'subsubsub => ' . $testdoc->xmd_get_ns_uri($subsubsub) . "\n";
  67. foreach ($testdoc->attributes[$subsub] as $name => $value)
  68. $stuff .= $name . ' => ' . $testdoc->xmd_get_ns_uri($subsub, $name) . "\n";
  69. showDoc('Namespace-URI of subsub and subsubsub; attributes have none', $stuff);
  70. $testdoc->xmd_update('!newtag', 'text for newtag');
  71. showDoc("After update '!newtag', 'text for newtag'");
  72. $testdoc->xmd_update('newtag', 'new text for newtag');
  73. showDoc("After update 'newtag', 'new text for newtag'");
  74. $testdoc->xmd_update('newtag/@someatt', 'attval');
  75. showDoc("After update 'newtag/@someatt', 'attval'");
  76. $testdoc->xmd_update('newtag/~', '');
  77. showDoc("After update 'newtag/~', ''");
  78. $keepdoc = $testdoc;
  79. $wrongdoc = "<html>\n <body>\n <p>Text</p>\n <p>More text" .
  80. "\n </body>\n</html>";
  81. $testdoc = new xmddoc(explode("\n", $wrongdoc));
  82. showDoc('Xml doc with syntax error + error message',
  83. $wrongdoc . "\n\n" . $testdoc->error);
  84. $xmlFile = 'imsmanifest_reload.xml';
  85. ($presXmlFileContents = @file_get_contents_n($xmlFile))
  86. or die('XML file ' . htmlspecialchars($xmlFile) . ' is missing...');
  87. showDoc('XML file to be parsed', $presXmlFileContents);
  88. $testdoc = new xmddoc(explode("\n", $presXmlFileContents));
  89. unset($presXmlFileContents);
  90. if ($testdoc->error) die($xmlFile . ':<br><br>' . $testdoc->error);
  91. $testdoc->xmd_update_many('metadata/lom/general/title,metadata/lom/general/description', 'langstring/@lang', 'fr');
  92. $testdoc->xmd_copy_foreign_child($keepdoc, $keepdoc->xmd_select_single_element('sometag[2]'));
  93. showDoc('After parsing, and after changing 2* langstring/@lang to fr, ' .
  94. 'and after adding a foreign doc, reconstruction from memory');
  95. showDoc('Element tagname of first metadata/lom/* element',
  96. $testdoc->name[$testdoc->xmd_select_single_element('metadata/lom/*')]);
  97. showDoc('Element namespace URI of metadata/lom/*[2]',
  98. $testdoc->xmd_get_ns_uri($testdoc->xmd_select_single_element('metadata/lom/*[2]')));
  99. showDoc('Number of metadata/lom/* elements',
  100. count($testdoc->xmd_select_elements('metadata/lom/*')));
  101. showDoc('Number of resources/resource/file elements with @href',
  102. count($testdoc->xmd_select_elements_where_notempty(
  103. 'resources/resource/file', '@href')));
  104. $elems = $testdoc->xmd_select_elements_where('resources/resource',
  105. 'file[1]/@href', 'three.html');
  106. showDoc('Resource identifier where file[1]/@href is three.html',
  107. $testdoc->xmd_value('@identifier', $elems[0]));
  108. $elems = $testdoc->xmd_select_elements_where('resources/resource', '@identifier',
  109. $testdoc->xmd_value('organizations/organization/item[2]/@identifierref'));
  110. showDoc('Resource href for item[2]',
  111. $testdoc->xmd_value('@href', $elems[0]));
  112. $stuff = '';
  113. foreach (array('@identifier', 'metadata/schema', '*/*/*/*[1]/langstring',
  114. 'resources/resource[3]/@href', 'resources/resource[3]/file/@href',
  115. 'resources/resource[3]/@*', 'resources/resource[3]/-/@href',
  116. 'resources/resource[3]/+/@href', 'resources/resource[1]/-/@href',
  117. 'resources/../../../../../../../@identifier', '@*', 'resources/@*',
  118. 'organizations/organization/item[4]/title',
  119. 'organizations/organization/item[-2]/title',
  120. 'organizations/organization/item[4]/@*',
  121. 'organizations/organization/item[4]/@*item',
  122. 'organizations/organization/item[2]/+item/title',
  123. 'organizations/organization/item[2]/+/+/+/title',
  124. 'organizations/organization/item[2]/-item',
  125. 'organizations/organization/item[1]/-item',
  126. 'organizations/organization/item[1]/-',
  127. 'organizations/organization/item[1]/-/@.'
  128. ) as $path)
  129. $stuff .= $path . ' => ' . $testdoc->xmd_value($path) . "\n";
  130. showDoc('Values of: @identifier, metadata/schema, ... (see below)', $stuff);
  131. function showHtml($path)
  132. {
  133. global $testdoc; echo '<h4>Html-value of ', htmlspecialchars($path),
  134. '</h4><pre>', $testdoc->xmd_html_value($path), '</pre>';
  135. }
  136. showHtml('/*/organizations/organization/item[1]/title');
  137. showHtml('organizations/organization/item/title');
  138. showHtml('organizations/organization/item/title *');
  139. showHtml('Titles: -% organizations/organization/item/titl , %- .');
  140. // if no elements are found, prefix and postfix are not generated
  141. showHtml('Titles: -% organizations/organization/item/title , %- .');
  142. showHtml('<ul><li> -% resources/resource/file/../@identifier </li><li> %- </li></ul>');
  143. showHtml('metadata/lom/general/description/langstring');
  144. echo '<h5>The same, but in a HTML construct:</h5>',
  145. $testdoc->xmd_html_value('metadata/lom/general/description/langstring');
  146. function getmicrotime()
  147. {
  148. list($usec, $sec) = explode(" ",microtime());
  149. return ((float)$usec + (float)$sec);
  150. }
  151. $xmlFile = 'imsmanifest_reload.xml';
  152. ($presXmlFileContents = @file_get_contents_n($xmlFile))
  153. or die('XML file ' . htmlspecialchars($xmlFile) . ' is missing...');
  154. $presXmlFileContents = explode("\n", $presXmlFileContents);
  155. $seconds = getmicrotime();
  156. $testdoc2 = new xmddoc($presXmlFileContents);
  157. $seconds = getmicrotime() - $seconds;
  158. showDoc('Time to parse', $seconds);
  159. $seconds = getmicrotime();
  160. $testdoc2->xmd_cache();
  161. $seconds = getmicrotime() - $seconds;
  162. showDoc('Time to cache', $seconds);
  163. $seconds = getmicrotime();
  164. $testdoc = new xmddoc($testdoc2->names, $testdoc2->numbers,
  165. $testdoc2->textstring);
  166. $seconds = getmicrotime() - $seconds;
  167. showDoc('Time to restore from cache', $seconds);
  168. showDoc('OK after restore');
  169. ?>