[go: up one dir, main page]

Jump to content

User:Jfmantis/pagesCreated.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.
/*
*	Pages Created -- finds all the pages created by a user
*	see [[User:Jfmantis/pagesCreated]]
*/

(function() {

/*
*	create <li> list item for one article
*	right now, just a link to the page & the date
*/
function makeCreatedPageItem(contrib) {
	var item = document.createElement("li");

	var link = document.createElement("a");
	link.href = mw.util.getUrl(contrib.title);
	link.innerHTML = contrib.title;

	item.appendChild(link);

	item.innerHTML += " . . " + new Date(contrib.timestamp).toDateString();

	return item;
}

/*
*	looks through all of a user's non-minor namespace 0 edits,
*	looking for edits tagged as "new"
*
*	the arguments all in one object so that it can be expanded
*	in the future without having to add a bunch more parameters
*/
function findPagesCreated(bundle) {
	bundle.api.get({
		action: "query",
		rawcontinue: '',
		list: "usercontribs",
		ucuser: bundle.user,
		ucstart: bundle.start,
		ucprop: "flags|title|timestamp",
		ucshow: "!minor",
		uclimit: 500,
		ucnamespace: 0
	}).done( function(data) {
		$.each(data.query.usercontribs, function(index, contrib) {
			if (contrib.new != undefined) {
				bundle.list.appendChild(makeCreatedPageItem(contrib));
				bundle.count++;
			}
		});

		if (data["query-continue"]) {	// more contributions
			bundle.start = data["query-continue"].usercontribs.ucstart
			setTimeout(function() { findPagesCreated(bundle); }, 3000);
		} else {	// done
			$("#pc-status")[0].innerHTML = "<br />" + bundle.user + " has created " + bundle.count + " articles";
		}
	}).fail( function(error) {
		alert(error);
	});
}

/*
*	change title, clear content area, etc.
*/
function setupPagesCreated(user) {
	// set new title
	mw.util.$content.find("#firstHeading")[0].innerHTML = "Pages created by " + user;

	// status bar (text + waiting gif)
	var status = document.createElement("span");
	status.id = "pc-status";
	status.innerHTML = "<br />Fetching user data...";

	// heading for results
	var heading = document.createElement("h3");
	heading.innerHTML = "Articles";

	// list of results
	var articles = document.createElement("ul");
	articles.id = "pc-articles";

	var body = mw.util.$content.find("#bodyContent")[0];
	body.innerHTML = "";
	body.appendChild(status);
	body.appendChild(heading);
	body.appendChild(articles);

	var api = new mw.Api();
	api.get({
		action: "query",
		list: "users",
		ususers: user,
		usprop: "editcount"
	}).done(function(data) {
		// 500 results per request, 1 request every 3 seconds
		var count = data.query.users[0].editcount;
		status.innerHTML = "<br />User has " + count + " edits, this should take less than ";
		status.innerHTML += (3 * Math.round(count / 500)) + " seconds &nbsp;&nbsp;&nbsp;";

		var waitgif = document.createElement("img");
		waitgif.src = "https://upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif";
		status.appendChild(waitgif);

		findPagesCreated(
			{"api": api,
			"user": user,
			"list": articles,
			"start": "",
			"count": 0}
		);
	}).fail(function(error) {
		alert(error);
	});

	return false;
}

mw.loader.using("mediawiki.util", function() {
	// add portlet when page is User or User_talk, but not on subpages
	if ((mw.config.get('wgNamespaceNumber') == 2 || mw.config.get('wgNamespaceNumber') == 3) && (mw.config.get('wgTitle').indexOf("/") == -1)) {
		if (mw.util.getParamValue("pagesCreated")) {
			setupPagesCreated(mw.config.get('wgTitle'));
		} else {
			mw.util.addPortletLink("p-tb",
				document.location.toString() + "?pagesCreated=true",
				"Pages created", "pc-pages",
				"Get a list of all pages created by this user"
			);
		}
	}
});

})();