jenkins-bot has submitted this change and it was merged.
Change subject: Hygiene: Introduce Schema class
......................................................................
Hygiene: Introduce Schema class
Change-Id: Ib36d9e32977923ddd63de28709028df604c3836f
---
M includes/Resources.php
A javascripts/Schema.js
A tests/qunit/test_Schema.js
3 files changed, 121 insertions(+), 0 deletions(-)
Approvals:
Jhernandez: Looks good to me, but someone else must approve
Phuedx: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/Resources.php b/includes/Resources.php
index 6427256..3a65718 100644
--- a/includes/Resources.php
+++ b/includes/Resources.php
@@ -233,6 +233,7 @@
'javascripts/Panel.js',
'javascripts/Section.js',
'javascripts/Page.js',
+ 'javascripts/Schema.js',
'javascripts/application.js',
),
'position' => 'bottom',
diff --git a/javascripts/Schema.js b/javascripts/Schema.js
new file mode 100644
index 0000000..2e90195
--- /dev/null
+++ b/javascripts/Schema.js
@@ -0,0 +1,88 @@
+( function ( M, $ ) {
+ var Schema,
+ browser = M.require( 'browser' ),
+ Class = M.require( 'Class' );
+
+ /**
+ * @class Schema
+ * @extends Class
+ */
+ Schema = Class.extend( {
+ /**
+ * A set of defaults to log to the schema
+ *
+ * @type {Object}
+ * @cfg {Object} defaults Default options hash.
+ * @cfg {String} defaults.mobileMode whether user is in stable
beta or alpha
+ */
+ defaults: {
+ mobileMode: M.getMode()
+ },
+ /**
+ * Name of Schema to log to
+ * @type {String}
+ */
+ name: undefined,
+ /**
+ * @param {Object} defaults
+ * @param {String} [schemaName]
+ */
+ initialize: function ( defaults, schemaName ) {
+ defaults = $.extend( this.defaults, defaults || {} );
+ if ( schemaName ) {
+ this.name = schemaName;
+ }
+ if ( !this.name ) {
+ throw new Error( 'Schema needs to define a
schema name.' );
+ }
+ this.defaults = defaults;
+ Class.prototype.initialize.apply( this, arguments );
+ },
+ /**
+ *
+ * @method
+ * @param {Object} data to log
+ * @return {jQuery.Deferred}
+ */
+ log: function ( data ) {
+ if ( mw.eventLog ) {
+ return mw.eventLog.logEvent( this.name,
$.extend( this.defaults, data ) );
+ } else {
+ return $.Deferred().reject( 'EventLogging not
installed.' );
+ }
+ }
+ } );
+
+ /**
+ * Retrieve and, if not present, generate a random session ID
+ * (32 alphanumeric characters).
+ * FIXME: Use mw.user
+ * FIXME: Fall back to using cookies if localStorage isn't supported
+ *
+ * @method
+ * @static
+ * @return {string}
+ */
+ Schema.getSessionId = function () {
+ var sessionId;
+ if ( !browser.supportsLocalStorage() ) {
+ return '';
+ }
+ sessionId = localStorage.getItem( 'sessionId' );
+
+ if ( !sessionId ) {
+ // FIXME: use mw.user.generateRandomSessionId when we
can,
+ // as of now mediawiki.user has no mobile target (yay,
targets in RL!)
+ sessionId = '';
+ while ( sessionId.length < 32 ) {
+ // http://stackoverflow.com/a/8084248/365238
+ sessionId += Math.random().toString( 36
).slice( 2, 32 + 2 - sessionId.length );
+ }
+ localStorage.setItem( 'sessionId', sessionId );
+ }
+ return sessionId;
+ };
+
+ M.define( 'Schema', Schema );
+
+}( mw.mobileFrontend, jQuery ) );
diff --git a/tests/qunit/test_Schema.js b/tests/qunit/test_Schema.js
new file mode 100644
index 0000000..c3ecbad
--- /dev/null
+++ b/tests/qunit/test_Schema.js
@@ -0,0 +1,32 @@
+( function ( $, M ) {
+ var Schema = M.require( 'Schema' );
+
+ QUnit.module( 'Schema' );
+
+ QUnit.test( '#getSessionId', 3, function ( assert ) {
+ var sessionId = Schema.getSessionId();
+ assert.strictEqual( typeof sessionId, 'string', 'session ID is
a string' );
+ assert.strictEqual( sessionId.length, 32, 'session ID is 32
chars long' );
+ assert.strictEqual( Schema.getSessionId(), sessionId, 'session
ID is not regenerated if present' );
+ } );
+
+ QUnit.test( '#initialize', 3, function ( assert ) {
+ var s1, s2, s3, SubSchema;
+ // Creating a schema without name throws
+ try {
+ s1 = new Schema();
+ } catch ( ex ) {
+ assert.ok( true );
+ }
+
+ s2 = new Schema( {}, 'aname' );
+ assert.strictEqual( s2.name, 'aname', 'explicit name gets set'
);
+
+ SubSchema = Schema.extend( {
+ name: 'subname'
+ } );
+ s3 = new SubSchema( {} );
+ assert.strictEqual( s3.name, 'subname', 'subclassed name works'
);
+ } );
+
+}( jQuery, mw.mobileFrontend ) );
--
To view, visit https://gerrit.wikimedia.org/r/182147
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib36d9e32977923ddd63de28709028df604c3836f
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
Gerrit-Reviewer: Bmansurov <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Jhernandez <[email protected]>
Gerrit-Reviewer: Phuedx <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits