Jump to content

User:Alexis Jazz/ExportAllTheThings.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// ExportAllTheThings
// made for personal use, please complain to your God if this doesn't work for you
// only shows something if you visit Special:PrefixIndex
// This script is public domain, irrevocably released as WTFPL Version 2[www.wtfpl.net/about/] by its author, Alexis Jazz
var AJsIT = {};
AJsIT.addEv = function() {
	var AJsITButEv = setInterval(function(){
		$('#AJsImportTool_button')[0].addEventListener('click',AJsIT.import);
		clearInterval(AJsITButEv);
	},200);
};
AJsIT.doImport = function(url,url2) {
	AJsIT.pagename = decodeURIComponent(AJsIT.page.replace(/.*\/wiki\/(.*)/,'$1'));
	AJsIT.ForeignApi = new mw.ForeignApi('https://'+new mw.Uri(url).host+'/w/api.php');
	AJsIT.params = {action:'query',titles:AJsIT.pagename,format:'json',formatversion:2,prop:'revisions',rvprop:'content'};
	AJsIT.ForeignApi.get(AJsIT.params).then( function ( data ) {
		AJsIT.editPage(data.query.pages[Object.keys(data.query.pages)[0]].revisions[0].content);

	});
};
AJsIT.editPage = function(res){
	AJsIT.pagename = decodeURIComponent(AJsIT.page.replace(/.*\/wiki\/(.*)/,'$1'));
	AJsIT.params = {
		action:'edit',
		assertuser:mw.config.get('wgUserName'),
		text:res,
		title:AJsIT.pagename,
		summary:'Imported page from '+AJsIT.page+' [[[w:en:User:Alexis Jazz/AJsImportTool.js|AJsImportTool]]]'
	};
	if ( ! $('#AJsIT_overwrite')[0].checked ) {
		AJsIT.params.createonly = 1;
	}
	mw.loader.using(['mediawiki.api']).then(function(){
		if ( $('#AJsImportTool_domain')[0].value != '' ) {
			AJsIT.exportDomain = $('#AJsImportTool_domain')[0].value;
			mw.storage.set('AJsImportTool',$('#AJsImportTool_domain')[0].value);
		} else {
			AJsIT.exportDomain = new mw.Uri(AJsIT.page).host;
		}
		AJsIT.exportApi = 'https://'+AJsIT.exportDomain+'/w/api.php';
		AJsIT.ForeignApi = new mw.ForeignApi(AJsIT.exportApi);
		AJsIT.ForeignApi.postWithEditToken(AJsIT.params).then( function ( data ) {
			AJsIT.editDone('AJsITsuccess');			
		},function(){
			AJsIT.editDone('AJsITfail');
		});
	});
};
AJsIT.editDone = function(myClass){
	if ( AJsIT.page != '' ) {
		AJsIT.statusline = document.createElement('li');
		AJsIT.statusline.classList.add(myClass);
		if ( myClass == 'AJsITsuccess' ) {
			AJsIT.link = document.createElement('a');
			AJsIT.link.innerText = AJsIT.params.title;
			AJsIT.link.href = 'https://'+AJsIT.exportDomain+'/wiki/'+encodeURIComponent(AJsIT.params.title);
			AJsIT.statusline.append(AJsIT.link);
		} else {
			AJsIT.statusline.innerText = AJsIT.params.title;
		}
		$('#AJsImportTool_status ol').append(AJsIT.statusline);
		$('.oo-ui-messageDialog-container')[0].scrollTo({top:9000});
	}
	if ( AJsIT.pages.length > 0 ) {
		AJsIT.page = AJsIT.pages.pop();
		AJsIT.doImport(AJsIT.page);
	}
};
AJsIT.import = function(skip) {
	if ( skip != 1 ) {
		AJsIT.pages = $('#AJsImportTool_pages')[0].value.split('\n');
	}
	AJsIT.page = AJsIT.pages.pop();
	if ( AJsIT.page.match(/\/wiki\//)){
		AJsIT.doImport(AJsIT.page);
	} else if ( AJsIT.pages.length > 0 ) {
		console.log('skipped '+AJsIT.page);
		AJsIT.editDone('AJsITfail');
	}
};
if ( (mw.config.get('wgCanonicalSpecialPageName') == 'Prefixindex' && mw.config.get('wgNamespaceNumber') == -1) ) {
mw.loader.using(['oojs-ui','mediawiki.api','mediawiki.ForeignApi']).then(function(){
	mw.util.addCSS('.AJsITsuccess{color:#0a0} .AJsITfail{color:#a00} #AJsIT_go{text-align:center}');
	if ( mw.config.get('wgPageName') != 'Special:Import') {
		AJsIT.urlsFromPage = '';
		for (int=0;int<500;int++){
		if (!$('.mw-prefixindex-list li a')[int]){break;}
			AJsIT.urlsFromPage = AJsIT.urlsFromPage+'\n'+$('.mw-prefixindex-list li a')[int].href;
		}
	}
	AJsIT.domain = ( mw.storage.get('AJsImportTool') || '');
	OO.ui.alert(new OO.ui.HtmlSnippet('Export to:<br><input type="text" id="AJsImportTool_domain" placeholder="en.wikipedia.org" value="'+AJsIT.domain+'"><br><br>Enter URLs, one per line<br><textarea rows="10" id="AJsImportTool_pages" placeholder="https://en.wiktionary.org/wiki/Faroffistan&#10;https://en.wiktionary.org/wiki/licorice_wheel">'+AJsIT.urlsFromPage+'</textarea><input type="checkbox" id="AJsIT_overwrite" checked="1"> Overwrite existing pages<br><br><div id="AJsIT_go"><button id="AJsImportTool_button">Import/Export</button></div><div id="AJsImportTool_status"><ol></ol></div>'),{size:'large'}).done( function() {
	});
	AJsIT.addEv();
});
}