User:Dalba/OneClickArchiver.js

/* Derived from https://fa.wikipedia.org/wiki/mediawiki:Gadget-OneClickArchiver.js */ /*global jQuery, mediaWiki */ (function($, mw) {	'use strict';	var archivePage;	var OneClickArchiver = {		install: function{			OneClickArchiver.getArchiveConfig(OneClickArchiver.enable);		},		getArchiveConfig: function(callback){			OneClickArchiver.loadPage(mw.config.get('wgPageName'), function(pageText){ var archiveConfig = {}; var confTemplate = pageText.match(/\{\{User:Dexbot\/Archivebot\s*?\n(\|.+\n)+\}\}/i); if (confTemplate === null) { archiveConfig.counter = 1; archiveConfig.base = mw.config.get('wgPageName') + '/Archive'; } else { confTemplate = confTemplate[0]; if (confTemplate.search(/\| ?counter ?= ?(\d+)/) > -1) { archiveConfig.counter = parseInt(confTemplate.match(/\| ?counter ?= ?(\d+)/)[1]); }					if (confTemplate.search(/\| ?archive ?= ?(.+)%\(counter\)s/) > -1) { archiveConfig.base = confTemplate.match(/\| ?archive ?= ?(.+)%\(counter\)s/)[1].trim; }				}				if (archiveConfig.counter === undefined | archiveConfig.base === undefined ) { if (mw.config.get('wgPageName') === "Wikipedia:Administrators'_noticeboard") { var now = new Date; var month_en = now.toLocaleDateString('en', {month: 'long'}).toLowerCase; var month = mw.msg(month_en); var year = mw.language.convertNumber(now.getFullYear).replace('٬', ''); archivePage = mw.config.get('wgPageName') + '/' + month + ' ' + year; } else { console.log('Warning: OneClickArchiver cannot determine archive page!'); return; }				} else { archivePage = archiveConfig.base + ' ' + mw.language.convertNumber(archiveConfig.counter); }				callback; });		},		enable: function {			$('h2 .mw-editsection').each(function{ var href = $(this).find('a').first.attr('href'); var span = $(' ',{					class: 'mw-archivesection'				}).insertAfter(this); span.append($(' ', { class: 'mw-editsection-bracket', text: '[' }));               span.append($('', { href: '#', title: 'Move this section to archive', style: 'font-size: smaller;', text: 'Archive', }).click(function(e) { e.preventDefault; mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']).done(function {                       OneClickArchiver.processRequest(href);                    }); }));               span.append($(' ', { class: 'mw-editsection-bracket', text: ']' }));			});		},		processRequest: function(href) {			OneClickArchiver.pageExists( archivePage, function { var m = href.match(/section=(\d+)/); if (m && m.length == 2) { var sectionId = m[1]; OneClickArchiver.loadPageSection(							mw.config.get('wgPageName'),							sectionId,							function(sectionText) {								window.sectionText = sectionText;								if (mw.config.get('wgNamespaceNumber') !== 4 || sectionText.search(/==[^=]+==\n+\{\{(Archive top|Closed)/i) > -1 ) {									OneClickArchiver.doArchive(sectionId, sectionText);								} else {									mw.notify('This discussion has not been closed yet! Close the discussion before archiving.');								}							}						); }				},				function { mw.notify('Archive page does not exist: ' + archivePage); }			);		},		doArchive: function(sectionId, sectionText){			if (confirm('Are you sure you want to archive this section in ' + archivePage.replace('_', ' ') + '?') === false ) {				return;			}			mw.notify('Archiving section in ' + archivePage + ' ...');			OneClickArchiver.savePageSection( archivePage, 'new', sectionText, null, function { mw.notify('Removing section from ' + mw.config.get('wgPageName') + ' ...'); OneClickArchiver.savePageSection(						mw.config.get('wgPageName'),						sectionId,						,						'Archiving to ' + archivePage + ,						function{							mw.notify('Archive complete! Reloading ...');							setTimeout(function{ location.href = mw.util.getUrl(mw.config.get('wgPageName')); }, 1000);						}					);				}			);		},		pageExists: function(title, callback, fallback) {			(new mw.Api).get({ action: 'query', prop: 'info', titles: title, format: 'json' }).done(function(data) { if (Object.keys(data.query.pages)[0] == "-1") { fallback; } else { callback; }			});		},		loadPage: function(title, callback) {			(new mw.Api).get({ action: 'query', prop: 'revisions', titles: title, rvprop: 'content', format: 'json' }).done(function(data) { callback($.map(data.query.pages, function(value) { return value; })[0].revisions[0]['*']); }).fail(function(data) { mw.notify(data); });		},		loadPageSection: function(title, section, callback) {			(new mw.Api).get({ action: 'query', prop: 'revisions', titles: title, rvsection: section, rvprop: 'content', format: 'json' }).done(function(data) { callback($.map(data.query.pages, function(value) { return value; })[0].revisions[0]['*']); }).fail(function(data) { mw.notify(data); });		},		savePageSection: function(title, sectionId, text, summary, callback) {			(new mw.Api).post({ action: 'edit', title: title, section: sectionId, text: text, summary: summary, minor: '1', token: mw.user.tokens.get('csrfToken') }).done(function(data) { if (data.error && data.error.info) { if (data.error.info == 'missingtitle') { mw.notify('Page ' + title + ' does not exist!', {							type: 'error'						}); } else { mw.notify(data.error.info, {							type: 'error'						}); }				} else { callback; }			}).fail(function(data) { mw.notify(data); });		}	}

if (mw.config.get('wgAction') === 'view' & $.inArray(mw.config.get('wgNamespaceNumber'), [3, 4]) > -1 ) { if (mw.config.get('wgPageName') === "Wikipedia:Administrators'_noticeboard" && $.inArray('sysop', mw.config.get('wgUserGroups')) < 0) { return; // only sysops can archive WP:ANB }		if (mw.config.get('wgPageName') === "Wikipedia:Requests_for_page_protection") { return; // this page and its archive have incompatile structures }		mw.loader.using(['mediawiki.api', 'mediawiki.language', 'mediawiki.jqueryMsg']).then(function{			return new mw.Api.loadMessagesIfMissing(['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']);			}).then(OneClickArchiver.install); } })(jQuery, mediaWiki);