From 39799190878ff19299eebc99e000afa92660e3cc Mon Sep 17 00:00:00 2001 From: Matthias Mullie Date: Tue, 26 Jan 2016 09:23:32 +0100 Subject: [PATCH] Display human-readable wiki names for cross-wiki notifications Bug: T121936 Change-Id: Ic14fe65f4ecc6db94fc6774ca0c39d1c2a47db9d --- i18n/en.json | 1 + i18n/qqq.json | 1 + includes/ForeignNotifications.php | 42 ++++++++++++++++++- .../EchoForeignPresentationModel.php | 8 +++- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/i18n/en.json b/i18n/en.json index 4ade98276..f086b11a6 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -157,6 +157,7 @@ "echo-rev-deleted-text-view": "This page revision has been suppressed.", "notification-header-foreign-alert": "More alerts from $3 {{PLURAL:$4|and one other wiki|and $4 other wikis|0=}}", "notification-header-foreign-message": "More messages from $3 {{PLURAL:$4|and one other wiki|and $4 other wikis|0=}}", + "echo-foreign-wiki-lang": "$1 - $2", "apihelp-echomarkread-description": "Mark notifications as read for the current user.", "apihelp-echomarkread-param-list": "A list of notification IDs to mark as read.", "apihelp-echomarkread-param-all": "If set, marks all of a user's notifications as read.", diff --git a/i18n/qqq.json b/i18n/qqq.json index 820240b7a..3667e32b7 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -178,6 +178,7 @@ "echo-rev-deleted-text-view": "Short message displayed instead of edit content when revision text is suppressed.", "notification-header-foreign-alert": "Flyout-specific format for displaying notification header of having alert notifications on foreign wikis.\n\nParameters:\n* $1 - the formatted username of the current user.\n* $2 - the username for GENDER\n* $3 - 1 of the foreign wikis you have notifications on\n* $4 - the amount of remaining other wikis you have notifications on", "notification-header-foreign-message": "Flyout-specific format for displaying notification header of having message notifications on foreign wikis.\n\nParameters:\n* $1 - the formatted username of the current user.\n* $2 - the username for GENDER\n* $3 - 1 of the foreign wikis you have notifications on\n* $4 - the amount of remaining other wikis you have notifications on", + "echo-foreign-wiki-lang": "Title of the wiki for which you're seeing foreign notifications:\n\nParameters:\n* $1 - the name of the site (e.g. 'Wikipedia', 'Wikiversity', ...)\n* $2 - the language of the website (e.g. 'English', 'Deutsch', ...)", "apihelp-echomarkread-description": "{{doc-apihelp-description|echomarkread}}", "apihelp-echomarkread-param-list": "{{doc-apihelp-param|echomarkread|list}}", "apihelp-echomarkread-param-all": "{{doc-apihelp-param|echomarkread|all}}", diff --git a/includes/ForeignNotifications.php b/includes/ForeignNotifications.php index 42e26e45b..b0081a8a5 100644 --- a/includes/ForeignNotifications.php +++ b/includes/ForeignNotifications.php @@ -134,15 +134,53 @@ class EchoForeignNotifications { $data = array(); foreach ( $wikis as $wiki ) { - list( $major, $minor ) = $wgConf->siteFromDB( $wiki ); + $siteFromDB = $wgConf->siteFromDB( $wiki ); + list( $major, $minor ) = $siteFromDB; $server = $wgConf->get( 'wgServer', $wiki, $major, array( 'lang' => $minor, 'site' => $major ) ); $scriptPath = $wgConf->get( 'wgScriptPath', $wiki, $major, array( 'lang' => $minor, 'site' => $major ) ); + $data[$wiki] = array( - 'title' => $wiki, + 'title' => $this->getWikiTitle( $wiki, $siteFromDB ), 'url' => $server . $scriptPath . '/api.php', ); } return $data; } + + /** + * @param string $wikiId + * @param array $siteFromDB $wgConf->siteFromDB( $wikiId ) result + * @return mixed|string + */ + protected function getWikiTitle( $wikiId, array $siteFromDB = null ) { + global $wgConf, $wgLang; + + $msg = wfMessage( 'project-localized-name-'.$wikiId ); + // check if WikimediaMessages localized project names are available + if ( $msg->exists() ) { + return $msg->text(); + } else { + // don't fetch $site, $langCode if known already + if ( $siteFromDB === null ) { + $siteFromDB = $wgConf->siteFromDB( $wikiId ); + } + list( $site, $langCode ) = $siteFromDB; + + // try to fetch site name for this specific wiki, or fallback to the + // general project's sitename if there is no override + $wikiName = $wgConf->get( 'wgSitename', $wikiId ) ?: $wgConf->get( 'wgSitename', $site ); + $langName = $wgLang->fetchLanguageName( $langCode, $wgLang->getCode() ); + + if ( !$langName ) { + // if we can't find a language name (in language-agnostic + // project like mediawikiwiki), including the language name + // doesn't make much sense + return $wikiName; + } + + // ... or use generic fallback + return wfMessage( 'echo-foreign-wiki-lang', $wikiName, $langName )->text(); + } + } } diff --git a/includes/formatters/EchoForeignPresentationModel.php b/includes/formatters/EchoForeignPresentationModel.php index 0e7b00d5c..db86eab9e 100644 --- a/includes/formatters/EchoForeignPresentationModel.php +++ b/includes/formatters/EchoForeignPresentationModel.php @@ -20,9 +20,15 @@ class EchoForeignPresentationModel extends EchoEventPresentationModel { $msg = parent::getHeaderMessage(); $data = $this->event->getExtra(); - $msg->params( reset( $data['wikis'] ) ); + $msg->params( $this->getWikiName( reset( $data['wikis'] ) ) ); $msg->numParams( count( $data['wikis'] ) - 1 ); return $msg; } + + protected function getWikiName( $wiki ) { + $foreign = new EchoForeignNotifications( new User ); + $data = $foreign->getApiEndpoints( array( $wiki ) ); + return $data[$wiki]['title']; + } }