User:Ricordisamoa/LiveDH.js

From Wikidata
Jump to navigation Jump to search

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>
 * LiveDH.js by [[User:Ricordisamoa]]
 * uses jQuery & Ajax
 * helps sysops on Wikidata with DRs
*/
$(document).ready(function(){
	if(mw.config.get('wgPageName') !== "User:Ricordisamoa/LiveDH"){
		mw.util.addPortletLink("p-navigation", mw.util.getUrl("User:Ricordisamoa/LiveDH"), "LiveDH", "LiveDH-go", "Go to LiveDH");
		return;
	}
	if(mw.config.get('wgAction') !== "view"){
		return;
	}
	if(mw.config.get('wgUserGroups').indexOf("sysop") === -1){
		alert("You're not an administrator!");
		return;
	}
	/*var deleteItem=function(itemId,reason){
		$.post(
			mw.util.wikiScript("api"),
			{
				action:"delete",
				format:"json",
				title:itemId,
				reason:reason,
				token:mw.user.tokens.get("csrfToken")
			},
			function(data){
				console.log(data);
			}
		);
	};*/
	var table = $("<table>").appendTo(
		$("#mw-content-text")
		.empty()
	);
	$("<button>")
	.text("\u21BB")
	.attr("title", "reload current RfDs")
	.click(function(){refresh();})
	.prependTo("#mw-content-text");
	var options = {
		rejected: $("<input>")
			.attr("type", "checkbox")
			.prependTo(
				$("<label>")
				.text("rejected")
				.prependTo("#mw-content-text")
			),
		notempty: $("<input>")
			.attr("type", "checkbox")
			.prependTo(
				$("<label>")
				.text("notempty")
				.prependTo("#mw-content-text")
			),
		empty: $("<input>")
			.attr("type", "checkbox")
			.prop("checked", true)
			.prependTo(
				$("<label>")
				.text("empty")
				.prependTo("#mw-content-text")
			)
	};
	var tbody = $("<tbody>").appendTo(table),
	columnHeaders = ["entity ID", "link to section", "sitelinks", "discussion"],
	thead = $("<thead>").append(
		$("<tr>")
		.append(
			$.map(columnHeaders, function(txt){
				return $("<th>").text(txt);
			})
		)
	).appendTo(table);
	var empty = $("<span>").text("No live DRs at the moment.").hide().insertAfter(table),
	refresh = function(){
		$.get(
			mw.util.wikiScript(),
			{title: "Wikidata:Requests for deletions"},
			function(data){
				console.groupCollapsed("LiveDH - fetching current DRs...");
				console.log(data);
				console.groupEnd();
				var Q = {},
				other = {};
				$(data).find("h2>.mw-headline").not(":has(a.new)")// items not already deleted
				.filter(function(){
					if(options.rejected.prop("checked") === false && $(this).parent().nextUntil("h2").find("img[src='//upload.wikimedia.org/wikipedia/commons/thumb/a/a2/X_mark.svg/15px-X_mark.svg.png']").length > 0){
						return false;
					}
					return true;
				})
				.each(function(i,e){
					var title = $(e).text(),
					nu = $(e).parent().nextUntil("h2"),
					link = $(nu[0]).find("span.plainlinks a").first().attr("href");
					if(typeof link === "undefined"){
						return;
					}
					var reason = decodeURIComponent(link.match(/&wpReason=(.+)$/)[1]).replace(/\+/g, " ");
					(/^Q[0-9]+$/.test(title) ? Q : other)[title] = {content: nu.not("p:has(span.plainlinks)").get(), sectionId: $(e).attr("id"), reason: reason};
				});
				console.log(Q);
				if(Q.length === 0){
					empty.show();
					table.hide();
					return;
				}
				else{
					table.show();
					empty.hide();
				}
				$.getJSON(
					mw.util.wikiScript("api"),
					{
						format: "json",
						action: "wbgetentities",
						ids: Object.keys(Q).join("|"),
						props: "sitelinks"
					},
					function(data){
						if(!data.success || Object.keys(data.entities).length === 0) return;
						var filtered = [];
						$.each(data.entities, function(itemId, entity){
							itemId = itemId.toUpperCase();
							var length = typeof(entity.sitelinks) === "object" ? Object.keys(entity.sitelinks).length : 0;
							if(
								(length === 0 && options.empty.prop("checked") === false) ||
								(length !== 0 && options.notempty.prop("checked") === false)
							) return;
							filtered.push(itemId);
							var values = Q[itemId],
							oldRow = $("tr").filter(function(){return $(this).attr("name") === itemId;}),
							newRow = $("<tr>")
							.attr("name", itemId)
							.append(
								$("<a>")
								.text(itemId)
								.attr("href", mw.util.getUrl(itemId))
								.wrap("<td>").parent()
							)
							.append(
								$("<a>")
								.text("#" + values.sectionId)
								.attr("href", mw.util.getUrl("Wikidata:Requests for deletions") + "#" + values.sectionId)
								.wrap("<td>").parent()
							)
							.append(
								$("<td>")
								.text(length)
							)
							.append(
								$("<div>")
								.addClass("mw-collapsed")
								.append(values.content)
								.makeCollapsible()
								.wrap("<td>").parent()
							);
							if(oldRow.get().length === 0){
								newRow.prependTo(tbody);
							}
							else if(oldRow.children("td:nth-child(3)").text() != length || oldRow.find("td:last-child div.mw-collapsible-content").html() != values.content){
								oldRow.replaceWith(newRow);
							}
						});
						tbody.children("tr").filter(function(){
							return filtered.indexOf($(this).attr("name")) === -1;
						}).remove();
					}
				);
			},
			"html"
		);
	};
	refresh();
	setInterval(refresh, 20000);
});
/* </nowiki> */