User:Technical 13/Scripts/imageTagger.js

From wikishia

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>
( function ( mw, $ ) {
	function processResult( doAction ) {
		mw.notify( 'Processing request to tag with "' + doAction + '".', { title: 'Processing...', autoHide: false } );
		var pageid = mw.config.get( 'wgArticleId' );
		new mw.Api().get( {
			action: 'query',
			pageids: pageid,
			prop: 'revisions',
			rvprop: 'content',
			indexpageids: 1,
			continue: ''
		} ).done( function( pageContents ) {
			mw.notify( 'Got page contents...', { title: 'Processing...', autoHide: false } );
			var txt = pageContents.query.pages[ pageid ].revisions[ 0 ][ '*' ];
			var summary = '';
			if ( !txt ) return;
			switch ( doAction ) {
				case 'nfr' :
					txt = '{{Non-free reduce}}\n\n' + txt;
					summary += 'Tagging image - non free reduce - for image reduction as per non-free guideline [[Wikipedia:Image resolution]]';
					break;
				case 'ncd' :
					txt.replace( /\{\{[\s\t\n\r]*([mc]t?w?c|needs[ -_]commons[ -_]category|((copy|move)[ -_]?)?to[ -_]?(wikimedia[ -_])?commons)[\s\t\n\r]*\}\}/ig, '' );
					txt = '{{subst:ncd}}\n\n' + txt;
					summary += 'Tagging image as on already on commons ({{[[Template:NowCommons|NowCommons]]}})';
					break;
				case 'mtc' :
					txt = '{{Copy to Wikimedia Commons|human=' + mw.config.get( 'wgUserName' ) + '}}\n\n' + txt;
					summary += 'Tagging image to be [[WP:MITTC|Copied to Wikimedia Commons]] ({{[[Template:Copy to Wikimedia Commons|Copy to Wikimedia Commons]]}})';
					break;
				case 'pdc' :
					if ( txt.match( 'Category:PD chem' ) !== null ) return;
					txt = txt + '\n[[Category:PD chem]]\n';
					summary += 'Tagging image to be in [[:Category:PD chem]]';
					break;
				case 'ccs' :
					if ( txt.match( 'Category:Classified Chemical Structures' ) !== null ) return;
					txt = txt + '\n[[Category:Classified Chemical Structures]]\n';
					summary += 'Tagging image to be in [[:Category:Classified Chemical Structures]]';
					break;
				case 'struct' :
					if ( txt.match( 'Category:Chemical structures' ) !== null ) return;
					txt = txt + '\n[[Category:Chemical structures]]\n';
					summary += 'Tagging image to be in [[:Category:Chemical structures]]';
					break;
				default :
					mw.notify( 'There was an error processing your request.\n\n\t\t\t\t\tPlease try again.', { title: 'Error!', autoHide: false } );
					return;					
			}
			mw.notify( 'Posting new page contents...', { title: 'Processing...', autoHide: false } );
			new mw.Api().postWithToken( 'edit', {
				action: 'edit',
				pageid: pageid,
				text: txt,
				summary: summary
			} ).done( function( endResult ) {
				mw.notify( '"' + doAction + '" was posted.  Reloading page.', { title: 'Done!', autoHide: false } );
				location.reload();
			} );
		} );
	}

	if ( mw.config.get( 'wgNamespaceNumber' ) === 6 ) {
		var doNfr = mw.util.addPortletLink( 'p-cactions', '#', 'nfr', 'ca-nfr' );
		$( doNfr ).click( function ( e ) {
			e.preventDefault();
			processResult( 'nfr' );
		} );
		var doMtc = mw.util.addPortletLink( 'p-cactions', '#', 'mtc', 'ca-mtc' );
		$( doMtc ).click( function ( e ) {
			e.preventDefault();
			processResult( 'mtc' );
		} );
		var doNcd = mw.util.addPortletLink( 'p-cactions', '#', 'ncd', 'ca-ncd' );
		$( doNcd ).click( function ( e ) {
			e.preventDefault();
			processResult( 'ncd' );
		} );
		var doPdc = mw.util.addPortletLink( 'p-cactions', '#', 'pdc', 'ca-pdc' );
		$( doPdc ).click( function ( e ) {
			e.preventDefault();
			processResult( 'pdc' );
		} );
		var doCcs = mw.util.addPortletLink( 'p-cactions', '#', 'ccs', 'ca-ccs' );
		$( doCcs ).click( function ( e ) {
			e.preventDefault();
			processResult( 'ccs' );
		} );
		var doStruct = mw.util.addPortletLink( 'p-cactions', '#', 'struct', 'ca-struct' );
		$( doStruct ).click( function ( e ) {
			e.preventDefault();
			processResult( 'struct' );
		} );
	}
} ( mediaWiki, jQuery ) );
//</nowiki>