User:Gryllida/js/processWLinks-0.1.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
// <nowiki>
/*
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 [[w:...]], 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
* http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization
technical details of customizing the toolbar
* http://www.mediawiki.org/wiki/Extension:WikiEditor/Toolbar customization/Library
snippets of code for common additions to the toolbar
*/
mw.loader.using(['mediawiki.api'], function () {
"use strict";
// <input type="radio" id="contactChoice1"
// name="contact" value="email">
// <label for="contactChoice1">Email</label>
// 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 + ' ([[User:Gryllida/js/processWLinks-0.1.js|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\n[[CAT:' + 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($('<tr><td>' + dest + '</td><td>No change</td><td>Localify</td><td>Add category</td></tr>'));
var $tr = $('<tr></tr>');
// title
var $td1 = $('<td></td>', {text: dest});$tr.append($td1); // 1
// no change
var $r1 = $('<input/>', {type: 'radio', name: dest, value: 'nochange', 'checked': 'true'});
var $td2 = $('<td></td>').append($r1);$tr.append($td2); // 2
/*// change to use W (if isHard is set)
var $r_w = $('<input/>', {type: 'radio', name: dest, value: 'changeToW', class: 'gryllida-w-changeToW'});
var $td_w = $('<td></td>');$tr.append($td_w);
if(isHard){$td_w.append($r_w)}*/
// localify
var $r2 = $('<input/>', {type: 'radio', name: dest, value: 'localify', class: 'gryllida-w-localify'});
var $td3 = $('<td></td>').append($r2);$tr.append($td3);
// add cat
var $chBox = $('<input/>', {type: 'checkbox', name: dest, value: 'addcat', class: 'gryllida-w-checkbox'});
var $td4 = $('<td></td>').append($chBox);
// glue
$tr.append($td4);
$table.append($tr);
};
var _processWLinks= function(){
// First, it looks through for hard foreign links, like [[w:...]], and changes them to use 'W' template.
var $hardForeignLinks = $('p > a.extiw');
if($hardForeignLinks.length>0){
var $ul = $('<ul></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($('<li></li>', {text: dest}));
namesToWify.push(dest);
});
var $buttonW = $('<input/>', {
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 = $('<table></table>', {id: 'gryllida-process-w-links-table', 'class': 'wikitable'});
$table.append($('<tr><th>Title</th><th>No change</th><th>Localify</th><th>Add category</th></tr>'));
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 = $('<input/>', {
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 ([[phab:T54542#555387]])
if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {
$.when(
mw.loader.using( 'ext.wikiEditor' ), $.ready
).then( customizeToolbar );
}
} );
}*/
});
// </nowiki>