MediaWiki:Gadget-blockNotificationsButton.js

De Wikinoticias, la fuente libre de noticias

Nota: Después de guardar, debes recargar la caché de tu navegador para ver los cambios:

  • Mozilla: Pulsa Recargar (o Ctrl-R)
  • Internet Explorer / Opera: Ctrl-F5
  • Safari: Cmd-R
  • Konqueror Ctrl-R.
/** 
* BlockNotificationsButton - Añade un botón que permite notificar a un usuario que ha sido bloqueado
* Gadget creado originalmente por !Silent de la Wikipedia en Portugués y adaptado a Wikinoticias en Español por AlvaroMolina
**/

/* global jQuery, mediaWiki */
/* jshint laxbreak:true */

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

mw.messages.set( {
	// General
	'bbn-buttonText': 'Enviar una notificación del bloqueo',
	'bbn-checkingBlockRegister': 'Consultando los registros de bloqueo del usuario...',
	'bbn-editUserDiscussion': 'Editando la página de discusión del usuario...',
	'bbn-sectionTitle': 'Notificación de Bloqueo',
	'bbn-summarySufix': ' (Usando [[MediaWiki:Gadget-blockNotificationsButton.js|BlockNotificationsButton]])',
	'bbn-success': 'Notificación de bloqueo enviada con éxito (<a href="$1#Notificación_de_Bloqueo">Ver Notificación</a>)',
	'bbn-successBlock': 'El bloqueo se ha realizado con éxito',

	// Errors
	'bbn-apiError': 'Error: La API devolvió el código de error "$1": $2',
	'bbn-unknownError': 'Error: La API devolvió un resultado desconocido',
	'bbn-requestFail': 'Error: La solicitud ha fallado',
} );

/**
 * @class bbn
 */
var bbn = {};

/**
 * Mensajes
 * @param {string} name Nombre del mensaje
 * @param {string|number} [$N] Parámetros dinámicos para el mensaje (i.e. the values for $1, $2, etc)
 * @see [[mw:ResourceLoader/Default_modules#mediaWiki.message]]
 * @return {string}
 */
bbn.message = function ( /*name[, $1[, $2[, ... ]]]*/ ) {
	return mw.message.apply( this, arguments ).plain();
};

/**
 * Traducción al Español de la duración de los bloqueos
 * @param {string} duration Duración de los bloqueos
 * @return {string}
 */
bbn.translateDurationPT = function ( duration ) {
	var translation;

	duration = duration.split( ' ' );

	if ( duration[ 0 ] === 'infinite' ) {
		return 'para siempre';
	}

	translation = {
		'minute': 'minuto',
		'hour': 'hora',
		'day': 'día',
		'week': 'semana',
		'month': 'mes',
		'year': 'año'
	};

	if ( duration[ 0 ] !== '1' ) {
		translation.month = 'meses';
	}

	return duration[ 0 ] + ' '
		+ translation[ duration[ 1 ].replace( /s$/, '' ) ]
		+ ( ( translation.month === 'meses' && duration[ 1 ].indexOf( 'month' ) === -1 ) ? 's' : '' );
};

/**
 * Send the notify
 */
bbn.sendNotify = function () {
	var logevents,
		userNameBlocked = $( '#mw-content-text' ).find( 'a' ).html();

	mw.notify( bbn.message( 'bbn-checkingBlockRegister' ) );

	$.getJSON( mw.util.wikiScript( 'api' ), {
		action: 'query',
		list: 'logevents',
		format: 'json',
		leprop: 'title|user|timestamp|details|comment',
		lelimit: '1',
		leuser: mw.config.get( 'wgUserName' ),
		letitle: 'User:' + userNameBlocked
	} ).done( function ( data ) {
		logevents = data.query.logevents[ 0 ];

		mw.notify( bbn.message( 'bbn-editUserDiscussion' ) );

		( new mw.Api() ).editPage( {
			title: 'User talk:' + userNameBlocked,
			section: 'new',
			watchlist: 'preferences',
			sectiontitle: bbn.message( 'bbn-sectionTitle' ),
			text: '{{Subst:Aviso bloqueo'
				+ '|1=' + bbn.translateDurationPT( logevents.params.duration )
				+ '.}' + '} ~~' + '~~',
			summary: bbn.message( 'bbn-sectionTitle' ) + bbn.message( 'bbn-summarySufix' ),
			done: {
				success: function () {
					mw.notify( $.parseHTML( bbn.message( 'bbn-success', mw.util.getUrl( 'User talk:' + userNameBlocked ) ) ) );
				},
				apiError: function ( data ) {
					mw.notify( bbn.message( 'bbn-apiError', data.code, data.info ) );
					$( '#bbn-sendMsg' ).attr( 'disabled', 'false' );
				},
				unknownError: function () {
					mw.notify( bbn.message( 'bbn-unknownError' ) );
					$( '#bbn-sendMsg' ).attr( 'disabled', 'false' );
				}
			}
		} ).fail( function () {
			mw.notify( bbn.message( 'bbn-requestFail' ) );
		} );
	} );
};

/**
 * Ejecución del gadget
 */
bbn.run = function () {
	if ( !$( '.mw-htmlform-submit' ).length ) {
		$( '#mw-content-text' ).append(
			$( '<input type="button" id="bbn-sendMsg" value="' + bbn.message( 'bbn-buttonText' ) + '" />' ).on( 'click', function () {
				bbn.sendNotify();
				$( this ).attr( 'disabled', 'true' );
			} )
		);
	}
};

if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Block' ) {
	$( bbn.run );
}

}( mediaWiki, jQuery ) );