Gaan na inhoud

Gebruiker:MGA73/replaceToCommons.js

Vanuit Wiktionary, die vrye woordeboek.

Let wel: Na die wysiging is dit dalk nodig om u blaaier se kasgeheue te verfris voordat u die veranderinge sal sien:

  • Firefox / Safari: hou Shift en kliek Herlaai, of druk Ctrl-F5 of Ctrl-R (⌘-R op 'n Mac)
  • Google Chrome: Druk Ctrl-Shift-R (⌘-Shift-R op 'n Mac)
  • Internet Explorer / Edge: Hou Ctrl en kliek Refresh, of druk Ctrl-F5
  • Opera: Gaan na Kieslys → Settings (Opera → Preferences op 'n Mac) en dan na Privacy & security → Clear browsing data → Cached images and files.
/* 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('Replacing with ' + newName);
    process.progress(function(){
        requiredReplacements--; 
        if(requiredReplacements === 0) {
            mw.notify('All file uses have been replaced');
        }
    })
    $('.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') + ']] => [[:Lêer:' + newName +']]', text: newText, title: page, minor: true }).done(function(){
                mw.notify('File replaced in '+page);
                process.notify();
                });
            } else {
                mw.notify('File not replaced in '+page);
            }
        });
    });
}

function safeReplaceFile(filename) {
    var api = new mw.Api();
    api.get({action: 'query', titles: 'Lêer:' + 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('Local copy of the file exists');
        }
        else {
            mw.notify('File does not exist. Please check manually.');
        }
    });
}

function getNowCommonsName() {
    var api = new mw.Api();
    api.get({action: 'parse', page: mw.config.get('wgPageName'), prop: 'wikitext'}).done(function(d){
        var wikitext = d.parse.wikitext['*'];
        var match = wikitext.match(/\{\{NowCommons\|([^}]+)\}\}/);
        if (match) {
            var newName = match[1].replace(/Lêer:|Beeld:|File:|Image:/,'').replace('_', ' ');
            safeReplaceFile(newName);
        } else {
            var newName = prompt("Enter the name of the replacement image");
            newName = newName.replace(/Lêer:|Beeld:|File:|Image:/,'').replace('_', ' ');
            safeReplaceFile(newName);
        }
    });
}

$(mw.util.addPortletLink('p-cactions','#','Replace with Commons','#ca-js-moveToCommons')).click(function(){
    getNowCommonsName();
});
});