Source for file XMLExporter.class.php

Documentation is available at XMLExporter.class.php

  1. <?php
  2. /**
  3. * @since 10/17/05
  4. * @package polyphony.exporter
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: XMLExporter.class.php,v 1.13 2007/09/19 14:04:44 adamfranco Exp $
  10. */
  11.  
  12. require_once("Archive/Tar.php");
  13. require_once(HARMONI."/Primitives/Chronology/DateAndTime.class.php");
  14. require_once(POLYPHONY."/main/library/Exporter/XMLRepositoryExporter.class.php");
  15.  
  16. /**
  17. * Exports into XML for use with the XML Importer
  18. *
  19. * @since 10/17/05
  20. * @package polyphony.exporter
  21. *
  22. * @copyright Copyright &copy; 2005, Middlebury College
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  24. *
  25. * @version $Id: XMLExporter.class.php,v 1.13 2007/09/19 14:04:44 adamfranco Exp $
  26. */
  27. class XMLExporter {
  28. /**
  29. * Constructor
  30. *
  31. * @access public
  32. * @since 10/17/05
  33. */
  34. function XMLExporter () {
  35. $this->setupSelf();
  36. }
  37.  
  38. /**
  39. * Creates the child lists
  40. *
  41. * @access public
  42. * @since 10/31/05
  43. */
  44. function setupSelf () {
  45. $this->_childExporterList = array("XMLRepositoryExporter");/*, "XMLSetImporter", "XMLHierarchyImporter", "XMLGroupImporter", "XMLAgentImporter");*/
  46. $this->_childElementList = array("repositories", "sets", "hierarchy",
  47. "groups", "agents");
  48. }
  49.  
  50. /**
  51. * Initializes the export by creating a dir in /tmp/
  52. *
  53. * @param string
  54. * @access public
  55. * @since 10/31/05
  56. */
  57. function withCompression ($compression, $class = 'XMLExporter') {
  58. if (!(strtolower($class) == strtolower('XMLExporter')
  59. || is_subclass_of(new $class, 'XMLExporter')))
  60. {
  61. die("Class, '$class', is not a subclass of 'XMLExporter'.");
  62. }
  63. $exporter = new $class;
  64. $now = DateAndTime::now();
  65. $exporter->_tmpDir = "/tmp/export_".$now->asString();
  66. while (file_exists($exporter->_tmpDir)) {
  67. $now = DateAndTime::now();
  68. $exporter->_tmpDir = "/tmp/export_".$now->asString();
  69. }
  70. mkdir($exporter->_tmpDir);
  71. $exporter->_compression = $compression;
  72. return $exporter;
  73. }
  74.  
  75. /**
  76. * finds the appropriate key for Archive_Tar
  77. *
  78. * @param string
  79. * @return string
  80. * @access public
  81. * @since 10/31/05
  82. */
  83. function getTarKey ($extension) {
  84. switch ($extension) {
  85. case ".tar.gz":
  86. return "gz";
  87. default :
  88. return "gz";
  89. }
  90. }
  91.  
  92. /**
  93. * Exporter of All things
  94. *
  95. * @access public
  96. * @since 10/17/05
  97. */
  98. function exportAll () {
  99. $this->setupXML($this->_tmpDir);
  100. fwrite($this->_xml, "<import>\n");
  101. foreach ($this->_childElementList as $child) {
  102. $exportFn = "export".ucfirst($child);
  103. if (method_exists($this, $exportFn))
  104. $this->$exportFn();
  105. }
  106.  
  107. fwrite($this->_xml, "</import>");
  108. fclose($this->_xml);
  109.  
  110. return $this->_tmpDir;
  111. }
  112.  
  113. /**
  114. * creates xmlfile and initializes it
  115. *
  116. * @param string $dir the directory in which the xml file resides
  117. * @access public
  118. * @since 10/31/05
  119. */
  120. function setupXML ($dir) {
  121. $this->_xml = fopen($dir."/metadata.xml", "w");
  122. fwrite($this->_xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  123. }
  124. /**
  125. * Exporter of repositories
  126. *
  127. * Adds repositoryfile elements to the xml, which pass off to individual
  128. * repository Importers.
  129. *
  130. * @return <##>
  131. * @access public
  132. * @since 10/17/05
  133. */
  134. function exportRepositories () {
  135. $rm = Services::getService("Repository");
  136. $children =$rm->getRepositories();
  137. while ($children->hasNext()) {
  138. $child =$children->next();
  139. $childId =$child->getId();
  140. fwrite($this->_xml, "\t<repositoryfile>".$childId->getIdString().
  141. "/metadata.xml</repositoryfile>\n");
  142. $exporter = XMLRepositoryExporter::withDir($this->_tmpDir);
  143. $exporter->export($childId); // ????
  144. unset($exporter);
  145. }
  146. }
  147. /**
  148. * Compress a directory with status stars. Return the resulting file path
  149. *
  150. * @return string the new archive path.
  151. * @static
  152. * @access public
  153. * @since 12/12/06
  154. */
  155. function compressWithStatus () {
  156. $archiveBaseName = "export_".md5(time()." ".rand());
  157. // Get the number of files in the directory and initialize the status stars
  158. $status = new StatusStars(_("Compressing the archive"));
  159. $numFiles = $this->numFiles($this->_tmpDir);
  160. $status->initializeStatistics($numFiles);
  161. $filesSeen = 1;
  162. $PID = $this->run_in_background(
  163. 'tar -v -czf /tmp/'.$archiveBaseName.$this->_compression.
  164. " -C ".str_replace(":", "\:", $this->_tmpDir)." . ",
  165. 0, str_replace(":", "\:", $this->_tmpDir)."-compress_status");
  166. while($this->is_process_running($PID)) {
  167. $lines = count(file($this->_tmpDir."-compress_status"));
  168. for ($i = $filesSeen; $i < $lines; $i++) {
  169. $status->updateStatistics();
  170. $filesSeen++;
  171. }
  172. sleep(1);
  173. }
  174. // Finish off any last statistics
  175. for ($i = $filesSeen; $i <= $numFiles; $i++)
  176. $status->updateStatistics();
  177. // Remove our status file
  178. unlink($this->_tmpDir."-compress_status");
  179. // Remove the source directory
  180. shell_exec('rm -R '.str_replace(":", "\:", $this->_tmpDir));
  181. return '/tmp/'.$archiveBaseName.$this->_compression;
  182. }
  183. /**
  184. * Run linux command in background and return the PID created by the OS
  185. *
  186. * Posted to PHP.net by jesuse.gonzalez@venalum.com.ve on 15-Jul-2005 11:34
  187. * Addition of output file parameter and inclusion into this class by Adam Franco
  188. *
  189. * @param string $Command
  190. * @param integer $Priority
  191. * @param string $outputFile
  192. * @return integer
  193. * @access public
  194. * @since 12/13/06
  195. */
  196. function run_in_background($Command, $Priority = 0, $outputFile = '/dev/null')
  197. {
  198. if($Priority)
  199. $PID = shell_exec("nohup nice -n $Priority $Command > $outputFile & echo $!");
  200. else
  201. $PID = shell_exec("nohup $Command > $outputFile & echo $!");
  202. return($PID);
  203. }
  204. /**
  205. * Verifies if a process is running in linux
  206. *
  207. * Posted to PHP.net by jesuse.gonzalez@venalum.com.ve on 15-Jul-2005 11:34
  208. * Inclusion into this class by Adam Franco.
  209. *
  210. * @param integer $PID
  211. * @return boolean
  212. * @access public
  213. * @since 12/13/06
  214. */
  215. function is_process_running($PID)
  216. {
  217. exec("ps $PID", $ProcessState);
  218. return(count($ProcessState) >= 2);
  219. }
  220. /**
  221. * Return the number of files in a directory (recursively) including the directory
  222. * its self;
  223. *
  224. * @param string $dir
  225. * @return integer
  226. * @access public
  227. * @since 12/12/06
  228. */
  229. function numFiles ($dir) {
  230. $numFiles = 1;
  231. if (is_dir($dir)) {
  232. if ($dh = opendir($dir)) {
  233. while (($file = readdir($dh)) !== false) {
  234. if ($file != "." && $file != "..") {
  235. if (is_dir($dir."/".$file))
  236. $numFiles = $numFiles + $this->numFiles($dir."/".$file);
  237. else
  238. $numFiles++;
  239. }
  240. }
  241. closedir($dh);
  242. }
  243. }
  244. return $numFiles;
  245. }
  246. }
  247. ?>

Documentation generated on Wed, 19 Sep 2007 10:28:31 -0400 by phpDocumentor 1.3.0RC3