//<nowiki>
$(function (){
var MAP_bot_config = {
name: '[[User:DannyS712/Map bot|Map bot]]',
version: 1.7,
testing: true,
// disclaimer: "Bot in trial ([[Wikipedia:Bots/Requests for approval/DannyS712 bot 15|BRFA]])",
// debug: true
};
var MAP_bot_advert = 'Task 15: Add {{Germany district OSM map}} to the infobox with ' + MAP_bot_config.name + ' (version ' + MAP_bot_config.version + ')';
//if (MAP_bot_config.testing){
// MAP_bot_advert = MAP_bot_config.disclaimer + ": " + MAP_bot_advert;
//}
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
importScript( 'User:DannyS712 test/page.js' );
importScript( 'User:DannyS712 test/JSON.js' );
mw.loader.using( 'mediawiki.util', function () {
$(document).ready( function () {
if ( mw.config.get( 'wgPageName' ) === "User:DannyS712_test/infobox.json" || true) {
var link = mw.util.addPortletLink( 'p-cactions', 'javascript:void(0)', 'MAP bot', 'ca-MAPbot', 'MAP bot');
$( link ).click( function ( event ) {
event.preventDefault();
MAP_bot();
//MAP_bot_setup2();
} );
}
} );
} );
function MAP_bot (){
var list = get_JSON( 'User:DannyS712 test/infobox7.json' );
console.log( list );
for (var iii = 0; iii < list.length; iii++){
MAP_page( list[iii] );
}
}
function MAP_page( page ){
var page_content = get_page( page.page );
//console.log( "The content of the page is:", page_content);
if (should_edit( page, page_content) ) {
console.log( "The page to be edited is:", page.page, "and the map to be added is:", page.map);
var replacement = '$1' + page.map + '$3';
var new_content = page_content.replace( /({{Infobox (?:District DE|German district)(?:.|\n)*?\s*map\s*=)(.*)((?:.|\n)*?}})/i, replacement);
if (new_content === page_content) {
//return false;
console.log( "Error: the page", page.page, "does not contain a map parameter to replace; trying a work around");
new_content = page_content.replace( /({{Infobox (?:District DE|German district))/i, '$1\n| map=' + page.map);
if (new_content === page_content) {
console.log( "Error, work around failed. Content was:", page_content);
return false;
}
console.log( "Work around content: ", new_content );
set_page ( page.page, new_content, MAP_bot_advert, false );
return true;
} else {
console.log( "The new page content would be:", new_content);
set_page ( page.page, new_content, MAP_bot_advert, false );
return true;
}
}
return false;
}
function should_edit ( page, content ){
if ( !content ) {
console.log( 'Unfortunately, the page', page.page, 'does not exist');
return false;
}
var index = content.toLowerCase().indexOf('{{germany district osm map');
if ( index > -1) {
console.log( 'Unfortunately, the page', page.page, 'already has a map template (at index:', index, '), and so it will not be edited.');
return false;
}
index = content.toLowerCase().indexOf('{{infobox district de');
if ( index === -1 ){
index = content.toLowerCase().indexOf('{{infobox german district');
if ( index === -1 ){
console.log( 'Unfortunately, the page', page.page, 'does not have a proper infobox, and so it will not be edited.');
return false;
}
}
console.log( "Should edit the page:", page.page);
return true;
}
function MAP_bot_setup2 () {
var list = get_JSON( 'User:DannyS712 test/infobox3.json' );
console.log( list );
var to_edit = [];
for (var iii = 0; iii < list.length; iii++){
var page = list[iii];
var name = page.page;
console.log( page );
var content = get_page( name );
if (content) if (should_edit( name, content)) to_edit.push( page );
}
console.log ( to_edit );
var stringy = JSON.stringify( to_edit, null, 2 );
set_JSON ( 'User:DannyS712 test/infobox4.json', stringy, 'Record to edit');
}
function MAP_bot_setup () {
var list = get_JSON( 'User:DannyS712 test/infobox.json' );
list = list[0];
console.log( list );
var with_Q = [];
for (var iii = 0; iii < list.length; iii++){
with_Q.push( {page: list[iii], q: get_wikibase( list[iii] ) } );
}
console.log ( with_Q );
var stringy = JSON.stringify( with_Q, null, 2 );
set_JSON ( 'User:DannyS712 test/infobox2.json', stringy, 'Record Qs');
}
function get_wikibase ( page ){
var request = {
action: 'query',
prop: 'pageprops',
titles: page,
format: 'json',
formatversion: 2
};
var result = null;
console.log( request );
$.ajax({
url: scriptUrl,
type: 'get',
data: request,
async: false,
dataType: 'json',
success: function(data) {
console.log( data );
result = data.query.pages['0'].pageprops.wikibase_item;
}
});
console.log( result );
return result;
}
});
//</nowiki>