מדיה ויקי:סקריפטים/78.js

מתוך ויקיפדיה, האנציקלופדיה החופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
/*

סקריפט לטיפול מהיר בערכים חדשים שנוצרו במרחב הערכים ואינם ראויים לו

לסקריפט שני יישומים:

* העברה מהירה לדף טיוטה של יוצר הערך: לאחר הזנת הסיבה להעברה, הדף יועבר למרחב טיוטה וינוטרלו הקטגוריות הקיימות בו. הסקריפט ישלח הודעה למשתמש על כך שהערך הועבר למרחב טיוטה וישלח בקשה למפעילים למחוק את הדף המקורי (אם המשתמש הוא מפעיל מערכת, הפעולה האחרונה לא תתבצע)

* בקשת מחיקה מהירה: (מיועד בעיקר לערכי הבל או ערכים לא אנציקלופדיים שנכתבו על ידי אנונימים) - שליחת בקשת מחיקה מהירה לדף בקשות ממפעילים, ניתן להזין סיבה למחיקה וישנן כמה הצעות נפוצות לבחירה.

נכתב על ידי [[משתמש:קובי כרמל]]

version 4.3
*/

var pagesCare = {

	/** VARIABLES **/

	currentLocation: 'מדיה ויקי:סקריפטים/78.js',

	panel: $('<div>', {id: 'pc-panel'}).append(
		$('<div>', {id: 'pc-titlebar'}).append(
			$('<span>', {id: 'pc-close', 'class': 'ui-icon ui-icon-closethick', title: 'סגירה'}),
			$('<div>', {id: 'pc-caption'})
		),
		$('<span>', {id: 'pc-creator'}),
		$('<form>', {id: 'pc-form', onsubmit: 'return false'}).append(
			$('<div>', {'class': 'pc-dialog'}).text('מה לעשות עם הדף הזה?'),
			$('<div>', {'class': 'pc-action'}).append(
				$('<input>', {name: 'pc-type', id: 'pca-move', 'class': 'pca-opt', type: 'radio', value: 1}),
				$('<label>', {'for': 'pca-move'}).text('להעביר אותו לטיוטה')
			),
			$('<dd>', {id: 'pc-custom'}).append(
				$('<div>', {'class': 'pc-dialog'}).text('איזו הודעה יקבל המשתמש?'),
				$('<div>', {'class': 'pc-action'}).append(
					$('<input>', {name: 'pc-msg', id: 'pcm-default', type: 'radio', value: 1}),
					$('<label>', {'for': 'pcm-default'}).text('הודעת ברירת המחדל')
				),
				$('<div>', {'class': 'pc-action'}).append(
					$('<input>', {name: 'pc-msg', id: 'pcm-custom', type: 'radio', value: 2, checked: true}),
					$('<label>', {'for': 'pcm-custom'}).text('הודעה מותאמת אישית')
				),
				$('<textarea>', {placeholder: 'הודעה (ללא חתימה)'}),
				$('<div>', {'class': 'pc-dialog'}).text('להעביר גם את דף השיחה?'),
				$('<div>', {'class': 'pc-action'}).append(
					$('<input>', {name: 'pc-move-talk', id: 'pcm-mty', type: 'radio', checked: true}),
					$('<label>', {'for': 'pcm-mty'}).text('כן')
				),
				$('<div>', {'class': 'pc-action'}).append(
					$('<input>', {name: 'pc-move-talk', id: 'pcm-mtn', type: 'radio'}),
					$('<label>', {'for': 'pcm-mtn'}).text('לא')
				)
			),
			$('<div>', {'class': 'pc-action'}).append(
				$('<input>', {name: 'pc-type', id: 'pca-delete', 'class': 'pca-opt', type: 'radio', value: 2}),
				$('<label>', {'for': 'pca-delete'}).text('למחוק אותו')
			),
			$('<div>', {id: 'pc-reason'}).append(
				$('<div>', {'class': 'pc-dialog'}).text('למה?'),
				$('<input>'),
				$('<button>').text('אישור'),
				$('<div>', {id: 'pc-status'})
			)
		)
	),

	token: mw.user.tokens.get('csrfToken'),

	/** METHODS **/

	addEvents: function(){
	
		$('.pc-icon').click(pagesCare.showPanel);

		$('#pc-close').click(pagesCare.destroy);

		$('.pca-opt').on('click', function(){

			if(! this.checked)
				return;

			pagesCare.action = this.value;
			
			if($.client.profile().name == 'msie')
				$('#pc-reason').show();
			else
				$('#pc-reason').slideDown(function(){
					$(this).children('input').focus();
				});

			$('#pc-custom').toggle(this.value == 1);

		});

		$('#pc-custom input').on('click', function(){

			if(! this.checked)
				return;

			$('#pc-custom textarea').toggle(this.value == 2);

			if($.client.profile().name != 'msie')
				$('#pc-custom textarea').focus();
		});

		$('#pc-reason input').on({
			keyup: function(){
				$(this).next().toggle(!! this.value);
			},
			change: function(){
				pagesCare.reason = this.value;
			}
		});

		$('#pc-custom textarea').on('change', function(){
			pagesCare.userMsg = this.value;
		});

		$('#pc-reason button').click(pagesCare.run);
	},

	build: function(){
		var li = $('.mw-newpages-time, .newpage, .mw-history-histlinks:last:not(:has(>span:last>a))').parents('li, table'),
			link = $('<span>', {
				'class': 'pc-icon ui-icon ui-icon-transferthick-e-w script78',
				title: 'טיפול מהיר'
			});

		li.before(link);
		pagesCare.panel.hide().appendTo('#mw-content-text');
		pagesCare.addEvents();
	},

	deleteQuery: function(){

		pagesCare.sysopQuery(function(){
			pagesCare.done('בקשתך נשלחה בהצלחה!');
		});

	},

	destroy: function(){
		$('#pc-panel, #pc-reason, #pc-reason button, #pc-status, #pc-custom').hide();

		$('#pc-form')[0].reset();

		pagesCare.pageName = null;
		pagesCare.action = null;
		pagesCare.reason = null;
		pagesCare.userMsg = "";
	},

	done: function(msg){
		mw.notify(msg);
		pagesCare.destroy();
	},

	error: function(msg){
		pagesCare.updateStatus(msg, 'red', true);
	},

	move: function(){

		var self = this,
			oldPage = pagesCare.pageName,
			user = pagesCare.user,
			newPage = "טיוטה:" + oldPage,
			draftTalk = "שיחת טיוטה:" + oldPage;

		this.error = function(info){
			if(!mw.addDialog) {
				mw.notify('error: [[MediaWiki:סקריפטים/78.js]]');
				return;
			}
			mw.addDialog({
				title: 'ההעברה נכשלה',
				content: [
					$('<p>').text('אירעה שגיאה במהלך ביצוע ההעברה.'),
					$('<h6>').text('פרטי השגיאה:'),
					$('<p>').css('direction', 'ltr').text(info)
				],
				buttons: {אישור: function(){
					$('#mweDialog').dialog('close');
					mw.notify('ההעברה נכשלה');
				}}
			});

			$('.ui-dialog .ui-dialog-buttonpane')
				.css('text-align', 'center')
				.children()
				.css('float', 'none');

			pagesCare.error('אירעה שגיאה');
		};

		this.notifyUser = function(){
			var 
				header = '\n==' + oldPage + '==',
				template = '{{הועבר לטיוטה|' + oldPage + '}}', 
				sig = '~~' + '~~',
				explain = 	'<p style="text-align:center;font-weight:bold;">הסבר להעברת הדף, והבעיות שדורשות תיקון</p>',
				transclude = '{{#lsth:' + draftTalk + '|הערך הועבר לטיוטה}}',
				param = {
					action: 'edit',
					appendtext: [header, template, sig, explain, transclude].join('\n\n'),
					format: 'json',
					summary: 'הועבר לטיוטה',
					title: 'שיחת משתמש:' + user,
					token: pagesCare.token
				};

			pagesCare.updateStatus('מודיע למשתמש');

			$.post('/w/api.php', param, function(){
				if($.inArray('sysop', mw.config.get('wgUserGroups')) + 1) // user with sysop rights
					return pagesCare.done('הערך הועבר בהצלחה!');

				pagesCare.sysopQuery(function(){
					pagesCare.done('הערך הועבר בהצלחה!');
				}, 'הועבר למרחב טיוטה', true);
			});
		};

		this.parseDraft = function(){

			var param = {
				action: 'parse',
				format: 'json',
				page: newPage,
				prop: 'wikitext'
			};

			pagesCare.updateStatus('קובע תצורת טיוטה');

			$.post('/w/api.php', param, function(res){
				var parseCat = res.parse.wikitext['*'].replace(/\[\[\s*קטגוריה:\s*/g, '[[:קטגוריה:');
				var content = parseCat;
				var param = {
					action: 'edit',
					summary: '[[' + pagesCare.currentLocation + '|סקריפט טיפול מהיר]]: קביעת תצורת טיוטה',
					text: content,
					title: newPage,
					token:  pagesCare.token
				};

				$.post('/w/api.php', param, function(){
					self.notifyUser();
				});

			}, 'json');

		};

		this.send = function(){

			if(! pagesCare.userMsg)
				return pagesCare.error('יש להזין הודעה למשתמש');

			var param = {
				action: 'move',
				format: 'json',
				from: oldPage,
				to: newPage,
				token:  pagesCare.token,
				reason: '[[' + pagesCare.currentLocation + '|סקריפט טיפול מהיר]]: ' + pagesCare.reason
			};

			if($('#pcm-mty').is(':checked'))
				param.movetalk = 1;

			if(mw.config.get('wgUserGroups').includes('sysop'))
				param.noredirect = 1;
				
			pagesCare.updateStatus('מעביר');

			$.post('/w/api.php', param, function(res){
				if(res.error)
					return self.error(res.error.info);
				self.parseDraft();
			}, 'json');
			
			var draftTalkPageExplain = '\n== הערך הועבר לטיוטה ==\n' +
										'=== הסבר ===\n' +
										pagesCare.userMsg + '\n~~' + '~~\n';
			mw.loader.using('mediawiki.api')
			.then(function() {
				new mw.Api().postWithEditToken({
					action: 'edit',
					title: draftTalk,
					appendtext: draftTalkPageExplain,
					watchlist: 'watch'
				});
			});
		};

	},

	showPanel: function(){
		var top = $(this).offset().top - 135,
			li = $(this).next(),
			title = (
                (!mw.config.get('wgCanonicalSpecialPageName') && mw.config.get('wgAction')=='history') ?
                ($('<a>').text(mw.config.get('wgTitle')).attr('href', $('#ca-nstab-main a').attr('href'))):
                (li.find('.mw-title a, .mw-contributions-title, .mw-newpages-pagename'))
            ),
			userLink = (mw.config.get('wgCanonicalSpecialPageName')=='Contributions') ? $('.mw-contributions-user-tools a').first() : li.find('.mw-userlink').last(),
			user = userLink.text(),
			gender = userLink.attr('title') && userLink.attr('title').split(':')[0] == 'משתמשת' ? 'יוצרת' : 'יוצר';
		pagesCare.destroy();
		pagesCare.pageName = title.text();
		pagesCare.user = user;

		$('#pc-caption').html(title.clone());
		$('#pc-creator').text(gender + ': ').append(userLink.clone());
		$('#pc-custom textarea').toggle(true);
		pagesCare.panel.show().css('top', top);
	},

	sysopQuery: function(complete, reason, isRedirect){

		var article = pagesCare.pageName,
			hrefType = isRedirect ? '{{בקשת מחיקה|' + article + '|' + (reason || pagesCare.reason) + '|הפניה=כן' + '}}' : '{{בקשת מחיקה|' + article + '|' + (reason || pagesCare.reason) + '}}';

		var param = {
			action : 'edit',
			appendtext : '\n* ' + hrefType + ' ~~' + '~~',
			section : 1,
			summary : '/* בקשות מחיקה */ [[' + article + ']]',
			title : 'ויקיפדיה:בקשות ממפעילים',
			token : pagesCare.token
		};

		pagesCare.updateStatus('יוצר בקשת מחיקה');

		$.post('/w/api.php', param, complete);
	},

	run: function(){

		if(! pagesCare.reason)
			return pagesCare.error('יש לציין סיבה');

		switch(pagesCare.action){
			case '1': new pagesCare.move().send(); break;
			case '2': pagesCare.deleteQuery(); break;
			default: pagesCare.error('לא נבחרה פעולה');
		}
	},

	updateStatus: function(msg, color, stat){
		$('#pc-status').show().css('color', color || 'inherit').text(msg + (stat ? '' : '...'));
	}
};

$(function(){

	mw.loader.using('jquery.ui', pagesCare.build);
	mw.hook('wikipage.content').add(pagesCare.build);
	importStylesheet(pagesCare.currentLocation + '/style.css');
});