Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
export_to_xml.php
1 <?php
28 /*
29  *
30  *
31  * Example usage:
32  * php scripts/import/export_to_xml.php . 3:35,4:36 1 >export.xml
33  *
34  * First argument specifies system root path
35  *
36  * Second argument specifies which asset should be moved underneath which parent asset,
37  * 3:35 means asset with id 3 will be moved underneath parent asset with id 35
38  *
39  * Third argument specifies create link type
40  *
41  */
42 
43 error_reporting(E_ALL);
44 ini_set('memory_limit', '1024M');
45 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
46 
47 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
48 if (empty($SYSTEM_ROOT)) {
49  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
50  exit();
51 }
52 
53 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
54  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
55  exit();
56 }
57 
58 $asset_infos = (isset($_SERVER['argv'][2])) ? explode(',',$_SERVER['argv'][2]) : Array();
59 if (empty($asset_infos)) {
60  echo "ERROR: You need to supply the asset id for the asset you want to export and parent asset it will link to as the second argument with format 3:75,4:46 (assetid 3 links to assetid 75, assetid 4 links to asset id 46)\n";
61  exit();
62 }
63 
64 $initial_link_type = (isset($_SERVER['argv'][3])) ? $_SERVER['argv'][3] : '';
65 if (empty($initial_link_type)) {
66  echo "ERROR: You need to supply the initial link type as the third argument\n";
67  exit();
68 }
69 
70 require_once $SYSTEM_ROOT.'/core/include/init.inc';
71 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
72 
73 // log in as root
74 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
75 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
76  echo "Failed logging in as root user\n";
77  exit();
78 }
79 $warned = FALSE;
80 $asset_id_map = Array();
81 
82 echo "<?xml version=\"1.0\" encoding=\"".SQ_CONF_DEFAULT_CHARACTER_SET."\"?>\n";
83 echo "<actions>\n";
84 
85 foreach($asset_infos as $asset_info) {
86  $asset_from_to_id = explode(':',$asset_info);
87  if(!isset($asset_from_to_id[0]) || !isset($asset_from_to_id[1])) trigger_error("Failed to parse second argument\n", E_USER_ERROR);
88  printCreateXML($asset_from_to_id[0], $asset_from_to_id[1], $initial_link_type);
89 }
90 foreach($asset_infos as $asset_info) {
91  $asset_from_to_id = explode(':',$asset_info);
92  if(!isset($asset_from_to_id[0])) trigger_error("Failed to parse second argument\n", E_USER_ERROR);
93  printAttributeXML($asset_from_to_id[0]);
94 }
95 foreach($asset_infos as $asset_info) {
96  $asset_from_to_id = explode(':',$asset_info);
97  if(!isset($asset_from_to_id[0])) trigger_error("Failed to parse second argument\n", E_USER_ERROR);
98  printMetadataXML($asset_from_to_id[0]);
99 }
100 foreach($asset_infos as $asset_info) {
101  $asset_from_to_id = explode(':',$asset_info);
102  if(!isset($asset_from_to_id[0])) trigger_error("Failed to parse second argument\n", E_USER_ERROR);
103  printNoticeLinksXML($asset_from_to_id[0]);
104 }
105 foreach($asset_infos as $asset_info) {
106  $asset_from_to_id = explode(':',$asset_info);
107  if(!isset($asset_from_to_id[0])) trigger_error("Failed to parse second argument\n", E_USER_ERROR);
108  printPermissionXML($asset_from_to_id[0]);
109 }
110 
111 echo "</actions>\n\n";
112 
113 
122  function printCreateXML($asset_id, $parent, $link_type, $value='', $is_dependant=0, $is_exclusive=0) {
123 
124  global $asset_id_map;
125 
126  $asset = &$GLOBALS['SQ_SYSTEM']->am->getAsset($asset_id);
127  if (is_null($asset)) exit();
128 
129  // system assets are not allowed to be exported.
130  if (replace_system_assetid($asset->id) != NULL) trigger_error("Can not export system asset!\n", E_USER_ERROR);
131 
132 
133  $action_code = getAssetType($asset).'_'.$asset->id;
134 
135  echo_headline('CREATING ASSET: '.$asset->name);
136  $asset_id_map[$asset->id] = $action_code;
137 
138  echo "<action>\n";
139  echo " <action_id>create_".$action_code."</action_id>\n";
140 
141  if ($GLOBALS['SQ_SYSTEM']->am->isTypeDecendant(getAssetType($asset), 'file')) {
142  $file_path = _saveFileAsset($asset);
143  echo " <action_type>create_file_asset</action_type>\n";
144  echo " <file_path>".$file_path."</file_path>\n";
145  } else {
146  echo " <action_type>create_asset</action_type>\n";
147  }
148 
149  echo " <type_code>".getAssetType($asset)."</type_code>\n";
150  echo " <link_type>".$link_type."</link_type>\n";
151  echo " <parentid>".$parent."</parentid>\n";
152  echo " <value>".$value."</value>\n";
153  echo " <is_dependant>".$is_dependant."</is_dependant>\n";
154  echo " <is_exclusive>".$is_exclusive."</is_exclusive>\n";
155  echo "</action>\n\n";
156 
157 
158  // then, if it has web paths, we add those
159  $paths = $asset->getWebPaths();
160  foreach ($paths as $path) {
161 
162  echo "<action>\n";
163  echo " <action_id>add_".$action_code."_path</action_id>\n";
164  echo " <action_type>add_web_path</action_type>\n";
165  echo " <asset>[[output://create_".$action_code.".assetid]]</asset>\n";
166  echo " <path>".$path."</path>\n";
167  echo "</action>\n\n";
168  }
169 
170  $dependants = Array();
171  if (getAssetType($asset) != 'Design_Customisation' && getAssetType($asset) != 'Design' && getAssetType($asset) != 'Design_Css') {
172  // Then, if it has dependant children, we add those too
173  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, TRUE);
174  } else if (getAssetType($asset) == 'Design' || getAssetType($asset) == 'Design_Css') {
175 
176  // okie this part is a bit tricky
177  // we need to check if
178  // - we are dealing with design
179  // - if design has a parse file attached
180  // - and the attribute name we are dealing is 'contents'
181  // - last but not least the value isnt not empty
182  $parse_file = $asset->data_path.'/parse.txt';
183  if (is_file($parse_file)) _updateParseFileForDesign($asset_id_map[$asset_id], $parse_file);
184 
185  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, '', FALSE, 'major', NULL, TRUE);
186 
187  } else if (getAssetType($asset) == 'Design_Customisation') {
188  // if we are dealing with customisations, then only deal with
189  // the design areas that are customised rest of the design areas
190  // will be generated once Matrix processes the parse file
191  $dependants = $asset->getCustomisedAreas();
192  // now this customistation can further have customisations
193  // take care of those ones too in our export
194  $dependants = array_merge($dependants, $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, 'design_customisation'));
195  }
196 
197  foreach ($dependants as $link_info) {
198  if (!strpos($link_info['minorid'], ':')) {
199  $parent = '[[output://create_'.$action_code.'.assetid]]';
200 
201  // If the asset already exists, let's link to it; otherwise, create it.
202  if (array_key_exists($link_info['minorid'], $asset_id_map)) {
203  printLinkXML($parent, $link_info, $action_code);
204  } else {
205  printCreateXML($link_info['minorid'], $parent, $link_info['link_type'], $link_info['value'], $link_info['is_dependant'], $link_info['is_exclusive']);
206  }
207 
208  }
209  }
210 
211  // Now let's do the non-dependant children, shall we?
212  $children = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, 0);
213  foreach ($children as $link_info) {
214  if (!strpos($link_info['minorid'], ':')) {
215  $parent = '[[output://create_'.$action_code.'.assetid]]';
216 
217  if (array_key_exists($link_info['minorid'], $asset_id_map)) {
218  printLinkXML($parent, $link_info, $action_code);
219  } else {
220  printCreateXML($link_info['minorid'], $parent, $link_info['link_type'], $link_info['value'], $link_info['is_dependant'], $link_info['is_exclusive']);
221  }
222 
223  }
224  }
225 
226 
227  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
228 
229  }//end printXML
230 
231  function printLinkXML($parent, $link_info, $action_code) {
232 
233  global $asset_id_map;
234 
235  // if we created the asset, get it from array, otherwise, if it's a system asset, we have to use system asset name.
236  if (isset($asset_id_map[$link_info['minorid']])){
237  $remap_id = "[[output://create_".$asset_id_map[$link_info['minorid']].".assetid]]";
238  }
239  else {
240  //before we use asset id, we should make sure if it's a system asset, we use system asset name instead of id.
241  //because new system may have different system assets ids.
242  $system_asset_name = replace_system_assetid($link_info['minorid']);
243  $system_asset_name == NULL ? $remap_id = $link_info['minorid'] : $remap_id = "[[system://".$system_asset_name."]]" ;
244  }
245 
246  echo "<action>\n";
247  echo " <action_id>link_".$action_code."</action_id>\n";
248  echo " <action_type>create_link</action_type>\n";
249  echo " <asset>".$remap_id."</asset>\n";
250  echo " <assetid>".$parent."</assetid>\n";
251  echo " <is_major>0</is_major>\n";
252  echo " <link_type>".$link_info['link_type']."</link_type>\n";
253  echo " <value>".$link_info['value']."</value>\n";
254  echo " <is_dependant>".$link_info['is_dependant']."</is_dependant>\n";
255  echo " <is_exclusive>".$link_info['is_exclusive']."</is_exclusive>\n";
256  echo "</action>\n\n";
257 
258  }
259 
260  function printAttributeXML($asset_id) {
261 
262  global $asset_id_map;
263 
264  $assets_done = Array();
265 
266  $asset = &$GLOBALS['SQ_SYSTEM']->am->getAsset($asset_id);
267  if (is_null($asset)) exit();
268 
269  // print some attributes
270  foreach ($asset->vars as $attr_name => $attr_info) {
271  $attr = $asset->getAttribute($attr_name);
272  $value = $attr->getContent();
273 
274  // unserilize it, we want clean script
275  if(isSerialized($value) && !empty($value)){
276  $value = var_export(unserialize($value),TRUE);
277  }
278  // if the value is an assetid attribute, we should match it with asset_id_map first
279  if (preg_match("/assetid/",$attr_name) && isset($asset_id_map[$value])){
280  $value = "[[output://create_".$asset_id_map[$value].".assetid]]";
281  }
282  $assets_done[$asset->id] = TRUE;
283 
284  if (!empty($value) && !((getAssetType($asset) == 'Design' || getAssetType($asset) == 'Design_Css') && $attr_name == 'contents')) {
285  // if default character set is utf-8 we dont wanna mess around the characters
286  // we are for now just checking for it not to be utf-8 but can append other encoding
287  // we might want to skip later on
288  if (strtolower(SQ_CONF_DEFAULT_CHARACTER_SET) != 'utf-8') {
289  $result = '';
290  // escape everything else (chars > 126)
291  for ($i = 0; $i < strlen($value); ++$i) {
292  $ord = ord($value[$i]);
293  if ($ord > 126) {
294  $result .= '&#'.$ord.';';
295  } else {
296  $result .= $value[$i];
297  }
298  }// end for
299 
300  if ($result !== '') $value = $result;
301  $value = preg_replace('/(\r\n)+/', '<br/>' ,$value);
302  }
303 
304  echo "<action>\n";
305  echo " <action_id>set_".$asset_id_map[$asset_id].'_'.$attr_name."</action_id>\n";
306  echo " <action_type>set_attribute_value</action_type>\n";
307  echo " <asset>[[output://create_".$asset_id_map[$asset_id].".assetid]]</asset>\n";
308  echo " <attribute>".$attr_name."</attribute>\n";
309  $value = _parseValue($value, $attr_name, 'set_'.$asset_id_map[$asset_id].'_'.$attr_name);
310 
311  echo " <value><![CDATA["._escapeCDATA($value)."]]></value>\n";
312  echo "</action>\n\n";
313  }
314  }
315 
316  $dependants = Array();
317  if (getAssetType($asset) != 'Design_Customisation' && getAssetType($asset) != 'Design' && getAssetType($asset) != 'Design_Css') {
318  // Then, if it has dependant children, we add those too
319  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, TRUE);
320  } else if (getAssetType($asset) == 'Design' || getAssetType($asset) == 'Design_Css') {
321  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, '', FALSE, 'major', NULL, TRUE);
322  } else if (getAssetType($asset) == 'Design_Customisation') {
323  // if we are dealing with customisations, then only deal with
324  // the design areas that are customised rest of the design areas
325  // will be generated once Matrix processes the parse file
326  $dependants = $asset->getCustomisedAreas();
327  // now this customistation can further have customisations
328  // take care of those ones too in our export
329  $dependants = array_merge($dependants, $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, 'design_customisation'));
330  }
331 
332  foreach ($dependants as $link_info) {
333  if (!strpos($link_info['minorid'], ':')) {
334  if (!array_key_exists($link_info['minorid'], $assets_done)) {
335  printAttributeXML($link_info['minorid']);
336  }
337  }
338  }
339 
340  // Now let's do the non-dependant children, shall we?
341  $children = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, 0);
342  foreach ($children as $link_info) {
343  if (!strpos($link_info['minorid'], ':')) {
344  if (!array_key_exists($link_info['minorid'], $assets_done)) {
345  printAttributeXML($link_info['minorid']);
346  }
347  }
348  }
349 
350  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
351 
352  }//end printAttributeXML()
353 
354  function printNoticeLinksXML($asset_id) {
355 
356  global $asset_id_map;
357 
358  $assets_done = Array();
359 
360  $notice_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset_id, SQ_LINK_NOTICE, '', FALSE, 'major');
361  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset_id);
362 
363  foreach ($notice_links as $notice_link) {
364  if (strpos($notice_link['minorid'], ':')) continue;
365  // if we created the asset, get it from array, otherwise, if it's a system asset, we have to use system asset name.
366  if (isset($asset_id_map[$asset_id])){
367  $remap_id = "[[output://create_".$asset_id_map[$asset_id].".assetid]]" ;
368  }
369  else {
370  //before we use asset id, we should make sure if it's a system asset, we use system asset name instead of id.
371  //because new system may have different system assets ids.
372  $system_asset_name = replace_system_assetid($asset_id);
373  $system_asset_name == NULL ? $remap_id = $asset_id : $remap_id = "[[system://".$system_asset_name."]]" ;
374  }
375  printLinkXML($remap_id, $notice_link, 'notice_'.$asset_id.'_to_'.$notice_link['minorid']);
376 
377  }//end foreach
378 
379  $assets_done[$asset_id] = TRUE;
380 
381  $dependants = Array();
382  if (getAssetType($asset) != 'Design_Customisation' && getAssetType($asset) != 'Design') {
383  // Then, if it has dependant children, we add those too
384  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, TRUE);
385  } else if (getAssetType($asset) == 'Design') {
386  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, '', FALSE, 'major', NULL, TRUE);
387  } else if (getAssetType($asset) == 'Design_Customisation') {
388  // if we are dealing with customisations, then only deal with
389  // the design areas that are customised rest of the design areas
390  // will be generated once Matrix processes the parse file
391  $dependants = $asset->getCustomisedAreas();
392  // now this customistation can further have customisations
393  // take care of those ones too in our export
394  $dependants = array_merge($dependants, $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, 'design_customisation'));
395  }
396 
397  foreach ($dependants as $link_info) {
398  if (!strpos($link_info['minorid'], ':')) {
399  if (!array_key_exists($link_info['minorid'], $assets_done)) {
400  printNoticeLinksXML($link_info['minorid']);
401  }
402  }
403  }
404 
405  // Now let's do the non-dependant children, shall we?
406  $children = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset_id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, 0);
407  foreach ($children as $link_info) {
408  if (!strpos($link_info['minorid'], ':')) {
409  if (!array_key_exists($link_info['minorid'], $assets_done)) {
410  printNoticeLinksXML($link_info['minorid']);
411  }
412  }
413  }
414 
415 
416  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
417 
418  }//end printNoticeLinksXML()
419 
420  function printPermissionXML($asset_id) {
421 
422  global $asset_id_map;
423 
424  $assets_done = Array();
425  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset_id);
426 
427  // Now, some permissions
428  $read_permissions = $GLOBALS['SQ_SYSTEM']->am->getPermission($asset_id, SQ_PERMISSION_READ, NULL, FALSE, FALSE, TRUE, TRUE);
429  foreach ($read_permissions as $permission_id => $permission_granted) {
430  // if the asset is created by this script, use asset_id_map
431  if (isset($asset_id_map[$permission_id])){
432  $remap_id = "[[output://create_".$asset_id_map[$permission_id].".assetid]]" ;
433  }
434  else {
435  //if it's a system asset, we use system asset name instead of id.
436  //because new system may have different system assets ids.
437  $system_asset_name = replace_system_assetid($permission_id);
438  $system_asset_name == NULL ? $remap_id = $permission_id : $remap_id = "[[system://".$system_asset_name."]]" ;
439  }
440 
441 
442 
443  // if the action is to deny a previous permission, make sure we will remove the permission first
444  // and if there is no existing permission to remove, fail silently
445  if($permission_granted == 0) {
446  echo "<action>\n";
447  echo " <action_id>set_permission_".$asset_id."_read_".$permission_id."</action_id>\n";
448  echo " <action_type>set_permission</action_type>\n";
449  echo " <asset>[[output://create_".$asset_id_map[$asset_id].".assetid]]</asset>\n";
450  echo " <permission>1</permission>\n";
451  echo " <mute_error>1</mute_error>\n";
452  echo " <granted>-1</granted>\n";
453  echo " <userid>".$remap_id."</userid>\n";
454  echo "</action>\n";
455  }
456  echo "<action>\n";
457  echo " <action_id>set_permission_".$asset_id."_read_".$permission_id."</action_id>\n";
458  echo " <action_type>set_permission</action_type>\n";
459  echo " <asset>[[output://create_".$asset_id_map[$asset_id].".assetid]]</asset>\n";
460  echo " <permission>1</permission>\n";
461  echo " <granted>".$permission_granted."</granted>\n";
462  echo " <userid>".$remap_id."</userid>\n";
463  echo "</action>\n";
464 
465  }//end foreach
466 
467  $write_permissions = $GLOBALS['SQ_SYSTEM']->am->getPermission($asset_id, SQ_PERMISSION_WRITE, NULL, FALSE, FALSE, TRUE, TRUE);
468  foreach ($write_permissions as $permission_id => $permission_granted) {
469 
470  // if the asset is created by this script, use asset_id_map
471  if (isset($asset_id_map[$permission_id])){
472  $remap_id = "[[output://create_".$asset_id_map[$permission_id].".assetid]]" ;
473  }
474  else {
475  //if it's a system asset, we use system asset name instead of id.
476  //because new system may have different system assets ids.
477  $system_asset_name = replace_system_assetid($permission_id);
478  $system_asset_name == NULL ? $remap_id = $permission_id : $remap_id = "[[system://".$system_asset_name."]]" ;
479  }
480 
481  // if the action is to deny a previous permission, make sure we will remove the permission first
482  // and if there is no existing permission to remove, fail silently
483  if($permission_granted == 0) {
484  echo "<action>\n";
485  echo " <action_id>set_permission_".$asset_id."_write_".$permission_id."</action_id>\n";
486  echo " <action_type>set_permission</action_type>\n";
487  echo " <asset>[[output://create_".$asset_id_map[$asset_id].".assetid]]</asset>\n";
488  echo " <permission>2</permission>\n";
489  echo " <mute_error>1</mute_error>\n";
490  echo " <granted>-1</granted>\n";
491  echo " <userid>".$remap_id."</userid>\n";
492  echo "</action>\n";
493  }
494  echo "<action>\n";
495  echo " <action_id>set_permission_".$asset_id."_write_".$permission_id."</action_id>\n";
496  echo " <action_type>set_permission</action_type>\n";
497  echo " <asset>[[output://create_".$asset_id_map[$asset_id].".assetid]]</asset>\n";
498  echo " <permission>2</permission>\n";
499  echo " <granted>".$permission_granted."</granted>\n";
500  echo " <userid>".$remap_id."</userid>\n";
501  echo "</action>\n";
502 
503  }//end foreach
504 
505  $admin_permissions = $GLOBALS['SQ_SYSTEM']->am->getPermission($asset_id, SQ_PERMISSION_ADMIN, NULL, FALSE, FALSE, TRUE, TRUE);
506  foreach ($admin_permissions as $permission_id => $permission_granted) {
507 
508  if (isset($asset_id_map[$permission_id])){
509  $remap_id = "[[output://create_".$asset_id_map[$permission_id].".assetid]]" ;
510  }
511  else {
512  $system_asset_name = replace_system_assetid($permission_id);
513  $system_asset_name == NULL ? $remap_id = $permission_id : $remap_id = "[[system://".$system_asset_name."]]" ;
514  }
515 
516  // if the action is to deny a previous permission, make sure we will remove the permission first
517  // and if there is no existing permission to remove, fail silently
518  if($permission_granted == 0) {
519  echo "<action>\n";
520  echo " <action_id>set_permission_".$asset_id."_admin_".$permission_id."</action_id>\n";
521  echo " <action_type>set_permission</action_type>\n";
522  echo " <asset>[[output://create_".$asset_id_map[$asset_id].".assetid]]</asset>\n";
523  echo " <permission>3</permission>\n";
524  echo " <mute_error>1</mute_error>\n";
525  echo " <granted>-1</granted>\n";
526  echo " <userid>".$remap_id."</userid>\n";
527  echo "</action>\n";
528  }
529  echo "<action>\n";
530  echo " <action_id>set_permission_".$asset_id."_admin_".$permission_id."</action_id>\n";
531  echo " <action_type>set_permission</action_type>\n";
532  echo " <asset>[[output://create_".$asset_id_map[$asset_id].".assetid]]</asset>\n";
533  echo " <permission>3</permission>\n";
534  echo " <granted>".$permission_granted."</granted>\n";
535  echo " <userid>".$remap_id."</userid>\n";
536  echo "</action>\n";
537 
538  }//end foreach
539 
540  $assets_done[$asset_id] = TRUE;
541 
542  $dependants = Array();
543  if (getAssetType($asset) != 'Design_Customisation' && getAssetType($asset) != 'Design') {
544  // Then, if it has dependant children, we add those too
545  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, TRUE);
546  } else if (getAssetType($asset) == 'Design') {
547  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, '', FALSE, 'major', NULL, TRUE);
548  } else if (getAssetType($asset) == 'Design_Customisation') {
549  // if we are dealing with customisations, then only deal with
550  // the design areas that are customised rest of the design areas
551  // will be generated once Matrix processes the parse file
552  $dependants = $asset->getCustomisedAreas();
553  // now this customistation can further have customisations
554  // take care of those ones too in our export
555  $dependants = array_merge($dependants, $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, 'design_customisation'));
556  }
557 
558  foreach ($dependants as $link_info) {
559  if (!strpos($link_info['minorid'], ':')) {
560  if (!array_key_exists($link_info['minorid'], $assets_done)) {
561  printPermissionXML($link_info['minorid']);
562  }
563  }
564  }
565 
566  // Now let's do the non-dependant children, shall we?
567  $children = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset_id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, 0);
568  foreach ($children as $link_info) {
569  if (!strpos($link_info['minorid'], ':')) {
570  if (!array_key_exists($link_info['minorid'], $assets_done)) {
571  printPermissionXML($link_info['minorid']);
572  }
573  }
574  }
575 
576 
577  }//end printPermissionXML()
578 
579 
591  function _saveFileAsset(&$asset, $file=Array())
592  {
593  if (!is_null($asset)) {
594  $file_info = $asset->getExistingFile();
595  if (empty($file_info)) {
596  trigger_error('File not found in data directory for the file asset #'.$asset->id, E_USER_ERROR);
597  }
598  $file_type = getAssetType($asset);
599  $assetid = $asset->id;
600  } else {
601  $file_info = $file;
602  $file_type = $file['type'];
603  $assetid = substr($file_info['filename'], 0 ,strpos($file_info['filename'], '_'));
604  }
605 
606  $export_path = 'export/'.$file_type.'/'.$assetid.'/'.$file_info['filename'];
607 
608  // check to see if an export/ directory exists. If not, create it.
609  if (!file_exists('export/')) {
610  mkdir("export", 0775);
611  }
612 
613  // echeck to see if the type_code directory exists
614  if (!file_exists('export/'.$file_type.'/')) {
615  mkdir('export/'.$file_type, 0775);
616  }
617 
618  // create a directory for this file based on the assetid
619  mkdir('export/'.$file_type.'/'.$assetid, 0775);
620 
621  if (copy($file_info['path'], $export_path)) {
622  return $export_path;
623  } else {
624  die('Could not copy file');
625  }
626 
627 
628  }//end _saveFileAsset()
629 
630 
642  function _parseValue($value, $attribute_name, $actionid)
643  {
644  global $asset_id_map;
645  global $warned;
646  $assetid_mapped = Array();
647 
648  $shadow_reg = '|.\/?a=(\d+:\w*)|';
649  $normal_reg = '|.\/?a=(\d+)|';
650  $shadow_matches = Array();
651  $normal_matches = Array();
652 
653  preg_match_all($shadow_reg, $value, $shadow_matches);
654  preg_match_all($normal_reg, $value, $normal_matches);
655 
656  if(isset($shadow_matches[1])) $shadow_matches = $shadow_matches[1];
657  if(isset($normal_matches[1])) $normal_matches = $normal_matches[1];
658  $replace_assetids = Array();
659  foreach ($shadow_matches as $data) {
660  $replace_assetids[] = $data;
661  }
662  foreach ($normal_matches as $data) {
663  $replace_assetids[] = $data;
664  }
665 
666  foreach ($replace_assetids as $replace_assetid) {
667  if (isset($asset_id_map[$replace_assetid])) {
668  $value = preg_replace('|.\/?a='.$replace_assetid.'|', '?a=[[output://create_'.$asset_id_map[$replace_assetid].'.assetid]]', $value);
669  $assetid_mapped[] = $replace_assetid;
670  }
671  }
672 
673  $globals_processed = FALSE;
674  // we have got the /?a=XX type assetids to be replaced
675  // lets not leave away the %globals_xxxxxx_xxx:assetid%
676  $kywrd_reg_global_shdw = '|%globals_asset_[a-z]+:(\d+:\w*)%|';
677  $kywrd_reg_global_real = '|%globals_asset_[a-z]+:(\d+)%|';
678  preg_match_all($kywrd_reg_global_shdw, $value, $globals_match);
679  preg_match_all($kywrd_reg_global_real, $value, $globals_match_2);
680 
681  if(isset($globals_match[1]))$globals_match = $globals_match[1];
682  if(isset($globals_match_2[1]))$globals_match_2 = $globals_match_2[1];
683 
684  $replace_globals_assetids = Array();
685  foreach ($globals_match as $data) {
686  $replace_globals_assetids[] = $data;
687  }
688  foreach ($globals_match_2 as $data) {
689  $replace_globals_assetids[] = $data;
690  }
691 
692  foreach ($replace_globals_assetids as $replace_globals_assetid) {
693  if (isset($asset_id_map[$replace_globals_assetid])) {
694  $value = preg_replace('|%globals_asset_([a-z]+):'.$replace_globals_assetid.'%|',
695  '%globals_asset_$1:[[output://create_'.$asset_id_map[$replace_globals_assetid].'.assetid]]',
696  $value);
697  $assetid_mapped[] = $replace_globals_assetid;
698  $globals_processed = TRUE;
699  }
700  }
701 
702  // lastly if we are adding the 'additional_get' attribute
703  // check for the directly mentioned assetids
704  // do this only if we have NOT processed globals keywords
705  if (!$globals_processed && $attribute_name != 'html' && $attribute_name != 'quiz_answers' && $attribute_name != 'statuses') {
706  preg_match_all('/\'([0-9]+)\'/', $value, $assetid_match);
707  if (isset($assetid_match[1])) {
708  foreach (array_unique($assetid_match[1]) as $assetid) {
709  // check to see
710  // - the assetid isnt 0
711  // - the asset exists on the system
712  // - and also that we are importing it
713  // if any of the above fails we can be certain that it is something we do nto want
714  if ($assetid != '0' && array_key_exists($assetid, $asset_id_map) && $GLOBALS['SQ_SYSTEM']->am->assetExists($assetid)) {
715  $value = preg_replace('/'.$assetid.'/', '[[output://create_'.$asset_id_map[$assetid].'.assetid]]', $value);
716  $assetid_mapped[] = $assetid;
717  }
718  }
719  }
720  }// end if
721 
722  if(!empty($assetid_mapped)) {
723  if(!$warned) {
724  echo_text("\n\n\nWARNING : Matrix has found following Assetid(s) appearing in attribute value and mapped. This should be reviewed !!!\n");
725  $warned = TRUE;
726  }
727 
728  $mapped_str = implode('", "', $assetid_mapped);
729  $echo_str = '"'.$mapped_str.'" for Action ID "'.$actionid.'" to set attribute "'.$attribute_name.'"'."\n";
730  echo_text($echo_str);
731  }
732 
733  return $value;
734 
735  }//end _parseValue()
736 
737 
738  function printMetadataXML($asset_id) {
739 
740  global $asset_id_map;
741 
742  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
743  $assets_done = Array();
744 
745  $asset = &$GLOBALS['SQ_SYSTEM']->am->getAsset($asset_id);
746  if (is_null($asset)) exit();
747 
748  foreach($mm->getSchemas($asset_id) as $schema_id => $granted) {
749  $schema_info = $mm->getAssetSchemaInfo($asset_id, $schema_id);
750  echo "<action>\n";
751  echo " <action_id>set_".$asset_id_map[$asset_id]."_metadata_schema_".$schema_id."</action_id>\n";
752  echo " <action_type>set_metadata_schema</action_type>\n";
753  echo " <asset>[[output://create_".$asset_id_map[$asset_id].".assetid]]</asset>\n";
754  echo " <schemaid>".(isset($asset_id_map[$schema_id]) ? "[[output://create_".$asset_id_map[$schema_id].".assetid]]" : $schema_id)."</schemaid>\n";
755  echo " <granted>".$schema_info['granted']."</granted>\n";
756  echo " <cascades>".$schema_info['cascades']."</cascades>\n";
757  echo "</action>\n\n";
758 
759  $metadata = $mm->getMetadata($asset_id, $schema_id);
760  foreach ($metadata as $field_id => $field_info) {
761  $field_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($mm->getFieldAssetIdFromName($asset_id, $field_info[0]['name']));
762  if($field_asset->type() == 'metadata_field_text') {
763  $value = $field_info[0]['value'];
764  if (strtolower(SQ_CONF_DEFAULT_CHARACTER_SET) != 'utf-8') {
765  $result = '';
766  // escape everything else (chars > 126)
767  for ($i = 0; $i < strlen($value); ++$i) {
768  $ord = ord($value[$i]);
769  if ($ord > 126) {
770  $result .= '&#'.$ord.';';
771  } else {
772  $result .= $value[$i];
773  }
774  }// end for
775  if ($result !== '') $value = $result;
776  $value = preg_replace('/(\r\n)+/', '<br/>' ,$value);
777  }
778  $field_info[0]['value']= $value;
779  }
780 
781  echo "<action>\n";
782  echo " <action_id>set_".$asset_id_map[$asset_id]."_metadata_field_".$field_id."</action_id>\n";
783  echo " <action_type>set_metadata_value</action_type>\n";
784  echo " <asset>[[output://create_".$asset_id_map[$asset_id].".assetid]]</asset>\n";
785  echo " <fieldid>".(isset($asset_id_map[$field_id]) ? "[[output://create_".$asset_id_map[$field_id].".assetid]]" : $field_id)."</fieldid>\n";
786  echo " <value><![CDATA["._escapeCDATA($field_info[0]['value'])."]]></value>\n";
787  echo "</action>\n\n";
788  } // end foreach metadata
789  } // end foreach schema
790 
791  $dependants = Array();
792  if (getAssetType($asset) != 'Design_Customisation' && getAssetType($asset) != 'Design') {
793  // Then, if it has dependant children, we add those too
794  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, TRUE);
795  } else if (getAssetType($asset) == 'Design') {
796  $dependants = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, '', FALSE, 'major', NULL, TRUE);
797  } else if (getAssetType($asset) == 'Design_Customisation') {
798  // if we are dealing with customisations, then only deal with
799  // the design areas that are customised rest of the design areas
800  // will be generated once Matrix processes the parse file
801  $dependants = $asset->getCustomisedAreas();
802  // now this customistation can further have customisations
803  // take care of those ones too in our export
804  $dependants = array_merge($dependants, $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_BACKEND_NAV, 'design_customisation'));
805  }
806 
807  foreach ($dependants as $link_info) {
808  if (!strpos($link_info['minorid'], ':')) {
809  if (!array_key_exists($link_info['minorid'], $assets_done)) {
810  printMetadataXML($link_info['minorid']);
811  }
812  }
813  }
814 
815  // Now let's do the non-dependant children, shall we?
816  $children = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_SC_LINK_SIGNIFICANT, '', FALSE, 'major', NULL, 0);
817  foreach ($children as $link_info) {
818  if (!strpos($link_info['minorid'], ':')) {
819  if (!array_key_exists($link_info['minorid'], $assets_done)) {
820  printMetadataXML($link_info['minorid']);
821  }
822  }
823  }
824 
825  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
826 
827  }//end printMetadataXML()
828 
829 
837  function getAssetType(&$asset)
838  {
839  $class = get_class($asset);
840  return $class;
841 
842  }//end getAssetType()
843 
852  function echo_headline($s)
853  {
854  fwrite(STDERR, "--------------------------------------\n$s\n--------------------------------------\n");
855 
856  }//end echo_headline()
857 
858 
867  function echo_text($s)
868  {
869  fwrite(STDERR, "$s\n");
870 
871  }//end echo_text()
872 
881  function replace_system_assetid($assetid)
882  {
883  foreach($GLOBALS['SQ_SYSTEM']->am->_system_assetids as $asset_name => $asset_id){
884  if($assetid == $asset_id) {
885  return $asset_name;
886  }
887  }
888  return NULL;
889 
890  }// end replace_system_assetid()
891 
892 
901  function isSerialized($str)
902  {
903  return ($str == serialize(false) || @unserialize($str) !== false);
904 
905  }// end isSerialized()
906 
907 
916  function _escapeCDATA($value)
917  {
918  return preg_replace('|(<\!\[CDATA\[.*?)\]\]>|ms', '$1]]]]><![CDATA[>', $value);
919 
920  }// end _escapeCDATA()
921 
922 
931  function _updateParseFileForDesign($design_id, $parse_file_path)
932  {
933  $file_info = Array(
934  'filename' => $design_id.'_parse.txt',
935  'path' => $parse_file_path,
936  'type' => 'txt_file',
937  );
938 
939  $null_asset = NULL;
940 
941  // lets copy the parse file to a location and
942  // then add an action to add it to our new design asset
943  $new_file_path = _saveFileAsset($null_asset, $file_info);
944 
945  if ($new_file_path != '') {
946  echo "<action>\n";
947  echo " <action_id>set_".$design_id."_parse_file</action_id>\n";
948  echo " <action_type>set_design_parse_file</action_type>\n";
949  echo " <asset>[[output://create_".$design_id.".assetid]]</asset>\n";
950  echo " <file_path>".$new_file_path."</file_path>\n";
951  echo "</action>\n\n";
952  }
953 
954  }// end _updateParseFileForDesign()
955 
956 
957 ?>