diff --git a/plugins/Pinboard/Controller/APIv1.php b/plugins/Pinboard/Controller/APIv1.php index 90e663f965..bd909d494e 100644 --- a/plugins/Pinboard/Controller/APIv1.php +++ b/plugins/Pinboard/Controller/APIv1.php @@ -393,21 +393,26 @@ class APIv1 extends Controller $user = $check; } - $tags_freq = []; + // Convert the list of all tags to a map from canon to + // variations (i.e. tags that canonicalize to the same) + $canon_to_variations = []; foreach ($user->getActor()->getNoteTags(Pin::note_type) as $tag) { - if (!isset($tags_freq[$tag->getCanonical()])) { - $tags_freq[$tag->getCanonical()] = []; + if (!isset($canon_to_variations[$tag->getCanonical()])) { + $canon_to_variations[$tag->getCanonical()] = []; } - $tags_freq[$tag->getCanonical()][] = $tag->getTag(); + $canon_to_variations[$tag->getCanonical()][] = $tag->getTag(); } - foreach ($tags_freq as $canon => $variations) { + // Pick the most common variation and assign to it the count + // of all the variations of each canon tag + $most_common_variant_frequencies = []; + foreach ($canon_to_variations as $variations) { $freqs = array_count_values($variations); arsort($freqs); - $tags_freq[$canon] = $freqs[array_key_first($freqs)]; + $most_common_variant_frequencies[array_key_first($freqs)] = \count($variations); } - return self::respond($tags_freq); + return self::respond($most_common_variant_frequencies); } public function unimplmented(Request $request)