PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/exchange/code/trunk/administrator/components/com_exchange/models/migrate.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 260 lines | 179 code | 27 blank | 54 comment | 15 complexity | ad4204746c3585622e867fa532de6700 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: migrate.php 280 2010-09-18 02:14:15Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_exchange
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License version 2 or later.
  8. * @link http://www.theartofjoomla.com
  9. */
  10. // no direct access
  11. defined('_JEXEC') or die;
  12. jimport('joomla.application.component.model');
  13. jimport('joomla.filesystem.folder');
  14. jimport('joomla.filesystem.file');
  15. /**
  16. * @package NewLifeInIT
  17. * @subpackage com_exchange
  18. */
  19. class ExchangeModelMigrate extends JModel
  20. {
  21. /**
  22. * Get a list of the paths to search for definition files
  23. *
  24. * @return array
  25. */
  26. protected function getPaths()
  27. {
  28. $paths = array(
  29. 'builtin' => JPATH_COMPONENT.DS.'adapters'.DS.'migrate',
  30. 'custom' => JPATH_ADMINISTRATOR.DS.'custom'.DS.'com_exchange'.DS.'migrate'
  31. );
  32. return $paths;
  33. }
  34. /**
  35. * Get a list of the migration definition files
  36. *
  37. * @return array An arry files
  38. */
  39. function getFiles()
  40. {
  41. $result = array();
  42. $paths = self::getPaths();
  43. foreach ($paths as $type => $path)
  44. {
  45. if (is_dir($path))
  46. {
  47. $files = JFolder::files($path, '\.xml$');
  48. foreach ($files as $file)
  49. {
  50. $xml = simplexml_load_file($path.DS.$file);
  51. $temp = new stdClass;
  52. $temp->type = $type;
  53. $temp->file = $file;
  54. $temp->adapter = JFile::stripExt($file);
  55. $temp->name = (string) $xml->name;
  56. $temp->url = (string) $xml->url;
  57. $temp->date = (string) $xml->date;
  58. $temp->author = (string) $xml->author;
  59. $temp->description = (string) $xml->description;
  60. $result[] = $temp;
  61. }
  62. }
  63. }
  64. return $result;
  65. }
  66. /**
  67. * Get the contents of a migration definition file
  68. *
  69. * @return string
  70. */
  71. function getFile()
  72. {
  73. $paths = self::getPaths();
  74. $adapter = $this->getState('adapter');
  75. $result = new JException('Adapter file not found');
  76. foreach ($paths as $path)
  77. {
  78. $file = $path.DS.$adapter.'.xml';
  79. if (file_exists($file)) {
  80. $result = file_get_contents($file);
  81. $this->setState('adapter_file', $file);
  82. break;
  83. }
  84. }
  85. return $result;
  86. }
  87. function getForm()
  88. {
  89. $result = null;
  90. $this->getFile();
  91. if ($file = $this->getState('adapter_file')) {
  92. $result = new JParameter('', $file);
  93. }
  94. return $result;
  95. }
  96. function migrate()
  97. {
  98. // Load the helpers
  99. require JPATH_COMPONENT.DS.'helpers'.DS.'migrate.php';
  100. require JPATH_COMPONENT.DS.'helpers'.DS.'catalog.php';
  101. require JPATH_COMPONENT.DS.'helpers'.DS.'zine.php';
  102. require JPATH_COMPONENT.DS.'helpers'.DS.'comments.php';
  103. $buffer = $this->getFile();
  104. $dom = new DOMDocument;
  105. $dom->preserveWhitespace = false;
  106. $dom->loadXML($buffer);
  107. $document = &$dom->documentElement;
  108. // Use simple xml for the easy stuff
  109. $xml = simplexml_import_dom($dom);
  110. $iterator = new XmlIterator();
  111. $iterator->setModel($this);
  112. $iterator->iterate($xml->transforms);
  113. }
  114. function getSourceDBO()
  115. {
  116. static $instance = null;
  117. if ($instance == null)
  118. {
  119. if ($database = $this->getState('srcDBName'))
  120. {
  121. global $mainframe;
  122. $options = array(
  123. 'driver' => $mainframe->getCfg('dbtype'),
  124. 'host' => $mainframe->getCfg('host'),
  125. 'user' => $mainframe->getCfg('user'),
  126. 'password' => $mainframe->getCfg('password'),
  127. 'database' => $database,
  128. );
  129. $instance = &JDatabase::getInstance($options);
  130. }
  131. else
  132. {
  133. $instance = &JFactory::getDBO();
  134. }
  135. }
  136. return $instance;
  137. }
  138. function &getDestDBO()
  139. {
  140. return JFactory::getDBO();
  141. }
  142. /**
  143. * Get the value of an object passed into the model
  144. *
  145. * @param string The name of the option
  146. * @param mixed The default value to use if not found
  147. * @param string The casting
  148. */
  149. function getOption($key, $default = null, $cast = null)
  150. {
  151. $options = &$this->getState('params');
  152. return JArrayHelper::getValue($options, $key, $default, $cast);
  153. }
  154. }
  155. /**
  156. * @package jXExchange
  157. * @abstract
  158. */
  159. class XMLIterator
  160. {
  161. protected $parent = null;
  162. protected $model = null;
  163. function __construct(&$parent = null)
  164. {
  165. $this->parent = $parent;
  166. }
  167. function setModel(&$model)
  168. {
  169. $this->model = &$model;
  170. }
  171. function &getModel()
  172. {
  173. if ($this->parent == null) {
  174. return $this->model;
  175. } else {
  176. return $this->parent->getModel();
  177. }
  178. }
  179. /**
  180. * Iterates over a node
  181. * @param DOMElement An xml doc element
  182. * @return string The assembled html
  183. */
  184. function iterate(&$node, &$value = null)
  185. {
  186. $result = array();
  187. foreach ($node->children() as $child)
  188. {
  189. $name = strtolower($child->getName());
  190. $name = preg_replace('#[^A-Z0-9_]#i', '', $name);
  191. $class = 'XMLTransform'.$name;
  192. if (!class_exists($class))
  193. {
  194. $path = JPATH_COMPONENT.DS.'models'.DS.'transforms'.DS.$name.'.php';
  195. if (file_exists($path)) {
  196. require_once($path);
  197. } else {
  198. $result[] = JError::raiseWarning(500, $name.' class file unavailable');
  199. }
  200. }
  201. if (class_exists($class)) {
  202. $transform = new $class($this);
  203. $temp = $transform->iterate($child, $value);
  204. $result[] = $temp;
  205. }
  206. else {
  207. $result[] = JError::raiseWarning(500, $class.' class unavailable');
  208. }
  209. }
  210. return $result;
  211. }
  212. /**
  213. * Progress marker
  214. *
  215. * Attempts to display a message and flush buffers as best it can to allow progress to be seen in real time on large runs.
  216. * May not always be successful depending on the server configuration.
  217. *
  218. * @param string A message.
  219. */
  220. function out($text)
  221. {
  222. static $prof;
  223. if (!$prof)
  224. {
  225. jimport('joomla.utilities.profiler');
  226. $prof = new JProfiler();
  227. echo '<style> * {font-family:monospace;}</style>';
  228. }
  229. echo $prof->mark($text);
  230. ob_flush();
  231. flush();
  232. }
  233. }