Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
migrate_metadata_text_to_multiple_text.php
1 <?php
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);
26 }
27 
28 require_once 'Console/Getopt.php';
29 
30 $shortopt = 's:';
31 $longopt = Array('from=', 'to=', 'delimiter=');
32 
33 $args = Console_Getopt::readPHPArgv();
34 array_shift($args);
35 $options = Console_Getopt::getopt($args, $shortopt, $longopt);
36 if (empty($options[0])) usage();
37 
38 $REPORT_ONLY = FALSE;
39 $FROM_ASSETID = FALSE;
40 $TO_ASSETID = FALSE;
41 $DELIMITER = ' ';
42 $SYSTEM_ROOT = '';
43 foreach ($options[0] as $option) {
44  switch ($option[0]) {
45  case 's':
46  if (empty($option[1])) usage();
47  if (!is_dir($option[1])) usage();
48  $SYSTEM_ROOT = $option[1];
49  break;
50  case '--from':
51  $FROM_ASSETID = $option[1];
52  break;
53  case '--to':
54  $TO_ASSETID = $option[1];
55  break;
56  case '--delimiter':
57  $DELIMITER = $option[1];
58  break;
59  }
60 
61 }
62 if (empty($SYSTEM_ROOT)) {
63  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
64  usage();
65 }
66 
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";
69  usage();
70 }
71 
72 if (empty($FROM_ASSETID)) usage();
73 if (empty($TO_ASSETID)) usage();
74 
75 
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';
79 
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";
83  exit(1);
84 }
85 
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";
89  exit(1);
90 }
91 $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
92 
93 // Find which assets contain both fields
94 $from_schema_link = $GLOBALS['SQ_SYSTEM']->am->getParents($from_asset->id, 'metadata_schema');
95 $from_schema_applied = Array();
96 
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));
99 }
100 
101 $to_schema_link = $GLOBALS['SQ_SYSTEM']->am->getParents($to_asset->id, 'metadata_schema');
102 $to_schema_applied = Array();
103 
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));
106 }
107 
108 $from_names = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($from_schema_applied, 'asset', FALSE, 'name');
109 
110 foreach ($from_schema_applied as $assetid) {
111  if (array_search($assetid, $to_schema_applied) === FALSE) {
112  continue;
113  }
114 
115  $asset_metadata = $mm->getMetadata($assetid);
116 
117  $field_value = $mm->getMetadataFieldValues($assetid);
118  $field_value = $field_value[$from_asset->name];
119 
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)) {
126  echo ' [ OK ]'."\n";
127  } else {
128  echo ' [FAILED]'."\n";
129  }
130  $mm->regenerateMetadata($assetid);
131  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
132 
133  }
134 
135 }
136 
137 exit(0);
138 
139 function usage()
140 {
141  echo 'Usage: '.basename($_SERVER['argv'][0]).' -s <system root> --from=<from asset ID> --to=<to asset ID> --delimiter=<delimiter>'."\n";
142  echo "\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";
148  echo "\n";
149  echo 'NOTES:'."\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";
154 
155  exit(1);
156 }//end usage();
157 ?>