MediaWiki:UpdateLang.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// Invokes { {subst:lle}}, used in [[MediaWiki:Commons-editnotices-template-subpage-lang]] for /lang subpages
$(async () => {
	const namespaceNumber = mw.config.get('wgNamespaceNumber')
	const title = mw.config.get('wgTitle');
	const pageName = mw.config.get('wgPageName');
	if (!title.endsWith('/lang') || namespaceNumber !== 10) {
		return;
	}

	function preSavedTransform(content, title) {
		return new mw.Api().post({
			action: 'parse',
			text: content,
			contentmodel: 'wikitext',
			prop: 'text',
			onlypst: '',
			title: title || 'API'
		}).then(data => data.parse.text['*']);
	}

//	function prefixedPages(page, namespace) {
//		return new mw.Api().get({
//			action: 'query',
//			list: 'prefixsearch',
//			pssearch: page,
//			psnamespace: namespace,
//			pslimit: 500 // TODO: Implement proper pagination
//		}).then(data => data.query.prefixsearch.map(x => x.title));
//	}
// 	const baseTitle = title.replace(/\/lang$/, '/');
// 	const allCodes = (await preSavedTransform(`{${''}{subst:#invoke:Languages|forEachLanguage|pattern=$lc,}}`))
// 		.trim()
// 		.replace(/,$/, '')
// 		.split(',');
// 	const codes = (await prefixedPages(baseTitle, namespaceNumber))
// 		.filter(x => x.split('/').length === 2)
// 		.map(x => x.split('/')[1])
// 		.filter(code => allCodes.includes(code))
// 		.sort();
// 	const names = (await preSavedTransform(codes.map(x => `{${''}{subst:#language:${x}}}`).join('\n')))
// 		.trim()
// 		.split('\n');
// 	const result = `<span style="font-size:small;line-height:1.4" class="plainlinks noprint"><!--
// ${codes
// 		.map((x, i) => [x, names[i]])
// 		.map(x => `-->[//commons.wikimedia.org/wiki/Template:${baseTitle}${x[0]} <bdi class="language lang-${x[0]}" lang="${x[0]}">${x[1]}</bdi>]&nbsp;<b>∙</b>&#32;<!--`)
// 		.join('\n')
// }
// -->{${''}{edit|Template:${title}}}<!--
// --></span><noinclude>

// [${''}[Category:Language link templates|${title}]]
// </noinclude>`;
	const result = await preSavedTransform(`{${''}{subst:lle}}`, pageName);

	$('#wpTextbox1').val(result);
	$('#wpSummary').val(`[[MediaWiki:UpdateLang.js|Semi-automated]] updated of /lang using {${''}{subst:lle}}`);
	$('#wpDiff').click();
});