| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
class midcom_helper_replicator_importer extends midcom_baseclasses_components_purecode |
|---|
| 14 |
{ |
|---|
| 15 |
|
|---|
| 16 |
* Possible processing error. |
|---|
| 17 |
* |
|---|
| 18 |
* @var string |
|---|
| 19 |
* @access protected |
|---|
| 20 |
*/ |
|---|
| 21 |
var $error = ''; |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
* Lists imported object counts indexed by mgdschema class name |
|---|
| 25 |
*/ |
|---|
| 26 |
var $counter = array(); |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
* Initializes the class. |
|---|
| 30 |
*/ |
|---|
| 31 |
function __construct() |
|---|
| 32 |
{ |
|---|
| 33 |
$this->_component = 'midcom.helper.replicator'; |
|---|
| 34 |
|
|---|
| 35 |
parent::__construct(); |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
* This is a static factory method which lets you dynamically create importer instances. |
|---|
| 40 |
* It takes care of loading the required class files. The returned instances will be created |
|---|
| 41 |
* but not initialized. |
|---|
| 42 |
* |
|---|
| 43 |
* On any error (class not found etc.) the factory method will call generate_error. |
|---|
| 44 |
* |
|---|
| 45 |
* <b>This function must be called statically.</b> |
|---|
| 46 |
* |
|---|
| 47 |
* @param string $type Type of the importer |
|---|
| 48 |
* @return midcom_helper_replicator_importer A reference to the newly created importer instance. |
|---|
| 49 |
* @static |
|---|
| 50 |
*/ |
|---|
| 51 |
function & create($type) |
|---|
| 52 |
{ |
|---|
| 53 |
$filename = MIDCOM_ROOT . "/midcom/helper/replicator/importer/{$type}.php"; |
|---|
| 54 |
|
|---|
| 55 |
if (!file_exists($filename)) |
|---|
| 56 |
{ |
|---|
| 57 |
$_MIDCOM->generate_error(MIDCOM_ERRCRIT, "Requested importer file {$type} is not installed."); |
|---|
| 58 |
|
|---|
| 59 |
} |
|---|
| 60 |
require_once($filename); |
|---|
| 61 |
|
|---|
| 62 |
$classname = "midcom_helper_replicator_importer_{$type}"; |
|---|
| 63 |
if (!class_exists($classname)) |
|---|
| 64 |
{ |
|---|
| 65 |
$_MIDCOM->generate_error(MIDCOM_ERRCRIT, "Requested importer class {$type} is not installed."); |
|---|
| 66 |
|
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
* Php 4.4.1 does not allow you to return a reference to an expression. |
|---|
| 71 |
* http://www.php.net/release_4_4_0.php |
|---|
| 72 |
*/ |
|---|
| 73 |
$class = new $classname(); |
|---|
| 74 |
return $class; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
* This is the checkpoint of the importer. |
|---|
| 79 |
* |
|---|
| 80 |
* This should be overridden in subclasses for more contextual handling of dependencies. |
|---|
| 81 |
* |
|---|
| 82 |
* @param midgard_object &$object The Object to import |
|---|
| 83 |
* @return boolean Whether the object may be imported with this importer |
|---|
| 84 |
*/ |
|---|
| 85 |
function is_importable(&$object) |
|---|
| 86 |
{ |
|---|
| 87 |
return true; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
* This is the main entry point of the importer. |
|---|
| 92 |
* |
|---|
| 93 |
* This should be overridden in subclasses for more contextual handling of dependencies. |
|---|
| 94 |
* |
|---|
| 95 |
* @param string &$xml XML replication content |
|---|
| 96 |
* @param boolean $use_force Whether to force importing |
|---|
| 97 |
* @return boolean Whether importing was successful |
|---|
| 98 |
*/ |
|---|
| 99 |
function import_xml(&$xml, $use_force = false) |
|---|
| 100 |
{ |
|---|
| 101 |
$GLOBALS['midcom_helper_replicator_logger']->push_prefix(__CLASS__ . '::' . __FUNCTION__); |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
// Call silenced to avoid warnings generated by missed dependencies |
|---|
| 105 |
$objects = @midcom_helper_replicator_unserialize($xml, $use_force); |
|---|
| 106 |
|
|---|
| 107 |
if (empty($objects)) |
|---|
| 108 |
{ |
|---|
| 109 |
$this->error = mgd_errstr(); |
|---|
| 110 |
$GLOBALS['midcom_helper_replicator_logger']->log('midcom_helper_replicator_unserialize() failed, errstr: ' . mgd_errstr(), MIDCOM_LOG_ERROR); |
|---|
| 111 |
$GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); |
|---|
| 112 |
return false; |
|---|
| 113 |
} |
|---|
| 114 |
foreach ($objects as $object) |
|---|
| 115 |
{ |
|---|
| 116 |
|
|---|
| 117 |
if (is_a($object, 'midgard_blob')) |
|---|
| 118 |
{ |
|---|
| 119 |
if (!$this->is_importable($object)) |
|---|
| 120 |
{ |
|---|
| 121 |
$GLOBALS['midcom_helper_replicator_logger']->log('is_importable() returned false', MIDCOM_LOG_ERROR); |
|---|
| 122 |
$GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); |
|---|
| 123 |
return false; |
|---|
| 124 |
} |
|---|
| 125 |
if (!$_MIDCOM->dbfactory->import_blob($object, $xml, $use_force)) |
|---|
| 126 |
{ |
|---|
| 127 |
$this->error = mgd_errstr(); |
|---|
| 128 |
$GLOBALS['midcom_helper_replicator_logger']->log("Failed to import midgard_blob for object {$object->parentguid}, errstr: {$this->error}", MIDCOM_LOG_ERROR); |
|---|
| 129 |
$GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); |
|---|
| 130 |
return false; |
|---|
| 131 |
} |
|---|
| 132 |
$GLOBALS['midcom_helper_replicator_logger']->log("Imported midgard_blob for object {$object->parentguid}"); |
|---|
| 133 |
continue; |
|---|
| 134 |
} |
|---|
| 135 |
$object_class = get_class($object); |
|---|
| 136 |
if (!$this->import_object($object, $use_force)) |
|---|
| 137 |
{ |
|---|
| 138 |
|
|---|
| 139 |
$this->error = mgd_errstr(); |
|---|
| 140 |
$GLOBALS['midcom_helper_replicator_logger']->log("Failed to import {$object_class} {$object->guid} (action: {$object->action}), errstr: {$this->error}", MIDCOM_LOG_ERROR); |
|---|
| 141 |
$GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); |
|---|
| 142 |
return false; |
|---|
| 143 |
} |
|---|
| 144 |
$GLOBALS['midcom_helper_replicator_logger']->log("Imported {$object_class} {$object->guid} (action: {$object->action})"); |
|---|
| 145 |
|
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
$GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); |
|---|
| 149 |
return true; |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
* Imports given object received from midgard_replicator::unserialize |
|---|
| 154 |
* |
|---|
| 155 |
* Does some basic sanity checks before calling $_MIDCOM->dbfactory->import() |
|---|
| 156 |
* @param string $object Importable Midgard object |
|---|
| 157 |
* @param boolean $use_force Whether to force importing |
|---|
| 158 |
* @return boolean Whether importing was successful |
|---|
| 159 |
*/ |
|---|
| 160 |
function import_object(&$object, $use_force) |
|---|
| 161 |
{ |
|---|
| 162 |
$GLOBALS['midcom_helper_replicator_logger']->push_prefix(__CLASS__ . '::' . __FUNCTION__); |
|---|
| 163 |
if (is_a($object, 'midgard_blob')) |
|---|
| 164 |
{ |
|---|
| 165 |
$GLOBALS['midcom_helper_replicator_logger']->log("BLOBS can only be imported from XML, the unserialized object does not contain the binary data", MIDCOM_LOG_ERROR); |
|---|
| 166 |
$GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); |
|---|
| 167 |
return false; |
|---|
| 168 |
} |
|---|
| 169 |
if ( !is_object($object) |
|---|
| 170 |
|| !isset($object->action)) |
|---|
| 171 |
{ |
|---|
| 172 |
$GLOBALS['midcom_helper_replicator_logger']->log('given argument is not valid object from midgard_replicator::unserialize', MIDCOM_LOG_ERROR); |
|---|
| 173 |
$GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); |
|---|
| 174 |
return false; |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
if (!$this->is_importable($object)) |
|---|
| 178 |
{ |
|---|
| 179 |
$GLOBALS['midcom_helper_replicator_logger']->log('is_importable() returned false', MIDCOM_LOG_ERROR); |
|---|
| 180 |
$GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); |
|---|
| 181 |
return false; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
$stat = $_MIDCOM->dbfactory->import($object, $use_force); |
|---|
| 185 |
if (!$stat) |
|---|
| 186 |
{ |
|---|
| 187 |
$this->error = mgd_errstr(); |
|---|
| 188 |
} |
|---|
| 189 |
$GLOBALS['midcom_helper_replicator_logger']->pop_prefix(); |
|---|
| 190 |
return $stat; |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
* Main entry point for importer, must be overridden by subclass |
|---|
| 195 |
* |
|---|
| 196 |
* @return boolean always false (subclasses must override this) |
|---|
| 197 |
*/ |
|---|
| 198 |
function import() |
|---|
| 199 |
{ |
|---|
| 200 |
debug_push_class(__CLASS__, __FUNCTION__); |
|---|
| 201 |
debug_add('Subclasses MUST override this method', MIDCOM_LOG_ERROR); |
|---|
| 202 |
debug_pop(); |
|---|
| 203 |
return false; |
|---|
| 204 |
} |
|---|
| 205 |
} |
|---|
| 206 |
?> |
|---|