User:Mr. Stradivarius/gadgets/TransclusionCount.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Mr. Stradivarius/gadgets/TransclusionCount. |
/* TransclusionCount
*
* This script adds a link to Jarry1250's Template Transclusion Count tool to
* the Tools menu. The link is for the current page, or the referenced page if
* you are viewing a subpage of Special:WhatLinksHere.
*
* To install, add the following to your [[Special:MyPage/common.js]]:
importScript("User:Mr. Stradivarius/gadgets/TransclusionCount.js") // Linkback: [[User:Mr. Stradivarius/gadgets/TransclusionCount.js]]
*/
$( document ).ready(function() {
function getReferencedTitle( title ) {
// Gets the referenced title for title objects of pages that
// reference a specific page. For example, for the title object for
// Special:WhatLinksHere/User:Example it would give the title object
// for User:Example.
var pageName = title.getMain();
var baseSubpageName = pageName.replace( /.*?\//, "" );
if ( pageName === baseSubpageName ) {
return title;
} else {
return new mw.Title( baseSubpageName );
}
}
// Find the target title
// We detect whether we are on a subpage of Special:WhatLinksHere, and if
// so, we use the referenced page. For other special pages, we exit; these
// pages cannot be transcluded, so it would not make much sense to link to
// the transclusion count tool from them.
// TODO: Handle Special:WhatLinksHere pages using the "target" URL query
// parameter.
// TODO: Handle other special pages that can reference a specific page, like
// Special:PrefixIndex and Special:RecentChangesLinked.
var target = new mw.Title(
mw.config.get( 'wgTitle' ),
mw.config.get( 'wgNamespaceNumber' )
);
if ( target.getNamespaceId() === -1 ) {
// We are in the Special namespace.
if ( mw.config.get( "wgCanonicalSpecialPageName" ) === "Whatlinkshere" ) {
var referencedTitle = getReferencedTitle( target );
if ( referencedTitle.toString() !== target.toString() ) {
// We are on a subpage of Special:WhatLinksHere, so use the
// referenced page
target = referencedTitle;
} else {
return;
}
} else {
return;
}
}
// Assemble the template count URL
var url = new mw.Uri( "https://tools.wmflabs.org/templatecount/index.php" );
url.extend( {
lang: mw.config.get( "wgContentLanguage" ),
name: target.getMain(),
namespace: target.getNamespaceId()
} );
mw.util.addPortletLink( "p-tb", url.toString(), "Transclusion count" );
});