User:DaxServer/scripts/TransclusionCount.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
//<nowiki>
$.when(
$.ready,
mw.loader.using( 'mediawiki.util' )
).then(function () {
if (mw.config.get('wgAction') !== 'view') {
return
}
let url = `https://templatetransclusioncheck.toolforge.org/index.php?lang=${mw.config.get('wgContentLanguage')}&name=`
if ([10, 828].includes(mw.config.get('wgNamespaceNumber'))) {
$.ajax(
mw.util.wikiScript('api'),
{
data: {
format: 'json',
formatversion: 2,
action: 'query',
prop: 'transcludedin',
titles: mw.config.get('wgPageName'),
tilimit: 500
},
beforeSend: (req, settings) => url = mw.config.get('wgNamespaceNumber') === 10 ? url : settings.url,
},
).then((response, _, xhr) => {
let total = 'transcludedin' in response.query.pages[0] ? response.query.pages[0].transcludedin.length : 0
if (mw.config.get('wgNamespaceNumber') === 828 && total < 500) {
total--
}
const more = 'continue' in response
let word = 'invocations', urlend = ''
if (mw.config.get('wgNamespaceNumber') === 10) {
word = 'translcusions'
urlend = encodeURIComponent(`Template:${mw.config.get('wgTitle')}`).replace(/%20/g, '+')
}
const content = `<a href="${url}${urlend}" target="_blank">${total}${more ? '+' : ''} ${word}</a>`
$('#siteSub').append(`<span style="float:right;">${content}</span>`).css('display', 'inherit')
})
}
if (mw.config.get('wgNamespaceNumber') === 14) {
mw.hook('wikipage.content').add((container) => {
const contentLinks = $(container).find('.mw-category a');
const articleRX = new RegExp(mw.config.get('wgArticlePath').replace('$1', '(.*)'));
let templatesSet = new Set(), templatesLinks = []
let templateLabels = []
for (let key in mw.config.get('wgNamespaceIds')) {
if ([10, 828].includes(mw.config.get('wgNamespaceIds')[key])) {
templateLabels.push(key)
}
}
for (const node of contentLinks) {
const match = articleRX.exec(node)
if (!match) {
continue
}
templateLabels.forEach(label => {
if (match[1].toLowerCase().startsWith(`${label}:`)) {
const template = decodeURIComponent(match[1])
if ( ! templatesLinks[template] ) {
templatesLinks[template] = [];
}
templatesLinks[template].push( node );
templatesSet.add(template)
}
})
}
let templatesArray = Array.from(templatesSet)
while (templatesArray.length > 0) {
$.get(
mw.util.wikiScript('api'),
{
format: 'json',
formatversion: 2,
action: 'query',
prop: 'transcludedin',
titles: templatesArray.pop(),
tilimit: 500,
}
).then((response) => {
for (const page of response.query.pages) {
let encodedFrom = response.query.normalized === undefined
? page.title
: response.query.normalized.find(each => each.to === page.title).from
const more = 'continue' in response
const total = 'transcludedin' in page ? page.transcludedin.length : 0
const content = ` (<a href="${url}${encodeURIComponent(page.title).replace(/%20/g, '+')}" target="_blank">${total}${more ? '+' : ''} ti</a>)`
for (const node of templatesLinks[encodedFrom]) {
$(node).after(content)
}
}
})
}
})
}
})
//</nowiki>