User:Steinsplitter/consent.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.
/**
 * Tag images with {{consent}} fast
 * [[m:User:Hoo man]]; 2013-03-04
 * Derivative work: [[User:Rillke]]; 2013-08-14
 *
 * jshint validate
 * <nowiki>
 */

/*global mw:false, $:false, alert:false*/
$( document ).ready( function() {
	'use strict';
	var template2Search = 'consent';
	
	if ( mw.config.get( 'wgAction' ) === 'view' && 
		mw.config.get( 'wgNamespaceNumber' ) === 6 && 
		!$('#commons-template-consent').length ) {
			$( mw.util.addPortletLink( 'p-tb', '#', '+consent' ) )
				.click( function( event ) {
					event.preventDefault();

					mw.loader.using( [ 'mediawiki.util', 'mediawiki.api', 'user.options', 'ext.gadget.libWikiDOM' ], function() {
						var i, section, data, licensingIDs;
						
						// The h2 with the license has a different id for every language...
						licensingIDs = [
							// en
							'Licensing',
							'Licensing\\:',
							// de
							'Lizenz',
							'Lizenz\\:'
						];

						for( i = 0; i < licensingIDs.length; i++ ) {
							if ( $( '#' + licensingIDs[i] ).length ) {
								section = '#' + licensingIDs[i];
								break;
							}
						}

						if ( section ) {
							section = mw.util.getParamValue(
								'section',
								$( section )
									.parent()
									.find( 'span.mw-editsection a' )
									.attr( 'href' )
							);
						}
						
						data = {
							title: mw.config.get( 'wgPageName' ),
							action: 'raw'
						};
						
						if ( section ) {
							data.section = section;
						}

						$.ajax( {
								url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ),
								data: data,
								cache: false
							} )
							.done( function( text ) {
								var api = new mw.Api(),
									dom = mw.libs.wikiDOM.parser.text2Obj( text ),
									template2Add = '\n{{' + template2Search + '}}',
									afterTemplates = [
										'Information'
									],
									edit, inserted, insertText, insertLast;
								
								if ( dom.nodesByType.template ) {
									insertText = function() {
										text += template2Add;
									};
									insertLast = function() {
										var template = dom.nodesByType.template[dom.nodesByType.template.length - 1];
										// Do not append something after localized messages
										if ( 'h' === template.parent.type ) {
											return insertText();
										}
										dom.nodesByType.template[dom.nodesByType.template.length - 1].after( template2Add );
										text = mw.libs.wikiDOM.parser.obj2Text( dom );
									};
									if ( !section ) {
										$.each( dom.nodesByType.template, function( i, tl ) {
											var template = tl.parts[0][0];
											if ( !template ) return;
											
											template = mw.libs.wikiDOM.normalizeTitle( template.replace(/^[Tt]emplate:/, '') );
											if ( $.inArray( template, afterTemplates ) === -1 ) return;
											
											tl.after( template2Add );
											text = mw.libs.wikiDOM.parser.obj2Text( dom );
											inserted = true;
											return false;
										});
									}
									if ( !inserted ) {
										insertLast();
									}
								} else {
									insertText();
								}

								edit = {
									action: 'edit',
									title: mw.config.get( 'wgPageName' ),
									text: text,
									summary: '/* {{int:license-header}} */ +consent using [[User:Steinsplitter/consent.js|consent.js]]',
									token: mw.user.tokens.get( 'csrfToken' ),
									nocreate: true
								};
								if ( section ) {
									edit.section = section;
								}

								api.post( edit )
									.done( function() {
										window.location.href =
											mw.config.get( 'wgServer' ) + mw.config.get( 'wgArticlePath' ).replace( /\$1/g, mw.config.get( 'wgPageName' ) );
									} )
									.fail( function() {
										alert( "An error occured while editing the page" );
									} );
							} )
							.fail( function() {
								alert( "An error occured while editing the page" );
							} );
					} );
				} );
	}
} );
// </nowiki>