Gergő Tisza has uploaded a new change for review.

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

Change subject: Remove CORS metrics
......................................................................

Remove CORS metrics

Bug: T507
Bug: T97475
Bug: T108206
Bug: T108707
Change-Id: I0d84b7cc57495951b7d13ce21402c77789e436de
---
M ImageMetrics.php
D resources/cors-test.js
M resources/head.js
M resources/loader.js
D resources/logger/CorsLogger.js
D tests/qunit/logger/CorsLogger.test.js
6 files changed, 6 insertions(+), 197 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ImageMetrics 
refs/changes/82/230982/1

diff --git a/ImageMetrics.php b/ImageMetrics.php
index 41400de..a494eab 100644
--- a/ImageMetrics.php
+++ b/ImageMetrics.php
@@ -29,14 +29,10 @@
 /** @var int|bool: If set, logs once per this many requests for logged-in 
users. False if unset. **/
 $wgImageMetricsLoggedinSamplingFactor = false;
 
-/** @var int|bool: If set, tests and logs CORS support once per this many 
requests. False if unset. **/
-$wgImageMetricsCorsSamplingFactor = false;
-
 $wgMessagesDirs['ImageMetrics'] = __DIR__ . '/i18n';
 
 $wgHooks['EventLoggingRegisterSchemas'][] = function( array &$schemas ) {
        $schemas['ImageMetricsLoadingTime'] = 10078363;
-       $schemas['ImageMetricsCorsSupport'] = 11686678;
 };
 
 
@@ -45,14 +41,12 @@
                'scripts'       => array(
                        'logger/Logger.js',
                        'logger/LoadingTimeLogger.js',
-                       'logger/CorsLogger.js',
                ),
                'localBasePath' => __DIR__ . '/resources',
                'remoteExtPath' => 'ImageMetrics/resources',
                'dependencies'  => array(
                        'oojs',
                        'schema.ImageMetricsLoadingTime',
-                       'schema.ImageMetricsCorsSupport',
                ),
                'targets'       => array( 'desktop', 'mobile' ),
        ),
