From 10899af666f6dfa8446a0068444c0d5193bbcce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 2 Sep 2020 01:00:08 +0200 Subject: [PATCH] Fix parsing links in Parsoid documents without short URLs Move the code so that we check for "?title=" query parameter first, because we don't handle this right in the other code path. Use parse_url() instead of wfParseUrl() because the latter doesn't accept relative URLs, and we don't care about the other differences. Bug: T261711 Depends-On: I4da952876e1c3d1a41d06b51f7e26015ff5e34d7 Change-Id: I70fac2b41befd782b0a47a4f726ae748dc0f775d --- includes/ApiDiscussionToolsEdit.php | 1 + includes/CommentUtils.php | 11 ++++++----- modules/Parser.js | 9 +++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/includes/ApiDiscussionToolsEdit.php b/includes/ApiDiscussionToolsEdit.php index cfff0f740..e638c0fa4 100644 --- a/includes/ApiDiscussionToolsEdit.php +++ b/includes/ApiDiscussionToolsEdit.php @@ -82,6 +82,7 @@ class ApiDiscussionToolsEdit extends ApiBase { } $parser = CommentParser::newFromGlobalState( $container ); + $comment = $parser->findCommentById( $commentId ); if ( !$comment ) { $this->dieWithError( [ 'apierror-discussiontools-commentid-notfound', $commentId ] ); diff --git a/includes/CommentUtils.php b/includes/CommentUtils.php index 61b1a1d6c..1d4828c6e 100644 --- a/includes/CommentUtils.php +++ b/includes/CommentUtils.php @@ -302,6 +302,12 @@ class CommentUtils { * @return Title|null */ public static function getTitleFromUrl( string $url ) : ?Title { + $bits = parse_url( $url ); + $query = wfCgiToArray( $bits['query'] ?? '' ); + if ( isset( $query['title'] ) ) { + return Title::newFromText( $query['title'] ); + } + $config = MediaWikiServices::getInstance()->getMainConfig(); // TODO: Set the correct base in the document? if ( strpos( $url, './' ) === 0 ) { @@ -309,11 +315,6 @@ class CommentUtils { } elseif ( strpos( $url, '://' ) === false ) { $url = 'https://local' . $url; } - $bits = wfParseUrl( $url ); - $query = wfCgiToArray( $bits['query'] ?? '' ); - if ( isset( $query['title'] ) ) { - return Title::newFromText( $query['title'] ); - } $articlePathRegexp = '/' . str_replace( preg_quote( '$1', '/' ), diff --git a/modules/Parser.js b/modules/Parser.js index 781027d53..b6c024482 100644 --- a/modules/Parser.js +++ b/modules/Parser.js @@ -480,17 +480,18 @@ function getTitleFromUrl( url ) { // T106244: URL encoded values using fallback 8-bit encoding (invalid UTF-8) cause mediawiki.Uri to crash return null; } + if ( url.query.title ) { + return mw.Title.newFromText( url.query.title ); + } + articlePathRegexp = new RegExp( mw.util.escapeRegExp( mw.config.get( 'wgArticlePath' ) ) .replace( mw.util.escapeRegExp( '$1' ), '(.*)' ) ); - if ( ( match = url.path.match( articlePathRegexp ) ) ) { return mw.Title.newFromText( decodeURIComponent( match[ 1 ] ) ); } - if ( url.query.title ) { - return mw.Title.newFromText( url.query.title ); - } + return null; }