User:DannyS712/AccountsCreated.js

From Meta, a Wikimedia project coordination wiki

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.
// <nowiki>
// Quick script to show the accounts created by a user on any wiki
// @author DannyS712
$(() => {
const AccountsCreated = {};
window.AccountsCreated = AccountsCreated;

AccountsCreated.init = function () {
	window.document.title = 'AccountsCreated search';
	$( '#firstHeading' ).text( 'AccountsCreated search' );
	mw.loader.using(
		[ 'mediawiki.api', 'mediawiki.ForeignApi', 'mediawiki.util', 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows' ],
		AccountsCreated.run
	);
};

AccountsCreated.run = function () {
	var userSearch = new OO.ui.TextInputWidget( {
		value: mw.util.getParamValue( 'targetUser' )
	} );
	var userSearchLayout = new OO.ui.FieldLayout(
		userSearch,
		{ label: 'User' }
	);
	var submit = new OO.ui.ButtonInputWidget( { 
		label: 'Search',
		flags: [
			'primary',
			'progressive'
		]
	} );
	submit.on( 'click', function () {
		console.log( userSearch );
		AccountsCreated.onSubmit( userSearch.value );
	} );
	$( window ).on( 'keypress', function ( e ) {
		// press enter to start
		if ( e.which == 13 ) {
			submit.simulateLabelClick();
		}
	} );
	
	var fieldSet = new OO.ui.FieldsetLayout( { 
		label: 'Search for accounts created by a user'
	} );
	fieldSet.addItems( [
		userSearchLayout,
		new OO.ui.FieldLayout( submit )
	] );

	var $results = $('<div>')
		.attr( 'id', 'accountscreated-results' );
	$('#mw-content-text').empty().append(
		fieldSet.$element,
		$( '<hr>' ),
		$results
	);
};

AccountsCreated.onSubmit = function ( userName ) {
	console.log( 'Form submitted with: ' + userName );
	$( '#accountscreated-results' ).empty();
	new mw.Api().get( {
		action: 'query',
		meta: 'globaluserinfo',
		guiuser: userName,
		guiprop: 'merged',
		formatversion: 2
	} ).done( AccountsCreated.afterQuery );
};

AccountsCreated.afterQuery = function ( res ) {
	console.log( res );
	var merged = res.query.globaluserinfo.merged;
	var user = res.query.globaluserinfo.name;
	//console.log( merged );
	
	var $output = $( '#accountscreated-results' );
	var $list = $( '<ul>' );
	$output.append( $list );
	merged.forEach( function ( site ) {
		var apiUrl = site.url.replace( /^https?:/, '' ) + '/w/api.php';
		//console.log( apiUrl );
		var api = new mw.ForeignApi( apiUrl );
		api.get( {
			action: 'query',
			list: 'logevents',
			leuser: user,
			leaction: 'newusers/create2',
			leprop: 'title|ids',
			formatversion: 2
		} ).done( function ( logs ) {
			//console.log( site.url, logs );
			var entries = logs.query.logevents;
			if ( entries.length !== 0 ) {
				console.log( 'Accounts created', entries );
				entries.forEach( function ( accountCreated ) {
					var nameCreated = accountCreated.title || '<hidden>';
					nameCreated = nameCreated.replace( 'User:', '' );
					var $li = $( '<li>' )
						.append(
							$( '<a>' )
								.attr( 'target', '_blank' )
								.attr( 'href', site.url + '/wiki/Special:Redirect/logid/' + accountCreated.logid )
								.text( site.url.replace( /^https?:\/\//, '' ) ),
							' -> ',
							$( '<a>' )
								.attr( 'target', '_blank' )
								.attr( 'href', '//meta.wikimedia.org/wiki/Special:CentralAuth/' + nameCreated )
								.text( nameCreated )
						);
					$list.append( $li );
				} );
			} else {
				var $li = $( '<li>' )
					.append(
						$( '<a>' )
							.attr( 'target', '_blank' )
							.attr( 'href', site.url + '/wiki/Special:Log/' + user )
							.text( site.url.replace( /^https?:\/\//, '' ) ),
						' -> none'
					);
				$list.append( $li );
			}
		} );
	} );
};

});

$(document).ready(() => {
	if ( mw.config.get( 'wgNamespaceNumber' ) === -1 ) {
		const page = mw.config.get( 'wgCanonicalSpecialPageName' );
		if ( page === 'Blankpage' ) {
			const page2 = mw.config.get( 'wgTitle' ).split( '/' );
			if ( page2[1] && page2[1] === 'AccountsCreated' ) {
				window.AccountsCreated.init();
			}
		} else if ( page === 'Contributions' ) {
			let links = $('#contentSub .mw-contributions-user-tools > .mw-changeslist-links');
			let targetUser = mw.config.get( 'wgRelevantUserName' );
			links.html( links.html() + '<span><a href="/wiki/Special:BlankPage/AccountsCreated?targetUser=' + targetUser + '">accounts created</a></span>');
		}
	}
});

// </nowiki>