User:Majr/fileNuke.js

mw.loader.using( 'mediawiki.api', function {	'use strict';	var api = new mw.Api;	var $galleryList = $( '#gallery-list' );	var deleteFiles = function( files, reason, count ) {		if ( count === undefined ) {			count = 0;		} else if ( $galleryList.length ) {			var $textbox = $galleryList.children( 'textarea' );			$textbox.prop( 'value', function( $, text ) { return text.replace( files[count - 1], '' ).replace( /\n+/g, '\n' ).trim; } );			$textbox[0].scrollTop = $textbox[0].scrollHeight;		}		if ( !files[count] ) {			return;		}		api.get( { action: 'query', list: 'backlinks', blredirect: true, bltitle: files[count] } ).done( function( data ) { var backlinks = data.query ? data.query.backlinks : null; if ( !backlinks.length || ( backlinks.length === 1 && backlinks[0].title === files[count] ) ) { api.postWithToken( 'edit', {					action: 'delete',					title: files[count],					reason: reason				} ).done( function {					console.info( 'Deleted:', files[count] );					mw.notify( 'Deleted: ' + files[count] );					deleteFiles( files, reason, count + 1 );				} ).fail( function( code, data ) {					handleError( code, data, files[count] );				} ); } else { console.info( 'Skipped:', files[count] ); mw.notify( 'Skipped: ' + files[count] ); deleteFiles( files, reason, count + 1 ); }		} ).fail( function( code, data ) { handleError( code, data, files[count] ); } );	};	var handleError = function( code, data, file ) {		var title, text;		if ( code === 'http' ) {			if ( data.textStatus === 'error' ) {				text = 'Connection error';			} else {				title = 'HTTP error';				text = data.textStatus;			}		} else {			title = 'API error';			text = data.error.info;		}		console.error( text + ':', file );		mw.notify( [ text + ' ' + file ], { title: title, autoHide: false } );	};

if ( $galleryList.length ) { $galleryList .append( $( ' ' ).addClass( 'mw-ui-button mw-ui-destructive delete' ).text( 'Delete' ) ) .append( $( ' ' ).addClass( 'mw-ui-input reason' ).css( { display: 'block', width: '100%', boxSizing: 'border-box', marginTop: '1px' } ).val( 'Unused file' ) ); $galleryList.children( '.delete' ).click( function {			var reason = $galleryList.children( '.reason' ).val;			deleteFiles( $galleryList.children( 'textarea' ).val.split( '\n' ), reason );		} ); } } );