From 260eb678f9a4e96d39ec4f941e52ad1d91d25740 Mon Sep 17 00:00:00 2001 From: Ben XO Date: Sun, 29 Dec 2019 02:13:34 +0000 Subject: [PATCH 1/2] Use comments_html content already generated by modules instead of recreating it. --- getid3/getid3.lib.php | 95 ++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/getid3/getid3.lib.php b/getid3/getid3.lib.php index bf32ccea..7ea0079b 100644 --- a/getid3/getid3.lib.php +++ b/getid3/getid3.lib.php @@ -1535,53 +1535,64 @@ public static function ImageExtFromMime($mime_type) { */ public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true) { + $comment_arrays = array('comments'); + if($option_tags_html) { + $comment_arrays[] = 'comments_html'; + } // Copy all entries from ['tags'] into common ['comments'] if (!empty($ThisFileInfo['tags'])) { foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) { foreach ($tagarray as $tagname => $tagdata) { foreach ($tagdata as $key => $value) { - if (!empty($value)) { - if (empty($ThisFileInfo['comments'][$tagname])) { - - // fall through and append value - - } elseif ($tagtype == 'id3v1') { - - $newvaluelength = strlen(trim($value)); - foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { - $oldvaluelength = strlen(trim($existingvalue)); - if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) { - // new value is identical but shorter-than (or equal-length to) one already in comments - skip - break 2; + foreach($comment_arrays as $comments) { + if ($comments != 'comments' && $key == 'picture') { + // pictures can take up a lot of space, and we don't need multiple copies of them + // let there be a single copy in [comments][picture], and not elsewhere + continue; + } + if (!empty($value)) { + if (empty($ThisFileInfo[$comments][$tagname])) { + + // fall through and append value + + } elseif ($tagtype == 'id3v1') { + + $newvaluelength = strlen(trim($value)); + foreach ($ThisFileInfo[$comments][$tagname] as $existingkey => $existingvalue) { + $oldvaluelength = strlen(trim($existingvalue)); + if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) { + // new value is identical but shorter-than (or equal-length to) one already in comments - skip + break 2; + } } - } - - } elseif (!is_array($value)) { - - $newvaluelength = strlen(trim($value)); - foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { - $oldvaluelength = strlen(trim($existingvalue)); - if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) { - $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value); - //break 2; - break; + + } elseif (!is_array($value)) { + + $newvaluelength = strlen(trim($value)); + foreach ($ThisFileInfo[$comments][$tagname] as $existingkey => $existingvalue) { + $oldvaluelength = strlen(trim($existingvalue)); + if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) { + $ThisFileInfo[$comments][$tagname][$existingkey] = trim($value); + //break 2; + break; + } } + } - - } - if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) { - $value = (is_string($value) ? trim($value) : $value); - if (!is_int($key) && !ctype_digit($key)) { - $ThisFileInfo['comments'][$tagname][$key] = $value; - } else { - if (!isset($ThisFileInfo['comments'][$tagname])) { - $ThisFileInfo['comments'][$tagname] = array($value); + if (is_array($value) || empty($ThisFileInfo[$comments][$tagname]) || !in_array(trim($value), $ThisFileInfo[$comments][$tagname])) { + $value = (is_string($value) ? trim($value) : $value); + if (!is_int($key) && !ctype_digit($key)) { + $ThisFileInfo[$comments][$tagname][$key] = $value; } else { - $ThisFileInfo['comments'][$tagname][] = $value; + if (!isset($ThisFileInfo[$comments][$tagname])) { + $ThisFileInfo[$comments][$tagname] = array($value); + } else { + $ThisFileInfo[$comments][$tagname][] = $value; + } } } } - } + } } } } @@ -1592,14 +1603,16 @@ public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true 'track' => 'track_number', ); foreach ($StandardizeFieldNames as $badkey => $goodkey) { - if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) { - $ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey]; - unset($ThisFileInfo['comments'][$badkey]); - } + foreach($comment_arrays as $comments) { + if (array_key_exists($badkey, $ThisFileInfo[$comments]) && !array_key_exists($goodkey, $ThisFileInfo[$comments])) { + $ThisFileInfo[$comments][$goodkey] = $ThisFileInfo[$comments][$badkey]; + unset($ThisFileInfo[$comments][$badkey]); + } + } } if ($option_tags_html) { - // Copy to ['comments_html'] + // Copy to ['comments_html'], if not already present. if (!empty($ThisFileInfo['comments'])) { foreach ($ThisFileInfo['comments'] as $field => $values) { if ($field == 'picture') { @@ -1610,7 +1623,7 @@ public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true foreach ($values as $index => $value) { if (is_array($value)) { $ThisFileInfo['comments_html'][$field][$index] = $value; - } else { + } elseif(empty($ThisFileInfo['comments_html'][$field][$index])) { $ThisFileInfo['comments_html'][$field][$index] = str_replace('�', '', self::MultiByteCharString2HTML($value, $ThisFileInfo['encoding'])); } } From bd3501c815586f8292316f832d5ff8425f466ba0 Mon Sep 17 00:00:00 2001 From: Ben XO Date: Sun, 29 Dec 2019 08:08:30 +0000 Subject: [PATCH 2/2] Revert block which breaks with tag_options_html true (track numbers don't appear in the comments_html) --- getid3/getid3.lib.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/getid3/getid3.lib.php b/getid3/getid3.lib.php index 7ea0079b..d588d82d 100644 --- a/getid3/getid3.lib.php +++ b/getid3/getid3.lib.php @@ -1603,12 +1603,10 @@ public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true 'track' => 'track_number', ); foreach ($StandardizeFieldNames as $badkey => $goodkey) { - foreach($comment_arrays as $comments) { - if (array_key_exists($badkey, $ThisFileInfo[$comments]) && !array_key_exists($goodkey, $ThisFileInfo[$comments])) { - $ThisFileInfo[$comments][$goodkey] = $ThisFileInfo[$comments][$badkey]; - unset($ThisFileInfo[$comments][$badkey]); - } - } + if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) { + $ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey]; + unset($ThisFileInfo['comments'][$badkey]); + } } if ($option_tags_html) {