@@ -77,7 +71,7 @@
  * @return bool
  */
 $wgHooks['BeforePageDisplay'][] = function ( &$out, &$skin ) {
-       if ( Action::getActionName( $out->getContext() ) === 'view' ) {
+       if ( $out->getTitle()->inNamespace( NS_FILE ) && Action::getActionName( 
$out->getContext() ) === 'view' ) {
                $out->addModules( array( 'ext.imageMetrics.head', 
'ext.imageMetrics.loader' ) );
        }
        return true;
@@ -88,12 +82,11 @@
  * @return bool
  */
 $wgHooks[ 'ResourceLoaderGetConfigVars' ][] = function ( &$vars ) {
-       global $wgImageMetricsSamplingFactor, 
$wgImageMetricsLoggedinSamplingFactor, $wgImageMetricsCorsSamplingFactor;
+       global $wgImageMetricsSamplingFactor, 
$wgImageMetricsLoggedinSamplingFactor;
        $vars[ 'wgImageMetrics' ] = array(
                'samplingFactor' => array(
                        'image' => $wgImageMetricsSamplingFactor,
                        'imageLoggedin' => 
$wgImageMetricsLoggedinSamplingFactor,
-                       'cors' => $wgImageMetricsCorsSamplingFactor,
                ),
        );
        return true;
@@ -108,7 +101,6 @@
        $testModules['qunit']['ext.imageMetrics.tests'] = array(
                'scripts' => array(
                        'tests/qunit/logger/LoadingTimeLogger.test.js',
-                       'tests/qunit/logger/CorsLogger.test.js',
                ),
                'dependencies' => array(
                        'ext.imageMetrics',
diff --git a/resources/cors-test.js b/resources/cors-test.js
deleted file mode 100644
index 885e35f..0000000
--- a/resources/cors-test.js
+++ /dev/null
@@ -1,8 +0,0 @@
-/**
- * CORS test payload. This file will not load if the user has a 
CORS-supporting browser and a
- * proxy that strips CORS headers.
- */
-( function ( mw ) {
-       'use strict';
-       mw.config.set( 'wgImageMetricsCorsTestSucceeded', true );
-}( mediaWiki ) );
diff --git a/resources/head.js b/resources/head.js
index c92df69..3ac1dc4 100644
--- a/resources/head.js
+++ b/resources/head.js
@@ -22,6 +22,7 @@
                        file = $file.get( 0 );
 
                if ( !file ) {
+                       mw.log( 'ImageMetrics: file missing' );
                        return;
                }
 
diff --git a/resources/loader.js b/resources/loader.js
index aeb2b2d..f14adca 100644
--- a/resources/loader.js
+++ b/resources/loader.js
@@ -9,11 +9,10 @@
 ( function ( mw, $ ) {
        'use strict';
 
-       var logImage, logCors,
+       var logImage,
                config = mw.config.get( 'wgImageMetrics', { samplingFactor: {} 
} ),
                imageFactor = config.samplingFactor.image,
-               loggedinImageFactor = config.samplingFactor.imageLoggedin,
-               corsFactor = config.samplingFactor.cors;
+               loggedinImageFactor = config.samplingFactor.imageLoggedin;
 
        /**
         * Makes a random decision (based on samplingRatio) whether an event 
should be logged.
@@ -38,18 +37,14 @@
        }
 
        logImage = isInSample( imageFactor );
-       logCors = isInSample( corsFactor );
 
-       if ( !logImage && !logCors ) {
+       if ( !logImage ) {
                return;
        }
 
        mw.loader.using( 'ext.imageMetrics', function () {
                if ( logImage ) {
                        mw.imageMetrics.LoadingTimeLogger.install( imageFactor 
);
-               }
-               if ( logCors ) {
-                       mw.imageMetrics.CorsLogger.install( corsFactor );
                }
        } );
 }( mediaWiki, jQuery ) );
diff --git a/resources/logger/CorsLogger.js b/resources/logger/CorsLogger.js
deleted file mode 100644
index b887087..0000000
--- a/resources/logger/CorsLogger.js
+++ /dev/null
@@ -1,113 +0,0 @@
-/**
- * JavaScript module for CORS metrics.
- * @see https://mediawiki.org/wiki/Extension:ImageMetrics
- *
- * @licence GNU GPL v2 or later
- * @author Tisza Gergő <[email protected]>
- */
-( function ( mw, $, oo ) {
-       'use strict';
-
-       /**
-        * @class mw.imageMetrics.CorsLogger
-        * @extends mw.imageMetrics.Logger
-        * @constructor
-        * @param {number} samplingFactor sampling factor
-        * @param {Object} location window.location
-        * @param {Object} mwConfig mw.config
-        * @param {Object} geo window.Geo
-        * @param {Object} eventLog mw.eventLog
-        */
-       function CorsLogger(
-               /* inherited arguments */
-               samplingFactor, location, mwConfig, geo, eventLog
-       ) {
-               CorsLogger.parent.call( this, samplingFactor, location, 
mwConfig, geo, eventLog );
-       }
-       oo.inheritClass( CorsLogger, mw.imageMetrics.Logger );
-
-       CorsLogger.prototype.schema = 'ImageMetricsCorsSupport';
-
-       /**
-        * Factory function to take care of dependency injection.
-        * @static
-        * @param {number} samplingFactor sampling factor
-        * @return {mw.imageMetrics.CorsLogger}
-        */
-       CorsLogger.create = function ( samplingFactor ) {
-               return new CorsLogger( samplingFactor, window.location, 
mw.config, window.Geo, mw.eventLog );
-       };
-
-       /**
-        * Sets up logging.
-        * @static
-        * @param {number} samplingFactor sampling factor
-        */
-       CorsLogger.install = function ( samplingFactor ) {
-               var promise,
-                       logger = CorsLogger.create( samplingFactor );
-
-               // randomize which request is fired first
-               if ( Math.random() >= 0.5 ) {
-                       promise = $.when(
-                               logger.loadScript( 'cors-test.js', true ),
-                               logger.loadScript( 'non-cors-test.js' )
-                       );
-               } else {
-                       promise = $.when(
-                               logger.loadScript( 'non-cors-test.js' ),
-                               logger.loadScript( 'cors-test.js', true )
-                       );
-               }
-
-               promise.done( $.proxy( logger, 'collect' ) );
-       };
-
-       /**
-        * Logs all CORS-related information
-        */
-       CorsLogger.prototype.collect = function () {
-               this.log( {
-                       xhrSupported: 'withCredentials' in new XMLHttpRequest(),
-                       xdomainSupported: typeof XDomainRequest !== 'undefined',
-                       imgAttributeSupported: 'crossOrigin' in 
document.createElement( 'img' ),
-                       scriptAttributeSupported: 'crossOrigin' in 
document.createElement( 'script' ),
-                       scriptLoaded: this.mwConfig.get( 
'wgImageMetricsCorsTestSucceeded', false ),
-                       sanityCheck: this.mwConfig.get( 
'wgImageMetricsNonCorsTestSucceeded', false )
-               } );
-       };
-
-       /**
-        * Loads a resource.
-        * @param {string} filename Name of the file
-        * @param {bool} [crossorigin] Use a crossorigin="anonymous" <script> 
attribute.
-        * @return {jQuery.Deferred}
-        */
-       CorsLogger.prototype.loadScript = function ( filename, crossorigin ) {
-               var script,
-                       deferred = $.Deferred();
-
-               script = $( '<script>' )
-                       .attr( {
-                               type: 'text/javascript',
-                               crossorigin: crossorigin ? 'anonymous' : 
undefined,
-                               // this will not work if wgExtensionAssetsPath 
is a relative URL (which is the
-                               // default) but there is no need for CORS 
loading of assets in that case anyway
-                               src: this.mwConfig.get( 'wgExtensionAssetsPath' 
) + '/ImageMetrics/resources/' + filename +
-                                       // add cache buster to avoid bias if 
CORS and normal requests are cached differently
-                                       '?_=' + Math.random()
-                       } )
-                       .get( 0 );
-
-               // jQuery subverts script insertion into an AJAX + eval call 
which would break the whole point, so
-               // we use native AJAX. Also, don't trust success/error 
handlers, some browsers treat them in weird
-               // ways for CORS calls; we will check directly whether the 
script has executed.
-               script.onload = $.proxy( deferred, 'resolve' );
-               script.onerror = $.proxy( deferred, 'resolve' );
-               $( 'head' ).get( 0 ).appendChild( script );
-
-               return deferred.promise();
-       };
-
-       mw.imageMetrics.CorsLogger = CorsLogger;
-}( mediaWiki, jQuery, OO ) );
diff --git a/tests/qunit/logger/CorsLogger.test.js 
b/tests/qunit/logger/CorsLogger.test.js
deleted file mode 100644
index dc73732..0000000
--- a/tests/qunit/logger/CorsLogger.test.js
+++ /dev/null
@@ -1,58 +0,0 @@
-( function ( mw ) {
-       'use strict';
-
-       QUnit.module( 'mw.imageMetrics.CorsLogger', QUnit.newMwEnvironment() );
-
-       function createCorsLogger( sandbox, options ) {
-               var logger,
-                       logEvent = sandbox.stub(),
-                       config = new mw.Map( options.config || {} );
-
-               logger = new mw.imageMetrics.CorsLogger( 
options.samplingFactor, options.location || {}, config,
-                       options.geo || {}, { logEvent: logEvent } );
-
-               options.logEvent = logEvent;
-               return logger;
-       }
-
-       QUnit.test( 'Constructor sanity test', 1, function ( assert ) {
-               var logger = createCorsLogger( this.sandbox, {} );
-               assert.ok( logger, 'Object created' );
-       } );
-
-       QUnit.test( 'Minimal logging scenario', 11, function ( assert ) {
-               var data,
-                       options = {
-                               samplingFactor: 1,
-                               config: {}
-                       },
-                       logger = createCorsLogger( this.sandbox, options );
-
-               logger.collect();
-               data = options.logEvent.firstCall.args[1];
-
-               assert.strictEqual( data.samplingFactor, 1, 'samplingFactor is 
logged correctly' );
-               assert.strictEqual( data.isHttps, false, 'isHttps is logged 
correctly' );
-               assert.strictEqual( data.country, undefined, 'country is not 
logged when Geo object is not available' );
-               assert.strictEqual( data.isAnon, true, 'isAnon is logged 
correctly' );
-               // the feature tests are trivial, no point in mocking them
-               assert.ok( 'xhrSupported' in data );
-               assert.ok( 'xdomainSupported' in data );
-               assert.ok( 'imgAttributeSupported' in data );
-               assert.ok( 'scriptAttributeSupported' in data );
-               assert.ok( 'scriptLoaded' in data );
-               assert.ok( 'sanityCheck' in data );
-
-               options.config.wgUserId = 1;
-               logger = createCorsLogger( this.sandbox, options );
-               logger.collect();
-               data = options.logEvent.firstCall.args[1];
-               assert.strictEqual( data.isAnon, false, 'isAnon is logged 
correctly' );
-       } );
-
-       QUnit.test( 'loadScript() sanity test', 1, function ( assert ) {
-               var logger = createCorsLogger( this.sandbox, {} ),
-                       promise = logger.loadScript( 'foo.js' );
-               assert.ok( 'then' in promise, 'loadScriptViaCors() runs 
successfully and returns a promise' );
-       } );
-} ( mediaWiki, jQuery ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0d84b7cc57495951b7d13ce21402c77789e436de
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ImageMetrics
Gerrit-Branch: master
Gerrit-Owner: Gergő Tisza <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to