User:Syunsyunminmin/SReporter.js

From Meta, a Wikimedia project coordination wiki

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.
/********************************************************
 * Forked from [[MediaWiki:Gadget-RequestDeletion.js]]  *
 * Author: [[User:Bene*]]                               *
 * Fork: [[User:Syunsyunminmin]]                        *
 *******************************************************/

// <nowiki>
$(function() {
	mw.loader.using([ 'mediawiki.util', 'jquery.i18n', 'jquery.ui' ], function () {
		$.i18n({ locale: mw.config.get('wgUserLanguage') });
		$.i18n().load({
			en: {
				'sReporter-title': 'Report',
				'sReporter-description': 'Report the user to Administrators\' noticeboard.',
				'sReporter-reason': 'Please give the reason for report:',
				'sReporter-report': 'Report',
				'sReporter-error-notsupported': 'This wiki ($1) is not supported.',
				'sReporter-error-reason': 'You must give a reason!',
				'sReporter-error-requested': 'Already reported',
				'sReporter-error-api': 'An error occurred while reporting the user: $1',
				'sReporter-success': 'Request successful',
				'sReporter-close': 'Close',
				'sReporter-username': 'User name:'
			},
			ja: {
				'sReporter-title': '報告',
				'sReporter-description': '利用者を管理者伝言板に報告する。',
				'sReporter-reason': '報告理由を記入してください:',
				'sReporter-report': '報告',
				'sReporter-error-notsupported': 'このウィキ ($1) はサポートされていません。',
				'sReporter-error-reason': '報告理由が必要です!',
				'sReporter-error-requested': '既に報告済み',
				'sReporter-success': '報告完了',
				'sReporter-close': '中止',
				'sReporter-username': '利用者名:'
			}
		});

		/* wiki config */
		var wikiconfigs = {
			commonswiki: {
				ANVpage: "Commons:Administrators' noticeboard/Vandalism",
				ANtemp: 'Report vandal',
				regex: '==\\s*$1\\s*==|{{\\s*[Vv]andal\\s*\\|\\s*(1(\\s*)=|)\\s*$1\\s*}}',
				needSign: true
			},
			wikidatawiki: {
				ANVpage: "Wikidata:Administrators' noticeboard",
				ANtemp: 'VandalReport',
				regex: '==\\s*Report concerning User:$1\\s*==|{{\\s*[Vv]andal\\s*\\|\\s*(1(\\s*)=|)\\s*$1\\s*}}',
				needSign: false
			},
			metawiki: {
				ANVpage: 'Meta:Requests for help from a sysop or bureaucrat',
				ANtemp: 'VandalReport',
				regex: '==\\s*Report concerning User:$1\\s*==|{{\\s*[Vv]andal\\s*\\|\\s*(1(\\s*)=|)\\s*$1\\s*}}',
				needSign: false
			}
		};

		// if already requested
		var requested = false;

		/**
		 * Add link to top of page.
		 */
		$('<div>')
			.attr('id', 'reportuser')
			.append(
				$('<form>')
					.attr('id', 'reportuser-form')
					.submit(reportuser)
					.append(
						$('<fieldset>')
							.append(
								$('<legend>')
									.text($.i18n('sReporter-description'))
							) // </legend>
							.append(
								$('<p>')
									.append(
										$('<label>')
											.attr('for', 'requested-user')
											.text($.i18n('sReporter-username'))
									) // </label>
									.append(
										$('<input>')
											.attr({
												type: 'text',
												id: 'requested-user',
												value: mw.config.get('wgRelevantUserName') !== null ? mw.config.get('wgRelevantUserName') : ''
											})
									) // </input>
							) // </p>
							.append(
								$('<p>')
									.append(
										$('<label>')
											.attr('for', 'reportuser-reason')
											.text($.i18n('sReporter-reason'))
									) // </label>
									.append(
										$('<input>')
											.attr({
												type: 'text',
												id: 'reportuser-reason'
											})
									) // </input>
							) // </p>
					)
			) // </form>
			.append(
				$('<p>')
					.attr('id', 'reportuser-result')
			) // </p>
			.appendTo('#content')
			.dialog({
				title: $.i18n('sReporter-title'),
				autoOpen: false,
				modal: true,
				width: 500,
				buttons: [ {
					id: 'reportuser-button-request',
					text: $.i18n('sReporter-report'),
					click: reportuser
				}, {
					id: 'reportuser-button-close',
					text: $.i18n('sReporter-close'),
					click: function () {
						$('#reportuser').dialog('close');
					}
				}]
			});
		var portletLink = mw.util.addPortletLink(
			'p-cactions',
			'#',
			$.i18n('sReporter-title'),
			'ca-reportuser',
			$.i18n('sReporter-description'),
			null,
			'#ca-reportuser'
		);
		$(portletLink).click(function(e) {
			e.preventDefault();
			$('#reportuser-result').html('');
			$('#reportuser').dialog('open');
		});

		function reportuser(event) {
			event.preventDefault();
			if (typeof SReprterConfig !== 'undefined') {
				Object.assign(wikiconfigs, SReprterConfig);
			}
			if (!(mw.config.get('wgDBname') in wikiconfigs)) {
				showError('notsupported', mw.config.get('wgDBname'));
				return;
			}
			var username = $('#requested-user').val();
			var api = new mw.Api();
			api.get({
				action: 'query',
				prop: 'revisions',
				formatversion: 2,
				rvprop: 'content',
				rvslots: 'main',
				rvlimit: 1,
				titles: wikiconfigs[mw.config.get('wgDBname')].ANVpage
			}).then(function (data) {
				if (data.error) {
					return;
				}
				var regex = wikiconfigs[mw.config.get('wgDBname')].regex.replace(/$1/g, username);
				if (data.query.pages[0].revisions[0].slots.main.content.match(new RegExp(regex, 'g')) !== null) {
					requested = true;
				}
				var reason = $('#reportuser-reason').val();
				if (requested) {
					showError('requested');
					requested = false; // can retry
				} else if (reason === '') {
					showError('reason');
				} else {
					addRequest(
						username,
						function () {
							$('#reportuser').dialog('close');
							mw.notify($.i18n('sReporter-success'));
						},
						reason
					);
				}
			});
		}

		function showError(err, p1) {
			$('#reportuser-result').html(
				$('<p>')
					.attr('class', 'error')
					.text($.i18n('sReporter-error-' + err, p1))
			);
		}

		/**
		 * Add new error request.
		 * @param name to be report
		 * @param success function called on success
		 * @param reason reason for report
		 */
		function addRequest(username, success, reason) {
			// removing unnecessary sign
			reason = reason.replace(/(.*)~~~~/g, '$1');
			// removing too too much spaces
			if (reason.charAt(0) === ' ') {
				reason = reason.substr(1);
			}

			var api = new mw.Api();

			var summary = 'Reporting [[Special:Contributions/' + username + '|' + username + ']]';
			summary += ' ([[:m:User:Syunsyunminmin/SReporter.js|SReporter]])';
			api.postWithEditToken({
				action: 'edit',
				title: wikiconfigs[mw.config.get('wgDBname')].ANVpage,
				watchlist: 'nochange',
				summary: summary,
				appendtext: '\n{{subst:' + wikiconfigs[mw.config.get('wgDBname')].ANtemp + '|1=' + username + '|2=' + reason + '}}' + (wikiconfigs[mw.config.get('wgDBname')].needSign ? '--~~~~' : '') + '\n'
			}).then(
				function (data) {
					if (data.error && data.error.info) {
						showError('api', data.error.info);
					} else {
						success();
					}
				},
				function (jqXHR, textStatus, errorThrown) {
					showError('api', textStatus);
				});
		}
	});
});
// </nowiki>