diff --git a/includes/CommentParser.php b/includes/CommentParser.php index b300f569f..250beca6d 100644 --- a/includes/CommentParser.php +++ b/includes/CommentParser.php @@ -521,8 +521,6 @@ class CommentParser { * @return string|null Username, or null */ private function getUsernameFromLink( Element $link ): ?string { - $username = null; - // Selflink: use title of current page if ( DOMCompat::getClassList( $link )->contains( 'mw-selflink' ) ) { $title = $this->title; @@ -532,13 +530,18 @@ class CommentParser { if ( !$title ) { return null; } - if ( $title->getNamespace() === NS_USER || $title->getNamespace() === NS_USER_TALK ) { - $username = $title->getText(); + + $username = null; + $namespaceId = $title->getNamespace(); + $mainText = $title->getText(); + + if ( $namespaceId === NS_USER || $namespaceId === NS_USER_TALK ) { + $username = $mainText; if ( strpos( $username, '/' ) !== false ) { return null; } } elseif ( $title->isSpecial( 'Contributions' ) ) { - $parts = explode( '/', $title->getText(), 2 ); + $parts = explode( '/', $mainText, 2 ); if ( !isset( $parts[1] ) ) { return null; } diff --git a/modules/Parser.js b/modules/Parser.js index 42e95bf7a..2929598c4 100644 --- a/modules/Parser.js +++ b/modules/Parser.js @@ -511,25 +511,30 @@ Parser.prototype.getUsernameFromLink = function ( link ) { if ( !title ) { return null; } + var username; + var namespaceId = title.getNamespaceId(); + var mainText = title.getMainText(); + var namespaceIds = mw.config.get( 'wgNamespaceIds' ); + if ( - title.getNamespaceId() === mw.config.get( 'wgNamespaceIds' ).user || - title.getNamespaceId() === mw.config.get( 'wgNamespaceIds' ).user_talk + namespaceId === namespaceIds.user || + namespaceId === namespaceIds.user_talk ) { - username = title.getMainText(); + username = mainText; if ( username.indexOf( '/' ) !== -1 ) { return null; } } else if ( - title.getNamespaceId() === mw.config.get( 'wgNamespaceIds' ).special && - title.getMainText().split( '/' )[ 0 ] === data.specialContributionsName + namespaceId === namespaceIds.special && + mainText.split( '/' )[ 0 ] === data.specialContributionsName ) { - username = title.getMainText().split( '/' )[ 1 ]; + username = mainText.split( '/' )[ 1 ]; if ( !username ) { return null; } // Normalize the username: users may link to their contributions with an unnormalized name - var userpage = mw.Title.makeTitle( mw.config.get( 'wgNamespaceIds' ).user, username ); + var userpage = mw.Title.makeTitle( namespaceIds.user, username ); if ( !userpage ) { return null; }