Daniel Werner has uploaded a new change for review.

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


Change subject: introduces mw.ext.wikibase, wikibase.repo and 
wikibase.repo.fetchedEntities
......................................................................

introduces mw.ext.wikibase, wikibase.repo and wikibase.repo.fetchedEntities

Declares that the global "wikibase" object should ideally be independent of 
MediaWiki. Therefore
mw.ext.wikibase got defined to act as bridge between MediaWiki and Wikibase. 
Code for configuring
Wikibase to be used in MediaWiki context should go there in the future. The 
Wikibase object and
sub modules should increasingly become independent of MediaWiki.
This requires a lot more work since this is currently violated throughout the 
entire codebase (not
just in JavaScript either). The purpose of this change is mostly to provide a 
place for more future
proof code.

With this change, the way the wikibase.fetchedEntities object is being 
constructed in MediaWiki
context got refactored accordingly. By default, wikibase.fetchedEntities is 
just an empty object,
only in MediaWiki context it will be filled based on the 'wbUsedEntities' 
mediawiki.config variable
provided by the backend. Also deprecates wikibase.fetchedEntities and moves it 
into
wikibase.repo.fetchedEntities since the current structure of the fetched 
content objects contained
in the object only makes sense in repo context.
Increases the reusability of the fetchedEntities code (e.g. on special pages 
where WB-UI should be
initialized) and moves the code into its own function.

The whole way we are using the global "fetchedEntities" throughout the code 
base is still bad and
we should still implement a proper store abstraction for proper injection.

Change-Id: Id6ddb4941d828e28530a50e2ce3e18e7c279462b
---
M lib/resources/Resources.php
A lib/resources/mw.ext.wikibase.js
M lib/resources/wikibase.js
M repo/resources/Resources.php
A repo/resources/mw.ext.wikibase.repo/repo.fetchedEntitiesFromMwConfig.js
A repo/resources/mw.ext.wikibase.repo/repo.js
A repo/resources/wikibase.repo/repo.fetchedEntities.js
A repo/resources/wikibase.repo/repo.js
M repo/resources/wikibase.ui.entityViewInit.js
9 files changed, 183 insertions(+), 51 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/09/89109/1

diff --git a/lib/resources/Resources.php b/lib/resources/Resources.php
index 0812d82..bb998b5 100644
--- a/lib/resources/Resources.php
+++ b/lib/resources/Resources.php
@@ -56,6 +56,16 @@
                        )
                ),
 
+               'mw.ext.wikibase' => $moduleTemplate + array(
+                       'scripts' => array(
+                               'mw.ext.wikibase.js',
+                       ),
+                       'dependencies' => array(
+                               'mediawiki',
+                               'wikibase',
+                       ),
+               ),
+
                'wikibase.parsers' => $moduleTemplate + array(
                        'scripts' => array(
                                'parsers/EntityIdParser.js',
@@ -684,6 +694,7 @@
                                
'jquery.valueview.experts.wikibase/experts.wikibase.js',
                        ),
                        'dependencies' => array(
+                               'wikibase',
                                'mw.ext.valueView',
                        ),
                ),
