MediaWiki:Common.js/attribution.js

З пляцоўкі Вікікрыніцы

Заўвага: Пасля апублікавання, вам можа спатрэбіцца ачыстка кэшу браўзера, каб убачыць унесеныя змены.

  • Firefox / Safari: націсніце Reload, утрымліваючы Shift, або націсніце Ctrl-F5 ці Ctrl-R (⌘-R на Макінтошах)
  • Google Chrome: Націсніце Ctrl-Shift-R (⌘-Shift-R на Mac)
  • Internet Explorer / Edge: націсніце Refresh, утрымліваючы Ctrl, або націсніце Ctrl-F5
  • Opera: Увайдзіце Menu → Settings (Opera → Preferences на Mac), далей Privacy & security → Clear browsing data → Cached images and files.
/*
; Скрыпт для фарматаваньня выгляду крыніцы імпартаваньня тэксту ў кароткім апісаньні зьменаў
; v1.0
; Аўтар: [[w:be-tarask:Удзельнік:Zedlik]]
; Пры наяўнасьці пытаньняў можна зьвяртацца да [[Вікікрыніцы:Супольнасць|супольнасьці]] або да [[Удзельнік:Taravyvan Adijene]]
*/

// канстанты

var attributionSummaryPrefix = "крыніца — ";
var attributionSummaryFieldOverridenMaxLength = 450;
var attributionReplaceingPlaceholderPrefix = "MATCHEDPLACEHODER";
var attributionReplaceingPlaceholderPostfix = "A";

if (mw.config.get('wgAction') == 'edit' || mw.config.get('wgAction') == 'submit') {
    $(document).ready(function() {
    	var $el = $('#wpSummary');
    	if($el.length && $el.byteLimit) {
	        $el.byteLimit(attributionSummaryFieldOverridenMaxLength);
    	}
    });
}

function getAttributionString(url, depth) {

    var attributionNameForAttribution = null;
    var attributionDomain = null;
    var attributionProject = null;
    var attributionPage = null;
    var revisionID = null;
    var matchedString = null;
    var secureConnection = false;

    var url2 = url;

    if (url2.match("https\\://secure\\.wikimedia\\.org/(wiki(m|p)edia|wikisource)/([a-zA-Z-]{2,20})/(w|wiki)/.*") != null) {
        url2 = url2.replace(new RegExp("https\\://secure\\.wikimedia\\.org/(wiki(?:p|m)edia|wikisource)/([a-zA-Z-]{2,20})/((?:w|wiki)/.*)",''), "http://$2.$1.org/$3");
        secureConnection = true;
    }
    else if (url2.match("https\\://.*") != null) {
        url2 = url2.replace(new RegExp("https\\://(.*)",''), "http://$1");
        secureConnection = true;
    }

    var p = new RegExp("http\\://([a-zA-Z-]{2,20})\\.(wiki(?:m|p)edia|wikisource).org/wiki/([^\\? ;]{1,500})(\\?oldid=([0-9]{1,10}))?");
    var result = p.exec(url2);
    if (result != null) {
        matchedString = result[0];
        attributionDomain = result[1];
        attributionProject = result[2];
        attributionPage = result[3];
        revisionID = result[5];
    }
    else {
        p = new RegExp("http\\://([a-zA-Z-]{2,20})\\.(wiki(?:m|p)edia|wikisource).org/w/index\\.php\\?title=([^\\& ;]{1,500})(\\&oldid=([0-9]{1,10}))?");
        result = p.exec(url2);
        if (result != null) {
            matchedString = result[0];
            attributionDomain = result[1];
            attributionProject = result[2];
            attributionPage = result[3];
            revisionID = result[5];
        }
    }

    attributionNameForAttribution = getPageNameForAttribution(attributionPage);
    var p2 = attributionNameForAttribution.replace(" ", "_");

    var attributionString = getAttributionStringA(attributionDomain, attributionProject, p2, revisionID, secureConnection);

    if (!attributionString) {
        attributionString = url;
    }
    else {
        if (matchedString && attributionString && (matchedString.replace(/^https?/i, "") != attributionString.replace(/^https?/i, ""))) {
            if (depth < 5) {
                var placeholder = attributionReplaceingPlaceholderPrefix + depth + attributionReplaceingPlaceholderPostfix;
                var matchedURL = url2.replace(matchedString, placeholder);
                matchedURL = getAttributionString(matchedURL, depth + 1);
                url2 = matchedURL.replace(placeholder, matchedString);
            }
        
            attributionString = attributionSummaryPrefix + attributionString;
            attributionString = url2.replace(matchedString, attributionString);
        }
        else {
            if (secureConnection) {
                attributionString = url2.replace(attributionSummaryPrefix + "http://", attributionSummaryPrefix + getProtocol(secureConnection) + "://");
            }
            else {
                attributionString = url2;
            }
        }
    }

    return attributionString;
}

function getAttributionStringA(domain, project, page, revid, secure) {
    if (domain && project && page) {
        var attributionType2;

        if (!revid) {
            attributionType2 = "[[" + domain + ":" + page.replace(/_/g, " ") + "]]";
        }
        else {
            attributionType2 = getProtocol(secure) + "://" + domain + "." + project + ".org/wiki/" + page +"?oldid=" + revid;
        }

        return attributionType2;
    }

    return "";
}


function getPageNameForQuery(page) {

    var p = new RegExp("^((\\%[0-9A-Fa-f]{2,2}|[!-~])*)$");
    var result = p.exec(page);

    if (result != null) {
        // already unescaped
        page = page.replace("_", "%20");
        return page;
    }

    page = escape(encodeToUtf8(page));
    page = page.replace("_", "%20");
    return page;
}

function getPageNameForAttribution(page) {

    var p = new RegExp("^((\\%[0-9A-Fa-f]{2,2}|[!-~])*)$");
    var result = p.exec(page);

    if (result != null) {
        // unescape

        page = decodeFromUtf8(unescape(page));
    }

    page = page.replace("_", " ");
    return page;
}

function getProtocol(secure) {
    var protocol = "http";
    if (secure) {
        protocol = "https";
    }
    return protocol;
}

function encodeToUtf8(string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }

    }

    return utftext;
}


function decodeFromUtf8(utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while ( i < utftext.length ) {

        c = utftext.charCodeAt(i);

        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        }
        else if((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return string;
}

// hook
if (mw.config.get('wgAction') == 'edit' || mw.config.get('wgAction') == 'submit') {
    $(function() {
        var wpSummary = document.getElementById('wpSummary');
        if(!wpSummary) {
            return; // поле кароткіх зьменаў ня знойдзенае
        }
        
        wpSummary.setAttribute('maxlength', attributionSummaryFieldOverridenMaxLength);
        
        $('#wpSave').on('click', function() { adoptAttributionStringForSummary(); });
        $('#wpPreview').on('click', function() { adoptAttributionStringForSummary(); });
        $('#wpDiff').on('click', function() { adoptAttributionStringForSummary(); });
    });    
}

function adoptAttributionStringForSummary(e) {

    var wpSummary = document.getElementById('wpSummary')
    
    if(!wpSummary) {
        return true; // поле кароткіх зьменаў ня знойдзенае
    }
    
    var summaryValue = wpSummary.value;
    summaryValue = getAttributionString(summaryValue, 0);
    wpSummary.value = summaryValue;
    
    return true;
}