משתמש:ערן/replaceToCommons.js

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

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

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
/* Replace image uses with commons copy or similar file */
$(function(){
if (mw.config.get('wgCanonicalNamespace') !== 'File') return; // valid only in file namespace
function replaceFile(newName) {
	var rgxFrom = new RegExp(/.+?:(.+)/.exec(mw.config.get('wgPageName'))[1].replace(/_/g, '[ _]')),
		process = new $.Deferred(),
		api = new mw.Api(),
	 	requiredReplacements = $('.mw-imagepage-linkstoimage a').length;

	mw.notify('מחליף ל' + newName);
	process.progress(function(){
		requiredReplacements--; 
		if(requiredReplacements === 0) {
			mw.notify('כל השימושים בקובץ הוסרו');
			if ( mw.config.get('wgUserGroups').indexOf('sysop') > -1)
			{
				api.postWithToken('edit', { action: 'delete', title: mw.config.get('wgPageName'), reason: '[[וק:וש|הקובץ נמצא בוויקישיתוף]]: [[:File:' + newName + ']]' });
			}
			else
			{
				api.postWithToken('edit', {
					action: 'edit',
					title: 'ויקיפדיה:בקשות ממפעילים', 
					summary: '[[:File:' + mw.config.get('wgPageName') + ']] - [[וק:וש|הקובץ נמצא בוויקישיתוף]]: [[:File:' + newName + ']]' ,
					appendtext: '\n*[[:File:' + mw.config.get('wgPageName') + ']] - קיים קובץ זהה בוויקישיתוף: [[:File:' + newName + ']] ~~'+'~~'
				}).done(function(d) {
					if (d && d.edit && d.edit.result == 'Success') mw.notify('נוספה בקשה בבקשות ממפעילים');
				});
			}
		}
	})
	$('.mw-imagepage-linkstoimage a').each(function(){
		var page = $(this).attr('title');
		api.get({action:'parse', page: page, prop: 'wikitext'}).done(function(d){
			var oldText = d.parse.wikitext['*'];
			var newText = d.parse.wikitext['*'].replace(rgxFrom, newName);
			if(oldText!=newText) {
				api.postWithToken('edit', {action:'edit', summary: '[['+mw.config.get('wgPageName') + ']] => [[:קובץ:' + newName +']]', text: newText, title: page }).done(function(){
				mw.notify('הקובץ הוחלף ב'+page);
				process.notify();
				});
			} else {
				//process.notify();
				mw.notify('הקובץ לא הוחלף ב'+page);
			}
		});
	});
}

function safeReplaceFile(filename) {
	var api = new mw.Api();
	api.get({action: 'query', titles: 'File:' + filename, prop: 'imageinfo', indexpageids: 1}).done((d) => {
		if ( d.query.pages["-1"] && d.query.pages["-1"].imagerepository === 'shared' ) {
			replaceFile(filename);
		} else if ( d.query.pageids.length === 1 && d.query.pageids[0] != -1) {
			mw.notify('קיים עותק מקומי של הקובץ');
		}
		else {
			mw.notify('הקובץ לא קיים. נא לבדוק ידנית.');
		}
	});
}

$(mw.util.addPortletLink('p-cactions','#','החלפה מוויקישיתוף','#ca-js-moevToCommons')).click(function(){
	var api = new mw.Api(), newName;
	if ($('.mw-imagepage-duplicates a').length === 1 ) {
		newName = /.+?:(.+)/.exec($('.mw-imagepage-duplicates a').text())[1].replace('_', ' ');
		replaceFile(newName);
	} else {
		newName = prompt("שם התמונה להחלפה");
		newName = newName.replace(/File:|קובץ:|תמונה:/,'').replace('_', ' ');
		safeReplaceFile(newName);
	}
});
});