Gilles has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/173641

Change subject: Add marker parameter to image requests coming from MediaViewer
......................................................................

Add marker parameter to image requests coming from MediaViewer

This will be needed by Erik Zachte for compiling per-file image view data.
Since Media Viewer does preloading, it skews the HTTP request-based
statistics. By marking image requests coming from Media Viewer,
it allows us to remove that bias.

I haven't made this configurable, maybe it should be?

Change-Id: Iac8e7770c1a379691547de4b6d47b7d54467f54d
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/1002
---
M resources/mmv/provider/mmv.provider.Image.js
M tests/qunit/mmv/provider/mmv.provider.Image.test.js
2 files changed, 67 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MultimediaViewer 
refs/changes/41/173641/1

diff --git a/resources/mmv/provider/mmv.provider.Image.js 
b/resources/mmv/provider/mmv.provider.Image.js
index e2e1b2b..cb5d187 100644
--- a/resources/mmv/provider/mmv.provider.Image.js
+++ b/resources/mmv/provider/mmv.provider.Image.js
@@ -50,6 +50,8 @@
                        start,
                        rawGet;
 
+               url = this.addQueryParameterToURI( url, 'mediaviewer' );
+
                if ( !this.cache[cacheKey] ) {
                        if ( this.imagePreloadingSupported() ) {
                                rawGet = $.proxy( provider.rawGet, provider, 
url, true );
@@ -129,5 +131,36 @@
                return window.MSInputMethodContext === undefined;
        };
 
+       /**
+        * Adds a query parameter to a URI
+        * @param {string} uri URI to modify
+        * @param {string} parameter parameter string to add
+        */
+       Image.prototype.addQueryParameterToURI = function ( uri, parameter ) {
+               var modifiedUri, prefix, split;
+
+               prefix = uri.indexOf( '?' ) > -1 ? '&' : '?';
+
+               hashPoint = uri.indexOf( '#' );
+
+               if ( hashPoint > -1 ) {
+                       split = uri.split( '#' );
+
+                       if ( split[ 0 ][ split[ 0 ].length - 1 ] === '?' ) {
+                               prefix = '';
+                       }
+
+                       modifiedUri = split[ 0 ] + prefix + parameter + '#' + 
split[ 1 ];
+               } else {
+                       if ( uri[ uri.length - 1 ] === '?' ) {
+                               prefix = '';
+                       }
+
+                       modifiedUri = uri + prefix + parameter;
+               }
+
+               return modifiedUri;
+       };
+
        mw.mmv.provider.Image = Image;
 }( mediaWiki, jQuery ) );
diff --git a/tests/qunit/mmv/provider/mmv.provider.Image.test.js 
b/tests/qunit/mmv/provider/mmv.provider.Image.test.js
index 6879bf7..03a1ab8 100644
--- a/tests/qunit/mmv/provider/mmv.provider.Image.test.js
+++ b/tests/qunit/mmv/provider/mmv.provider.Image.test.js
@@ -200,4 +200,38 @@
                        QUnit.start();
                } );
        } );
+
+       QUnit.test( 'addQueryParameterToURI', 6, function ( assert ) {
+               var imageProvider = new mw.mmv.provider.Image();
+
+               assert.strictEqual(
+                       imageProvider.addQueryParameterToURI( 
'http://foo.com/bar#baz', 'param' ),
+                       'http://foo.com/bar?param#baz',
+                       'Extra parameter added before hash' );
+
+               assert.strictEqual(
+                       imageProvider.addQueryParameterToURI( 
'http://foo.com/bar?#baz', 'param' ),
+                       'http://foo.com/bar?param#baz',
+                       'Extra parameter added before hash' );
+
+               assert.strictEqual(
+                       imageProvider.addQueryParameterToURI( 
'http://foo.com/bar?boo#baz', 'param' ),
+                       'http://foo.com/bar?boo&param#baz',
+                       'Extra parameter added before hash' );
+
+               assert.strictEqual(
+                       imageProvider.addQueryParameterToURI( 
'http://foo.com/bar?param2', 'param' ),
+                       'http://foo.com/bar?param2&param',
+                       'Extra parameter added at the end' );
+
+               assert.strictEqual(
+                       imageProvider.addQueryParameterToURI( 
'http://foo.com/bar?', 'param' ),
+                       'http://foo.com/bar?param',
+                       'Extra parameter added at the end' );
+
+               assert.strictEqual(
+                       imageProvider.addQueryParameterToURI( 
'http://foo.com/bar', 'param' ),
+                       'http://foo.com/bar?param',
+                       'Extra parameter added at the end' );
+       } );
 }( mediaWiki, jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/173641
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iac8e7770c1a379691547de4b6d47b7d54467f54d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MultimediaViewer
Gerrit-Branch: master
Gerrit-Owner: Gilles <gdu...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to