User:Gryllida/js/plagiarismcheck.js

Note: After saving, you may have to bypass your browser's cache to see the changes. Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac); IE: hold Ctrl while clicking Refresh, or press Ctrl-F5; Konqueror: simply click the Reload button, or press F5; Opera users may need to completely clear their cache in Tools→Preferences. — More skins

/*
Author : Svetlana Tkachenko svetlana@members.fsf.org
Licence: GPLv3+
Description: [beta] provides links to dupdet results against the external links present in the article
TODO: 
  [ ] add 'check plagiarism' links next to the sources, IF the article is not yet published
See also <https://en.wikinews.org/wiki/User:Gryllida/Tasks>.
*/ 
mw.loader.using(['mediawiki.api', 'jquery.ui'], function () {
	// 'jquery.ui',
	"use strict";
	
	/* 
	== Quit if this is not main namespace ==
	 */
	if (wgNamespaceNumber < 0)
		return;
	
	
	/*
	== Code ==
	*/
	var api = new mw.Api();
	
	/*
	== Show a dialog with links to dupdet ==
	This parses article contents, looks for URLs in the {{source}}
	templates, and provides a list of links to check the current article
	plagiarism score against these source URLs.
	*/
	function _showDupDetLinks(){
		/*
		== Show dialog ==
		This is stolen from 
		https://www.mediawiki.org/wiki/MediaWiki:Tutorial-QuickRC.js
		from
		https://www.mediawiki.org/wiki/Gadget_kitchen
		*/
		var $dialog = $( '<div></div>' )
			.html(
				'<p>Loading...</p>'
			)
			.dialog({
				autoOpen: true,
				title: 'Open DupDet - plagiarism check against external links.',
				width: '70%',
				position: ['top',30] ,
				modal: true
		});
		/*
		== Parse the page and find external URLs provided as {{source}} params ==
		This is Wikinews-specific. {{source|url=...|title=...}}
		Avoid using regex for this task.
		*/
		// Construct an empty list
		var list = $('<ul></ul>');
		// Get current page name and URL
		var title = mw.config.get( 'wgPageName' );
		var url1 = 'http:' + mw.config.get ('wgServer') + wgArticlePath.replace('$1', title);
		// Create an API object
		var api = new mw.Api();
		// Query the wiki API to parse the page
		api.get( { // Corresponds to "api.php?action=parse&page=test"
    		action: 'parse',
    		page: mw.config.get( 'wgPageName' )
		} ).done( function ( data ) {
			// Get all external links in the article
		    var elinks = data.parse.externallinks;
		    for(var i=0; i<elinks.length;i++){
		    	var elink = elinks[i];
		    	// Create the relevant dupdet URL
		    	var ddlink = 'https://tools.wmflabs.org/dupdet/compare.php?url1=';
		    	var ddlink = ddlink + url1 + '&url2=';
		    	var ddlink = ddlink + elink + '&minwords=2&minchars=13';
		    	// Add the dupdet URL to the list
		    	var li = $('<li style="line-height:2.5;">').append(
		    		$("<a />", {    href : ddlink, text: elink, target: '_blank'})
		    	);
		    	list.append(li);
		    }
		} );
		// Feedback links
		var a1 = $('<a>', {
			href: 'http://en.wikinews.org/wiki/User talk:Gryllida/js/plagiarismcheck.js',
			text: 'This script feedback'
		});
		var a2 = $('<a>', {
			href: 'https://en.wikinews.org/wiki/User:Gryllida/Tasks#Task_1:_Check_plagiarism',
			text: 'This script design'
		});
		var a3 = $('<a>', {
			href: 'https://en.wikinews.org/w/index.php?title=Wikinews:Water_cooler/technical&action=edit&section=new',
			text: 'Ask a technical question'
		});
		var li = $('<li style="line-height:2.5;">');
		li.append('[');
		li.append(a1);li.append(', ');
		li.append(a2);li.append(', ');
		li.append(a3);
		li.append(']');
		list.append(li);
		$dialog.html(list);
	}
	
	var link = mw.util.addPortletLink(mw.config.get('skin') === 'vector' ? 'p-views' : 'p-cactions',
		'javascript:void(0);', 'Open DupDet', 'p-gryllida-plagiarismcheck', 'Check plagiarism in dupdet', '6'
	);
	
	link.addEventListener('click', _showDupDetLinks);
	
});