User:Sarang/uexist.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/*	JS module "uexist.js" to return "ns" depending to a "link".
	"ns" can be U, Ut, Uw, Uwt, Un or Uf.


*/

//	 * ============================================================
	function pageexists( pagename, callback ) {
	mw.loader.using( 'mediawiki.api', function () {
		( new mw.Api() ).get(
			{
				action: 'query',
				prop: 'info',
				titles: pagename,
				formatversion: 2
			},
			{
				success: function ( response ) {
					var page = response.query.pages[ 0 ];
					if ( page.invalid ) {
						callback( null );
					} else if ( page.missing ) {
						callback( false );
					} else {
						callback( true );
					}
				},
				error: function () { callback( null ); }
			}
		);
		} );
	}
//	 * ============================================================
	function userexist( username, namespace ) 
	{	let retval = 'Un';
		pageexists ( username, (boolres) => 
		{	if (boolres)	retval = namespace;	// ns
			else 
			if (boolres == null)	// CommonsMaintenanceBot issue
							retval = 'Uf'; 
	//			else retval = 'Un';
			});
			return retval;
	}
//	 * ============================================================
	function u_ex ( link, ns ) 
	{ //if (ns === 'Un')
 			ns = userexist("User:" + link, 'U');				
 		if (ns === 'Un')
 			ns = userexist("User talk:" + link, 'Ut');
 		if (ns === 'Un')
 			ns = userexist("User:" + link + '~commonswiki', 'Uw');
 		if (ns === 'Un')
			ns = userexist("User talk:" + link + '~commonswiki', 'Uwt');
		return {ns};
	}

	export {u_ex};
// EOF </nowiki>