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
This commit is contained in:
Bartosz Dziewoński 2020-09-02 01:00:08 +02:00
parent 37e6f64503
commit 10899af666
3 changed files with 12 additions and 9 deletions

View file

@ -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 ] );

View file

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

View file

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