Jump to content

User:SD0001/draft-sort-burst.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 18:27, 24 June 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// NOTE: this doesn't work until the edit request at 
// User_talk:Enterprisey/draft-sorter.js#Interface-protected_edit_request_on_24_June_2019 
// is accepted.
/**
 * Sort drafts one after the other
 * 
 * Requires User:Enterprisey/draft-sort.js 
 * 
 * Activate by clicking on "Draft-sort (burst)" in the More menu.
 * Enterprisey's draft-sort opens up. Add WikiProject tags and save,
 * or skip if unsure (skip button is produced by this script).
 * Now you will be redirected to another currently submitted AfC draft 
 * that hasn't been sorted yet, and draft-sort automatically opens up.
 * The cycle continues.
 * 
 */

$.when(
	mw.loader.using(['mediawiki.util', 'mediawiki.api']),
	mw.loader.getScript('https://en.wikipedia.org/w/index.php?title=User:Enterprisey/draft-sort.js&action=raw&ctype=text/javascript'),
	$.ready
).then(function() {

	if (mw.config.get('wgNamespaceNumber') !== 118) return;

	$(mw.util.addPortletLink('p-tb', '#', 'Draft-sort (burst)', 'pt-draftsortburst', 'Sort drafts in burst mode')).click(function(e) {
		e.preventDefault();
		actions();
	});

	if (mw.util.getParamValue('draftsorttrigger')) {
		actions();
	}

	function actions() {

		setTimeout(function() {
			$('#pt-draftsort').click();
		}, 800);

		$(document).on('draft_sort:ready', function() {
			$('#draft-sorter-wrapper button.mw-ui-quiet').append(
				$('<button>')
					.addClass('mw-ui-button')
					.addClass('mw-ui-destructive')
					.addClass('mw-ui-quiet')
					.addClass('draft-sort-skip')
					.text('Skip')
			)
		});

		var nextDraft, cmcontinue;
		var api = new mw.Api();
		var query = {
			"action": "query",
			"list": "categorymembers",
			"cmtitle": "Category:Pending_AfC_submissions",
			"cmprop": "title",
			"cmnamespace": "118",
			"cmlimit": "50",
			"cmsort": "sortkey",
			"cmdir": "ascending",
			"cmstartsortkeyprefix": "P"
		};

		function checkTalkPages(r) {
			//console.log('in checkTalkPages');
			//console.log(r);
			if (r && r.query && r.query.categorymembers) {

				cmcontinue = r.continue.cmcontinue;
				var draftTalkPages = r.query.categorymembers.map(function(e) {
					return e.title.replace(/^Draft:/, 'Draft talk:');
				});
				return api.get({
					action: 'query',
					titles: draftTalkPages
				});
			}
		}

		function selectNextDraft(r) {
			//console.log('in selectNextDraft');
			//console.log(r);
			if (r && r.query && r.query.pages) {
				var found = false;
				$.each(r.query.pages, function(i,e) {
					if (parseInt(i) < 0) {
						var page = r.query.pages[i].title.replace(/^Draft talk:/, 'Draft:');
						if (page !== mw.config.get('wgPageName').replace(/_/g, ' ')) {
							nextDraft = page;
							found = true;
							return false;
						}
					}
				});
				//console.log('found:', found);
				if (!found) {
					//console.log('No suitable next draft found');
					query.cmcontinue = cmcontinue;
					postQuery();
					return;
				}

				$(document).on('draft_sort:saved', function() {
					window.location = '//en.wikipedia.org/wiki/' + nextDraft + '?draftsorttrigger=y';
				});

				$('.draft-sort-skip').click(function() {
					window.location = '//en.wikipedia.org/wiki/' + nextDraft + '?draftsorttrigger=y';
				});

			}
		}

		function postQuery() {
			//console.log(query);
			api.get(query)
				.then(checkTalkPages)
				.then(selectNextDraft)
				.fail(function(e) {
					console.log('API for getting next draft failed ' + e);
				});
		}

		postQuery();

	}

});