23 error_reporting(E_ALL);
24 if ((php_sapi_name() !=
'cli')) {
25 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
28 require_once
'Console/Getopt.php';
31 $longopt = Array(
'from=',
'to=',
'delimiter=');
33 $args = Console_Getopt::readPHPArgv();
35 $options = Console_Getopt::getopt($args, $shortopt, $longopt);
36 if (empty($options[0])) usage();
39 $FROM_ASSETID = FALSE;
43 foreach ($options[0] as $option) {
46 if (empty($option[1])) usage();
47 if (!is_dir($option[1])) usage();
48 $SYSTEM_ROOT = $option[1];
51 $FROM_ASSETID = $option[1];
54 $TO_ASSETID = $option[1];
57 $DELIMITER = $option[1];
62 if (empty($SYSTEM_ROOT)) {
63 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
67 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
68 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
72 if (empty($FROM_ASSETID)) usage();
73 if (empty($TO_ASSETID)) usage();
76 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
77 define(
'SQ_SYSTEM_ROOT', $SYSTEM_ROOT);
78 include_once $SYSTEM_ROOT.
'/core/include/init.inc';
80 $from_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($FROM_ASSETID);
81 if (empty($from_asset) || !is_a($from_asset,
'metadata_field_text')) {
82 echo
'ERROR: Asset #"'.$FROM_ASSETID.
'" is not of type Metadata Text Field'.
"\n";
86 $to_asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($TO_ASSETID);
87 if (empty($to_asset) || !is_a($to_asset,
'metadata_field_multiple_text')) {
88 echo
'ERROR: Asset #"'.$TO_ASSETID.
'" is not of type Metadata Multiple Text Field'.
"\n";
91 $mm = $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
94 $from_schema_link = $GLOBALS[
'SQ_SYSTEM']->am->getParents($from_asset->id,
'metadata_schema');
95 $from_schema_applied = Array();
97 foreach ($from_schema_link as $from_schema_assetid => $schema_type) {
98 $from_schema_applied = array_merge($from_schema_applied, $mm->getSchemaAssets($from_schema_assetid, TRUE));
101 $to_schema_link = $GLOBALS[
'SQ_SYSTEM']->am->getParents($to_asset->id,
'metadata_schema');
102 $to_schema_applied = Array();
104 foreach ($to_schema_link as $to_schema_assetid => $schema_type) {
105 $to_schema_applied = array_merge($to_schema_applied, $mm->getSchemaAssets($to_schema_assetid, TRUE));
108 $from_names = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo($from_schema_applied,
'asset', FALSE,
'name');
110 foreach ($from_schema_applied as $assetid) {
111 if (array_search($assetid, $to_schema_applied) === FALSE) {
115 $asset_metadata = $mm->getMetadata($assetid);
117 $field_value = $mm->getMetadataFieldValues($assetid);
118 $field_value = $field_value[$from_asset->name];
120 if (!empty($field_value)) {
121 $field_list = explode($DELIMITER, $field_value);
122 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
123 $asset_metadata[$to_asset->id][
'value'] = implode(
'; ', $field_list);
124 echo str_pad(
'#'.$assetid.
' '.$from_names[$assetid], 60);
125 if ($mm->setMetadata($assetid, $asset_metadata)) {
128 echo
' [FAILED]'.
"\n";
130 $mm->regenerateMetadata($assetid);
131 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
141 echo
'Usage: '.basename($_SERVER[
'argv'][0]).
' -s <system root> --from=<from asset ID> --to=<to asset ID> --delimiter=<delimiter>'.
"\n";
143 echo
' -s : the Matrix system root path'.
"\n";
144 echo
' --from : the asset ID of the Metadata Text Field to migrate from'.
"\n";
145 echo
' --to : the asset ID of the Metadata Multiple Text Field to migrate to'.
"\n";
146 echo
' --delimiter : delimiter between items (default is a single space) - multiple'.
"\n";
147 echo
' characters are allowed'.
"\n";
150 echo
'* If your delimiter contains a space, you may need to place your delimiter'.
"\n";
151 echo
' string in quotation marks - eg. --delimiter=", "'.
"\n";
152 echo
'* It is recommended to use the fully qualified path to your Matrix root to'.
"\n";
153 echo
' avoid warnings relating to file versioning in some assets (ie. not ".").'.
"\n";