Jump to content

User:DannyS712/deOrphan.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.
// Install with:
// <code><nowiki>		{{subst:Iusc|User:DannyS712/deOrphan.js}}																	</nowiki></code>
// or with
// <code><nowiki>		importScript( 'User:DannyS712/deOrphan.js' ); // Backlink: [[User:DannyS712/deOrphan.js]] 					</nowiki></code> 
//
// 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 [[User:DannyS712/deOrphan|deOrphaned]]!',
            	assert: 'user',
            	minor: true
            };
			
		}
	).then( function() {
		$progressDisplay.empty()
			.append(
				'Article deOrphaned!',
				$( '<small>' ).append(
					'(',
					$( '<a>' ).text( 'reload' ).on( 'click', () => location.reload() ),
					' | ',
					$( '<a>' ).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 = $( '<span>' ).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 = $( '<a>' )
        	.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 = $( '<small>' ).append(
        	'(',
        	$( '<a>' ).text( 'deOrphan' ).on( 'click', () => DeOrphan.removeTag() ),
        	')'
        );
        const $orphan = $( '<a>' ).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 ', $( '<b>' ).text( '1 link to this page' ), ' from an', $linksHere.text( 'other article' ), '. ', $deOrphan
            		);
            	}
            	break;
            case 2:
            	if (orphan_cutoff <= 2) {
            		$progressDisplay.append(
            			'There are ', $( '<b>' ).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
    );
}
});