User:Ricordisamoa/DeletionHelper.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* <nowiki>
 * DeletionHelper.js by [[User:Ricordisamoa]]
 * uses jQuery & Ajax
 * helps sysops (and not) on Wikidata with DRs
*/
$(document).ready(function(){
	var wgPageName = mw.config.get('wgPageName');
	var wgAction = mw.config.get('wgAction');
	if(wgPageName=="Wikidata:Requests_for_deletions"&&wgAction=="view"){// the page is 'Wikidata:Request for deletions' in 'view' mode
		$("h2 .mw-headline").not(":has(a.new)")// items not already deleted
		.each(function(i,e){
			var entityId=$(e).text();
			$.getJSON(
				mw.util.wikiScript("api"),
				{
					format:"json",
					action:"wbgetentities",
					ids:$(e).text()
				},
				function(data){
					if(data.success&&data.entities[Object.keys(data.entities)[0]]){
						var entity=data.entities[Object.keys(data.entities)[0]];
						var length=0;
						if(typeof(entity.sitelinks)=="object") length=Object.keys(entity.sitelinks).length;
						if(length===0) $(e).children("a").first().css("color","teal");
						$("<span>")
						.html("&nbsp;&nbsp;("+(length>0?length+" sitelink"+(length>1?"s":""):"empty")+")")
						.css("font-size","50%")
						.appendTo(e);
					}
				}
			);
			$.getJSON(
				mw.util.wikiScript("api"),
				{
					action:"query",
					format:"json",
					prop:"revisions",
					titles:entityId,
					rvprop:"content|user|ids",
					rvlimit:"max"
				},
				function(data){
					var pages=data.query.pages;
					pages=pages[Object.keys(pages)[0]].revisions;
					var remL={};
					$.each(pages,function(y,z){
						var links=Object.keys(JSON.parse(z["*"]).links);
						if(y<pages.length-1&&typeof(JSON.parse(pages[y+1]["*"]).links)==="object"){
							var nextLinks=Object.keys(JSON.parse(pages[y+1]["*"]).links);
							var removedLinks=$(nextLinks).not(links).get();
							removedLinks=$.map(removedLinks,function(e){return e.replace(/wiki$/,"");});
							if(removedLinks.length>0){
								var rl={revid:z.revid,parentid:z.parentid,links:removedLinks};
								if(typeof(remL[z.user])!="undefined") remL[z.user].push(rl);
								else remL[z.user]=[rl];
							}
						}
					});
					var p=$("<p>").css("color","#630");// change color, according to Reza1615
					if(Object.keys(remL).length===0) p.append("No sitelinks were removed");
					else $.each(remL,function(k,v){
						p.append("Removed ");
						var abcd=false;
						$.each(v,function(o,j){
							p.append(
								$("<a>")
								.text(j.links.join(", "))
								.attr("href",
									mw.util.wikiScript()+"?"+
									$.param({
										title:entityId,
										oldid:j.parentid,
										diff:j.revid
									})
								)
							);
							if(o<v.length-1){
								abcd=true;
								p.append(", ");
							}
						});
						p.append(" link"+(abcd===true?"s":"")+" by ")
						.append(
							$("<a>")
							.text(k)
							.attr("href",mw.util.getUrl("User:"+k))
						);
					});
					$(e).parent().next("p").find("span.plainlinks").first()// (delete | history | links | log)
					.after(p);
				}
			);
		});
		$("li").not(":has(a.new)")// items not already deleted
		.each(function(i,e){
			if(/[Qq](\d*) \(delete \| history \| links \| log\)/.test($(e).text()) === true){
				var entityId=$(e).text().replace(/(.*) \(delete \| history \| links \| log\)/,"$1");
				$.getJSON(
					mw.util.wikiScript("api"),
					{
						format:"json",
						action:"wbgetentities",
						ids:entityId
					},
					function(data){
						if(data.success&&data.entities[Object.keys(data.entities)[0]]){
							var entity=data.entities[Object.keys(data.entities)[0]];
							var length=0;
							if(typeof(entity.sitelinks)=="object") length=Object.keys(entity.sitelinks).length;
							if(length===0) $(e).children("a").first().css("color","teal");
							$("<span>")
							.html("&nbsp;&nbsp;("+(length>0?length+" <i>sitelink"+(length>1?"s":""):"empty")+"</i>)")
							.css("font-size","70%")
							.css("vertical-align","middle")
							.appendTo(e);
						}
					}
				);
			}
		});
		var work = [];
		$("h2>span.mw-editsection").each(function(i,e){
			var index=parseInt($(e).children("a").first().attr("href").replace(/^.+[\?&]section=([0-9]+)$/,"$1"));
			$(e).parent().attr("id","section"+index);
			work[index] = false; 
			$.get(
				mw.util.wikiScript(),
				{title:wgPageName,action:"raw",section:index}
			)
			.done(function(d){
				if(/\{\{([Nn]ot )?[Dd](eleted|one)(\|admin=[^\{\}]+)?\}\}/.test(d)===false){
					$("<a>")
					.html("&nbsp;Yes&nbsp;")
					.attr("title","Mark as deleted")
					.attr("style",'background:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAWCAMAAAA2GAaYAAAAQlBMVEUpflI7tXYxl2MujVwqgFQ1omk2pWwoeU8vkF4sh1gwlGA5rnI7s3Uzm2U3qG4oe1A0n2c4q3AtilovfVUrg1Y6sXPiEyGEAAAAKElEQVR4XgXAhQ2AMAAAsM4Ft/9fJYLVZ3HZVcVpE01dchteGYfm+QEULwDomj1/qwAAAABJRU5ErkJggg==") repeat-x scroll 50% 100% #3365BA !important; color: white; float: right; font-size: 0.7em; padding: 1px; position: relative; bottom: 12px; border-radius: 0px 0px 0px 6px;border-bottom:1px solid black;border-left:1px solid black')
					.click(function(event){
						location.hash = "#" + $(e).siblings(".mw-headline").first().attr("id");
						event.preventDefault();
						send(wgPageName,index,e,d,"","d");
					})
					.insertAfter($(e).parent())
					.tipsy({
            					gravity: 'ne',
            					fade: true
       					 });
					$("<a>")
					.html("&nbsp;No&nbsp;")
					.attr("title","Mark as not deleted")
					.attr("style",'Background:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAWCAMAAAA2GAaYAAAAQlBMVEWUAACeAACIAADIAACQAACKAACaAADDAAC8AACxAACFAADFAADAAACuAACXAACqAACmAAC5AACiAACNAADKAAC1AAB6LXXpAAAAKElEQVR4XgXAhQ2AMAAAsM4Ft/9fJV7Joll1l8+wOZxuQbUjexTR/AEV+QDoUwTrkQAAAABJRU5ErkJggg==") repeat-x scroll 50% 100% #CB0000 !important; color: white; float: right; font-size: 0.7em; padding: 1px; position: relative; bottom: 12px;border-bottom:1px solid black')
					.click(function(event){
						if(/^.*#.*$/.test(document.location) === false){ document.location = document.location + "#" + $(e).siblings(".mw-headline").first().attr("id");}
						location.hash = "#" + $(e).siblings(".mw-headline").first().attr("id");
						event.preventDefault();
						send(wgPageName,index,e,d,"","n");
					})
					.insertAfter($(e).parent())
					.tipsy({
            					gravity: 'ne',
            					fade: true
       					 });
					$("<a>")
					.html("&nbsp;#&nbsp;")
					.attr("title","Write a comment")
					.attr("style",'background:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAWCAMAAAA2GAaYAAAAQlBMVEUuXKkmS4ozZbkkSIQtW6YiRHwrV58hQ3owYLAxY7UnTY0jRoEqVZsyZLcvXq0oT5EqU5gxYbMsWaMlSocjRX8pUZU8hDFrAAAAKElEQVR4XgXAhQ2AMAAAsM4Ft/9fJaLNcOl2ZLdqdfocpuCRLF5F+wETiQDocXjMfwAAAABJRU5ErkJggg==") repeat-x scroll 50% 100% #3365BA !important; color: white; float: right; font-size: 0.7em; padding: 1px; position: relative; bottom: 12px; border-radius: 0px 0px 6px 0px;border-bottom:1px solid black;border-right:1px solid black')
					.click(function(event){
						if(/^.*#.*$/.test(document.location) === false){ document.location = document.location + "#" + $(e).siblings(".mw-headline").first().attr("id");}
						location.hash = "#" + $(e).siblings(".mw-headline").first().attr("id");
						event.preventDefault();
						send(wgPageName,index,e,d,prompt("Type the text to send to this request:\n\n* If you want to set a template\n   TEMPLATE|option1|option2#My Text\n\n* If you want to incremente the text, add a colon at the beginning of the text\n   :TEMPLATE#My Text          :My Text\n\n* If you leave the template option blank, it will set the default template (deleted or not deleted)\n   #My Text                            :#My Text"),"c");
					})
					.insertAfter($(e).parent())
					.tipsy({
            					gravity: 'ne',
            					fade: true
       					 });
				}else{
					$("<a>")
					.html("&nbsp;#&nbsp;")
					.attr("title","Write a comment")
					.attr("style",'background:url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAWCAMAAAA2GAaYAAAAQlBMVEUuXKkmS4ozZbkkSIQtW6YiRHwrV58hQ3owYLAxY7UnTY0jRoEqVZsyZLcvXq0oT5EqU5gxYbMsWaMlSocjRX8pUZU8hDFrAAAAKElEQVR4XgXAhQ2AMAAAsM4Ft/9fJaLNcOl2ZLdqdfocpuCRLF5F+wETiQDocXjMfwAAAABJRU5ErkJggg==") repeat-x scroll 50% 100% #3365BA !important; color: white; float: right; font-size: 0.7em; padding: 1px; position: relative; bottom: 12px; border-radius: 0px 0px 6px 6px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black')
					.click(function(event){
						if(/^.*#.*$/.test(document.location) === false){ document.location = document.location + "#" + $(e).siblings(".mw-headline").first().attr("id");}
						location.hash = "#" + $(e).siblings(".mw-headline").first().attr("id");
						event.preventDefault();
						send(wgPageName,index,e,d,prompt("Type the text to send to this request:\n\n* If you want to set a template\n   TEMPLATE|option1|option2#My Text\n\n* If you want to incremente the text, add a colon at the beginning of the text\n   :TEMPLATE#My Text          :My Text\n\n* If you leave the template option blank, it will set the default template (deleted or not deleted)\n   #My Text                            :#My Text"),"c");
					})
					.insertAfter($(e).parent())
					.tipsy({
            					gravity: 'ne',
            					fade: true
       					 });
				}
			});
		});
		function send(wgPageName,index,e,d,c,m){
			//CREATE SPINNER
			var spinner = '<pre id="rfdSpinner' + index + '"></pre>'; var nextindex = index+1;
			if(document.getElementById("section"+nextindex) == null){
				$("#mw-content-text").append(spinner);
			}else{
				$("#section"+nextindex).before(spinner);
			}
			$.createSpinner( { size: 'small', id: 'rfdSpinner'+index } ).appendTo( "#rfdSpinner"+index );
			//CREATE SPINNER
			work[index] = true;
			if( /==\s*\[\[(.*)\]\]\s*==/.test(d) === true ){
				var title = d.match(/==.*==/)[0].replace(/^==\s*\[\[(.*)\]\]\s*==/,"$1");
				if(m == "d"){var template = "Deleted";var resume = "deleted";}
				if(m == "n"){var template = "Not deleted";var resume = "not deleted";}
				$.ajax({
					url: mw.util.wikiScript( 'api' ),
					async: false,
					type: 'GET',
					dataType: 'json',
					data: {
						action: 'query',
						format: 'json',
						list: 'logevents',
						leprop: 'type|user',
						letitle: title,
						lelimit: '1'
					},
					success: function ( data ) {
						if(data.query.logevents.hasOwnProperty(0)){
							if(data.query.logevents[0].action == "delete"){
								if(m == "d"){
									if(data.query.logevents[0].user != mw.config.get( 'wgUserName' )){
										template = "deleted|admin=" + data.query.logevents[0].user;
										resume = "deleted by " + data.query.logevents[0].user;
									}
								}else if(m == "n"){
									if(!confirm("This page has been deleted! Are you sure you want to mark this request as not deleted?")){
										m="c";c="";
									}
								}else{
									if(data.query.logevents[0].user == mw.config.get( 'wgUserName' )){
										template = "deleted";
										resume = "deleted";
									}else{
										template = "deleted|admin=" + data.query.logevents[0].user;
										resume = "deleted by " + data.query.logevents[0].user;
									}	

								}
							}else{
								if(m == "d"){
									if(!confirm("This page hasn't been deleted! Are you sure you want to mark this request as deleted?")){
										m="c";c="";
									}
								}else{
									template = "not deleted";
									resume = "not deleted";	
								}
							}
						}
					}
				});
			}else{
				if(m == "d"){var template = "Done";var resume = "done";}
				if(m == "n"){var template = "Not done";var resume = "not done";}
				if(m == "c"){var template = "Done";var resume = "done";}
			}
			if(c == null){c = "";}
			if(m == "c" && c == ""){
				$( "#mw-spinner-rfdSpinner"+index ).append('<img src="//upload.wikimedia.org/wikipedia/commons/thumb/a/a2/X_mark.svg/20px-X_mark.svg.png" style="">');
				$("#rfdSpinner"+index).append('<span style="color:red;float:right"><strong>Aborted</strong></span>');
				work[index] = false;
				setTimeout(function(){$("#rfdSpinner"+index).remove()},3000);
				return;
			}
			if( /:*(.*)#(.*)/.test(c) === true ){
				if(c.replace(/^:*(.*)#(.*)$/,"$1") != ""){
					var template = c.replace(/^:*(.*)#(.*)$/,"$1");
					var resume = template;
				}
			}else if(c != ""){
				template="User:Jitrixis/void";
				resume = "commented";
			}
			point = c.replace(/^(:*).*$/,"$1")
			c = c.replace(/^:*(.*#)?(.*)$/,"$2");
			var n = d.match(/^:+/gm);
			if(point == "" && n != null){
				point = n[n.length-1];
				if(point.length >= 10){
					var size = point.length - 1;
					point = "{{od|" + size + "}}";
				}
			}
			point = ":" + point;
			template = "{{" + template + "}}";
			if(template == "{{User:Jitrixis/void}}"){template = "";}
			//CREATE SPINNER
			var spinner = "&nbsp;&nbsp;" + point + " " + template + "  " + c + " ~~~~"
			$("#rfdSpinner"+index).append(spinner);
			//CREATE SPINNER
			$.post(
				mw.util.wikiScript("api"),
				{
					action:"edit",
					appendtext: "\n" + point + " " + template + " " + c + " ~~~~",
					title:wgPageName,
					section:index,
					summary:"/* "+$(e).siblings(".mw-headline").first().attr("id")+" */ tagging as '" + resume + "' ([[MediaWiki:Gadget-DeletionHelper|DH]])",
					token:mw.user.tokens.get("editToken")
				}
			)
			.done(function(data){
				if(/error/.test(data) === true){
					$( "#mw-spinner-rfdSpinner"+index ).append('<img src="//upload.wikimedia.org/wikipedia/commons/thumb/a/a2/X_mark.svg/20px-X_mark.svg.png" style="">');
					$("#rfdSpinner"+index).append('<span style="color:red;float:right"><strong>Edit conflict detected</strong></span>');
				}else{
					$("#rfdSpinner"+index).append('<span style="color:green;float:right"><strong>Correctly sent</strong></span>');
					$( "#mw-spinner-rfdSpinner"+index ).append('<img style="margin-left:1px" src="//upload.wikimedia.org/wikipedia/commons/thumb/f/fb/Yes_check.svg/20px-Yes_check.svg.png">');
				}
				work[index] = false;
				setTimeout(function(){rld();},2500);
			})
			.fail(function(){
				$( "#mw-spinner-rfdSpinner"+index ).append('<img src="//upload.wikimedia.org/wikipedia/commons/thumb/a/a2/X_mark.svg/20px-X_mark.svg.png" style="">');
				$("#rfdSpinner"+index).append('<span style="color:green;float:right"><strong>The ajax request failed.</strong></span>');
				work[index] = false;
				setTimeout(function(){rld();},2500);
			});
		}
		function rld(){
			var reload = true;
			for(var nb in work){
				if(work[nb] === true){
					reload = false;
				}
			}
			if(reload === true){
				document.location.reload(true);
			}
		}
	}
});
/* </nowiki> */