diff --git a/lib/resources/mw.ext.wikibase.js b/lib/resources/mw.ext.wikibase.js
new file mode 100644
index 0000000..4f9dceb
--- /dev/null
+++ b/lib/resources/mw.ext.wikibase.js
@@ -0,0 +1,13 @@
+mediaWiki.ext = mediaWiki.ext || {};
+
+/**
+ * Entry point for MediaWiki "Wikibase" extension JavaScript code. Adds an 
extension object to the
+ * global MediaWiki object and provides some MediaWiki specific wikibase code.
+ *
+ * @since 0.5
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ */
+mediaWiki.ext.wikibase = new ( function MwExtWikibase() {
+       'use strict';
+} )();
diff --git a/lib/resources/wikibase.js b/lib/resources/wikibase.js
index bddac76..06c43a5 100644
--- a/lib/resources/wikibase.js
+++ b/lib/resources/wikibase.js
@@ -1,17 +1,13 @@
 /**
- * JavaScript for 'wikibase' extension
- * @see https://www.mediawiki.org/wiki/Extension:Wikibase
- *
- * @file
- * @ingroup WikibaseLib
- *
  * @licence GNU GPL v2+
- * @author Daniel Werner < daniel.werner at wikimedia.de >
- */
-
-/**
- * Global 'Wikibase' extension singleton.
+ * @author Daniel Werner < [email protected] >
+ * @author H. Snater < [email protected] >
+ *
+ * Global Wikibase singleton.
+ *
  * @since 0.1
+ *
+ * TODO: This should become fully independent of MediaWiki.
  *
  * TODO: startItemPageEditMode and stopItemPageEditMode should be removed or 
marked deprecated as
  *       soon as we can get rid of old wb.ui.PropertyEditTool ui. There should 
be no global edit
@@ -54,9 +50,11 @@
         * same as mediaWiki.log() but prefixes the log entry with 'wb:'
         */
        this.log = function() {
-               var args = $.makeArray( arguments );
-               args.unshift( 'wb:' );
-               mw.log.apply( mw.log, args );
+               if( mw ) {
+                       var args = $.makeArray( arguments );
+                       args.unshift( 'wb:' );
+                       mw.log.apply( mw.log, args );
+               }
        };
 
        /**
@@ -72,36 +70,20 @@
        this.entity = null;
 
        /**
-        * Holds very basic information about all entities used in the pages 
entity view. Entity IDs
-        * are used as keys for many inner hashes where each is an instance of 
wb.FetchedContent.
-        * Each of those wb.FetchedContent instances holds an instance of 
wb.Entity as its content. This
-        * entity data might be incomplete though, it is guaranteed that ID, 
and label are provided,
-        * for wb.Property instances it is also guaranteed that the datatype is 
provided.
-        *
-        * TODO: This should vanish from here (since its global) and should be 
replaced with a proper
-        *       store interface which will be injected into each 
jQuery.wikibase.entityview
-        *
-        * @since 0.4
-        *
-        * @type {Object}
-        */
-       this.fetchedEntities = {};
-
-       /**
         * Will hold a list of all the sites after getSites() was called. This 
will cache the result.
         *
         * TODO: This should go together with the old UI.
         *
-        * @var wikibase.Site[]
+        * @type wikibase.Site[]
         */
        this._siteList = null;
 
        /**
         * Holds a RevisionStore object to have access to stored revision ids.
         *
-        * TODO: Should go with the implementation of proper store interface 
(see fetchedEntities todo)
+        * TODO: (Bug 54082) Should go with the implementation of proper store 
interface.
         *
-        * @var wikibase.RevisionStore
+        * @type wikibase.RevisionStore
         */
        this._revisionStore = null;
 
