$option_value) {
if (in_array($option_name, ['h', 'help'])) {
fwrite(STDOUT, $help_text . PHP_EOL);
exit(0);
}
if (in_array($option_name, ['d','html-entity-decode'])) {
fwrite(STDOUT, "Set option: decode HTML entities" . PHP_EOL);
$html_decode = true;
continue;
}
// Set options which accept only one value
if (in_array($option_name, ['encoding']) && is_string($option_value)) {
$$option_name = $option_value;
fwrite(STDOUT, "Set option: {$option_name} - '{$option_value}'" . PHP_EOL);
continue;
}
if (in_array($option_name, ['c','copy-all'])) {
fwrite(STDOUT, "Set option: copy all data" . PHP_EOL);
$copy_all = true;
continue;
}
if (in_array($option_name, ['n','newlines'])) {
fwrite(STDOUT, "Set option: preserve newlines (convert
to newlines)" . PHP_EOL);
$preserve_newlines = true;
continue;
}
if (is_numeric($option_value) && (int) $option_value > 0) {
$option_name = str_replace('-', '_', $option_name);
$$option_name = $option_value;
fwrite(STDOUT, "Set option: $option_name" . PHP_EOL);
continue;
}
fwrite(
STDERR,
sprintf(
'ERROR: Option - %s - Invalid value provided, received type "%s"%s',
$option_name,
gettype($option_value),
PHP_EOL
)
);
fwrite(STDOUT, $help_text . PHP_EOL);
exit(1);
}
// Make sure we have everything we need before moving forward
if (!isset($html_field, $plaintext_field)) {
fwrite(STDERR, 'ERROR: Missing mandatory options!' . PHP_EOL . PHP_EOL);
fwrite(STDOUT, $help_text . PHP_EOL);
exit(1);
}
fwrite(STDOUT, "Removing HTML from field #{$html_field} and saving result in field #{$plaintext_field}" . PHP_EOL);
$html_rtf = get_resource_type_field($html_field);
if ($html_rtf === false) {
fwrite(STDERR, 'ERROR: Invalid metadata field for html-field option!' . PHP_EOL);
exit(1);
}
$plain_rtf = get_resource_type_field($plaintext_field);
if ($plain_rtf === false || !in_array($plain_rtf["type"], $TEXT_FIELD_TYPES)) {
fwrite(STDERR, 'ERROR: Invalid metadata field for plaintext-field option!' . PHP_EOL);
exit(1);
}
// Both fixed list fields and text fields are now using nodes underneath to store their values
if (in_array($html_rtf['type'], array_merge($FIXED_LIST_FIELD_TYPES, $TEXT_FIELD_TYPES))) {
$html_rtf_ref = $html_field;
$q = " SELECT rn.resource,
group_concat(n.`name` SEPARATOR ', ') AS `value`
FROM resource_node AS rn
INNER JOIN node AS n ON rn.node = n.ref
WHERE n.resource_type_field = ?
GROUP BY rn.resource";
$html_data = ps_query($q, ['i',$html_rtf_ref]);
}
$results = array_column($html_data ?? [], 'value', 'resource');
foreach ($results as $resource_ref => $html_value) {
if ($preserve_newlines) {
$html_value = str_replace('
', '\\n', $html_value);
}
$plaintxt_val = strip_tags($html_value);
if ($html_decode) {
if (isset($encoding)) {
$plaintxt_val = html_entity_decode($plaintxt_val, ENT_QUOTES, $encoding);
} else {
$plaintxt_val = html_entity_decode($plaintxt_val, ENT_QUOTES);
}
}
if (
!$copy_all
&& $html_value === $plaintxt_val
) {
continue;
}
if (trim($plaintxt_val) === '') {
fwrite(STDERR, "WARNING: Removing HTML for resource #{$resource_ref} results with no plain text data." . PHP_EOL);
continue;
}
$update_err = [];
if (update_field($resource_ref, $plaintext_field, $plaintxt_val, $update_err)) {
fwrite(STDOUT, "Updated resource #{$resource_ref}" . PHP_EOL);
} else {
fwrite(
STDERR,
"ERROR: Failed to update resource #{$resource_ref} with plain text value. Reason(s): " . implode('; ', $update_err) . PHP_EOL
);
}
}
fwrite(STDOUT, 'Successfully processed all records.' . PHP_EOL);