/**
* Tool for quick adding data to WLM tables
* @url: ]
*
* @author: creators of ]
* @author: ]
* @author: ] (consultations :))
**/
importStylesheet('Wikipedysta:Yarl/skrypty/WikiLubiZabytki.css');
importScript('MediaWiki:Gadget-JsMwApi.js');
mw.loader.load('//api.tiles.mapbox.com/mapbox.js/v1.6.4/mapbox.js');
mw.loader.load('//api.tiles.mapbox.com/mapbox.js/v1.6.4/mapbox.css', 'text/css');
/* Translatable strings */
mw.messages.set({
'wikiloves-dialog-title': 'Edycja zabytku numer #',
'wikiloves-dialog-text': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mauris lectus, hendrerit in luctus vel, mollis eget quam. Aenean aliquam.',
'wikiloves-edited': 'Edytujesz wpis:',
'wikiloves-desc': 'Opis',
'wikiloves-address': 'Adres',
'wikiloves-lat': 'Szerokość',
'wikiloves-lon': 'Długość',
'wikiloves-coord-search': 'szukaj na mapce',
'wikiloves-photo': 'Zdjęcie (bez prefiksu)',
'wikiloves-photo-search': 'szukaj na Commons po ID',
'wikiloves-commons': 'Galeria Commons (bez prefiksu)',
'wikiloves-save-button': ' OK ',
'wikiloves-editpage-button': 'Zapisz dane',
'wikiloves-editing-button': 'Zapisuję dane...',
'wikiloves-cancel-button': 'Anuluj',
'wikiloves-summary': 'poprawa danych ]',
'wikibugs-edit-error': '',
'wikibugs-token-error': '',
'wikibugs-unknown-token-error': '',
});
window.wikiLovesGadget = {
version: 1,
reportPage: 'Wikipedysta:Yarl/Log',
changes: 0,
/**
* Checks if a title of the page is valid for a report.
* @param title A title of the page.
*/
isValidPageName: function(title) {
// Empty title is not valid
if (title === '') {
return false;
}
// Special pages are not valid too
if (this.getPageNamespaceNumber(title) === -1) {
return false;
}
return true;
},
/**
* Returns a namespace number of a specifed page.
* @param title A title of the page.
*/
getPageNamespaceNumber: function(title) {
var matches = title.match(/^(+)/);
if (matches) {
var key = matches.toLowerCase();
var namespaces = mw.config.get('wgNamespaceIds');
var result = namespaces;
if (result !== null) {
return result;
}
}
return 0;
},
/**
* Returns a refined page title.
* @param title A title of the page.
*/
getRefinedPageName: function(title) {
return jQuery.trim(title.replace(/_/g, ' '));
},
/**
* Sets up the gadget.
*/
init: function() {
var this_ = this;
this.pageName = this.getRefinedPageName(mw.config.get('wgPageName'));
$.ajax("//pl.wikipedia.org/w/index.php?title=" + encodeURIComponent(this.pageName) + "&action=raw")
.done(function(data) {
//alert(data);
this_.rows = data.split("{{zabytki/wpis");
})
.fail(function() {
//alert("error");
})
.always(function() {
//alert("complete");
});
jQuery('.wz-numer').click(function(event) {
this_.currentRow = $(".wz-numer").index(this);
this_.open(this_.currentRow);
event.preventDefault();
});
jQuery("body").append("<div class='mw-notification wikiloves-gadget'>\
<p>Zmieniono <strong>"+this.changes+"</strong> rekordów</p>\
<button type='button'>Zapisz</button>\
</div>");
jQuery(".wikiloves-gadget button").click(function(){
if(this_.changes !== 0)
this_.submitForm();
});
},
/**
* Shows the dialog window.
*/
open: function(index) {
var this_ = this;
var row = this.rows;
//console.log(row);
var regex = new RegExp('(\\n? *\\| *numer *= *)(.*?)(\\n? *\\| *id *= *)(*)( *\\n? *\\| *zespół zabytków *= *)(.*?)(\\n? *\\| *nazwa *= *)(.*?)(\\n? *\\| *miejscowość *= *)(.*?)(\\n? *\\| *adres *= *)(.*?)(\\n? *\\| *szerokość *= *)(*)( *\\n? *\\| *długość *= *)(*)( *\\n? *\\| *zdjęcie *= *)((?:.*?)?)(\\n? *\\| *commons *= *)((?:.*?)?)(\\n\\}\\})(\\n\\{\\{zabytki\\/zespół zabytków\\/(?:dół|góra)\\}\\}|)$', 'mg');
var match = regex.exec(row);
//console.log(match);
if(match === null) {
alert('Błąd! Nie jesteś na stronie, którą można edytować bądź kod nie pasuje do regeksa :(');
return;
}
if(match === null) {
alert('Błąd! Kod nie pasuje do regeksa :(');
return;
}
var $dialog = jQuery('<div id="dialogContent"></div>').append(
'<form name="WikiLovesForm" enctype="multipart/form-data">\
<input id="wlName" type="text" name="wlName" class="text ui-widget-content ui-corner-all" value="' + match + '"/>\
<fieldset>\
<div id="wlMap" style="height: 300px; margin-bottom: 10px;"></div>\
<div class="wikiloves-inline">\
<label for="wlLat">' + mw.msg('wikiloves-lat') + '</label>\
<input id="wlLat" type="text" name="wlLat" class="text ui-widget-content ui-corner-all wlCoord" value="' + match + '" />\
</div>\
<div class="wikiloves-inline">\
<label for="wlLon">' + mw.msg('wikiloves-lon') + '</label>\
<input id="wlLon" type="text" name="wlLon" class="text ui-widget-content ui-corner-all wlCoord" value="' + match + '"/>\
</div>\
<div class="wikiloves-inline" style="text-align:right; width: 310px;">\
<span id="wlGoogle" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"><span class="ui-button-text">Zobacz w Google</span></span>\
<span id="wlSearch" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"><span class="ui-button-text"><strong>Szukaj położenia</strong></span></span>\
</div>\
<label for="wlPhoto">' + mw.msg('wikiloves-photo') + '</label>\
<div class="wikiloves-inline" style="width: 374px;">\
<input id="wlPhoto" type="text" name="wlPhoto" class="text ui-widget-content ui-corner-all wikiloves-inline-long" value="' + match + '" />\
</div>\
<div class="wikiloves-inline" style="text-align: right; width: 250px;">\
<a href="https://commons.wikimedia.org/w/index.php?title=Special:Search&search=identified+by+the+ID+'+match+'" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"><span class="ui-button-text">Szukaj w Commons po ID</span></a>\
</div>\
<label for="wlCommons">' + mw.msg('wikiloves-commons') + '</label>\
<input id="wlCommons" type="text" name="wlCommons" class="text ui-widget-content ui-corner-all" value="' + match + '" />\
</fieldset>\
<img id="wikiloves-loading-icon" src="//upload.wikimedia.org/wikipedia/commons/4/49/Linux_Ubuntu_Loader.gif" style="display:none;"/>\
</form>'
);
var buttons = {};
buttons = function() {
jQuery(this).dialog('close');
};
//click 'Save' button
buttons = function() {
//get new data
var form = document.forms.WikiLovesForm;
var info = {
'index': index,
'regex': regex,
'name': form.wlName.value,
'lat': form.wlLat.value,
'lon': form.wlLon.value,
'photo': form.wlPhoto.value,
'commons': form.wlCommons.value
};
if(info.name === match && info.lat === match && info.lon === match && info.photo === match && info.commons === match) {
} else {
this_.rows = this_.rows.replace(
info.regex,
'$1$2$3$4$5$6$7'+ info.name + '$9$10$11$12$13' + info.lat + '$15' + info.lon + '$17' + info.photo + '$19' + info.commons + '$21$22'
);
this_.changes++;
jQuery(".wikiloves-gadget strong").html(this_.changes);
jQuery(".wz-numer").eq(this_.currentRow).parent().css("background", "rgb(212, 241, 212)");
}
jQuery(this).dialog('close');
};
$dialog.dialog({
width: 700,
height: 655,
modal: true,
resizable: false,
draggable: true,
closeOnEscape: true,
dialogClass: 'reportBugDialog',
title: mw.msg('wikiloves-dialog-title') + match,
close: function() {
jQuery(this).dialog('destroy').remove();
},
buttons: buttons
});
//map data
var map = L.map('wlMap');
var coords = $('#wlLon').val().length>0 ? : ;
map.setView(coords, 16);
L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
maxZoom: 19,
minZoom: 4
}).addTo(map);
var marker = new L.Marker(coords, {
draggable: true,
icon: L.mapbox.marker.icon({
'marker-size': 'large',
'marker-symbol': 'star',
'marker-color': '#fa0'
})
}).on('dragend', function(e) {
$('#wlLat').val(e.target._latlng.lat.toFixed(6));
$('#wlLon').val(e.target._latlng.lng.toFixed(6));
});
marker.addTo(map);
map.on('click', function(e) {
marker.setLatLng(e.latlng);
$('#wlLat').val(marker.getLatLng().lat.toFixed(6));
$('#wlLon').val(marker.getLatLng().lng.toFixed(6));
});
$('#wlLat').change(function() {
if($(this).val().split(";").length > 1) {
var lat = $(this).val().split(";"),
lon = $(this).val().split(";");
$('#wlLat').val(lat);
$('#wlLon').val(lon);
marker.setLatLng();
} else
marker.setLatLng();
map.setView(, map.getZoom());
});
$('#wlLon').change(function() {
if($(this).val().split(";").length > 1) {
var lat = $(this).val().split(";"),
lon = $(this).val().split(";");
$('#wlLat').val(lat);
$('#wlLon').val(lon);
marker.setLatLng();
} else
marker.setLatLng();
map.setView(, map.getZoom());
});
$("#wlSearch").click(function() {
$.ajax({
type: "GET",
url: "//osm.vink.pl/osm/nid/"+match,
crossDomain : true
}).done(function(data) {
//console.log(data);
$('#wlLat').val(data.location.coordinates);
$('#wlLon').val(data.location.coordinates);
map.setView(, 17);
marker.setLatLng();
if(data.images.length > 0) {
var img = data.images;
if(img.lastIndexOf("Category", 0) === 0)
$('#wlCommons').val(img.substring(9));
else
if($('#wlPhoto').val() === "")
$('#wlPhoto').val(img.substring(5));
}
})
.fail(function() {
alert("Błąd pobierania danych.");
});
});
$("#wlGoogle").click(function() {
window.open('https://www.google.com/maps/preview?q='+$('#wlLat').val()+','+$('#wlLon').val(), '_blank');
});
},
/**
* Send data to server
*/
submitForm: function() {
$('.wikiloves-gadget button')
.attr("disabled", true)
.html(mw.msg('wikiloves-editing-button'));
var allRows = this.rows;
for (var i = 1; i < this.rows.length; i++) {
allRows += "{{zabytki/wpis";
allRows += this.rows;
}
//initialise the API
var api = JsMwApi(mw.util.wikiScript('api'));
var pageHandler = api.page(this.pageName);
//save new content
pageHandler.edit(function(text, save_function, res) {
save_function(
allRows, {
summary: mw.msg('wikiloves-summary'),
minor: false
}, function(res) {
if (res && res.edit && res.edit.result && res.edit.result === 'Success') {
window.location.reload();
} else {
alert('Błąd AJAX!');
$loadingIcon.hide();
}
});
});
}
};
jQuery(document).ready(function() {
var wgNamespaceNumber = mw.config.get('wgNamespaceNumber');
var wgTitle = mw.config.get('wgTitle');
((wgNamespaceNumber === 4 && (/^Wiki Lubi Zabytki\/.*?\/.+/).test(wgTitle))) ? wikiLovesGadget.init() : '';
});