User:Whym/rfds.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.
// this adds "(RFD archive)" links to the right of red links

// If someone is interested in using this script, I recommend running it on the browser's console after opening [[Special:Log/delete]] (and ''not'' adding it into a personal common.js). 

(function ($, mw, undefined) {
"use strict";

var rfds = mw.libs.RFDSearch = {

	targets: $('.mw-logline-delete > a.new'),

	run: async function (lang) {
		if (!rfds.targets.length) return;
		let nodes = {};
		rfds.targets.each(function(){
			let title = $(this).text();
			if (title.startsWith('User:') || title.startsWith('User talk:')) {
				// skip user pages
			} else {
				nodes[title] = $(this);
			}
		});
		let items = Object.keys(nodes);

		for (let i = 0; i < items.length; i += 50) {
			let cont = '|';
			let count = 0;
			while (cont) {
				let params = new URLSearchParams({
					format: 'json',
					action: 'query',
					prop: 'linkshere',
					lhnamespace: 4,
					titles: items.slice(i, i+50).join('|'),
					lhcontinue: cont
				});
				if (cont == '|') {
					params.delete('lhcontinue');
				}
				let url =  mw.util.wikiScript( 'api' ) + '?' + params.toString();
				let rsp = await fetch(url);
				let data = await rsp.json();
				Object.values(data['query']['pages']).forEach(function(x) {
					if (x['linkshere']) {
						let ls = x['linkshere'].filter(x => x['title'].startsWith('Wikidata:Requests for deletions'))
						if (ls.length > 0) {
							nodes[x['title']].after(' <a href="https://wikidata.org/wiki/' + ls[0]['title'] + '">(RFD archive)</a>');
							count += 1;
						}
					}
				});
				if (data['continue'] && data['continue']['lhcontinue']) {
					if ( cont != data['continue']['lhcontinue'] ) {
						cont = data['continue']['lhcontinue'];
					} else {
						console.log('continue aborted');
						console.log(data['continue']);
						cont = null;
					}
				} else {
					cont = null;
				}
				await new Promise(r => setTimeout(r, 500));
			}
			console.log(count);
		}
		console.log('RFDS finished');
	},

}; // RFDSearch

// executed from here when loaded
mw.loader.using(['mediawiki.util'], function () {
	rfds.run();
});

})(jQuery, mediaWiki);