jenkins-bot has submitted this change and it was merged.

Change subject: Add smw.util.namespace
......................................................................


Add smw.util.namespace

* Add smw.util.namespace.getList()
* Add smw.util.namespace.getId()
* Add smw.util.namespace.getName()

Change-Id: If526e56b4b6633bad0e40c54f7e3f36d1c41cd8a
---
M SemanticMediaWiki.hooks.php
M resources/smw/api/ext.smw.api.js
M resources/smw/ext.smw.js
M tests/qunit/smw/ext.smw.test.js
4 files changed, 344 insertions(+), 112 deletions(-)

Approvals:
  Mwjames: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/SemanticMediaWiki.hooks.php b/SemanticMediaWiki.hooks.php
index 14ad262..726c6a6 100644
--- a/SemanticMediaWiki.hooks.php
+++ b/SemanticMediaWiki.hooks.php
@@ -414,6 +414,12 @@
                        )
                );
 
+               // Available semantic namespaces
+               foreach ( array_keys( 
$GLOBALS['smwgNamespacesWithSemanticLinks'] ) as $ns ) {
+                       $name = MWNamespace::getCanonicalName( $ns );
+                       $vars['smw-config']['settings']['namespace'][$name] = 
$ns;
+               }
+
                foreach ( array_keys( $GLOBALS['smwgResultFormats'] ) as 
$format ) {
                        // Special formats "count" and "debug" currently not 
supported.
                        if ( $format != 'broadtable' && $format != 'count' && 
$format != 'debug' ) {
diff --git a/resources/smw/api/ext.smw.api.js b/resources/smw/api/ext.smw.api.js
index ddfdfb9..c0fe954 100644
--- a/resources/smw/api/ext.smw.api.js
+++ b/resources/smw/api/ext.smw.api.js
@@ -1,17 +1,35 @@
 /**
- * SMW Api base class
+ * This file is part of the Semantic MediaWiki Extension
+ * @see https://semantic-mediawiki.org/
  *
- * @since 1.9
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 
USA
  *
  * @file
+ * @ignore
+ *
+ * @since 1.9
  * @ingroup SMW
  *
- * @licence GNU GPL v2 or later
+ * @licence GNU GPL v2+
  * @author mwjames
  */
-/*global md5 */
 ( function( $, mw, smw ) {
        'use strict';
+
+       /*global md5 */
 
        /**
         * Constructor to create an object to interact with the API and SMW
@@ -19,13 +37,14 @@
         * @since 1.9
         *
         * @class
+        * @alias smw.Api
         * @constructor
         */
-       smw.Api = function() {};
+       smw.api = function() {};
 
        /* Public methods */
 
-       smw.Api.prototype = {
+       smw.api.prototype = {
 
                /**
                 * Convenience method to parse and map a JSON string
@@ -131,6 +150,6 @@
        };
 
        //Alias
-       smw.api = smw.Api;
+       smw.Api = smw.api;
 
 } )( jQuery, mediaWiki, semanticMediaWiki );
\ No newline at end of file
diff --git a/resources/smw/ext.smw.js b/resources/smw/ext.smw.js
index 97f7ba4..59601f9 100644
--- a/resources/smw/ext.smw.js
+++ b/resources/smw/ext.smw.js
@@ -1,29 +1,68 @@
 /**
- * JavaScript for Semantic MediaWiki.
- * @see http://semantic-mediawiki.org/
+ * This file is part of the Semantic MediaWiki Extension
+ * @see https://semantic-mediawiki.org/
  *
- * @licence GNU GPL v3 or later
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 
USA
+ *
+ * @file
+ *
+ * @since 1.9
+ * @ingroup SMW
+ *
+ * @licence GNU GPL v2+
  * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
+ * @author mwjames
  */
-/*global console:true message:true */
 
-// Declare instance
-var instance = ( function () {
+/**
+ * Declares global smw instance and namespace
+ *
+ * @class smw
+ */
+var instance = ( function ( $ ) {
        'use strict';
+
+       /*global console:true message:true */
 
        var instance = {};
 
+       /**
+        * Outputs a debug log
+        *
+        * @since 1.8
+        *
+        * @return {string}
+        */
        instance.log = function( message ) {
                if ( typeof mediaWiki === 'undefined' ) {
                        if ( typeof console !== 'undefined' ) {
-                               console.log( 'SMW: ' + message );
+                               console.log( 'SMW: ', message );
                        }
-               }
-               else {
-                       return mediaWiki.log.call( mediaWiki.log, 'SMW: ' + 
message );
+               } else {
+                       return mediaWiki.log.call( mediaWiki.log, 'SMW: ', 
message );
                }
        };
 
+       /**
+        * Outputs a message
+        *
+        * @since 1.8
+        *
+        * @return {string}
+        */
        instance.msg = function() {
                if ( typeof mediaWiki === 'undefined' ) {
                        message = window.wgSMWMessages[arguments[0]];
@@ -31,78 +70,10 @@
                        for ( var i = arguments.length - 1; i > 0; i-- ) {
                                message = message.replace( '$' + i, 
arguments[i] );
                        }
-
                        return message;
-               }
-               else {
+               } 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;
        };
 
        /**
@@ -110,14 +81,185 @@
         *
         * @since 1.9
         *
-        * @return {String}
+        * @return {string}
         */
        instance.version = function() {
                return mediaWiki.config.get( 'smw-config' ).version;
        };
 
+       /**
+        * Declares methods to access utility functions
+        * @class smw.util
+        * @alias smw.Util
+        */
+       instance.util = {
+
+               /**
+                * Strip some illegal chars: control chars, colon, less than, 
greater than,
+                * brackets, braces, pipe, whitespace and normal spaces. This 
still leaves some insanity
+                * intact, like unicode bidi chars, but it's a good start..
+                *
+                * Borrowed from mw.Title
+                *
+                * @ignore
+                * @param {string} s
+                * @return {string}
+                */
+               clean: function ( s ) {
+                       if ( s !== undefined ) {
+                               return s.trim().replace( 
/[\x00-\x1f\x23\x3c\x3e\x5b\x5d\x7b\x7c\x7d\x7f\s]+/g, '_' );
+                       }
+               },
+
+               /**
+                * Capitalizes the first letter of a string
+                *
+                * @ignore
+                * @param {string} s
+                * @return {string}
+                */
+               capitalize: function( s ) {
+                       return s.charAt(0).toUpperCase() + s.slice(1);
+               },
+
+               /**
+                * Declares methods to access information about namespace 
settings
+                * @class smw.util.namespace
+                * @static
+                */
+               namespace: {
+
+                       /**
+                        * Returns list of available namespaces
+                        *
+                        * @since 1.9
+                        *
+                        * @return {Object}
+                        */
+                       getList: function() {
+                               return instance.settings.get( 'namespace' );
+                       },
+
+                       /**
+                        * Returns namespace Id
+                        *
+                        * @since 1.9
+                        *
+                        * @param {string} key
+                        *
+                        * @return {number}
+                        */
+                       getId: function( key ) {
+                               if( typeof key === 'string' ) {
+                                       return this.getList()[ 
instance.util.capitalize( instance.util.clean( key ) ) ];
+                               }
+                               return undefined;
+                       },
+
+                       /**
+                        * Returns formatted localized name for a selected 
namespace
+                        *
+                        * @since 1.9
+                        *
+                        * @param {string} key
+                        *
+                        * @return {string}
+                        */
+                       getName: function( key ) {
+                               if( typeof key === 'string' ) {
+                                       var id = this.getId( key );
+                                       return id && mediaWiki.config.get( 
'wgFormattedNamespaces' )[id.toString()];
+                               }
+                               return undefined;
+                       }
+               }
+
+       };
+
+       /**
+        * Declares methods to access information about available formats
+        * @class smw.formats
+        * @alias smw.Formats
+        */
+       instance.formats = {
+
+               /**
+                * Returns list of available formats
+                *
+                * @since 1.9
+                * @extends smw.formats
+                *
+                * @return {Object}
+                */
+               getList: function() {
+                       return mediaWiki.config.get( 'smw-config' ).formats;
+               },
+
+               /**
+                * Returns localized name for  a select format
+                *
+                * @since 1.9
+                *
+                * @param {string} format
+                *
+                * @return {string}
+                */
+               getName: function( format ) {
+                       if( typeof format === 'string' ){
+                               return this.getList()[ instance.util.clean( 
format ).toLowerCase() ];
+                       }
+                       return undefined;
+               }
+       };
+
+       /**
+        * Access settings array
+        *
+        * @since 1.9
+        *
+        * @class smw.settings
+        * @alias smw.Settings
+        * @static
+        *
+        * @return {mixed}
+        */
+       instance.settings = {
+
+               /**
+                * Returns list of available settings
+                *
+                * @since 1.9
+                *
+                * @return {Object}
+                */
+               getList: function() {
+                       return mediaWiki.config.get( 'smw-config' ).settings;
+               },
+
+               /**
+                * Returns a specific settings value (see SMW\Settings::get)
+                *
+                * @since 1.9
+                *
+                * @param  {string} setting to be selected
+                *
+                * @return {mixed}
+                */
+               get: function( key ) {
+                       if( typeof key === 'string' ) {
+                               return this.getList()[key];
+                       }
+                       return undefined;
+               }
+       };
+
+       // Alias
+       instance.Util = instance.util;
+
+       // Make invoked methods public
        return instance;
-} )();
+
+} )( jQuery );
 
 // Assign namespace
 window.smw = window.semanticMediaWiki = instance;
diff --git a/tests/qunit/smw/ext.smw.test.js b/tests/qunit/smw/ext.smw.test.js
index 8af1e66..2da15f3 100644
--- a/tests/qunit/smw/ext.smw.test.js
+++ b/tests/qunit/smw/ext.smw.test.js
@@ -1,36 +1,65 @@
 /**
- * QUnit tests
+ * This file is part of the Semantic MediaWiki Extension
+ * @see https://semantic-mediawiki.org/
  *
- * @since 1.9
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 
USA
  *
  * @file
+ *
+ * @since 1.9
  * @ingroup SMW
  *
- * @licence GNU GPL v2 or later
+ * @licence GNU GPL v2+
  * @author mwjames
+ */
+
+/**
+ * Tests methods provided by ext.smw.js
+ *
  */
 ( function ( $, mw, smw ) {
        'use strict';
 
        QUnit.module( 'ext.smw', QUnit.newMwEnvironment() );
 
-       var pass = 'Passes because ';
-
        /**
         * Test initialization and accessibility
         *
         * @since: 1.9
         */
-       QUnit.test( 'init', 8, function ( assert ) {
+       QUnit.test( 'init', 14, 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' );
+               assert.ok( smw instanceof Object, 'the smw instance was 
accessible' );
+
+               assert.equal( $.type( smw.log ), 'function', '.log() was 
accessible' );
+               assert.equal( $.type( smw.msg ), 'function','.msg() was 
accessible' );
+
+               assert.equal( $.type( smw.settings.getList ), 'function', 
'.settings.getList() was accessible' );
+               assert.equal( $.type( smw.settings.get ), 'function', 
'.settings.get() was accessible' );
+
+               assert.equal( $.type( smw.formats.getName ), 'function', 
'.formats.getName() was accessible' );
+               assert.equal( $.type( smw.formats.getList ), 'function', 
'.formats.getList() was accessible' );
+
+               assert.equal( $.type( smw.util.clean ), 'function', 
'.util.clean() was accessible' );
+               assert.equal( $.type( smw.util.capitalize ), 'function', 
'.util.capitalize() was accessible' );         assert.equal( $.type( 
smw.util.namespace ), 'object', '.settings.ns object was accessible' );
+               assert.equal( $.type( smw.util.namespace.getList ), 'function', 
'.util.namespace.getList() was accessible' );
+               assert.equal( $.type( smw.util.namespace.getId ), 'function', 
'.util.namespace.getId() was accessible' );
+               assert.equal( $.type( smw.util.namespace.getName ), 'function', 
'.util.namespace.getName() was accessible' );
+
+               assert.equal( $.type( smw.version ), 'function', '.version() 
was accessible' );
 
        } );
 
@@ -41,10 +70,42 @@
         */
        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' );
+               assert.equal( $.type( smw.settings.getList() ), 'object', 
'.getList() returned a list of settings object' );
+               assert.equal( $.type( smw.settings.get( 'smwgQMaxLimit' ) ), 
'number', '.get( "smwgQMaxLimit" ) returned a value for the key' );
+               assert.equal( smw.settings.get( 'lula' ), undefined, '.get( 
"lula" ) returned undefined for an unknown key' );
+               assert.equal( smw.settings.get(), undefined, '.get() returned 
undefined for an empty key' );
+
+       } );
+
+       /**
+        * Test util function
+        *
+        * @since: 1.9
+        */
+       QUnit.test( 'util', 3, function ( assert ) {
+
+               assert.equal( smw.util.clean( ' Foo | ; : - < >_= () {} bar ' 
), 'Foo_;_:_-__=_()_bar', '.clean() returned a cleaned string' );
+               assert.equal( smw.util.clean( 'Foo | ; : - < >_= () {} bar' ), 
'Foo_;_:_-__=_()_bar', '.clean() returned a cleaned string' );
+               assert.equal( smw.util.capitalize( 'foo Foo bar' ), 'Foo Foo 
bar', '.capitalize() returned a capitalized string' );
+
+       } );
+
+       /**
+        * Test namespace function
+        *
+        * @since: 1.9
+        */
+       QUnit.test( 'util.namespace', 7, function ( assert ) {
+
+               assert.equal( $.type( smw.util.namespace.getList() ), 'object', 
'.getList() returned a list of namespaces' );
+
+               assert.equal( $.type( smw.util.namespace.getId( 'property' ) ), 
'number', '.getId( "property" ) returned a number' );
+               assert.equal( $.type( smw.util.namespace.getId( 'Property' ) ), 
'number', '.getId( "Property" ) returned a number' );
+               assert.equal( $.type( smw.util.namespace.getId( 'concept' ) ), 
'number', '.getId( "concept" ) returned a number' );
+               assert.equal( smw.util.namespace.getId( 'lula' ), undefined, 
'.getId( "lula" )  returned undefined for an unknown key' );
+
+               assert.equal( $.type( smw.util.namespace.getName( 'property' ) 
), 'string', '.getName( "property" ) returned a string' );
+               assert.equal( smw.util.namespace.getName( 'lula' ), undefined, 
'.getName( "lula" ) returned undefined for an unknown key' );
 
        } );
 
@@ -53,12 +114,16 @@
         *
         * @since: 1.9
         */
-       QUnit.test( 'formats', 4, function ( assert ) {
+       QUnit.test( 'formats', 7, 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' );
+               assert.equal( $.type( smw.formats.getList() ), 'object', 
'.getList() returned an object' );
+               assert.equal( $.type( smw.formats.getName( 'table' ) ), 
'string', '.getName( "table" ) returned a string' );
+               assert.equal( $.type( smw.formats.getName( ' table ' ) ), 
'string',  '.getName( " table " ) returned a string' );
+               assert.equal( $.type( smw.formats.getName( 'Table' ) ), 
'string', '.getName( "Table" ) returned a string' );
+
+               assert.equal( smw.formats.getName( 'lula' ), undefined, 
'.getName() returned undefined for an unknown key' );
+               assert.equal( smw.formats.getName( 123456 ), undefined, 
'.getName() returned undefined for an unknown key' );
+               assert.equal( smw.formats.getName(), undefined, '.getName() 
returned undefined for an empty key' );
 
        } );
 
@@ -69,7 +134,7 @@
         */
        QUnit.test( 'version', 1, function ( assert ) {
 
-               assert.equal( $.type( smw.version() ), 'string', pass + 
'returned a version' );
+               assert.equal( $.type( smw.version() ), 'string', '.version() 
returned a string' );
 
        } );
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If526e56b4b6633bad0e40c54f7e3f36d1c41cd8a
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <jamesin.hongkon...@gmail.com>
Gerrit-Reviewer: Mwjames <jamesin.hongkon...@gmail.com>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to