From d4074b8df7d45d38c21c7a62714a2a782229635d Mon Sep 17 00:00:00 2001 From: Krinkle Date: Wed, 16 Feb 2011 23:23:04 +0000 Subject: [PATCH] Adding function to check if the current page is the main space or (optionally) related to the main page (eg. talk page). Compatible with both Vector and Monobook. Both namespace and page title may contain spaces (eg. Wiktionary talk:Main Page) --- resources/mediawiki.util/mediawiki.util.js | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/resources/mediawiki.util/mediawiki.util.js b/resources/mediawiki.util/mediawiki.util.js index ad9c2996fe..96a53afec5 100644 --- a/resources/mediawiki.util/mediawiki.util.js +++ b/resources/mediawiki.util/mediawiki.util.js @@ -272,6 +272,36 @@ // Populated by init() '$content' : null, + /** + * Checks wether the current page is the wiki's main page. + * + * @param alsoRelated Boolean value, if true this function also returns true if the current page is + * in a different namespace page of the main page rather than the main page itself (eg. talk page) + * @return Boolean + */ + 'isMainPage' : function( alsoRelated ) { + var isRelatedToMainpage = false; + + // Don't insert colon between namespace and title if the namespace is empty (eg. main namespace) + var namespace = mw.config.get( 'wgFormattedNamespaces' )[mw.config.get( 'wgNamespaceNumber' )]; + namespace = namespace ? namespace + ':' : ''; + + // We can't use (wgMainPageTitle == wgPageName) since the latter is escaped (underscores) and has other + // slight variations that make comparison harder. + var isTheMainPage = mw.config.get( 'wgMainPageTitle' ) === ( namespace + mw.config.get( 'wgTitle' ) ); + + // Also check for the title in related namespaces ? + if ( typeof alsoRelated !== 'undefined' && alsoRelated === true ) { + var contentTabLink = wgServer + $( '#ca-talk' ).prev( 'li' ).find( 'a:first' ).attr( 'href' ); + isRelatedToMainpage = contentTabLink === mw.util.wikiGetlink( mw.config.get( 'wgMainPageTitle' ) ); + + return isRelatedToMainpage || isTheMainPage; + } + + return isTheMainPage; + }, + + /** * Add a link to a portlet menu on the page, such as: * -- 2.20.1