For faster navigation, this Iframe is preloading the Wikiwand page for Користувач:AS/taxon.js.

Користувач:AS/taxon.js

Матеріал з Вікіпедії — вільної енциклопедії.

// У статті з автоматичним таксобоксом, поряд з повідомленням "Бракує шаблону таксономії (створити)" додає посилання "створити автоматично". 
// Тож повідомлення виглядатиме "Бракує шаблону таксономії (створити | створити автоматично)" і
// дозволяє автоматично скопіювати потрібні шаблони таксономії з АнглВікі. Ребдагування будуть виконані з допомогою вашого облікового запису.

window.copier = {
    copyTemplate: function(name){
        name = name.trim();
        if (!name.match(/([Tt]emplate|[Шш]аблон):[Tt]axonomy\/.+/)) {
            name = copier.template(name);
        }
        var enapi = new mw.ForeignApi('https://en.wikipedia.org/w/api.php');
        var api = new mw.Api();
        var wdapi = new mw.ForeignApi('//www.wikidata.org/w/api.php');
        api.get({
            action: 'query',
            titles: name
        }).done(function(data) {
            var ukrainianPageExists = !data.query.pages['-1'];
            if (ukrainianPageExists) return;

            enapi.get({
                action: 'query',
                titles: name,
                prop: 'revisions|pageprops',
                rvprop: 'content'
            }).done(function(data) {
                if (data.query.pages['-1']) { mw.notify('Не знайдено шаблону ' + name + ' в АнглВікі'); return }

                var page = data.query.pages[Object.keys(data.query.pages)[0]];
                var wikibaseItem = page.pageprops && page.pageprops.wikibase_item;

                if (!page.revisions) return;

                var text = page.revisions[0]['*'];
                var sameAs = copier.getSameAs(text);
                if (sameAs) {
                    copier.copyTemplate(sameAs);
                }

                text = copier.strip(text);
                var scientificName = copier.getScientificName(text);
                copier.addUkrainian(text, scientificName, api, function(text){
                	api.create(name, { summary: 'автоматична копія з АнглВікі' }, text).done(function(data){
                    mw.notify($("<span>Створено <a href='" + mw.util.getUrl(name) + "'>" + name + "</a></span>"));

                    if (wikibaseItem) {
                        wdapi.postWithEditToken({
                            action: 'wbsetsitelink',
                            id: wikibaseItem,
                            linktitle: data.title,
                            linksite: 'ukwiki'
                        });
                    } else {
                    	wdapi.postWithEditToken({
                            action: 'wblinktitles',
                            tosite: 'enwiki',
                            totitle: name,
                            fromsite: 'ukwiki',
                            fromtitle: data.title
                        });
                    }
                });

                var parent = copier.stripComments(copier.getParent(text));

                if (parent) {
                    copier.copyTemplate(parent);
                }
                });
            });
        });
    },
    getSameAs: function(x){
        var m = x.match(/same_as\s*=\s*(.+)/);
        if (m) return m[1];
    },
    stripComments: function(x){
        return (x || '').replace(/<!--.*-->/, '');
    },
    getParent: function(x){
        var m = x.match(/parent\s*=\s*(.+)\s*/);
        if (m) return m[1];
    },
    getScientificName: function(x){
    	var m = x.match(/link\s*=\s*(.+)\s*/);
        if (m) {
        	var link = m[1];
        	var linkName = link.match(/(.+)\|(.+)/) 
        	if (linkName) link = linkName[2]
        	return link;
        }
    },
    strip: function(x){
        return x.replace(/<noinclude\>.+<\/noinclude\>/, '');
    },
    addUkrainian: function(x, scientificName, api, onDone){
    	if (scientificName){
    		api.get({
	            action: 'query',
	            titles: scientificName,
	            redirects: true
	        }).done(function(data) {
              var ukrainianPageExists = !data.query.pages['-1'];
	          if (ukrainianPageExists)
	          {
	          	var page = data.query.pages[Object.keys(data.query.pages)[0]];
	          	var ukrainianName = page.title
	          	if (scientificName == ukrainianName)
	          	  onDone(x.replace(/\}\}$/, "|ukrainian=\n))"));	
	          	else
	          	  onDone(x.replace(/\}\}$/, "|ukrainian=" + ukrainianName + "\n))"));
	          } 
	          else
	        	onDone(x.replace(/\}\}$/, "|ukrainian=\n))"));
	        })
    	}
    	else
        	onDone(x.replace(/\}\}$/, "|ukrainian=\n))"));
    },
    template: function(x){
        return "Template:Taxonomy/" + x;
    },
    init: function(){
	    var taxonomyWarnings = $('span:contains("Бракує шаблону таксономії")').filter(function(){ return $(this).text() === "Бракує шаблону таксономії"; });
	    taxonomyWarnings.each(function(){
	        var parent = $(this).parent();
	        var match = parent.find('.plainlinks a:contains("створити")').attr('href').match(/title=(.+)&/);
	        if (match.length === 0) return;
	        var name = match[1];
	
	        $(this).parent().find('.plainlinks').append(' | ');
	        var button = $('<a data-name="' + name + '">створити автоматично</a>');
	        button.on('click', function(){
	            copier.copyTemplate(name);
	        });
	        parent.find('.plainlinks').append(button);
	    });
	}
};

$(function(){
    setTimeout(function(){
            mw.loader.using(['oojs-ui', 'mediawiki.api'], function(){
                copier.init();
            });
    }, 1000);
});
{{bottomLinkPreText}} {{bottomLinkText}}
Користувач:AS/taxon.js
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?