User:Thundercraft5/global.js/LuaModuleImport.js

(async function { // jshint ignore:line	if (!mw.libs.QDmodal) importArticles({ type: "script", articles: [ "u:dev:MediaWiki:QDmodal.js", ],	});	await Promise.all([ new Promise(resolve => mw.hook('dev.qdmodal').add(resolve)), mw.loader.using(['mediawiki.ForeignApi', 'mediawiki.widgets.TitlesMultiselectWidget']), ]);

var wpApi = new mw.ForeignApi('https://en.wikipedia.org/w/api.php', { anonymous: true }); var devApi = new mw.ForeignApi('https://dev.fandom.com/api.php', { anonymous: true }); var api = new mw.Api; var modal = new mw.libs.QDmodal('LuaImporter-Modal'); var titleInput = new mw.widgets.TitleInputWidget({		api: wpApi,		id: "LuaImporter-input",	});

mw.util.addCSS("#LuaImporter-Modal { height: 60% }");

async function importPages(title, includeSubpages) { // jshint ignore:line const missingCheck = ({ query: { pages }}) => !('missing' in Object.values(pages)[0]);

const existsInDev = await devApi.get({			action: "query",			titles: title,		}).then(missingCheck); const existsInWP = await wpApi.get({			action: "query",			titles: title,		}).then(missingCheck);

if (!existsInWP && !existsInDev) return mw.notify(title + ' was not found in Wikipedia or the Dev Wiki');

async function create(existsInDev, title, content) { return await api.create(title, { 				summary: existsInDev					? "Importing " + title + " from dev wiki" 					: "Importing " + title + " from wikipedia",			}, content); }

function createDevModule(title) { return "return require(\"Dev:" + title.replace(/^.*?:/, '') + "\")"; }

const content = !existsInDev ? await wpApi.get({				action: "query",				prop: "revisions",				titles: title,				rvprop: "content",				rvslots: "*",			}).then(({ query: { pages }}) => Object.values(pages)[0].revisions[0].slots.main['*']) : createDevModule(title);

if (includeSubpages) { const rootApi = existsInDev ? devApi : wpApi; let subpages = await rootApi.get({				action: "query",				list: "prefixsearch",				pssearch: title,			}).then(({ query: { prefixsearch }}) => prefixsearch);

subpages = subpages .filter(page => !page.title.includes('/sandbox') 					&& !page.title.includes('/doc') 					&& !page.title.includes('/testcases') 					&& !page.title.endsWith('.css')) .map(({ title }) => title).concat(title);

const entries = existsInDev ? subpages.map(title => [title, createDevModule(title)]) : await rootApi.get({					action: "query",					prop: "revisions",					titles: subpages.join('|'),					rvslots: "*",					rvprop: "content",				}).then(({ query: { pages }}) => {					return Object.values(pages).map(({ title, revisions: [{ slots: { main: { "*": content }}}]}) => [title, content]);				});

return Promise.all(entries.map(([title, content]) => create(existsInDev, title, content).then(result => {				mw.notify(`Successfully created "${title}"!`);				return result;			}, e => {				mw.notify(`Failed to created "${title}": ${e}`);				return e;			}))); } else { return await create(existsInDev, title, content); }	}

var $checkbox = $(' ', {		checked: true,		type: "checkbox",		id: "LuaImporter-includeSubpages",	});

function submit { importPages(titleInput.getValue, $checkbox[0].checked).then( => titleInput.setDisabled(false), titleInput.setValue("Module:")); titleInput.setDisabled(true); }

function cancel { modal.hide; }

function showModal { modal.show({			title: "Lua Importer",			content: $(' ', { html: [ $(' ', {						title: 	'Include subpages of this module (excluding testcases, sandboxes, and documentation)',						html: [							$checkbox,							'Include subpages of this module (excluding testcases, sandboxes, and documentation)',						],					}), titleInput.$element, ],			}),			buttons: [{				handler: submit,				text: "Initiate",			}, {				handler: cancel,				text: "Cancel",			}]		}); }

titleInput.on('enter', submit); titleInput.setValue('Module:'); $('.tools').append($('', { html: $('', {			text: "Lua Importer",			click: showModal,			title: "Import Lua modules from dev.fandom.com or Wikipedia",			css: {				cursor: "pointer",			},		}), })); });