MediaWiki:Gadget-autopadlock.js

Da Semi del Verbo, l'enciclopedia dell'influenza del Vangelo sulla cultura

Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
/**
 * Aggiunge il template Protetta a seguito della protezione di una voce.
 *
 * @author https://it.wikipedia.org/wiki/Utente:Sakretsu
 */
/* global mediaWiki, jQuery */

( function ( mw, $ ) {
	'use strict';

	var api;

	var id = mw.config.get( 'wgArticleId' );

	/** Entro quanto inserire il template dopo la protezione, in secondi */
	var MAX_DELAY = 60;

	/**
	 * Verifica che la voce sia stata appena protetta dall'utente.
	 * 
	 * @param {function} editHandler - La funzione di modifica.
	 */
	function checkLastProtection( editHandler ) {
		api.get( {
			action: 'query',
			prop: 'revisions',
			pageids: id,
			rvprop: 'content',
			rvslots: 'main',
			rvlimit: 1,
			list: 'logevents',
			leprop: 'userid|timestamp',
			letitle: mw.config.get( 'wgPageName' ),
			letype: 'protect',
			lelimit: 1,
			format: 'json'
		} ).done( function( result ) {
			var data = result.query;
			var protection = data.logevents[ 0 ];
			var wikitext = data.pages[ id ].revisions[ 0 ].slots.main[ '*' ];
			if ( protection.userid === mw.config.get( 'wgUserId' ) &&
				 new Date() - new Date( protection.timestamp ) < MAX_DELAY * 1000 &&
				 !/\{\{ *[Pp]rotetta *\}\}/.test( wikitext ) ) {
				editHandler();
			}
		} ).fail( function ( error ) {
			console.log( 'Errore API', error );
		} );
	}

	/**
	 * Inserisce il template Protetta a inizio voce.
	 */
	function addPadlocks() {
		var text = '<noinclude>{\{Protetta}\}</noinclude>\n';
		api.postWithToken( 'csrf', {
			action: 'edit',
			pageid: id,
			prependtext: text,
			summary: '+Protetta',
			watchlist: 'nochange'
		} ).done( function () {
			mw.notify( 'Template Protetta aggiunto' );
		} ).fail( function ( error ) {
			console.log( 'Errore API', error );
			mw.notify( 'Errore all\'aggiunta del template Protetta' );
		} );
	}

	$( function () {
		if ( mw.config.get( 'wgNamespaceNumber' ) === 0 && id !== 0 &&
			 ( mw.config.get( 'wgRestrictionEdit' ).length ||
			 mw.config.get( 'wgRestrictionMove' ).length ) &&
			 !$( '#mw-indicator-protedit, #mw-indicator-prot_move' ).length ) {
			api = new mw.Api();
			checkLastProtection( addPadlocks );
		}
	} );
}( mediaWiki, jQuery ) );