User:Dixtosa/addWiki.js

// // Description: adds a new button "add wikipedia", which unsurprisingly adds to every language that lacks it while the article with the same name exists there. // TODO: only works for Georgian uk ru and hy // TODO: does not check if there is a link already under External links

importScript("User:Dixtosa/XMLize.js"); importScript("User:Dixtosa/minorEdits.js");

var lang2wlangcode = { "Georgian" : "ka", "English":"en", "Ukrainian" : "uk", "Russian" : "ru", "Armenian" : "hy", "Mingrelian" : "xmf" }; function exists(wlangcode, article) { let api = new mw.ForeignApi("https://"+ wlangcode +".wikipedia.org/w/api.php"); return api.get({		'action': 'mobileview', // ugly hack		'page': article,		'format': 'json'	}); }

function addWiki {	new mw.Api .edit( mw.config.values.wgPageName, function ( revision ) {		return changePromise( revision.content )				.then( function ( p ) { return { text: p.wikitext, summary: p.summary}; } );	})	.then( function {		mw.notify ( 'Saved! ');	}); }

function changePromise(wikitext) {	let addedCount = 0; let deferreds = []; let wikixml = WikiXml.parseWikitext (wikitext); let datas = wikixml.find("[level=2] > data:first-child");

datas.each(function (i, elem) {		let lang = $(elem).parent.attr("class");		let code = lang2wlangcode[lang];		if (code && $(elem).text.search("wiki") == -1)		{			deferreds.push( exists(code, mw.config.values.wgTitle).then(function(result){				if (result && result.mobileview)				{					$(elem).prepend(`\n`);					addedCount++;				}			}));		}	}); return $.when(...deferreds).then(function{		let wikitext = WikiXml.toWikitext(wikixml);		wikitext = clean(wikitext);		return {			wikitext: wikitext,			summary: addedCount > 0 ? "Added link(s) to Wikipedia articles" : "Minor changes"		};	}); }

if (mw.config.values.wgNamespaceNumber === 0 && mw.config.values.wgAction == "view") {	mw.loader.using(['mediawiki.api.edit'], function {		$(  => { $("#ca-talk").after("  Add Wikipedia  "); });	}); } //