CommentParser: Cache variables in getUsernameFromLink

Change-Id: I625e6ded3badd75a7a658c8d000576d0d165a18b
This commit is contained in:
Ed Sanders 2022-02-04 15:50:20 +00:00 committed by Bartosz Dziewoński
parent 8ad1df7dc8
commit 0b42aea276
2 changed files with 20 additions and 12 deletions

View file

@ -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;
}

View file

@ -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;
}