From 65638384fce9f15443e2a6896bb47db920b0a32c Mon Sep 17 00:00:00 2001 From: Stephane Bisson Date: Thu, 3 Dec 2015 10:52:46 -0500 Subject: [PATCH] Normalize links in Special:Notifications formatter Change-Id: I35354a3b27a59ee9740c6330bb3df22a8f7d6093 --- autoload.php | 1 + includes/formatters/EchoFlyoutFormatter.php | 53 +----------------- includes/formatters/LinkNormalizer.php | 56 +++++++++++++++++++ .../SpecialNotificationsFormatter.php | 8 +-- 4 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 includes/formatters/LinkNormalizer.php diff --git a/autoload.php b/autoload.php index ddb80dd57..dd2867db7 100644 --- a/autoload.php +++ b/autoload.php @@ -57,6 +57,7 @@ $wgAutoloadClasses += array( 'EchoHTMLEmailFormatter' => __DIR__ . '/includes/EmailFormatter.php', 'EchoHooks' => __DIR__ . '/Hooks.php', 'EchoIteratorDecorator' => __DIR__ . '/includes/iterator/IteratorDecorator.php', + 'EchoLinkNormalizer' => __DIR__ . '/includes/formatters/LinkNormalizer.php', 'EchoLocalCache' => __DIR__ . '/includes/cache/LocalCache.php', 'EchoMentionFormatter' => __DIR__ . '/includes/formatters/MentionFormatter.php', 'EchoMentionPresentationModel' => __DIR__ . '/includes/formatters/MentionPresentationModel.php', diff --git a/includes/formatters/EchoFlyoutFormatter.php b/includes/formatters/EchoFlyoutFormatter.php index 96d954020..4f7e37831 100644 --- a/includes/formatters/EchoFlyoutFormatter.php +++ b/includes/formatters/EchoFlyoutFormatter.php @@ -1,4 +1,5 @@ normalizeSecondaryLinks( $model->getSecondaryLinks() ); + $secondaryLinks = EchoLinkNormalizer::normalizeSecondaryLinks( $model->getSecondaryLinks() ); foreach ( $secondaryLinks as $link ) { $footerItems[] = Html::element( 'a', array( 'href' => $link['url'] ), $link['label'] ); } @@ -52,7 +53,7 @@ class EchoFlyoutFormatter extends EchoEventFormatter { // Add the primary link afterwards, if it has one $primaryLink = $model->getPrimaryLink(); if ( $primaryLink !== false ) { - $primaryLink = $this->normalizePrimaryLink( $primaryLink ); + $primaryLink = EchoLinkNormalizer::normalizePrimaryLink( $primaryLink ); $html .= Html::element( 'a', array( 'class' => 'mw-echo-notification-primary-link', 'href' => $primaryLink['url'] ), @@ -76,52 +77,4 @@ class EchoFlyoutFormatter extends EchoEventFormatter { ); } - /** - * Utility method to ensure B/C compat with previous getPrimaryLink return - * types, until all of them have been fixed. - * - * @deprecated - * @param string|array|bool $link - * @return string|bool - */ - protected function normalizePrimaryLink( $link ) { - // B/C for old format: [url, label] - if ( !isset( $link['url'] ) ) { - return array( - 'url' => $link[0], - 'label' => $link[1], - ); - } - - // current primary link format: ['url' => ..., 'label' => ...] - return $link; - } - - /** - * Utility method to ensure B/C compat with previous getSecondaryLinks - * return types, until all of them have been fixed. - * - * @deprecated - * @param array $link - * @return array - */ - protected function normalizeSecondaryLinks( array $link ) { - // B/C for old secondary links format: [url => label, ...] - if ( !isset( $link[0] ) || !isset( $link[0]['url'] ) ) { - $links = array(); - foreach ( $link as $url => $text ) { - $links[] = array( - 'url' => $url, - 'label' => $text, - 'description' => '', - 'icon' => false, - 'prioritized' => false, - ); - } - return $links; - } - - // current secondary links format: [['url' => ..., 'label' => ..., ...], ...] - return $link; - } } diff --git a/includes/formatters/LinkNormalizer.php b/includes/formatters/LinkNormalizer.php new file mode 100644 index 000000000..107548db6 --- /dev/null +++ b/includes/formatters/LinkNormalizer.php @@ -0,0 +1,56 @@ + $link[0], + 'label' => $link[1], + ); + } + + // current primary link format: ['url' => ..., 'label' => ...] + return $link; + } + + /** + * Utility method to ensure B/C compat with previous getSecondaryLinks + * return types, until all of them have been fixed. + * + * @deprecated + * @param array $link + * @return array + */ + public static function normalizeSecondaryLinks( array $link ) { + // B/C for old secondary links format: [url => label, ...] + if ( !isset( $link[0] ) || !isset( $link[0]['url'] ) ) { + $links = array(); + foreach ( $link as $url => $text ) { + $links[] = array( + 'url' => $url, + 'label' => $text, + 'description' => '', + 'icon' => false, + 'prioritized' => true, + ); + } + return $links; + } + + // current secondary links format: [['url' => ..., 'label' => ..., ...], ...] + return $link; + } + + + +} diff --git a/includes/formatters/SpecialNotificationsFormatter.php b/includes/formatters/SpecialNotificationsFormatter.php index aa1d2a8a8..cfe586744 100644 --- a/includes/formatters/SpecialNotificationsFormatter.php +++ b/includes/formatters/SpecialNotificationsFormatter.php @@ -40,11 +40,11 @@ class SpecialNotificationsFormatter extends EchoEventFormatter { $links = array(); $primaryLink = $model->getPrimaryLink(); if ( $primaryLink !== false ) { - $links[$primaryLink[0]] = $primaryLink[1]; + $links[] = EchoLinkNormalizer::normalizePrimaryLink( $primaryLink ); } - $links += $model->getSecondaryLinks(); - foreach ( $links as $target => $text ) { - $footerItems[] = Html::element( 'a', array( 'href' => $target ), $text ); + $links = array_merge( $links, EchoLinkNormalizer::normalizeSecondaryLinks( $model->getSecondaryLinks() ) ); + foreach ( $links as $link ) { + $footerItems[] = Html::element( 'a', array( 'href' => $link['url'] ), $link['label'] ); } $html .= Xml::tags(