Normalize links in Special:Notifications formatter

Change-Id: I35354a3b27a59ee9740c6330bb3df22a8f7d6093
This commit is contained in:
Stephane Bisson 2015-12-03 10:52:46 -05:00
parent b813835907
commit 65638384fc
4 changed files with 64 additions and 54 deletions

View file

@ -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',

View file

@ -1,4 +1,5 @@
<?php
use EchoLinkNormalizer;
/**
* A formatter for the notification flyout popup
@ -39,7 +40,7 @@ class EchoFlyoutFormatter extends EchoEventFormatter {
);
$footerItems = array( $ts );
$secondaryLinks = $this->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;
}
}

View file

@ -0,0 +1,56 @@
<?php
class EchoLinkNormalizer {
/**
* 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
*/
public static 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
*/
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;
}
}

View file

@ -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(