diff --git a/repo/resources/Resources.php b/repo/resources/Resources.php
index 403a68d..fb219a3 100644
--- a/repo/resources/Resources.php
+++ b/repo/resources/Resources.php
@@ -37,6 +37,31 @@
        );
 
        return array(
+               'wikibase.repo' => $moduleTemplate + array(
+                       'scripts' => array(
+                               'wikibase.repo/repo.js',
+                               'wikibase.repo/repo.fetchedEntities.js'
+                       ),
+                       'dependencies' => array(
+                               'wikibase'
+                       ),
+               ),
+
+               'mw.ext.wikibase.repo.fetchedEntitiesFromMwConfig' => 
$moduleTemplate + array(
+                       'scripts' => array(
+                               'mw.ext.wikibase.repo/repo.js',
+                               
'mw.ext.wikibase.repo/repo.fetchedEntitiesFromMwConfig.js'
+                       ),
+                       'dependencies' => array(
+                               'mw.ext.wikibase',
+                               'wikibase.repo',
+                               'wikibase.datamodel',
+                               'wikibase.serialization.entities',
+                               'wikibase.serialization.fetchedcontent',
+                               'jquery.json',
+                       ),
+               ),
+
                'wikibase.ui.entityViewInit' => $moduleTemplate + array(
                        'scripts' => array(
                                'wikibase.ui.entityViewInit.js' // should 
probably be adjusted for more modularity
@@ -50,9 +75,8 @@
                                'wikibase.datamodel',
                                'jquery.json',
                                'jquery.cookie',
-                               'wikibase.serialization.entities',
-                               'wikibase.serialization.fetchedcontent',
-                               'jquery.wikibase.claimgrouplabelscroll'
+                               'jquery.wikibase.claimgrouplabelscroll',
+                               
'mw.ext.wikibase.repo.fetchedEntitiesFromMwConfig',
                        ),
                        'messages' => array(
                                'wikibase-statements',
diff --git 
a/repo/resources/mw.ext.wikibase.repo/repo.fetchedEntitiesFromMwConfig.js 
b/repo/resources/mw.ext.wikibase.repo/repo.fetchedEntitiesFromMwConfig.js
new file mode 100644
index 0000000..8a52a53
--- /dev/null
+++ b/repo/resources/mw.ext.wikibase.repo/repo.fetchedEntitiesFromMwConfig.js
@@ -0,0 +1,63 @@
+/**
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ *
+ * Takes MediaWiki's mediawiki.config.get( 'wbUsedEntities' ) and builds the 
global local entity
+ * store from that (wikibase.fetchedEntities).
+ * Will invoked immediately after script inclusion. Exposing this function is 
for testing purposes
+ * only.
+ *
+ * @private For testing the mechanism only.
+ *
+ * @since 0.5
+ *
+ * TODO: Should vanish together with wikibase.fetchedEntities as bug 54082 
gets resolved.
+ */
+mediaWiki.ext.wikibase.repo.fetchedEntitiesFromMwConfig = ( function( $, wb, 
mwConfig ) {
+       'use strict';
+
+       var Entity = wb.Entity;
+       var FetchedContent = wb.store.FetchedContent;
+       var SerializerFactory = wb.serialization.SerializerFactory;
+
+       function fetchedEntitiesFromMwConfig() {
+               var unserializerFactory = new SerializerFactory();
+               var entityUnserializer = 
unserializerFactory.newUnserializerFor( Entity );
+
+               // unserializer for fetched content whose content is a 
wb.Entity:
+               var fetchedEntityUnserializer = 
unserializerFactory.newUnserializerFor(
+                       FetchedContent, {
+                               contentUnserializer: entityUnserializer
+                       }
+               );
+
+               var serializedFetchedEntities = 
getSerializedFetchedEntitiesFromConfig( 'wbUsedEntities' );
+
+               $.each( serializedFetchedEntities, function( id, 
serializedFetchedEntity ) {
+                       var fetchedEntity = 
fetchedEntityUnserializer.unserialize( serializedFetchedEntity );
+                       wb.fetchedEntities[ id ] = fetchedEntity;
+               } );
+       }
+
+       /**
+        * Helper for getting the serialized fetched entities in object form 
out of mw.config.
+        *
+        * @param {string} varName mw.config var name
+        * @returns {Object}
+        *
+        * @throws {Error} In case the given config variable name does not 
exist.
+        */
+       function getSerializedFetchedEntitiesFromConfig( varName ) {
+               if( !mwConfig.exists( varName ) ) {
+                       throw new Error( 'Can not load data for 
wikibase.fetchedEntities. mw.config variable "'
+                               + varName + '" is not set.' );
+               }
+               return $.evalJSON( mwConfig.get( varName ) );
+       }
+
+       // Allows to simply add this resource loader module to the output.
+       fetchedEntitiesFromMwConfig();
+
+       return fetchedEntitiesFromMwConfig;
+
+}( jQuery, wikibase, mediaWiki.config ) );
diff --git a/repo/resources/mw.ext.wikibase.repo/repo.js 
b/repo/resources/mw.ext.wikibase.repo/repo.js
new file mode 100644
index 0000000..60b9bb4
--- /dev/null
+++ b/repo/resources/mw.ext.wikibase.repo/repo.js
@@ -0,0 +1,11 @@
+/**
+ * Entry point for MediaWiki "WikibaseRepo" extension JavaScript code. Adds an 
extension object to
+ * the global MediaWiki object and provides some MediaWiki specific wikibase 
code.
+ *
+ * @since 0.5
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ */
+mediaWiki.ext.wikibase.repo = new ( function MwExtWikibaseRepo() {
+       'use strict';
+} )();
\ No newline at end of file
diff --git a/repo/resources/wikibase.repo/repo.fetchedEntities.js 
b/repo/resources/wikibase.repo/repo.fetchedEntities.js
new file mode 100644
index 0000000..57b1ee7
--- /dev/null
+++ b/repo/resources/wikibase.repo/repo.fetchedEntities.js
@@ -0,0 +1,30 @@
+/**
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ *
+ * Singleton holding very basic information about all entities used on a page 
(e.g. in an entity
+ * view). Entity IDs are used as keys for many inner hashes where each is an 
instance of
+ * wb.FetchedContent. Each of these wb.FetchedContent instances holds an 
instance of wb.Entity as
+ * its content. This entity data might be incomplete though, it is guaranteed 
that ID, and label are
+ * provided, for wb.Property instances it is also guaranteed that the data 
type is provided.
+ *
+ * TODO: (Bug 54082) This global store is bad and should be replaced by proper 
store implementations
+ *  which should then be properly injected into our view widgets.
+ *
+ * @since 0.5 (as plain object in wikibase.fetchedEntities since 0.4)
+ *
+ * @type {Object}
+ */
+wikibase.repo.fetchedEntities = new ( function WbRepoFetchedEntities() {
+       'use strict';
+} )();
+
+/**
+ * @deprecated Use wikibase.repo.fetchedEntities instead. Has been moved to 
wikibase.repo to make
+ *             clear that this should only be used in the repo part of 
wikibase.
+ *
+ * @since 0.4
+ *
+ * @type {Object}
+ */
+wikibase.fetchedEntities = wikibase.repo.fetchedEntities;
diff --git a/repo/resources/wikibase.repo/repo.js 
b/repo/resources/wikibase.repo/repo.js
new file mode 100644
index 0000000..546e79d
--- /dev/null
+++ b/repo/resources/wikibase.repo/repo.js
@@ -0,0 +1,10 @@
+/**
+ * Global Wikibase Repo singleton.
+ *
+ * @since 0.5
+ * @licence GNU GPL v2+
+ * @author Daniel Werner < [email protected] >
+ */
+wikibase.repo = new ( function WbRepo() {
+       'use strict';
+} )();
diff --git a/repo/resources/wikibase.ui.entityViewInit.js 
b/repo/resources/wikibase.ui.entityViewInit.js
index 64cab69..307fce0 100644
--- a/repo/resources/wikibase.ui.entityViewInit.js
+++ b/repo/resources/wikibase.ui.entityViewInit.js
@@ -77,24 +77,12 @@
                } );
 
                if( mw.config.get( 'wbEntity' ) !== null ) {
-                       var entityJSON = $.evalJSON( mw.config.get( 'wbEntity' 
) ),
-                               usedEntitiesJSON = $.evalJSON( mw.config.get( 
'wbUsedEntities' ) ),
-                               unserializerFactory = new 
wb.serialization.SerializerFactory(),
-                               entityUnserializer = 
unserializerFactory.newUnserializerFor( wb.Entity );
-
-                       // unserializer for fetched content whose content is a 
wb.Entity:
-                       var fetchedEntityUnserializer = 
unserializerFactory.newUnserializerFor(
-                               wb.store.FetchedContent, {
-                                       contentUnserializer: entityUnserializer
-                               }
-                       );
+                       var entityJSON = $.evalJSON( mw.config.get( 'wbEntity' 
) );
+                       var unserializerFactory = new 
wb.serialization.SerializerFactory();
+                       var entityUnserializer = 
unserializerFactory.newUnserializerFor( wb.Entity );
 
                        wb.entity = entityUnserializer.unserialize( entityJSON 
);
                        entityJSON = null;
-
-                       $.each( usedEntitiesJSON, function( id, 
fetchedEntityJSON ) {
-                               wb.fetchedEntities[ id ] = 
fetchedEntityUnserializer.unserialize( fetchedEntityJSON );
-                       } );
 
                        // if there are no aliases yet, the DOM structure for 
creating new ones is created manually since it is not
                        // needed for running the page without JS

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id6ddb4941d828e28530a50e2ce3e18e7c279462b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Werner <[email protected]>

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

Reply via email to