Jump to content

User:ערן/plagiabot.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//For easier tagging in User:EranBot/Copyright/rc
(function(){
if (!mw.config.get('wgPageName').startsWith(mw.config.get('plagiabotPage') || 'User:EranBot/Copyright/rc')) return;
	function reportStatus( status, $context ){
		var reportDiv = $context.parents('.plagiabot-entry');
		var diffHref = reportDiv.find('a[href^="//'+location.host+'/w/index.php?title="]:first').prop('href');
		var diffId = /diff=([0-9]+)/.exec(diffHref)[1];
		var api = new mw.Api();
		api.get({
			action:'query',
			prop:'revisions',
			titles: mw.config.get('wgPageName'),
			rvprop:'content',
		}).then(function(data){
			var rv;
			for(rv in data.query.pages)
			{
				var statusParam = 'status =';
				var origText = data.query.pages[rv].revisions[0]['*'];
				var diffIndex = origText.indexOf('diff = '+diffId);
				var statusLine = origText.indexOf(statusParam, diffIndex)
				var newText = origText.substr(0, statusLine+statusParam.length)+status+origText.substr(origText.indexOf('\n',statusLine));
				reportDiv.animate({opacity: 0.25}, 1000);
				api.postWithToken('edit', {
					action: 'edit', 
					title: mw.config.get('wgPageName'),
					summary: status,
					text: newText
				}).done(function(){
					reportDiv.hide(); //hide already reported
					mw.notify('New status saved.');
				});
			}
		});
	}
	
	function updateTagsView()
	{
		var selectedTags = $('.plagiabot-tag-checkbox:checked').map(function(){return this.value}).toArray();
		var selectedEntries = [], nonSelectedEntries = [];
		$('.plagiabot-entry').each(function(){
			var tags = $(this).find('.plagiabot-tag').text().split(';');
			var isSelected = false;
			for(var i=0; i < tags.length && !isSelected;i++) { isSelected = selectedTags.indexOf(tags[i]) != -1;  }
			if (isSelected) selectedEntries.push(this); else nonSelectedEntries.push(this);
		});
		$(selectedEntries).show()
		$('.plagiabot-entry:visible').prev('h2').show();
		$(nonSelectedEntries).hide();
		$('.plagiabot-entry:hidden').prev('h2').hide();
	}

	function addTagSelectors() {
		var currentTags = {}
		$('.plagiabot-tag').each(function(){
			var entryTags = $(this).text().split(';');
			for(var i=0;i<entryTags.length;i++){ currentTags[entryTags[i]] = 1;	}
		});
		
		var tagList = [];
		for(var tag in currentTags) { tagList.push(tag); }
		tagList = tagList.sort();
		
		var $tagSelection = $('<div></div>').css({'max-height':'400px', 'overflow':'auto', 'width': '80%', 'margin': 'auto'});
		for (var tagIndex=0;tagIndex<tagList.length;tagIndex++){
			var tagCheckbox = $('<input type="checkbox" class="plagiabot-tag-checkbox" name="plagiabot-tag'+tagIndex+'" id="plagiabot-tag'+tagIndex+'" checked>').click(updateTagsView).val(tagList[tagIndex]);
			var tagLabel = $('<label for="plagiabot-tag'+tagIndex+'"></label>').text(tagList[tagIndex]);
			$tagSelection.append($('<div class="mw-ui-checkbox"></div>').append(tagCheckbox).append(tagLabel).wrap('li'));
		}
		
		$('.plagiabot-entry:first').before($tagSelection);
		$tagSelection.makeCollapsible({collapsed:true});
		$tagSelection.before($('<div class="mw-ui-button">Select all</div>').click(function(){ $('.plagiabot-tag-checkbox').prop('checked', true);updateTagsView(); }));
		$tagSelection.before($('<div class="mw-ui-button">Unselect all</div>').click(function(){ $('.plagiabot-tag-checkbox').prop('checked', false);updateTagsView(); }));
	}
actionButtons = $('<div></div>').css('float','right');

reportOk = $('<div>FP<br><small>No copyvio</small></div>').addClass('mw-ui-button mw-ui-constructive').click(function(){
	reportStatus('FP', $(this));
});
reportCopyright = $('<div>TP<br><small>Copyvio</small></div>').addClass('mw-ui-button mw-ui-destructive').click(function(){
	reportStatus('TP', $(this));
})
actionButtons.append(reportOk)
actionButtons.append(reportCopyright)
$('.plagiabot-details').prepend(actionButtons);
mw.loader.using(['mediawiki.ui.checkbox', 'jquery.makeCollapsible'], addTagSelectors);
})();