r82310 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82309‎ | r82310 | r82311 >
Date:23:23, 16 February 2011
Author:krinkle
Status:ok (Comments)
Tags:
Comment:
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)
Modified paths:
  • /trunk/phase3/resources/mediawiki.util/mediawiki.util.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.js
@@ -273,6 +273,36 @@
274274 '$content' : null,
275275
276276 /**
 277+ * Checks wether the current page is the wiki's main page.
 278+ *
 279+ * @param alsoRelated Boolean value, if true this function also returns true if the current page is
 280+ * in a different namespace page of the main page rather than the main page itself (eg. talk page)
 281+ * @return Boolean
 282+ */
 283+ 'isMainPage' : function( alsoRelated ) {
 284+ var isRelatedToMainpage = false;
 285+
 286+ // Don't insert colon between namespace and title if the namespace is empty (eg. main namespace)
 287+ var namespace = mw.config.get( 'wgFormattedNamespaces' )[mw.config.get( 'wgNamespaceNumber' )];
 288+ namespace = namespace ? namespace + ':' : '';
 289+
 290+ // We can't use (wgMainPageTitle == wgPageName) since the latter is escaped (underscores) and has other
 291+ // slight variations that make comparison harder.
 292+ var isTheMainPage = mw.config.get( 'wgMainPageTitle' ) === ( namespace + mw.config.get( 'wgTitle' ) );
 293+
 294+ // Also check for the title in related namespaces ?
 295+ if ( typeof alsoRelated !== 'undefined' && alsoRelated === true ) {
 296+ var contentTabLink = wgServer + $( '#ca-talk' ).prev( 'li' ).find( 'a:first' ).attr( 'href' );
 297+ isRelatedToMainpage = contentTabLink === mw.util.wikiGetlink( mw.config.get( 'wgMainPageTitle' ) );
 298+
 299+ return isRelatedToMainpage || isTheMainPage;
 300+ }
 301+
 302+ return isTheMainPage;
 303+ },
 304+
 305+
 306+ /**
277307 * Add a link to a portlet menu on the page, such as:
278308 *
279309 * p-cactions (Content actions), p-personal (Personal tools),

Follow-up revisions

RevisionCommit summaryAuthorDate
r82311Follow-up r82310. using mw.config insteadkrinkle23:27, 16 February 2011
r83936Follow-up r82310 CR: Adding comment about document ready (since it uses jQuer...krinkle18:11, 14 March 2011

Comments

#Comment by Catrope (talk | contribs)   18:17, 17 February 2011

Why does this need to go into 1.17 and deployment?

#Comment by Krinkle (talk | contribs)   20:05, 17 February 2011

Well, I've come accross the need for this functionality on most if not all wikis. Some implementations better than others. Having it in core I think is appropiate, and since we're doing the Tour it would be helpful to get this out there right away on the wikis since we're going through them anyway.

The tag for 1.17 is indeed not needed as it's not a bugfix or anything.

#Comment by Krinkle (talk | contribs)   20:06, 17 February 2011

"most if not all wikis" referring to private/third party wikis as well as WMF wikis.

#Comment by He7d3r (talk | contribs)   14:19, 14 March 2011

What about

- var contentTabLink = wgServer
+ var contentTabLink = mw.config.get( 'wgServer' )

?

Besides, since it uses $( '#ca-talk' ), should the function be used only when document is ready? If so, wouldn't be good idea to add a comment about this?

Is it necessary to check if typeof alsoRelated !== 'undefined' before checking if alsoRelated === true?

#Comment by Krinkle (talk | contribs)   18:12, 14 March 2011

Yes to all;

  • mw.config should be used here since the globals are deprecated. I've fixed this already in the follow-up r82311 as shown above under "Follow-up revisions".
  • the document needs to be ready at this point. Thanks, I've added a comment in r83936 and documented it in this edit
  • If the variable is undefined comparing it to anything (such as true), JavaScript will throw a ReferenceError: Can't find variable: alsoRelated, so the undefined-check is required.

Status & tagging log