17 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';
37 function get_import_actions($import_file)
40 if (!is_file($import_file)) {
41 trigger_error(
'Import file "'.$import_file.
'" does not exist', E_USER_ERROR);
49 if ((php_sapi_name() ==
'cli')) {
51 $xml_import_vals = Array();
52 $import_actions = Array();
53 $p = xml_parser_create();
55 xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
56 xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
58 $xml_file = file_to_string($import_file);
59 xml_parse_into_struct($p, $xml_file, $xml_import_vals, $index);
62 if ($error_code = xml_get_error_code($p)) {
63 echo
'XML Error: '.xml_error_string($error_code).
' Line:'.xml_get_current_line_number($p).
' Col:'.xml_get_current_column_number($p).
"\n";
67 reset($xml_import_vals);
68 populate_import_action_array($import_actions, $xml_import_vals);
74 $import_actions = Array();
75 $fh = fopen($import_file,
'r');
76 $old_exec_time = ini_get(
'max_execution_time');
82 $p = xml_parser_create();
83 xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
84 xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
86 $xml_import_vals = Array();
87 $chunk_import_action = Array();
89 $content =
'<actions>';
95 if (!($line = fgets($fh, 9999)))
break;
100 if (trim($line) ==
'')
continue;
101 if (preg_match(
'/(<actions>)$/', trim($line)))
continue;
102 if (preg_match(
'/(^<\?xml version)/', trim($line))) {
106 if (preg_match(
'/(<\/actions>)$/', trim($line)))
break;
108 $content = $content.$line;
110 }
while (!preg_match(
'/(<\/action>)$/', $content) || $count < 500);
111 $content .=
'</actions>';
113 if ($content !=
'<actions></actions>') {
114 if ($xml_header !=
'') $content = $xml_header.$content;
115 xml_parse_into_struct($p, $content, $xml_import_vals, $index);
118 if ($error_code = xml_get_error_code($p)) {
119 echo
'XML Error: '.xml_error_string($error_code).
' Line:'.xml_get_current_line_number($p).
' Col:'.xml_get_current_column_number($p).
"\n";
125 reset($xml_import_vals);
126 populate_import_action_array($chunk_import_action, $xml_import_vals);
127 if (!empty($chunk_import_action)) $actions = array_merge($actions, $chunk_import_action[
'actions'][0][
'action']);
131 set_time_limit($old_exec_time);
132 $import_actions[
'actions'][0][
'action'] = $actions;
135 return $import_actions;
149 function populate_import_action_array(&$store, &$vals)
151 for (current($vals); NULL !== ($k = key($vals)); next($vals)) {
153 $value = array_get_index($data,
'value',
'');
154 $tag = array_get_index($data,
'tag',
'');
156 switch ($data[
'type']) {
159 if (isset($store[$tag])) {
160 $max = count($store[$tag]);
161 if (isset($data[
'attributes']) && count($data[
'attributes'])) {
162 foreach ($data[
'attributes'] as $n => $v) {
163 $store[$tag][$max][$n] = $v;
166 $store[$tag][$max][
'_value'] = $value;
168 if (isset($data[
'attributes']) && count($data[
'attributes'])) {
169 foreach ($data[
'attributes'] as $n => $v) {
170 $store[$tag][0][$n] = $v;
172 $store[$tag][0][
'_value'] = $value;
174 $store[$tag][0] = $value;
185 if (!isset($store[$tag]) || !
sizeof($store[$tag])) {
186 $store[$tag] = Array();
189 $max = count($store[$tag]);
190 if (isset($data[
'attributes']) && count($data[
'attributes'])) {
191 foreach ($data[
'attributes'] as $n => $v) {
192 $store[$tag][$max][$n] = $v;
195 populate_import_action_array($store[$tag][$max], $vals);
226 function execute_import_action($action, &$outputs, $input_values=array())
231 $action_class =
'trigger_action_'.$action[
'action_type'][0];
232 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset($action_class);
235 $class_instance =
new $action_class;
238 foreach ($action as $key => $value) {
240 if ($key ==
'action_id')
continue;
241 if ($key ==
'action_type')
continue;
243 $value = get_import_action_input_value($action, $key, $outputs, $input_values);
245 if (strtolower(SQ_CONF_DEFAULT_CHARACTER_SET) !=
'utf-8') {
246 $pattern =
'/((&#[0-9]*;))/';
247 preg_match_all($pattern, $value, $matches);
249 $matches = $matches[0];
250 $replacement = Array();
252 foreach ($matches as $match) {
253 $str = str_replace(
'&#',
'', $match);
254 $str= intval(str_replace(
';',
'', $str));
255 if ($str > 126 && $str < 256) {
256 $new_value = chr($str);
260 $replacement[] = $new_value;
262 $value = str_replace($matches, $replacement, $value);
263 $value = preg_replace(
'|<br/>|', chr(13), $value);
266 if ($key ==
'asset') {
270 $state[
'assetid'] = $value;
274 $function_name =
'addSetting'.ucfirst($key);
276 if (method_exists($class_instance, $function_name)) {
277 eval(
'$settings = '.$action_class.
'::'.$function_name.
'($value, $settings);');
281 $settings[$key] = $value;
285 eval(
'$output = '.$action_class.
'::execute($settings, $state);');
286 if ($output === FALSE)
return FALSE;
288 $outputs[$action[
'action_id'][0]] = $output;
290 if ($action_class ==
'trigger_action_set_attribute_value' && isset($output[
'attribute']) && $output[
'attribute'] ==
'varieties'){
291 if (isset($output[
'assetid'])) {
292 $image = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($output[
'assetid']);
293 if ($image && $image->type() ==
'image'){
294 $edit_fns = $image->getEditFns();
295 $edit_fns->_recreateVarietyImages($image);
296 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($image);
301 if ($action_class ==
'trigger_action_create_asset' && isset($output[
'assetid'])) {
303 $asset_imported = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($output[
'assetid']);
306 if ($asset_imported && $asset_imported->type() ==
'design_customisation') {
307 $parent_design = $asset_imported->getParentDesign();
308 $asset_imported->updateFromParent($parent_design);
309 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($parent_design);
311 }
else if ($asset_imported && strpos($asset_imported->type(),
'design_area') !== FALSE) {
316 $design_customisation = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($settings[
'parentid'],
'design_customisation', TRUE);
317 if ($design_customisation) {
318 $parent_design = $design_customisation->getParentDesign();
320 $custom_da_links = $design_customisation->getDesignAreaLink();
325 foreach ($custom_da_links as $custom_da_link) {
326 $parent_da = $parent_design->getDesignAreaLink($custom_da_link[
'value']);
327 if (empty($parent_da))
continue;
329 $data[$custom_da_link[
'value']] = Array($custom_da_link[
'minorid'], $custom_da_link[
'minor_type_code']);
331 $design_customisation_edit_fns = $design_customisation->getEditFns();
333 if ($data && !$design_customisation_edit_fns->_updateDesignAreaLink($design_customisation, $data)) {
334 $design_customisation->updateFromParent($parent_design);
337 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($design_customisation);
338 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($parent_design);
343 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset_imported);
366 function get_import_action_input_value($action, $attribute, $outputs=Array(), $input_values=array())
369 if (!isset($action[$attribute])) {
370 trigger_error(
'Failed getting '.$attribute.
' for '.$action[
'action_type'][0].
' action "'.$action[
'action_id'][0].
'" - attribute is not defined for action', E_USER_ERROR);
372 $value = $action[$attribute][0];
375 if (isset($input_values[(
string)$value]) ===
true)
return $input_values[$value];
379 $current_value = $value;
382 if (is_array($value))
return $value;
384 preg_match_all(
'|\[\[output://([^\]]+)\]\]|', $value, $matches);
385 foreach ($matches[0] as $key => $match) {
386 list($action_id, $output) = explode(
'.', $matches[1][$key], 2);
387 if (!isset($outputs[$action_id])) {
388 trigger_error(
'Failed getting '.$attribute.
' for '.$action[
'action_type'][0].
' action "'.$action[
'action_id'][0].
'" - attribute references action "'.$action_id.
'" that has not yet been run', E_USER_ERROR);
390 if (!isset($outputs[$action_id][$output])) {
391 trigger_error(
'Failed getting '.$attribute.
' for '.$action[
'action_type'][0].
' action "'.$action[
'action_id'][0].
'" - attribute references action output "'.$output.
'" that was not generated by action "'.$action_id.
'"', E_USER_ERROR);
393 $value = str_replace($match, $outputs[$action_id][$output], $value);
397 preg_match_all(
'|\[\[system://([^\]]+)\]\]|', $value, $matches);
398 foreach ($matches[0] as $key => $match) {
399 $system_asset = substr($match, 11, -2);
400 $assetid = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAssetid($system_asset);
402 trigger_error(
'Failed getting system asset "'.$system_asset.
'"', E_USER_ERROR);
404 $value = str_replace($match, $assetid, $value);
408 preg_match_all(
'|\[\[conf://([^\]]+)\]\]|', $value, $matches);
409 $orig = (isset($matches[0][0]) ? $matches[0][0] :
'');
410 foreach ($matches[1] as $key => $match) {
411 eval(
'$conf_value = SQ_CONF_'.strtoupper($match).
';');
412 if ($match ==
'system_root_urls') {
413 $tmp = explode(
"\n", $conf_value);
414 $conf_value = $tmp[0];
416 $value = str_replace($orig, $conf_value, $value);
420 preg_match_all(
'|\[\[dal://([^\]_]+)_([^\]]+)\]\]|', $value, $matches);
421 if (!empty($matches[1]) && !empty($matches[2])) {
422 include SQ_DATA_PATH.
'/private/conf/db.inc';
423 if (isset($db_conf[$matches[1][0]])) {
425 $dsn = $db_conf[$matches[1][0]];
427 if (($matches[1][0] ==
'db') && !isset($dsn[
'DSN']) && isset($dsn[0])) {
430 if (isset($dsn[$matches[2][0]])) {
432 $value = $dsn[$matches[2][0]];
439 preg_match_all(
'|\[\[construct://([^\]]+)\]\]|', $value, $matches);
440 for ($ii = 0; $ii < count($matches[0]); $ii++) {
441 $construct_value =
'';
442 switch ($matches[1][$ii]) {
443 case 'unit_test_path':
444 $construct_value = (defined(
'TC_CONF_UNIT_TEST_PATH') ? TC_CONF_UNIT_TEST_PATH :
'');
446 case 'construct_path':
447 $construct_value = (defined(
'TC_CONF_CONSTRUCT_PATH') ? TC_CONF_CONSTRUCT_PATH :
'');
449 case 'simpletest_path':
450 $construct_value = (defined(
'TC_CONF_SIMPLETEST_PATH') ? TC_CONF_SIMPLETEST_PATH :
'');
453 $construct_value = (defined(
'TC_CONF_MATRIX_URL') ? strip_url(TC_CONF_MATRIX_URL, TRUE):
'');
456 $construct_value = time();
459 $value = str_replace($matches[0][$ii], $construct_value, $value);
462 }
while ($current_value != $value);
479 function print_import_action_output_as_xml($action_id, $outputs, $indent=
'')
481 echo
"$indent<output>\n";
482 echo
"$indent\t<action_id>$action_id</action_id>\n";
483 foreach ($outputs as $key => $value) {
484 echo
"$indent\t<$key>$value</$key>\n";
486 echo
"$indent</output>\n";