Mwjames has uploaded a new change for review.

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


Change subject: Add methods to access config/settings array via JS
......................................................................

Add methods to access config/settings array via JS

* smw.settings()
* smw.settings.get()
* smw.formats.getList()
* smw.formats.getName()
* smw.version()

Change-Id: Ia6d1e342c3a1e7196bbb3019d212ec2b13d6c0cb
---
M SemanticMediaWiki.hooks.php
M resources/smw/ext.smw.js
M tests/qunit/smw/ext.smw.test.js
3 files changed, 135 insertions(+), 2 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/46/61346/1

diff --git a/SemanticMediaWiki.hooks.php b/SemanticMediaWiki.hooks.php
index 64f774c..d65fdb4 100644
--- a/SemanticMediaWiki.hooks.php
+++ b/SemanticMediaWiki.hooks.php
@@ -379,7 +379,9 @@
 
        /**
         * Hook: ResourceLoaderGetConfigVars called right before
-        * ResourceLoaderStartUpModule::getConfig returns
+        * ResourceLoaderStartUpModule::getConfig and exports static 
configuration
+        * variables to JavaScript. Things that depend on the current
+        * page/request state should use MakeGlobalVariablesScript instead
         *
         * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/ResourceLoaderGetConfigVars
         *
@@ -398,6 +400,14 @@
                        )
                );
 
+               foreach ( array_keys( $GLOBALS['smwgResultFormats'] ) as 
$format ) {
+                       // Special formats "count" and "debug" currently not 
supported.
+                       if ( $format != 'broadtable' && $format != 'count' && 
$format != 'debug' ) {
+                               $printer = SMWQueryProcessor::getResultPrinter( 
$format, SMWQueryProcessor::SPECIAL_PAGE );
+                               $vars['smw-config']['formats'][$format] = 
$printer->getName();
+                       }
+               }
+
                return true;
        }
 
diff --git a/resources/smw/ext.smw.js b/resources/smw/ext.smw.js
index 68d3ba5..97f7ba4 100644
--- a/resources/smw/ext.smw.js
+++ b/resources/smw/ext.smw.js
@@ -37,7 +37,85 @@
                else {
                        return mediaWiki.msg.apply( mediaWiki.msg, arguments );
                }
+
        };
+
+       /**
+        * Declares methods to access information about available formats
+        */
+       instance.formats = {
+
+               /**
+                * Returns "real" name in the current page content language of a
+                * select format
+                *
+                * @since 1.9
+                *
+                * @param {String} format
+                *
+                * @return {String}
+                */
+               getName: function( format ) {
+                       if( typeof format === 'string' ){
+                               return mediaWiki.config.get( 'smw-config' 
).formats[format];
+                       }
+                       return undefined;
+               },
+
+               /**
+                * Returns list of available formats
+                *
+                * @since 1.9
+                *
+                * @return {Object}
+                */
+               getList: function() {
+                       return mediaWiki.config.get( 'smw-config' ).formats;
+               }
+       };
+
+       /**
+        * Returns SMW settings array
+        *
+        * @see SMW\Settings::newFromGlobals
+        *
+        * @since 1.9
+        *
+        * @return {Mixed}
+        */
+       instance.settings = function() {
+               return mediaWiki.config.get( 'smw-config' ).settings;
+       };
+
+       /**
+        * Returns a specific settings value
+        *
+        * @see SMW\Settings::get
+        *
+        * @since 1.9
+        *
+        * @param  {String} key options to be selected
+        *
+        * @return {Mixed}
+        */
+       instance.settings.get = function( key ) {
+               if( typeof key === 'string' ){
+                       return mediaWiki.config.get( 'smw-config' 
).settings[key];
+               }
+               return undefined;
+       };
+
+       /**
+        * Returns SMW version
+        *
+        * @since 1.9
+        *
+        * @return {String}
+        */
+       instance.version = function() {
+               return mediaWiki.config.get( 'smw-config' ).version;
+       };
+
        return instance;
 } )();
 
diff --git a/tests/qunit/smw/ext.smw.test.js b/tests/qunit/smw/ext.smw.test.js
index 75217c1..8af1e66 100644
--- a/tests/qunit/smw/ext.smw.test.js
+++ b/tests/qunit/smw/ext.smw.test.js
@@ -21,10 +21,55 @@
         *
         * @since: 1.9
         */
-       QUnit.test( 'init', 2, function ( assert ) {
+       QUnit.test( 'init', 8, function ( assert ) {
 
                assert.ok( smw instanceof Object, pass + 'the smw instance was 
accessible' );
                assert.equal( $.type( smw.log ), 'function', pass + '.log() was 
accessible' );
+               assert.equal( $.type( smw.msg ), 'function', pass + '.msg() was 
accessible' );
+               assert.equal( $.type( smw.settings ), 'function', pass + 
'.settings() was accessible' );
+               assert.equal( $.type( smw.settings.get ), 'function', pass + 
'.settings.get() was accessible' );
+               assert.equal( $.type( smw.version ), 'function', pass + 
'.version() was accessible' );
+               assert.equal( $.type( smw.formats.getName ), 'function', pass + 
'.formats.getName() was accessible' );
+               assert.equal( $.type( smw.formats.getList ), 'function', pass + 
'.formats.getList() was accessible' );
+
+       } );
+
+       /**
+        * Test settings function
+        *
+        * @since: 1.9
+        */
+       QUnit.test( 'settings', 4, function ( assert ) {
+
+               assert.equal( $.type( smw.settings() ), 'object', pass + 
'returned a settings object' );
+               assert.equal( $.type( smw.settings.get( 'smwgQMaxLimit' ) ), 
'number', pass + 'returned a value for a specific key (smwgQMaxLimit)' );
+               assert.equal( smw.settings.get( 'lula' ), undefined, pass + 
'returned undefined for an unknown key' );
+               assert.equal( smw.settings.get(), undefined, pass + 'returned 
undefined for an empty key' );
+
+       } );
+
+       /**
+        * Test formats function
+        *
+        * @since: 1.9
+        */
+       QUnit.test( 'formats', 4, function ( assert ) {
+
+               assert.equal( $.type( smw.formats.getList() ), 'object', pass + 
'.getList() returned an object' );
+               assert.equal( $.type( smw.formats.getName( 'table' ) ), 
'string', pass + 'returned a name for a specific format (table)' );
+               assert.equal( smw.formats.getName( 'lula' ), undefined, pass + 
'returned undefined for an unknown key' );
+               assert.equal( smw.formats.getName(), undefined, pass + 
'returned undefined for an empty key' );
+
+       } );
+
+       /**
+        * Test version function
+        *
+        * @since: 1.9
+        */
+       QUnit.test( 'version', 1, function ( assert ) {
+
+               assert.equal( $.type( smw.version() ), 'string', pass + 
'returned a version' );
 
        } );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia6d1e342c3a1e7196bbb3019d212ec2b13d6c0cb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <jamesin.hongkon...@gmail.com>

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

Reply via email to