28 define (
'SQ_IN_IMPORT', 1);
30 error_reporting(E_ALL);
31 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
34 if(function_exists (
'gc_enable')) {
38 if ((php_sapi_name() !=
'cli')) trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
40 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
41 if (empty($SYSTEM_ROOT)) {
42 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
46 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
47 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
51 $import_file = (isset($_SERVER[
'argv'][2])) ? $_SERVER[
'argv'][2] :
'';
52 if (empty($import_file) || !is_file($import_file)) {
53 echo
"You need to supply the path to the import file as the second argument\n";
57 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
59 if (isset($_SERVER[
'argv'][3]) && $_SERVER[
'argv'][3] ==
'--root-node' ) {
60 $root_node_id = (isset($_SERVER[
'argv'][4])) ? $_SERVER[
'argv'][4] :
'';
61 if (empty($root_node_id)) {
62 echo
"you need to supply root node under which the assets are to be imported as fourth argument\n";
67 $root_user = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
69 require_once SQ_LIB_PATH.
'/import_export/import.inc';
70 $import_actions = get_import_actions($import_file);
73 if (isset($_SERVER[
'argv'][3]) && $_SERVER[
'argv'][3] ==
'--root-node' ) {
75 error_reporting(E_NOTICE);
76 $root_node = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($root_node_id);
77 if (is_null($root_node)) {
78 echo
"\nProvided assetid is not valid for given system, Script will stop execution\n";
82 error_reporting(E_ALL);
84 $import_actions[
'actions'][0][
'action'][0][
'parentid'][0] = $root_node_id;
87 $total_number = count($import_actions[
'actions'][0][
'action']);
90 _disconnectFromMatrixDatabase();
94 $import_file_name_hash = md5($import_file);
95 define(
'TEMP_FILE', SQ_TEMP_PATH.
'/import_from_xml_'.$import_file_name_hash.
'.tmp');
96 if(is_file(TEMP_FILE)) {
97 echo
"Previous progress file is detected, resuming to previous import.\n";
98 echo
"Using ".TEMP_FILE.
"\n\n";
99 $actions_done = unserialize(file_get_contents(TEMP_FILE));
103 $import_action_outputs = Array();
104 $nest_content_to_fix = Array();
105 $designs_to_fix = Array();
107 foreach ($import_actions[
'actions'][0][
'action'] as $index => $action) {
110 if(isset($actions_done[$action[
'action_id'][0]])) {
116 if($action[
'action_type'][0] ===
'create_asset' && $action[
'type_code'][0] ===
'Content_Type_Nest_Content') {
117 $nest_content_to_fix[] = $action[
'action_id'][0];
119 if($action[
'action_type'][0] ===
'create_asset' && $action[
'type_code'][0] ===
'Design') {
120 $designs_to_fix[] = $action[
'action_id'][0];
129 trigger_error(
'Process failed to fork', E_USER_ERROR);
135 _connectToMatrixDatabase();
136 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_OPEN);
137 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
139 if(is_file(TEMP_FILE))
140 $import_action_outputs = unserialize(file_get_contents(TEMP_FILE));
143 if(!checkAssetExists($action,
'parentid') || !checkAssetExists($action,
'assetid') || !checkAssetExists($action,
'asset')) {
144 trigger_error(
'Action ID "'.$action[
'action_id'][0].
'" contains non-exist assetid reference. Action skipped.', E_USER_WARNING);
145 _disconnectFromMatrixDatabase();
151 printActionId($action[
'action_id'][0]);
152 if (!execute_import_action($action, $import_action_outputs)) {
153 trigger_error(
'Action ID "'.$action[
'action_id'][0].
'" could not be executed', E_USER_WARNING);
156 printStatus($count.
'/'.$total_number);
161 $new_entry = array_slice( $import_action_outputs, -1, 1, TRUE );
162 $new_entry_key = key($new_entry);
163 if(isset($new_entry[$new_entry_key][
'old_value'])) unset($new_entry[$new_entry_key][
'old_value']);
164 if(isset($new_entry[$new_entry_key][
'new_value'])) unset($new_entry[$new_entry_key][
'new_value']);
165 $import_action_outputs = array_merge($import_action_outputs, $new_entry);
168 $temp_string = serialize($import_action_outputs);
169 string_to_file($temp_string, TEMP_FILE);
171 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
172 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
174 _disconnectFromMatrixDatabase();
180 pcntl_waitpid(-1, $status);
188 foreach ($nest_content_to_fix as $actionid) {
189 if(isset($import_action_outputs[$actionid])) {
190 $nest_content_id = $import_action_outputs[$actionid][
'assetid'];
191 $nest_content = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($nest_content_id);
192 $nest_content->_tmp[
'edit_fns'] = NULL;
193 $nest_content->linksUpdated();
198 foreach ($designs_to_fix as $design) {
199 if(isset($import_action_outputs[$design])) {
200 $design_id = $import_action_outputs[$design][
'assetid'];
201 $vars = Array(
'assetid' => $design_id);
202 $hh = $GLOBALS[
'SQ_SYSTEM']->getHipoHerder();
203 $hh->freestyleHipo(
'hipo_job_regenerate_design', $vars);
208 if(is_file(TEMP_FILE)) unlink(TEMP_FILE);
211 if(function_exists (
'gc_disable')) {
223 function printActionId($action_id)
225 if (strlen($action_id) > 66) {
226 $action_id = substr($action_id, 0, 66).
'...';
228 printf (
'%s%'.(70 - strlen($action_id)).
's', $action_id,
'');
241 function printStatus($status)
243 echo
"[ $status ]\n";
257 function checkAssetExists($action, $type=
'asset')
259 if(isset($action[$type][0]) && preg_match(
'/^[0-9]+$/', $action[$type][0])){
260 return ($GLOBALS[
'SQ_SYSTEM']->am->assetExists ($action[$type][0]));
272 function _disconnectFromMatrixDatabase()
275 if (isset($conn_id) && !empty($conn_id)) {
289 function _connectToMatrixDatabase()
291 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');