Jump to content

User:Sander Säde/allest4test.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.
//Script to quickly get a list of all WikiProject Estonia articles
$(CollectButton);
function CollectButton() {
	mw.util.addPortletLink("p-cactions", "javascript:allEst()", "wpe: allEst");
}

var estArticles = new Array();
var $content = $("#contentSub");

function allEst() {
	$content.html("");
	getAllEstonianArticles(null);
}

function getAllEstonianArticles(cmContinue) {
	var url = cmContinue == null ?
	"https://en.wikipedia.org/w/api.php?action=query&rawcontinue=&list=categorymembers&cmtitle=Category:WikiProject_Estonia_articles&cmlimit=500&cmprop=title&format=json"
	: "https://en.wikipedia.org/w/api.php?action=query&rawcontinue=&list=categorymembers&cmtitle=Category:WikiProject_Estonia_articles&cmlimit=500&cmprop=title&format=json&cmcontinue=" + cmContinue;
	
	$.ajax({
  		method: "GET",
  		dataType: "json",
  		url: url })
  	.done(function(data) {
  		$.each(data.query.categorymembers, function(i, v) {
  			estArticles.push(v.title);
  		});
	//progressbar	
		$content.html(Array(Math.floor(estArticles.length / 50)).join("|") + " " + estArticles.length);
   		if (data["query-continue"] == null) {
			$content.html("Posting updates: " + estArticles.length);
			updateEstArticles();
		}
		else {
			var cmContinueC = data["query-continue"]["categorymembers"]["cmcontinue"];
			$content.html($content.html() + " " + cmContinueC);
			getAllEstonianArticles(cmContinueC);
		}
  });
}

function updateEstArticles() {
	var text = '';
	var iPos = 0;
	var article;
	var prefix;
	estArticles.sort();
	for (i = 0; i < estArticles.length; i++) {
		iPos = estArticles[i].indexOf(":");
		prefix = estArticles[i].substring(0, iPos).split(" ")[0];
		if (prefix == "User") {
			continue;
		}
		article = estArticles[i].substring(iPos + 1);
		if (prefix == "Talk") {
			article = "# [[" + article + "]]·([[Talk:" + article + "|talk]])\r\n";
		}
		else {
			article = "# [[:" + prefix + ":" + article + "]]·([[" + prefix + " talk:" + article + "|talk]])\r\n";
		}
		text = text + article;
	}
	text = estArticles.length + " articles found\r\n\r\n" + text + "\r\n\r\n[[" + "Category:Wikipedia public watchlists]]";

	editPage({
		title: 'Wikipedia:WikiProject Estonia/publicwatchlist',
		text: text,
		summary: 'Updating Estonia-related articles (' + estArticles.length + ')'
	});
}

function editPage(info) {
	$.ajax({
		url: mw.util.wikiScript('api'),
		type: 'POST',
		dataType: 'json',
		data: {
			format: 'json',
			action: 'edit',
			title: info.title,
			text: info.text,
			summary: info.summary,
			token: mw.user.tokens.get('editToken')
		}
	})
.complete(function() {
   document.getElementById("contentSub").innerHTML = 'Update finished: ' + estArticles.length;
  })
  .error(function() {
  alert('The AJAX edit request failed.');
  });	
}