Use browser provided URL object instead of mw.Uri

Bug: T374314
Change-Id: I7de5ac9ba2de70e5a3a84fca1608c64ea1cd1aaf
This commit is contained in:
Ebrahim Byagowi 2024-09-10 14:34:22 +03:30 committed by Simon Legner
parent fc75d3aabe
commit 32a575f739
5 changed files with 19 additions and 21 deletions

View file

@ -93,7 +93,6 @@
"dependencies": [ "dependencies": [
"mediawiki.api", "mediawiki.api",
"mediawiki.Title", "mediawiki.Title",
"mediawiki.Uri",
"mediawiki.jqueryMsg", "mediawiki.jqueryMsg",
"mediawiki.router", "mediawiki.router",
"mediawiki.storage", "mediawiki.storage",

View file

@ -93,28 +93,26 @@ class ViewLogger {
* Records the amount of time the current image has been viewed * Records the amount of time the current image has been viewed
*/ */
recordViewDuration() { recordViewDuration() {
let uri; let url;
this.stopViewDuration(); this.stopViewDuration();
if ( recordVirtualViewBeaconURI ) { if ( recordVirtualViewBeaconURI ) {
try { try {
uri = new mw.Uri( recordVirtualViewBeaconURI ); url = new URL( recordVirtualViewBeaconURI, location );
uri.extend( { url.searchParams.set( 'duration', this.viewDuration );
duration: this.viewDuration, url.searchParams.set( 'uri', this.url );
uri: this.url
} );
} catch ( e ) { } catch ( e ) {
// the URI is malformed. We cannot log it. // the URI is malformed. We cannot log it.
return; return;
} }
try { try {
navigator.sendBeacon( uri.toString() ); navigator.sendBeacon( url.toString() );
} catch ( e ) { } catch ( e ) {
$.ajax( { $.ajax( {
type: 'HEAD', type: 'HEAD',
url: uri.toString() url: url.toString()
} ); } );
} }
@ -132,7 +130,7 @@ class ViewLogger {
* @param {string} url URL of the image to record a virtual view for * @param {string} url URL of the image to record a virtual view for
*/ */
attach( url ) { attach( url ) {
this.url = encodeURIComponent( url ); this.url = url;
this.startViewDuration(); this.startViewDuration();
$( this.window ) $( this.window )

View file

@ -47,13 +47,12 @@ class ImageProvider {
*/ */
get( url ) { get( url ) {
const cacheKey = url; const cacheKey = url;
const extraParam = {};
if ( this.imageQueryParameter ) { if ( this.imageQueryParameter ) {
try { try {
const uri = new mw.Uri( url ); const uri = new URL( url, location );
extraParam[ this.imageQueryParameter ] = null; uri.searchParams.set( this.imageQueryParameter, '' );
url = uri.extend( extraParam ).toString(); url = uri.toString();
} catch ( error ) { } catch ( error ) {
return $.Deferred().reject( error.message ); return $.Deferred().reject( error.message );
} }

View file

@ -51,10 +51,6 @@ class StripeButtons extends UiElement {
const match = image && image.src ? const match = image && image.src ?
image.src.match( /(lang|page)([\d\-a-z]+)-(\d+)px/ ) : // multi lingual SVG or PDF page image.src.match( /(lang|page)([\d\-a-z]+)-(\d+)px/ ) : // multi lingual SVG or PDF page
null; null;
const params = {};
if ( match ) {
params[ match[ 1 ] ] = match[ 2 ];
}
const commons = '//commons.wikimedia.org'; const commons = '//commons.wikimedia.org';
const isCommonsServer = String( mw.config.get( 'wgServer' ) ).includes( commons ); const isCommonsServer = String( mw.config.get( 'wgServer' ) ).includes( commons );
@ -62,12 +58,18 @@ class StripeButtons extends UiElement {
let isCommons = String( descriptionUrl ).includes( commons ); let isCommons = String( descriptionUrl ).includes( commons );
if ( imageInfo.pageID && !isCommonsServer ) { if ( imageInfo.pageID && !isCommonsServer ) {
const params = {};
if ( match ) {
params[ match[ 1 ] ] = match[ 2 ];
}
// The file has a local description page, override the description URL // The file has a local description page, override the description URL
descriptionUrl = imageInfo.title.getUrl( params ); descriptionUrl = imageInfo.title.getUrl( params );
isCommons = false; isCommons = false;
} else { } else {
const parsedUrl = new mw.Uri( descriptionUrl ); const parsedUrl = new URL( descriptionUrl, location );
parsedUrl.extend( params ); if ( match ) {
parsedUrl.searchParams.set( match[ 1 ], match[ 2 ] );
}
descriptionUrl = parsedUrl.toString(); descriptionUrl = parsedUrl.toString();
} }

View file

@ -134,7 +134,7 @@ const { ImageProvider } = require( 'mmv' );
imageProvider.imagePreloadingSupported = () => false; imageProvider.imagePreloadingSupported = () => false;
imageProvider.rawGet = function ( url ) { imageProvider.rawGet = function ( url ) {
assert.strictEqual( url, 'http://www.wikipedia.org/?foo', 'Extra parameter added' ); assert.strictEqual( url, 'http://www.wikipedia.org/?foo=', 'Extra parameter added' );
return $.Deferred().resolve(); return $.Deferred().resolve();
}; };