MediaWiki:Gadget-autopadlock.js: differenze tra le versioni
m (estraggo costante con il tempo, tante volte qualcuno volesse cambiarla in futuro) |
m (una versione importata) |
||
(Nessuna differenza)
|
Versione attuale delle 22:40, 30 ago 2020
/**
* 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 ) );