mw.Title: Add method to get title relative to an arbitrary namespace
authorAlex Monk <krenair@wikimedia.org>
Fri, 4 Jul 2014 17:02:07 +0000 (18:02 +0100)
committerCatrope <roan.kattouw@gmail.com>
Mon, 22 Sep 2014 17:42:34 +0000 (17:42 +0000)
I'm not sure where this kind of thing could be used outside of
dealing with templates/transclusion. Making it generic anyway.

Bug: 67448
Change-Id: Ie554adefec43997d362b5d7b45c30403912743b5

resources/src/mediawiki/mediawiki.Title.js
tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js

index fc8e7e9..95b18a8 100644 (file)
                        return text( this.getPrefixedDb() );
                },
 
+               /**
+                * Get the page name relative to a namespace
+                *
+                * Example:
+                *
+                * - "Foo:Bar" relative to the Foo namespace becomes "Bar".
+                * - "Bar" relative to any non-main namespace becomes ":Bar".
+                * - "Foo:Bar" relative to any namespace other than Foo stays "Foo:Bar".
+                *
+                * @param {number} namespace The namespace to be relative to
+                * @return {string}
+                */
+               getRelativeText: function ( namespace ) {
+                       if ( this.getNamespaceId() === namespace ) {
+                               return this.getMainText();
+                       } else if ( this.getNamespaceId() === NS_MAIN ) {
+                               return ':' + this.getPrefixedText();
+                       } else {
+                               return this.getPrefixedText();
+                       }
+               },
+
                /**
                 * Get the fragment (if any).
                 *
index 077ce70..5ece31b 100644 (file)
                }
        } );
 
+       QUnit.test( 'getRelativeText', 5, function ( assert ) {
+               var cases = [
+                       {
+                               text: 'asd',
+                               relativeTo: 123,
+                               expectedResult: ':Asd'
+                       },
+                       {
+                               text: 'dfg',
+                               relativeTo: 0,
+                               expectedResult: 'Dfg'
+                       },
+                       {
+                               text: 'Template:Ghj',
+                               relativeTo: 0,
+                               expectedResult: 'Template:Ghj'
+                       },
+                       {
+                               text: 'Template:1',
+                               relativeTo: 10,
+                               expectedResult: '1'
+                       },
+                       {
+                               text: 'User:Hi',
+                               relativeTo: 10,
+                               expectedResult: 'User:Hi'
+                       }
+               ], i, thisCase, title;
+
+               for ( i = 0; i < cases.length; i++ ) {
+                       thisCase = cases[i];
+
+                       title = mw.Title.newFromText( thisCase.text );
+                       assert.equal( title.getRelativeText( thisCase.relativeTo ), thisCase.expectedResult );
+               }
+       } );
 }( mediaWiki, jQuery ) );