User:Ricordisamoa/DataLiveRC.js

From Wikidata
Jump to navigation Jump to search

Note: After saving, 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: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* <nowiki>
 *
 * DataLiveRC by [[User:Ricordisamoa]]
 * inspired by the Original LiveRC
 * a live-AntiVandal tool, optimized for Wikidata (beta)
*/
$(document).ready(function(){
	$(".mw-tag-marker-new_editor_removing_sitelink").parent().parent().css("background-color", "orange");
	if(mw.config.get('wgPageName') !== "User:Ricordisamoa/DataLiveRC"){
		mw.util.addPortletLink("p-navigation", mw.util.getUrl("User:Ricordisamoa/DataLiveRC"), "DataLiveRC", "DataLiveRC-portlet", "Start DataLiveRC (experimental)");
		return;
	}
	document.title = "DataLiveRC - Wikidata";
	var getLabels = function(pages, callback){
		$.getJSON(
			mw.util.wikiScript("api"),
			$.extend(
				{
					action: "wbgetentities",
					languages: mw.config.get('wgUserLanguage'),
					format: "json",
					props: "labels"
				},
				pages.ids ? {ids: pages.ids.join("|")} : {sites: pages.sites, titles: pages.titles}
			)
		)
		.done(function(data){
			var labels = {};
			$.each(data.entities, function(k, v){
				if(k==-1) labels[k.toUpperCase()] = k;
				else labels[k.toUpperCase()] = (v && v.labels ? v.labels[mw.config.get('wgUserLanguage')].value + " " : "") + "<small>(" + k.toUpperCase() + ")</small>";
			});
			callback(labels, Object.keys(data.entities));
		});
	};
	var getRemovedLinks = function(entityId, revid, callback){
		$.getJSON(
			mw.util.wikiScript("api"),
			{
				action: "query",
				format: "json",
				prop: "revisions",
				titles: entityId,
				rvprop: "content",
				rvstartid: revid,
				rvlimit: 2
			},
			function(data){
				var pages = data.query.pages;
				pages = pages[Object.keys(pages)[0]].revisions;
				var currentLinks = Object.keys(JSON.parse(pages[0]["*"]).links);
				if(pages.length > 1 && typeof(JSON.parse(pages[1]["*"]).links) === "object"){
					var previousLinks = Object.keys(JSON.parse(pages[1]["*"]).links);
					var removedLinks = $(previousLinks).not(currentLinks).get();
					if(removedLinks.length === 1) callback(removedLinks[0].replace(/wiki$/, "") + ":" + JSON.parse(pages[1]["*"]).links[removedLinks[0]]);
					else callback(false);
				}
			}
		);
	};
	$("<table>")
	.append(
		$("<td>")
		.attr("colspan", 6)
		.append(
			$("<label>")
			.text("registered users")
			.prepend(
				$("<input>")
				.attr({
					type: "checkbox",
					id: "liverc-registered"
				})
			)
		)
		.append(
			$("<label>")
			.text("bots")
			.prepend(
				$("<input>")
				.attr({
					type: "checkbox",
					id: "liverc-bot"
				})
			)
		)
		.append(
			$("<label>")
			.text("own")
			.prepend(
				$("<input>")
				.attr({
					type: "checkbox",
					id: "liverc-own"
				})
			)
		)
		.append(
			$("<label>")
			.text("only latest revisions for page")
			.prepend(
				$("<input>")
				.attr({
					type: "checkbox",
					id: "liverc-latest"
				})
				.prop("checked", true)
			)
		)
		.append(
			$("<label>")
			.text("New editor removing a sitelink")
			.prepend(
				$("<input>")
				.attr({
					type: "radio",
					name: "liverc-tag",
					id: "liverc-ners",
				})
				.prop("checked", true)
			)
		)
		.append(
			$("<label>")
			.text("Language as description")
			.prepend(
				$("<input>")
				.attr({
					type: "radio",
					name: "liverc-tag",
					id: "liverc-lad",
					disabled: true
				})
			)
		)
		.append(
			$("<label>")
			.text("Unexpected value for gender")
			.prepend(
				$("<input>")
				.attr({
					type: "radio",
					name: "liverc-tag",
					id: "liverc-uvfg",
					disabled: true
				})
			)
		)
		.appendTo("<tr>").parent().appendTo("<thead>").parent()
	)
	.append(
		$("<tbody>")
		.attr("id", "liverc-recentchanges")
	)
	.replaceAll("#mw-content-text");
	var refresh = function(){
		var filters = [];
		if(!$("#liverc-bot").prop("checked")) filters.push("!bot");
		if(!$("#liverc-registered").prop("checked")) filters.push("anon");
		var tag = "";
		if($("#liverc-ners").prop("checked")) tag = "new editor removing sitelink";
		else if($("#liverc-lad").prop("checked")) tag = "adding language as description";
		else if($("#liverc-uvfg").prop("checked")) tag = "Unexpected value for gender";
		$.getJSON(
			mw.util.wikiScript("api"),
			$.extend(
				{
					action: "query",
					list: "recentchanges",
					format: "json",
					rcprop: "user|title|ids|comment|parsedcomment|sizes|tags|timestamp",
					rclimit: 20
				},
				$("#liverc-own").prop("checked") ? {} : {rcexcludeuser: mw.config.get("wgUserName")},
				filters.length > 0 ? {rcshow: filters.join("|")} : {},
				tag !== "" ? {rctag: tag} : {},
				$("#liverc-latest").prop("checked") ? {rctoponly: true} : {}
			)
		)
		.done(function(data){
			var ids = $.map(data.query.recentchanges, function(e, i){
				return e.title;
			});
			getLabels({ids: ids},function(labels){
				$("#liverc-recentchanges").empty();
				$.each(data.query.recentchanges, function(recentChangeIndex, recentChange){
					getRemovedLinks(recentChange.title, recentChange.revid, function(removedLinks){
						getLabels({sites: removedLinks.split(":")[0] + "wiki", titles : removedLinks.replace(/^[^:]+:/, "")}, function(removedLinksLabels, removedLinksIds){
							$("<tr>")
							.append(
								$("<td>")
								.append(new Date(recentChange.timestamp).toLocaleTimeString())
							)
							.append(
								$("<a>")
								.append(labels[recentChange.title.toUpperCase()])
								.attr("href", mw.util.getUrl(recentChange.title))
								.wrap("<td>").parent()
							)
							.append(
								$("<a>")
								.text(recentChange.user)
								.attr("href", mw.util.getUrl(recentChange.anon ? ("Special:Contributions/" + recentChange.user) : ("User:" + recentChange.user)))
								.wrap("<td>").parent()
							)
							.append(
								$("<a>")
								.text("diff")
								.attr("href", mw.util.wikiScript() + "?" + $.param({title: recentChange.title, oldid: recentChange.old_revid, diff: recentChange.revid}))
								.wrap("<td>").parent()
							)
							.append(
								$("<td>")
								.css("color", "gray")
								.append(recentChange.tags !== undefined ? recentChange.tags : "")
							)
							.append(
								$("<td>")
								.append(removedLinks !== false ?
									$("<a>")
									.text(removedLinks)
									.attr("href", mw.util.getUrl(removedLinks))
								: "")
								.append(removedLinksLabels[Object.keys(removedLinksLabels)[0]] != -1 ?
									(Object.keys(removedLinksLabels)[0] === recentChange.title ? " reverted" : " moved to ")
									: " now orphan"
								)
								.append(removedLinksLabels[Object.keys(removedLinksLabels)[0]] != -1 && Object.keys(removedLinksLabels)[0] !== recentChange.title ?
									$("<a>")
									.append(removedLinksLabels[Object.keys(removedLinksLabels)[0]])
									.attr("href", mw.util.getUrl(removedLinksIds[0]))
								: "")
							)
							.appendTo("#liverc-recentchanges");
						});
					});
				});
			});
		});
	};
	refresh();
	setInterval(refresh, 20000);
});
//</nowiki>