User:Bene*/iwconflict.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// <nowiki>
( function ( mw, $ )
{
	var i18n = {
		'en': {
			'name': 'Interwiki conflict',
			'description': 'Report an interwiki conflict at [[Wikidata:Interwiki conflicts]].',
			'title': 'Title',
			'items': 'Items',
			'comment': 'Comment',
			'report': 'Report',
			'close': 'Close'
		},
		'de': {
			'name': 'Interwiki-Konflikt',
			'description': 'Melde einen Interwiki-konflikt auf [[Wikidata:Interwiki conflicts]].',
			'title': 'Titel',
			'items': 'Datenobjekte',
			'comment': 'Kommentar',
			'report': 'Melden',
			'close': 'Schließen'
		}
	};
 
	var lang = mw.config.get( 'wgUserLanguage' );
 
	if( !( lang in i18n ) ) {
		lang = 'en';
	}
	
	/**
	 * Returns the localized value of a message. Default lang is en.
	 */
	function i18nVal( key ) {
		if( i18n[ lang ][ key ] ) {
			return getLinks( i18n[ lang ][ key ] );
		}
		else if( key in i18n[ 'en' ] ) {
			return getLinks( i18n[ 'en' ][ key ] );
		}
		else {
			return '[' + key + ']';
		}
	}
	
	/**
	 * Returns the localized value of a message and replaces all keys with the given params (in format $1, $2, ...). Default lang is en.
	 */
	function i18nValP( key, params ) {
		var val = i18nVal( key );
		for( var i = 0; i < params.length; i++ ) {
			val = val.replace( '$' + ( i + 1 ), params[ i ] );
		}
		return val;
	}
	
	/**
	 * Returns the text where all wikilinks are replaced by html links.
	 */
	function getLinks( value ) {
		return value.replace( /\[\[([^\]]*)\]\]/g, '<a href="/wiki/$1">$1</a>' );
	}
	
	/**
	 * Initialises the dialog and the link. Called on ready.
	 */
	function init() {
		mw.loader.using( [ 'jquery.ui' ], function () {
			var dialog = '<style>#iwconflict label { width:75px; float: left; text-align: right; padding-top: 0.3em; margin-right: 1.5em; } #iwconflict input { display: block; } #iwconflict a { color: #007EDF; }</style>';
			dialog += '<div id="iwconflict"><fieldset id="iwconflict-form"><legend>' + i18nVal( 'name' ) + '</legend>';
			dialog += '<p><label for="iwconflict-title">' + i18nVal( 'title' ) + ': </label><input type="text" id="iwconflict-title"></p>';
			dialog += '<p><label for="iwconflict-items">' + i18nVal( 'items' ) + ': </label><input type="text" id="iwconflict-items"></p>';
			dialog += '<p><label for="iwconflict-comment">' + i18nVal( 'comment' ) + ': </label><input type="text" id="iwconflict-comment"></p>';
			dialog += '</fieldset>';
			dialog += '<p>' + i18nVal( 'description' ) + '</p></div>';
			var dialogContent = $( dialog ).appendTo( '#content' );
			dialogContent.dialog( {
				title: '<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/1e/Symbol_plain_red_3.svg/200px-Symbol_plain_red_3.svg.png" width="20" alt="!" title="!" style="margin-right: 1.5em;">' + i18nVal( 'name' ),
				autoOpen: false,
				modal: true,
				width: 500,
				buttons: [ {
					id: 'iwconflict-button-report',
					text: i18nVal( 'report' ),
					click: function() {
						$.createSpinner( {
							size: 'large',
							type: 'block',
							id: 'iwconflict-spinner'
						} ).appendTo( '#iwconflict' );
						report( $( '#iwconflict-title' ).val(), $( '#iwconflict-items' ).val(), $( '#iwconflict-comment' ).val() );
					}
				}, {
					id: 'iwconflict-button-close',
					text: i18nVal( 'close' ),
					click: function() {
						$( '#iwconflict' ).dialog( 'close' );
					}
				} ]
			} );
			
			var portletLink = mw.util.addPortletLink(
				'p-cactions',
				'#',
				i18nVal( 'name' ),
				'ca-iwconflict',
				i18nVal( 'description' ),
				null,
				'#iwconflict-link'
			);
			$( portletLink ).click( show );
		} );
	}
	
	/**
	 * Shows the dialog.
	 */
	function show() {
		$( '#iwconflict' ).dialog( 'open' );
		return false;
	}
	
	/**
	 * Function called on success.
	 */
	function success() {
		$( '#iwconflict' ).dialog( 'close' );
		mw.notify( 'Conflict reported' );
		$( '#iwconflict-spinner' ).remove();
	}
	
	/**
	 * Function called on an error.
	 *
	 * @param error The error (error.info should contain the error info)
	 */
	function error( error ) {
		$( '#iwconflict' ).dialog( 'close' );
		mw.notify( 'Error: ' + error.info );
		$( '#iwconflict-spinner' ).remove();
	}
	
	/**
	 * Reports the interwikiconflict at Wikidata:Interwiki conflicts using {{interwiki conflict}}.
	 *
	 * @param title The title of the conflict (used for heading)
	 * @param items The items that are implied in the conflict (seperated by / )
	 * @param comment An additionally comment
	 */
	function report( title, items, comment ) {
		var appendtext = '\n\n=== ' + title + ' ===\n\n{{interwiki conflict\n| items = ' + items + '\n| status = not resolved\n| discussion = ' + comment + ' --~~~~\n}}\n';
		$.ajax( {
			type: 'POST',
  			url: mw.util.wikiScript( 'api' ),
			data: {
				'format': 'json',
				'action': 'edit',
				'title': 'Wikidata:Interwiki conflicts/Unresolved/' + new Date().getFullYear(),
				'summary': 'added new conflict ([[User:Bene*/iwconflict.js|IW]])',
				'appendtext': appendtext,
				'token': mw.user.tokens.get( 'csrfToken' )
			},
			success: function( data ) {
				if( data.error && data.error.info ) {
					error( data.error );
				}
				else {
					success();
				}
			},
			error: function( jqXHR, textStatus, errorThrown ) {
				// TODO: manage possible errors
			}
		} );
	}
	
	$( document ).ready( init );
} ( mediaWiki, jQuery ) );
// </nowiki>