/exchange/code/trunk/administrator/components/com_exchange/models/migrate.php
PHP | 260 lines | 179 code | 27 blank | 54 comment | 15 complexity | ad4204746c3585622e867fa532de6700 MD5 | raw file
- <?php
- /**
- * @version $Id: migrate.php 280 2010-09-18 02:14:15Z eddieajau $
- * @package NewLifeInIT
- * @subpackage com_exchange
- * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License version 2 or later.
- * @link http://www.theartofjoomla.com
- */
- // no direct access
- defined('_JEXEC') or die;
- jimport('joomla.application.component.model');
- jimport('joomla.filesystem.folder');
- jimport('joomla.filesystem.file');
- /**
- * @package NewLifeInIT
- * @subpackage com_exchange
- */
- class ExchangeModelMigrate extends JModel
- {
- /**
- * Get a list of the paths to search for definition files
- *
- * @return array
- */
- protected function getPaths()
- {
- $paths = array(
- 'builtin' => JPATH_COMPONENT.DS.'adapters'.DS.'migrate',
- 'custom' => JPATH_ADMINISTRATOR.DS.'custom'.DS.'com_exchange'.DS.'migrate'
- );
- return $paths;
- }
- /**
- * Get a list of the migration definition files
- *
- * @return array An arry files
- */
- function getFiles()
- {
- $result = array();
- $paths = self::getPaths();
- foreach ($paths as $type => $path)
- {
- if (is_dir($path))
- {
- $files = JFolder::files($path, '\.xml$');
- foreach ($files as $file)
- {
- $xml = simplexml_load_file($path.DS.$file);
- $temp = new stdClass;
- $temp->type = $type;
- $temp->file = $file;
- $temp->adapter = JFile::stripExt($file);
- $temp->name = (string) $xml->name;
- $temp->url = (string) $xml->url;
- $temp->date = (string) $xml->date;
- $temp->author = (string) $xml->author;
- $temp->description = (string) $xml->description;
- $result[] = $temp;
- }
- }
- }
- return $result;
- }
- /**
- * Get the contents of a migration definition file
- *
- * @return string
- */
- function getFile()
- {
- $paths = self::getPaths();
- $adapter = $this->getState('adapter');
- $result = new JException('Adapter file not found');
- foreach ($paths as $path)
- {
- $file = $path.DS.$adapter.'.xml';
- if (file_exists($file)) {
- $result = file_get_contents($file);
- $this->setState('adapter_file', $file);
- break;
- }
- }
- return $result;
- }
- function getForm()
- {
- $result = null;
- $this->getFile();
- if ($file = $this->getState('adapter_file')) {
- $result = new JParameter('', $file);
- }
- return $result;
- }
- function migrate()
- {
- // Load the helpers
- require JPATH_COMPONENT.DS.'helpers'.DS.'migrate.php';
- require JPATH_COMPONENT.DS.'helpers'.DS.'catalog.php';
- require JPATH_COMPONENT.DS.'helpers'.DS.'zine.php';
- require JPATH_COMPONENT.DS.'helpers'.DS.'comments.php';
- $buffer = $this->getFile();
- $dom = new DOMDocument;
- $dom->preserveWhitespace = false;
- $dom->loadXML($buffer);
- $document = &$dom->documentElement;
- // Use simple xml for the easy stuff
- $xml = simplexml_import_dom($dom);
- $iterator = new XmlIterator();
- $iterator->setModel($this);
- $iterator->iterate($xml->transforms);
- }
- function getSourceDBO()
- {
- static $instance = null;
- if ($instance == null)
- {
- if ($database = $this->getState('srcDBName'))
- {
- global $mainframe;
- $options = array(
- 'driver' => $mainframe->getCfg('dbtype'),
- 'host' => $mainframe->getCfg('host'),
- 'user' => $mainframe->getCfg('user'),
- 'password' => $mainframe->getCfg('password'),
- 'database' => $database,
- );
- $instance = &JDatabase::getInstance($options);
- }
- else
- {
- $instance = &JFactory::getDBO();
- }
- }
- return $instance;
- }
- function &getDestDBO()
- {
- return JFactory::getDBO();
- }
- /**
- * Get the value of an object passed into the model
- *
- * @param string The name of the option
- * @param mixed The default value to use if not found
- * @param string The casting
- */
- function getOption($key, $default = null, $cast = null)
- {
- $options = &$this->getState('params');
- return JArrayHelper::getValue($options, $key, $default, $cast);
- }
- }
- /**
- * @package jXExchange
- * @abstract
- */
- class XMLIterator
- {
- protected $parent = null;
- protected $model = null;
- function __construct(&$parent = null)
- {
- $this->parent = $parent;
- }
- function setModel(&$model)
- {
- $this->model = &$model;
- }
- function &getModel()
- {
- if ($this->parent == null) {
- return $this->model;
- } else {
- return $this->parent->getModel();
- }
- }
- /**
- * Iterates over a node
- * @param DOMElement An xml doc element
- * @return string The assembled html
- */
- function iterate(&$node, &$value = null)
- {
- $result = array();
- foreach ($node->children() as $child)
- {
- $name = strtolower($child->getName());
- $name = preg_replace('#[^A-Z0-9_]#i', '', $name);
- $class = 'XMLTransform'.$name;
- if (!class_exists($class))
- {
- $path = JPATH_COMPONENT.DS.'models'.DS.'transforms'.DS.$name.'.php';
- if (file_exists($path)) {
- require_once($path);
- } else {
- $result[] = JError::raiseWarning(500, $name.' class file unavailable');
- }
- }
- if (class_exists($class)) {
- $transform = new $class($this);
- $temp = $transform->iterate($child, $value);
- $result[] = $temp;
- }
- else {
- $result[] = JError::raiseWarning(500, $class.' class unavailable');
- }
- }
- return $result;
- }
- /**
- * Progress marker
- *
- * Attempts to display a message and flush buffers as best it can to allow progress to be seen in real time on large runs.
- * May not always be successful depending on the server configuration.
- *
- * @param string A message.
- */
- function out($text)
- {
- static $prof;
- if (!$prof)
- {
- jimport('joomla.utilities.profiler');
- $prof = new JProfiler();
- echo '<style> * {font-family:monospace;}</style>';
- }
- echo $prof->mark($text);
- ob_flush();
- flush();
- }
- }