Merge "resourceloader: Simplify StringSet fallback"
[lhc/web/wiklou.git] / resources / src / mediawiki.Title / Title.js
index 1d0aa67..0bcb2b6 100644 (file)
@@ -4,7 +4,7 @@
  * @since 1.18
  */
 
-( function ( mw, $ ) {
+( function () {
        /**
         * Parse titles into an object structure. Note that when using the constructor
         * directly, passing invalid titles will result in an exception. Use #newFromText to use the
                return null;
        };
 
+       /**
+        * Check if a given namespace is a talk namespace
+        * @param {number} namespaceId Namespace ID
+        * @return {boolean} Namespace is a talk namespace
+        */
+       Title.isTalkNamespace = function ( namespaceId ) {
+               return !!( namespaceId > NS_MAIN && namespaceId % 2 );
+       };
+
        /**
         * Whether this title exists on the wiki.
         *
         * @static
         * @param {string|mw.Title} title prefixed db-key name (string) or instance of Title
         * @return {boolean|null} Boolean if the information is available, otherwise null
+        * @throws {Error} If title is not a string or mw.Title
         */
        Title.exists = function ( title ) {
                var match,
                 */
                getName: function () {
                        if (
-                               $.inArray( this.namespace, mw.config.get( 'wgCaseSensitiveNamespaces' ) ) !== -1 ||
+                               mw.config.get( 'wgCaseSensitiveNamespaces' ).indexOf( this.namespace ) !== -1 ||
                                !this.title.length
                        ) {
                                return this.title;
                        }
                },
 
+               /**
+                * Check if the title is in a talk namespace
+                *
+                * @return {boolean} The title is in a talk namespace
+                */
+               isTalkPage: function () {
+                       return Title.isTalkNamespace( this.getNamespaceId() );
+               },
+
+               /**
+                * Get the title for the associated talk page
+                *
+                * @return {mw.Title|null} The title for the associated talk page, null if not available
+                */
+               getTalkPage: function () {
+                       if ( !this.canHaveTalkPage() ) {
+                               return null;
+                       }
+                       return this.isTalkPage() ?
+                               this :
+                               Title.makeTitle( this.getNamespaceId() + 1, this.getMainText() );
+               },
+
+               /**
+                * Get the title for the subject page of a talk page
+                *
+                * @return {mw.Title|null} The title for the subject page of a talk page, null if not available
+                */
+               getSubjectPage: function () {
+                       return this.isTalkPage() ?
+                               Title.makeTitle( this.getNamespaceId() - 1, this.getMainText() ) :
+                               this;
+               },
+
+               /**
+                * Check the the title can have an associated talk page
+                *
+                * @return {boolean} The title can have an associated talk page
+                */
+               canHaveTalkPage: function () {
+                       return this.getNamespaceId() >= NS_MAIN;
+               },
+
                /**
                 * Whether this title exists on the wiki.
                 *
        // Expose
        mw.Title = Title;
 
-}( mediaWiki, jQuery ) );
+}() );