User:Gryllida/js/processWLinks-0.1.js

// /* Author : Svetlana Tkachenko svetlana@members.fsf.org Licence: GPLv3+ Description: [beta] adds '{{W Links' tab

1. First, it looks through for hard foreign links, like ..., and lists them to you. Clicking submit changes them to use the 'W' template and reloads the page.

2. If no hard foreign links are found, for each {{W link which has a local target you are presented with three options: - no change - change it to a local link, do not add category - change it to a local link, add category (if it does not exist already) Clicking submit makes the necessary modifications and reloads the page.

Version: 0.1 Release date: 2018-03-16

TODO: [ ] test

technical details of customizing the toolbar snippets of code for common additions to the toolbar
 * http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization
 * http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization/Library



mw.loader.using(['mediawiki.api'], function {	"use strict";	//     // Email     // var text = ; // XXX    var text = ;    var cats = [];    var gotAnswers = 0;	var api = new mw.Api;	var _saveText = function(summary, $button){		console.log('3');		api.postWithToken('edit',{ action: 'edit', title: mw.config.get( 'wgPageName' ), text: text, summary: summary + ' (assisted)' }).done(function (data){ console.log('4'); $button.val('Submitted'); location.reload(true); });	};	var _makeChanges = function{		$('#gryllida-process-w-links-submit-button').val('Submitting...');		var summary = 'updated wikilinks ';		var $localifications = $('.gryllida-w-localify:checked');		$localifications.each(function(i){ var $localification = $($localifications[i]); var name = $localification.attr('name'); console.log('To localify: ' + name); var re = new RegExp('{{W\\|'+name+'([^}]*)}}', 'gi'); text = text.replace(re, ''+name+'$1'); summary += name + ', '; });		console.log('1');		summary += 'and cats ';		var $categorizations = $('.gryllida-w-checkbox:checked');		$categorizations.each(function(i){ var $categorization = $($categorizations[i]); var name = $categorization.attr('name'); text += '\r\nCAT:' + name + ''; summary += name + ', '; });		/*summary += '; change to W ';		var $changetoWizations = $('.gryllida-w-changeToW:checked');		$changetoWizations.each(function(i){ var $changetoWization = $($changetoWizations[i]); var name = $changetoWization.attr('name'); console.log('To changetoW: ' + name); var re = new RegExp('{{:w:\\'+name+'([^}]*)}}', 'gi'); text = text.replace(re, '{{W|'+name+'$1}}'); summary += name + ', '; });*/		console.log('2');		if($localifications.length + $categorizations.length > 0){			_saveText(summary, $('#gryllida-process-w-links-submit-button'));		}	};   var _showButtonIfICan=function($button){    	gotAnswers +=1;    	if(gotAnswers == 2){			$button.click(function{ _makeChanges; });   		$button.css('visibility', 'visible');    	}    };    var _loadPageContentsAndAssignButtonAction = function($button){	    // == Cats ==	    // https://en.wikipedia.org/w/api.php?action=query&titles=Albert%20Einstein&prop=categories    	api.get( { // Corresponds to "api.php?action=parse&page=test" action: 'query', titles: mw.config.get( 'wgPageName' ), prop: 'categories' } ).done( function ( data ) { var catObjects = data.query.pages[wgArticleId].categories; cats = catObjects.map(catObjects => catObjects.title); console.log(cats); var $boxes = $('.gryllida-w-checkbox'); $boxes.each(function(i){				var $box = $($boxes[i]);				var boxCat = 'Category:'+$box.attr('name');				console.log(boxCat);				if(cats.includes(boxCat)){					$box.css('visibility', 'hidden');				}			}); _showButtonIfICan($button); });		// == Text ==   	api.get( { // Corresponds to "api.php?action=parse&page=test" action: 'parse', page: mw.config.get( 'wgPageName' ), prop: 'wikitext' } ).done( function ( data ) { text = data.parse.wikitext['*']; console.log(text); _showButtonIfICan($button); // $button.css('visibiity', 'visible'); // XXX UNCOMMENT });   };	var _addRow = function(dest, text, $table, isHard){		//$table.append($('  ' + dest + '  No change  Localify  Add category  '));		var $tr = $('  ');		// title		var $td1 = $('  ', {text: dest});$tr.append($td1); // 1		// no change		var $r1 = $(' ', {type: 'radio', name: dest, value: 'nochange', 'checked': 'true'});		var $td2 = $('  ').append($r1);$tr.append($td2); // 2		/*// change to use W (if isHard is set)		var $r_w = $(' ', {type: 'radio', name: dest, value: 'changeToW', class: 'gryllida-w-changeToW'});		var $td_w = $('  ');$tr.append($td_w);		if(isHard){$td_w.append($r_w)}*/		// localify		var $r2 = $(' ', {type: 'radio', name: dest, value: 'localify', class: 'gryllida-w-localify'});		var $td3 = $('  ').append($r2);$tr.append($td3);		// add cat		var $chBox = $(' ', {type: 'checkbox', name: dest, value: 'addcat', class: 'gryllida-w-checkbox'});		var $td4 = $('  ').append($chBox); // glue $tr.append($td4); $table.append($tr); };	var _processWLinks= function{ // First, it looks through for hard foreign links, like ..., and changes them to use 'W' template. var $hardForeignLinks = $('p > a.extiw'); if($hardForeignLinks.length>0){ var $ul = $(''); var namesToWify = []; $('#content').prepend($ul); $hardForeignLinks.each(function(i){					var $a = $($hardForeignLinks[i]);					var dest = $a.attr('title').substr(2)					var text = $a.text;					$ul.append($('', {text: dest}));					namesToWify.push(dest);				}); var $buttonW = $(' ', {					type: 'button', 					id: 'gryllida-change-to-w-links-submit-button', 					value: 'Change to W'}); $buttonW.click(function{					$buttonW.val('Getting page content...');					// get page content					api.get( { // Corresponds to "api.php?action=parse&page=test" action: 'parse', page: mw.config.get( 'wgPageName' ), prop: 'wikitext' } ).done( function ( data ) { $buttonW.val('Processing page content...'); text = data.parse.wikitext['*']; console.log(text); // edit $buttonW.val('Replacing...'); $(namesToWify).each(function(i){							var nameToWify = namesToWify[i];							console.log('To localify: ' + nameToWify);							var re = new RegExp('\\[\\[\\:w\\:'+nameToWify+'([^\\]]*)\\]\\]', 'gi');							text = text.replace(re, '{{W|'+nameToWify+'$1}}');						}); $buttonW.val('Saving...'); _saveText('changed hard foreign links to use W', $buttonW); });				});				$ul.after($buttonW); } else { var $table = $(' ', {id: 'gryllida-process-w-links-table', 'class': 'wikitable'}); $table.append($(' Title  No change  Localify  Add category  ')); var $spans = $('.interwiki-link-local'); $spans.each(function(i){					var $span = $($spans[i]);					var $a = $span.find('a');					var dest = $a.attr('title');					var text = $a.text;					console.log('Adding row: '+dest);					_addRow(dest, text, $table, 0);				}); $('#content').prepend($table); var $button = $(' ', {					type: 'button', 					id: 'gryllida-process-w-links-submit-button', 					value: 'Submit'}); $button.css('visibility', 'hidden'); $table.after($button); _loadPageContentsAndAssignButtonAction($button); }	};	var link = mw.util.addPortletLink(mw.config.get('skin') === 'vector' ? 'p-views' : 'p-cactions',		'javascript:void(0);', '{{W'+ '}} Links', 'p-gryllida-processWLinks', 'Processes W links in current page', '6'	); link.addEventListener('click', _processWLinks); /*var customizeToolbar = function { $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {			'section': 'main',			'group': 'insert',			'tools': {				'fixWLinks': {					label: 'Fix W links',					type: 'button',					icon: '//upload.wikimedia.org/wikipedia/commons/thumb/3/35/Wikipedia-W-bold-in-square-Clean.svg/23x-Wikipedia-W-bold-in-square-Clean.svg.png',					'action': {				       'type': 'callback',						execute: function(context){							_processWLinks;						}					},				}			}		}); };	// Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) { mw.loader.using( 'user.options' ).then( function {			// This can be the string "0" if the user disabled the preference (T54542)			if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {				$.when( mw.loader.using( 'ext.wikiEditor' ), $.ready ).then( customizeToolbar );			}		} ); }*/ });

//