User:SPage (WMF)/JStransforms.js

( function ( $, mw ) {	if ( !mw.config.get( 'wgTitle' ).match( /^SPage \(WMF\)\/(JS test|ApiEx utility)$/ ) ) {		return;	}	mw.loader.using( 'mediawiki.Uri', function { var transforms = { "make API ex": makeApiEx, "linkify": makeLinks },		$cpanel;

/**		 * given an apiEx uri struct, * returns a method invocation * e.g. take https://en.wikipedia.org/w/api.php?action=query&list=geosearch&format=json&gscoord=37.786952%7C-122.399523&gsradius=10000&gslimit=10 * and turn it into */		function toApiEx( apiEx ) { var res = ''; return res; }

function makeApiEx ( str ) { // TODO: This doesn't work if the input has more than one URL in it. var uri = new mw.Uri( str ), apiEx = {}, hostMatch; if ( uri.host && ( uri.host != 'en.wikipedia.org') ) { hostMatch = uri.host.match( /(.*)\.wikipedia.org/ ); if ( hostMatch && ( hostMatch[1] != 'en') ) { apiEx.lang = hostMatch[1]; } else { apiEx.site = uri.host; }			}			apiEx.query = uri.query; return toApiEx( apiEx ); }

/**		 * Given a URL, possibly turn it into an interwiki foo:path wiki link. */		function makeIWLinkFromURL( url ) { // https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.Uri var	uri = new mw.Uri( url ), path, query, fragment, interwiki = null, isWiki = false; // Bail if no host or a complex URL like http://usr:pwd@www.example.com:81/path if ( !uri.host || uri.user || uri.password || uri.port ) { return url + "(WELL AT LEAST IT WAS PROCESSED)"; }

// Prepare the rest of the URL for reassembly rest = uri.path.replace( '^/', ''); if ( !$.isEmptyObject( uri.query ) ) { rest += '?' + uri.getQueryString; }			if ( uri.fragment ) { rest += '#' + uri.fragment; }

switch ( uri.host.toLowerCase ) { case 'phabricator.wikimedia.org': interwiki = 'phab:'; break;

case 'www.mediawiki.org': case 'mediawiki.org': // Assume this is running on mediwiki.org interwiki = ''; isWiki = true; break; case 'en.wikipedia.org': interwiki = 'w:'; isWiki = true; break; }

if ( interwiki === null ) { return url; }

if ( isWiki ) {

// mw:Special:Search?search=findme doesn't work if ( uri.query ) { return url; }				// http://mediawiki.org/w/COPYING won't work as interwiki. if ( !rest.match( /^\/wiki\// ) ) { return url; }				rest = rest.replace( /^\/wiki\//, ''); // TODO: title within wiki link should change underscores to spaces. Use mw.Title ? }			// NOT adding a text for the wiki link. return  + interwiki + rest + ; }

/**		 * Linkify all the things * "T1234 and https://phabricator.wikimedia.org/maniphest/task/create/?projects=dev.wikimedia.org,documentation and https://www.mediawiki.org/wiki/User:SPage_(WMF)/Sandbox#wiki_links" * "but not aT1234, T1234, https://www.mediawiki.org/wiki/Special:Search?search=lookup" */		function makeLinks ( str ) { var res = str, // Detects the URLs to pass to makeIWLinkFromURL. // from http://stackoverflow.com/a/8943487/1162195, but exclude ftp and file // TODO ignore URL if it has square bracket in front or if it's in an href="". urlRegex =/(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, // T1234, but not thisT1234 or phab:T1234 or .org/T1234 phabTRE = /(^|[^\/:\w])(T\d{3,7})(\W)/g; res = res.replace( urlRegex, makeIWLinkFromURL ) .replace( phabTRE, '$1$2$3' ); }

function doTransform { var res, transform; transform = $( 'input[name="workJStransform"]:checked' ).val; res = transforms[transform]( $( '#inp' ).val ); $cpanel.find( '#result').html( res ); }

// Set up the input area $cpanel = $( '  ' ); // Create a radio button for each transform $.each( transforms, function( key, value ) {		   var $element=$('' + key + ' ');		    $cpanel.append( $element );		} ); // Check the first radiobutton $cpanel.find( '[name="workJStransform"]:first').prop( 'checked', true ); // Do the transform whenever any input element changes. $cpanel.find( '#inp,input[name="workJStransform"]' ).change( doTransform ); $cpanel.append( ' ' );

$ ( document ).ready( function {		   $( '#workArea' ).html( $cpanel );		}); 	} ); window.console.log( 'mw.makeApiEx defined' ); } )( jQuery, mediaWiki );