Jump to content

User:Novem Linguae/Scripts/DetectG4G5.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Novem Linguae (talk | contribs) at 07:59, 15 December 2021 (add). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// <nowiki>

/*
- Displays an alert if an article may be a CSD G4 (previous AFD) or CSD G5 (created by a sockpuppet)
- Useful for new page patrolling
- Only runs on pages that have not been marked as reviewed
*/

// TODO: Code review. Is there a way to reduce the # of API queries?

$(async function() {
	function displayWarning(html) {
		$('#contentSub').before(`<div class="DetectG4G5" style="background-color: red">${html}</div>`);
	}

	async function isReviewed(title) {
		let api = new mw.Api();
		let response = await api.get( {
			action: 'query',
			format: 'json',
			list: 'logevents',
			letype: 'pagetriage-curation',
			letitle: title,
		} );
		let isReviewed = typeof response.query.logevents[0] !== 'undefined' && response.query.logevents[0].action === 'reviewed';
		return isReviewed;
	}

	async function afdExists(title) {
		let title = 'Wikipedia:Articles for deletion/' + title;
		return await pageExists(title);
	}

	async function pageExists(title) {
		let api = new mw.Api();
		let response = await api.get( {
			action: 'query',
			format: 'json',
			prop: 'revisions',
			titles: title,
		} );
		let exists = typeof response.query.pages['-1'].missing !== 'undefined';
		return exists;
	}

	async function isBlocked(username) {
		return true;
	}

	async function isGloballyLocked(username) {
		return true;
	}

	async function getPageCreator(title) {
		return '';
	}
	
	// don't run when not viewing articles
	let action = mw.config.get('wgAction');
	if ( action != 'view' ) return;
	
	// don't run when viewing diffs
	let isDiff = mw.config.get('wgDiffNewId');
	if ( isDiff ) return;
	
	let isDeletedPage = ( ! mw.config.get('wgCurRevisionId') );
	if ( isDeletedPage ) return;
	
	// Only run in mainspace
	let namespace = mw.config.get('wgNamespaceNumber');
	let title = mw.config.get('wgPageName'); // includes namespace, underscores instead of spaces
	if ( namespace !== 0 && title != 'User:Novem_Linguae/sandbox' ) return;
	
	if ( await isReviewed(title) ) {
		return;
	}

	if ( await afdExists(title) ) {
		displayWarning('<span style="font-weight:bold">CSD G4:</span> There is an AFD page for this article. It may qualify for CSD G4.');
	}

	let pageCreator = await getPageCreator(title);
	if ( await isBlocked(pageCreator) || await isGloballyLocked(pageCreator) ) {
		displayWarning('<span style="font-weight:bold">CSD G5:</span> The creator of this page is blocked or globally locked. This article may qualify for CSD G5.');
	}
});

// </nowiki>