User:Technical 13/Scripts/imageTagger.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Technical 13/Scripts/imageTagger. |
//<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>