User:R'n'B/ndr.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.
/* ndr.js : Wikipedia app to process non-disambiguation redirect lists
 * Copyright (c) 2012 [[en:User:R'n'B]]
 *  Creative Commons Attribution-ShareAlike License applies
 * Requires MediaWiki 1.17, and jQuery 1.4.2 (included with MediaWiki)
 */
// Version 0.2; alpha release
/*global console, mw, jQuery, importScript */
(function ($) {
	// pseudo-global variables
	var app,
		startup;
	importScript("User:R'n'B/wrappi.js");
	mw.util.addCSS(
'ul {\
    line-height: 1.5em;\
    list-style-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAANCAMAAABW4lS6AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAZQTFRFAFKM////QIUK9QAAAAJ0Uk5T/wDltzBKAAAAGklEQVR42mJgBAEGokgGBjBGBxBxsBqAAAMACHwALd5r8ygAAAAASUVORK5CYII=");\
    list-style-type: square;\
    margin: 0.3em 0 0 1.5em;\
    padding: 0;\
}\
.pagehistory li {\
    border: 1px solid white;\
}\
li {\
    margin-bottom: 0.1em;\
}'
	);
	app = mw.RnB.ndr = {
        $activerow: null,
		setup: function () {
			this.rowindex = 1; // start at 1 because 0 is header row
			this.numrows = $('table tr').length;
			this.dummyform = $('<span id="dummyform"></span').html(
'<input id="delbtn" type="button" value="Delete">\
<input id="skipbtn" type="button" accesskey="i" value="Skip">'
				);
			this.cookiename = ("NDR" + 
				/Non-disambiguation_redirects\/([0-9]{3})/
				.exec(mw.config.get("wgPageName"))[1]
			);
            return app.getnextrow();
		},
        getnextrow: function () {
			var m, firstrow = mw.cookie.get(this.cookiename);
			console.log(firstrow, $('table tr').eq(this.rowindex).find('td').eq(0).text());
			if (firstrow !== undefined && 
					$('table tr').eq(this.rowindex).find('td').eq(0).text()
					< firstrow) {
				return app.skipbutton();
			}
            if (this.$activerow !== null) {
				$('#dummyform').detach();
				this.$activerow.css("font-weight", "normal");
            }
            this.$activerow = $('table tr').eq(this.rowindex);
			this.redirect = $('td', this.$activerow).eq(0).text();
			this.originaltarget = $('td', this.$activerow).eq(1).text();
			this.target = $('td', this.$activerow).eq(2).text();
			this.originaltarget = (this.originaltarget == "-") ? this.target : this.originaltarget;
			this.blcount = $('td', this.$activerow).eq(3).text().trim();
			this.ablcount = $('td', this.$activerow).eq(4).text().trim();
			m = this.redirect.match(/^(.*) \(disambiguation\)$/);
			if (!m || this.ablcount !== "0" || 
					$('td', this.$activerow).eq(0).children('a').hasClass("new")) {
				this.$activerow = null;
				return app.skipbutton();
			}
			$('td', this.$activerow).eq(0).append(this.dummyform);
			$('#delbtn').unbind('click', app.delbutton);
			$('#skipbtn').unbind('click', app.skipbutton);
			$('#delbtn').click(app.delbutton);
			$('#skipbtn').click(app.skipbutton);
			this.$activerow.css("font-weight", "bold");
			mw.cookie.set(this.cookiename, this.redirect);
        },
		// DON'T USE this IN THE FOLLOWING EVENT HANDLERS!
		delbutton: function () {
			var url = mw.config.get("wgServer") + mw.config.get("wgScript")
				+ "?title=" + mw.util.wikiUrlencode(app.redirect)
				+ "&action=delete&wpReason="
				+ mw.util.rawurlencode('[[WP:CSD#G14|G14]]: Unnecessary disambiguation page');
			console.log(url);
			window.open(url, "_blank");
			$('td', app.$activerow).eq(0).find('a').addClass('new').removeClass('external');
			return app.skipbutton();
		},
		skipbutton: function () {
			// skip this item and go to the next
			app.rowindex += 1;
			if (app.rowindex < app.numrows) {
				return app.getnextrow();
			} else {
				mw.cookie.set(this.cookiename, null);
			}
		}
	};
	startup = function () {
		if (mw.RnB) {
			mw.loader.using('mediawiki.cookie', function() {app.setup();});
		} else {
			setTimeout(startup, 100);
		}
	};
	$(startup);
} (jQuery));