Удзельнік:Ssvb/InterWikiTransclusion.js
Выгляд
Заўвага: Пасля апублікавання, вам можа спатрэбіцца ачыстка кэшу браўзера, каб убачыць унесеныя змены.
- Firefox / Safari: націсніце Reload, утрымліваючы Shift, або націсніце Ctrl-F5 ці Ctrl-R (⌘-R на Макінтошах)
- Google Chrome: Націсніце Ctrl-Shift-R (⌘-Shift-R на Mac)
- Edge: націсніце Refresh, утрымліваючы Ctrl, або націсніце Ctrl-F5.
// <nowiki>
if(!self.ws_messages) self.ws_messages = { };
function ws_msg(name) {
var m = self.ws_messages[name];
if(m) {
return m;
} else {
return name;
}
}
/* cross-domain transclusions through the API */
// translate from a number scheme to a [0-9] scheme
function translate_number(number_str, dest_lang) {
if (dest_lang === null) {
return number_str;
}
var src_lang = mw.config.get('wgPageContentLanguage');
if (src_lang === 'bn' && dest_lang !== 'bn') {
number_str = number_str
.replace(/০/g,'0')
.replace(/১/g,'1')
.replace(/২/g,'2')
.replace(/৩/g,'3')
.replace(/৪/g,'4')
.replace(/৫/g,'5')
.replace(/৬/g,'6')
.replace(/৭/g,'7')
.replace(/৮/g,'8')
.replace(/৯/g,'9');
} else if (src_lang !== 'bn' && dest_lang === 'bn') {
number_str = number_str
.replace(/0/g, '০')
.replace(/1/g, '১')
.replace(/2/g, '২')
.replace(/3/g, '৩')
.replace(/4/g, '৪')
.replace(/5/g, '৫')
.replace(/6/g, '৬')
.replace(/7/g, '৭')
.replace(/8/g, '৮')
.replace(/9/g, '৯');
}
// if needed add more translation here.
return number_str;
}
// title should be a Page: namespace title i.e. in the form page_name/xxx where xxx is a page number
// either in [0-9] form or an alternate number scheme, page_name should not contain any /
function translate_title(title, dest_lang)
{
page_name = title.split("/");
/*
if (mw.config.get('wgUserName') == 'Phe') {
alert(translate_number(page_name[1], dest_lang));
}
*/
return page_name[0] + "/" + translate_number(page_name[1], dest_lang);
}
function get_url(lang, project) {
if (!project) {
project = 'wikisource';
}
if (lang === 'commons' || lang === 'species' || lang === 'meta') {
project = 'wikimedia';
}
if (lang === "old" || lang === "oldwikisource") {
return 'wikisource.org';
} else {
return lang + '.' + project + '.org';
}
}
function api_url(lang, project) {
return '//' + get_url(lang, project) + '/w/api.php';
}
/* transform all href from relative to absolute, so that they still point to the right place */
function absoluteLinks(text, lang, project) {
if (!text) return '';
return text
.replace(/href="\/wiki\//g, 'href="//' + get_url(lang, project) + '/wiki/')
.replace(/href="\/w\//g, 'href="//' + get_url(lang, project) + '/w/');
}
// iw_trans_callback() and iw_trans() allow cross-wiki transclusion from namespace:0
function iw_trans_callback(res){
var target = res.requestid.replace(/___.*/i, "");
var spans = $('span.iwtrans');
for (var i = 0; i<spans.length; i++) {
var item = spans[i];
if (item.title == target) {
var m = item.title.split("|");
lang=m[0];
project = 'wikisource'; //default
if (m.length > 2) {
project = m[2];
}
$(item).html(absoluteLinks(res.parse.text['*'], lang, project));
mw.hook( 'wikipage.content' ).fire( $(item) );
}
}
}
function iw_trans() {
var item, m, url, query, spans = $( 'span.iwtrans' );
for(var i = 0; i<spans.length; i++) {
item = spans[i];
m = item.title.split("|");
lang = m[0];
title = m[1];
project = 'wikisource'; //default
if (m.length > 2) {
project = m[2];
}
query = {
format: 'json',
requestid: item.title + '___' + Math.random(),
action: 'parse',
text: '{{:' + title + '}}<references/>',
callback: 'iw_trans_callback',
title: title
};
url = api_url(lang, project) + '?' + $.param( query );
mw.loader.load( url );
}
}
// iw_pages_callback() and iw_pages() allow transclusions from namespace Page: to namespace Page:
function iw_pages_callback(res) {
var target = res.requestid.replace(/___.*/i, "");
var spans = $('span');
for(var i = 0; i<spans.length; i++) {
var item = spans[i];
if( ($(item).hasClass("iwpages") || $(item).hasClass("iwpage")) && item.title == target ) {
var m = item.title.split("|");
lang=m[0];
project = 'wikisource'; //default
var txt = absoluteLinks(res.parse.text['*'], lang, project);
/* fix for it.wikisource */
$txt = $('.SAL, .numeropagina', txt).remove().end();
$(item).html($txt);
mw.hook( 'wikipage.content' ).fire( $(item) );
}
}
//remove the quality header from the imported page
$('.iwpage .prp-page-qualityheader').remove();
}
function iw_pages() {
/**
* Build something like ' name="value"'
*/
function html_attr( name, value ) {
if( !value ) {
console.log( name );
}
return ' ' + name + '="' + value.replace( '"', '"' ) + '"';
}
var item, m, query, wikitext,
count = 0,
spans = $( 'span.iwpages' );
for(var i = 0; i<spans.length; i++) {
item = spans[i];
// extracts each part
m = item.title.split('|');
if( m.length >= 4 ) {
lang = m[0];
project = 'wikisource';
title = m[1];
from = translate_number(m[2], null);
to = translate_number(m[3], null);
fromsection = '';
tosection = '';
onlysection = '';
wikitext = '<pages' + html_attr( 'index', title ) + html_attr( 'from', from ) + html_attr( 'to', to );
if (m.length > 4) {
fromsection = m[4];
}
if (m.length > 5) {
tosection = m[5];
}
if (m.length > 6) {
onlysection = m[6];
wikitext += ' onlysection='+onlysection+' /><references/>';
} else if (fromsection === '' ) {
wikitext += ' tosection='+tosection+' /><references/>';
} else {
wikitext += ' fromsection='+fromsection+' tosection='+tosection+' /><references/>';
}
// TODO: why the callback method? can't we use a .done() ?
query = {
format: 'json',
title: title,
requestid: item.title + '___' + Math.random(),
action: 'parse',
callback: 'iw_pages_callback',
text: wikitext
};
url = api_url( lang ) + '?' + $.param( query );
mw.loader.load( url );
} else {
console.error( "malformed iwpage element", item, m );
}
}
spans = $( 'span.iwpage' ) ;
for(i = 0; i<spans.length; i++) {
item = spans[i];
m = item.title.split("|");
lang = m[0];
project = 'wikisource';
count = count + 1;
title = "Page:" + translate_title(m[1], lang);
if(m.length > 2) {
section = m[2];
} else {
section = false;
}
if(section) {
section_title='lst|'+title+'|'+section;
} else {
section_title = title;
}
query = {
format: 'json',
requestid: item.title + '___' + Math.random(),
action: 'parse',
callback: 'iw_pages_callback',
text: '{{' + section_title + '}}<references/>',
title: title
};
url = api_url(lang) + '?' + $.param( query );
mw.loader.load(url);
}
cs = document.getElementById("corr-info");
if(cs && count) {
if(count==1) {
cs.innerHTML += ' ' + ws_msg('iwtrans') + ' <a href="//' + get_url(lang) + '/w/index.php?title='+ encodeURIComponent( title ) +'">' + get_url( lang ) + '</a>.';
} else {
cs.innerHTML += ws_msg('iwtrans2');
}
}
}
$( function() {
iw_trans();
iw_pages();
} );
// </ nowiki>