User:DannyS712/deOrphan.js

// Install with: // // or with //  // // Special thanks to User:Technical 13 and his script, User:Technical 13/Scripts/OrphanStatus.js, which I forked // before later rewriting the script. // If forking this script, please note my contributions / give me credit

$(function { const DeOrphan = {}; DeOrphan.removeTag = function {	const $progressDisplay = $('.loadinganimation');	$progressDisplay.text( 'Article deOrphaning in progress...' );	new mw.Api.edit( mw.config.get( 'wgPageName' ), function ( revision ) { const newContent = revision.content .replace( /\{\{Orphan(.*?)\}\}[\|\n]/gi, '' )// Parse out orphan template (not) in multiple issues .replace( /\| *Orphan *=[\d\w\s\n]*(.*?\}\})/gi, '$1' ); // Parse out old style multiple issues orphan parameter return { text: newContent, summary: 'Article deOrphaned!', assert: 'user', minor: true };		}	).then( function { $progressDisplay.empty .append(				'Article deOrphaned!',				$( ' ' ).append( '(',					$( '' ).text( 'reload' ).on( 'click', => location.reload ),					' | ',					$( '' ).text( 'diff' )						.attr( 'href', "//en.wikipedia.org/w/index.php?title=" + encodeURIComponent( mw.config.get( 'wgTitle' ) ) + "&diff=cur&oldid=prev" ),					')' )			);       } ); }; DeOrphan.render = function  {	mw.util.addCSS(` .loadinganimation { font-size: medium !important; color: #000 !important; font-family: sans-serif !important; float: right; } .loadinganimation a { font-weight: bold; } `);   $progressDisplay = $( ' ' ).addClass( 'loadinganimation' );    $( '#firstHeading' ).append( $progressDisplay );    new mw.Api.get( { action: 'query', list: 'backlinks', format: 'json', blfilterredir: 'nonredirects', bllimit: 500, blnamespace: 0, bltitle: mw.config.get( 'wgTitle' ) } ).done( function ( response ) { var orphan_cutoff = (window.user_orphan_cutoff || 1); var backLinks = response.query.backlinks.length; const $linksHere = $( '' ) .attr( 'href', '//en.wikipedia.org/w/index.php?title=Special:WhatLinksHere/' + encodeURIComponent( mw.config.get( 'wgTitle' ) ) + '&namespace=0&hideredirs=1&hidetrans=1' ) .text( 'other articles' ); const $deOrphan = $( ' ' ).append(       	'(', $( '' ).text( 'deOrphan' ).on( 'click', => DeOrphan.removeTag ), ')'       );        const $orphan = $( '' ).text( 'orphan' ).attr( 'href', '//en.wikipedia.org/wiki/Wikipedia:Orphan' ); switch ( backLinks ){ case 0: if (orphan_cutoff <= 0) { $progressDisplay.append(           			'This page is an ', $orphan, ' as no ', $linksHere, ' link to it'            		); }               break; case 1: if (orphan_cutoff <= 1) { $progressDisplay.append(           			'There is ', $( '' ).text( '1 link to this page' ), ' from an', $linksHere.text( 'other article' ), '. ', $deOrphan            		); }           	break; case 2: if (orphan_cutoff <= 2) { $progressDisplay.append(           			'There are ', $( '' ).text( '2 links to this page' ), ' from ', $linksHere, '. ', $deOrphan            		); }           	break; default: $progressDisplay.append(           		'This page is not an ', $orphan, ' as it meets the "Rule of Three" by having three or more links from ', $linksHere, '. ', $deOrphan            	); break; }   } ); }; if ( mw.config.get( 'wgNamespaceNumber' ) === 0 && mw.config.get( 'wgAction' ) === 'view' && $.inArray( 'All orphaned articles', mw.config.get( 'wgCategories' ) ) >= 0 ) {   $( '.ambox-Orphan' ).css( 'display', 'inherit' );    mw.loader.using( [ 'mediawiki.util', 'mediawiki.api' ], DeOrphan.render ); } });