User:DannyS712 test/bilateral.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:DannyS712 test/bilateral. |
(function() {
var BL_config = {
// name: '[[User:DannyS712/Bilateral bot|Bilateral bot]]',
name: '[[User:DannyS712 test/bilateral.js]]',
version: 1.0
};
var BL_summary = 'Add a short description with ' + BL_config.name + ' (version ' + BL_config.version + ')';
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
var names = {
"Afghanistan": "The Islamic Republic of Afghanistan",
"Albania": "the Republic of Albania",
"Algeria": "the People's Democratic Republic of Algeria",
"Andorra": "the Principality of Andorra",
"Angola": "the Republic of Angola",
"Antigua and Barbuda": "Antigua and Barbuda",
"Argentina": "the Argentine Republic",
"Armenia": "the Republic of Armenia",
"Australia": "Australia",
"Austria": "the Republic of Austria",
"Azerbaijan": "the Republic of Azerbaijan",
"Bahamas": "the Commonwealth of the Bahamas",
"Bahrain": "the Kingdom of Bahrain",
"Bangladesh": "the People's Republic of Bangladesh",
"Barbados": "Barbados",
"Belarus": "the Republic of Belarus",
"Belgium": "the Kingdom of Belgium",
"Belize": "Belize",
"Benin": "the Republic of Benin",
"Bhutan": "the Kingdom of Bhutan",
"Bolivia": "the Plurinational State of Bolivia",
"Bosnia and Herzegovina": "Bosnia and Herzegovina",
"Botswana": "the Republic of Botswana",
"Brazil": "the Federative Republic of Brazil",
"Brunei Darussalam": "Brunei Darussalam",
"Bulgaria": "the Republic of Bulgaria",
"Burkina Faso": "Burkina Faso",
"Burundi": "the Republic of Burundi",
"Cabo Verde": "Republic of Cabo Verde",
"Cambodia": "the Kingdom of Cambodia",
"Cameroon": "the Republic of Cameroon",
"Canada": "Canada",
"Central African Republic": "the Central African Republic",
"Chad": "the Republic of Chad",
"Chile": "the Republic of Chile",
"China": "the People's Republic of China",
"Colombia": "the Republic of Colombia",
"Comoros": "the Union of the Comoros",
"Congo": "the Republic of the Congo",
"Cook Islands": "the Cook Islands",
"Costa Rica": "the Republic of Costa Rica",
"Croatia": "the Republic of Croatia",
"Cuba": "the Republic of Cuba",
"Cyprus": "the Republic of Cyprus",
"Czechia": "the Czech Republic",
"Côte d'Ivoire": "the Republic of Côte d'Ivoire",
"Ivory Coast": "the Republic of Côte d'Ivoire",
"Democratic People's Republic of Korea": "the Democratic People's Republic of Korea",
"Democratic Republic of the Congo": "the Democratic Republic of the Congo",
"Denmark": "the Kingdom of Denmark",
"Djibouti": "the Republic of Djibouti",
"Dominica": "the Commonwealth of Dominica",
"Dominican Republic": "the Dominican Republic",
"Ecuador": "the Republic of Ecuador",
"Egypt": "the Arab Republic of Egypt",
"El Salvador": "the Republic of El Salvador",
"Equatorial Guinea": "the Republic of Equatorial Guinea",
"Eritrea": "the State of Eritrea",
"Estonia": "the Republic of Estonia",
"Eswatini": "the Kingdom of Eswatini",
"Ethiopia": "the Federal Democratic Republic of Ethiopia",
"Faroe Islands": "Faroe Islands",
"Fiji": "the Republic of Fiji",
"Finland": "the Republic of Finland",
"France": "the French Republic",
"Gabon": "the Gabonese Republic",
"Gambia": "the Republic of the Gambia",
"Georgia (country)": "Georgia",
"Germany": "the Federal Republic of Germany",
"Ghana": "the Republic of Ghana",
"Greece": "the Hellenic Republic",
"Grenada": "Grenada",
"Guatemala": "the Republic of Guatemala",
"Guinea": "the Republic of Guinea",
"Guinea-Bissau": "the Republic of Guinea-Bissau",
"Guyana": "the Republic of Guyana",
"Haiti": "the Republic of Haiti",
"Honduras": "the Republic of Honduras",
"Hungary": "Hungary",
"Iceland": "the Republic of Iceland",
"India": "the Republic of India",
"Indonesia": "the Republic of Indonesia",
"Iran (Islamic Republic of)": "the Islamic Republic of Iran",
"Iran": "the Islamic Republic of Iran",
"Iraq": "the Republic of Iraq",
"Ireland": "Ireland",
"Israel": "the State of Israel",
"Italy": "the Republic of Italy",
"Jamaica": "Jamaica",
"Japan": "Japan",
"Jordan": "the Hashemite Kingdom of Jordan",
"Kazakhstan": "the Republic of Kazakhstan",
"Kenya": "the Republic of Kenya",
"Kiribati": "the Republic of Kiribati",
"Kuwait": "the State of Kuwait",
"Kyrgyzstan": "the Kyrgyz Republic",
"Lao People's Democratic Republic": "the Lao People's Democratic Republic",
"Laos": "the Lao People's Democratic Republic",
"Latvia": "the Republic of Latvia",
"Lebanon": "the Lebanese Republic",
"Lesotho": "the Kingdom of Lesotho",
"Liberia": "the Republic of Liberia",
"Libya": "State of Libya",
"Lithuania": "the Republic of Lithuania",
"Luxembourg": "the Grand Duchy of Luxembourg",
"Madagascar": "the Republic of Madagascar",
"Malawi": "the Republic of Malawi",
"Malaysia": "Malaysia",
"Maldives": "the Republic of Maldives",
"Mali": "the Republic of Mali",
"Malta": "the Republic of Malta",
"Marshall Islands": "the Republic of the Marshall Islands",
"Mauritania": "the Islamic Republic of Mauritania",
"Mauritius": "the Republic of Mauritius",
"Mexico": "the United Mexican States",
"Micronesia (Federated States of)": "the Federated States of Micronesia",
"Micronesia": "the Federated States of Micronesia",
"Monaco": "the Principality of Monaco",
"Mongolia": "Mongolia",
"Montenegro": "Montenegro",
"Morocco": "the Kingdom of Morocco",
"Mozambique": "the Republic of Mozambique",
"Myanmar": "the Republic of the Union of Myanmar",
"Namibia": "the Republic of Namibia",
"Nauru": "the Republic of Nauru",
"Nepal": "the Federal Democratic Republic of Nepal",
"Netherlands": "the Kingdom of the Netherlands",
"New Zealand": "New Zealand",
"Nicaragua": "the Republic of Nicaragua",
"Niger": "the Republic of the Niger",
"Nigeria": "the Federal Republic of Nigeria",
"Niue": "Niue",
"Norway": "the Kingdom of Norway",
"Oman": "the Sultanate of Oman",
"Pakistan": "the Islamic Republic of Pakistan",
"Palau": "the Republic of Palau",
"Panama": "the Republic of Panama",
"Papua New Guinea": "Independent State of Papua New Guinea",
"Paraguay": "the Republic of Paraguay",
"Peru": "the Republic of Peru",
"Philippines": "the Republic of the Philippines",
"Poland": "the Republic of Poland",
"Portugal": "the Portuguese Republic",
"Qatar": "the State of Qatar",
"Republic of Korea": "the Republic of Korea",
"Republic of Moldova": "the Republic of Moldova",
"Romania": "Romania",
"Russian Federation": "the Russian Federation",
"Rwanda": "the Republic of Rwanda",
"Saint Kitts and Nevis": "Saint Kitts and Nevis",
"Saint Lucia": "Saint Lucia",
"Saint Vincent and the Grenadines": "Saint Vincent and the Grenadines",
"Samoa": "the Independent State of Samoa",
"San Marino": "the Republic of San Marino",
"Sao Tome and Principe": "the Democratic Republic of Sao Tome and Principe",
"Saudi Arabia": "the Kingdom of Saudi Arabia",
"Senegal": "the Republic of Senegal",
"Serbia": "the Republic of Serbia",
"Seychelles": "the Republic of Seychelles",
"Sierra Leone": "the Republic of Sierra Leone",
"Singapore": "the Republic of Singapore",
"Slovakia": "the Slovak Republic",
"Slovenia": "the Republic of Slovenia",
"Solomon Islands": "Solomon Islands",
"Somalia": "the Federal Republic of Somalia",
"South Africa": "the Republic of South Africa",
"South Sudan": "the Republic of South Sudan",
"Spain": "the Kingdom of Spain",
"Sri Lanka": "the Democratic Socialist Republic of Sri Lanka",
"Sudan": "the Republic of the Sudan",
"Suriname": "the Republic of Suriname",
"Sweden": "the Kingdom of Sweden",
"Switzerland": "the Swiss Confederation",
"Syrian Arab Republic": "the Syrian Arab Republic",
"Tajikistan": "the Republic of Tajikistan",
"Thailand": "the Kingdom of Thailand",
"The former Yugoslav Republic of Macedonia": "The former Yugoslav Republic of Macedonia",
"Macedonia": "Republic of Macedonia",
"Timor-Leste": "the Democratic Republic of Timor-Leste",
"Togo": "the Togolese Republic",
"Tokelau": "Tokelau",
"Tonga": "the Kingdom of Tonga",
"Trinidad and Tobago": "the Republic of Trinidad and Tobago",
"Tunisia": "the Republic of Tunisia",
"Turkey": "the Republic of Turkey",
"Turkmenistan": "Turkmenistan",
"Tuvalu": "Tuvalu",
"Uganda": "the Republic of Uganda",
"Ukraine": "Ukraine",
"United Arab Emirates": "the United Arab Emirates",
"United Kingdom": "the United Kingdom of Great Britain and Northern Ireland",
"United Republic of Tanzania": "the United Republic of Tanzania",
"Tanzania": "the United Republic of Tanzania",
"United States of America": "the United States of America",
"United States": "the United States of America",
"Uruguay": "the Eastern Republic of Uruguay",
"Uzbekistan": "the Republic of Uzbekistan",
"Vanuatu": "the Republic of Vanuatu",
"Venezuela (Bolivarian Republic of)": "the Bolivarian Republic of Venezuela",
"Venezuela": "the Bolivarian Republic of Venezuela",
"Vietnam": "the Socialist Republic of Viet Nam",
"Yemen": "the Republic of Yemen",
"Zambia": "the Republic of Zambia",
"Zimbabwe": "the Republic of Zimbabwe",
};
mw.loader.using( 'mediawiki.util', function () {
$(document).ready( function () {
mw.util.addPortletLink( 'p-cactions', null, 'bilateral', 'ca-bilateral-relations', 'bilateral-relations');
$('#ca-bilateral-relations').click( function ( event ) {
event.preventDefault();
if ( mw.config.get( 'wgNamespaceNumber' ) == 14) get_page_list( mw.config.get( 'wgPageName' ), 0 );
else bilateral_run();
} );
} );
} );
function bilateral_run(){
var title = mw.config.get( 'wgPageName' );
title = title.replace( /_/g, ' ');
console.log( title );
var regex = /([a-z]+(?: [a-z]+)*)–([a-z]+(?: [a-z]+)*) relations/gi;
var match = regex.exec( title );
if (match !== null){
var content = get_page( title );
console.log( content );
var already_has = false;
if (content.indexOf('{{Short description\|') > -1) already_has = true;
console.log( already_has );
if (!(already_has)){
for (var iii = 0; iii < match.length; iii++){
if (match[iii] in names){
match[iii] = names[match[iii]];
}
}
//console.log ( match );
var short_desc = "{{Short description|1=Diplomatic relations between " + match[1] + " and " + match[2] + "}}";
//console.log ( short_desc );
var new_content = short_desc + "\n" + content;
console.log( new_content );
//set_new( title, new_content );
}
}
}
function get_page_list( category, current_depth ){
var get_pages = {
action: 'query',
list: 'categorymembers',
cmlimit: 'max',
cmtitle: category,
cmprop: 'title',
format: 'json'
};
var result = null;
$.ajax({
url: scriptUrl,
type: 'get',
data: get_pages,
dataType: 'json',
async: false,
success: function(catResponse) {
//console.log( catResponse );
var pages = catResponse.query.categorymembers;
//console.log( pages );
var good_pages = [];
var sub_cats = [];
for (var i = 0; i < pages.length; i++) {
if ( pages[i].ns === 0 ) {
good_pages.push(pages[i].title);
}
else if (pages[i].ns === 14) {
sub_cats.push(pages[i].title);
}
}
console.log( good_pages );
console.log( sub_cats );
if (current_depth < 1){
for (var j = 0; j < sub_cats.length; j++){
good_pages = good_pages.concat( get_page_list(sub_cats[j], current_depth + 1));
}
}
result = good_pages;
console.log( result );
}
});
return result;
}
function get_page( name ){
var page_to_get = {
action: 'query',
titles: name,
prop: 'revisions',
rvprop: 'content',
format: 'json',
formatversion: 2
};
var result = null;
$.ajax({
url: scriptUrl,
type: 'get',
data: page_to_get,
dataType: 'json',
async: false,
success: function(page) {
//console.log( page );
result = page.query.pages["0"].revisions["0"].content;
//console.log( result );
}
});
return result;
}
function set_new ( page, new_content ){
console.log( page, new_content );
var to_send = {
action: 'edit',
title: page,
//pageid: mw.config.get( 'wgArticleId' ),
text: new_content,
summary: BL_summary,
token: mw.user.tokens.get( 'csrfToken' )
};
console.log( to_send );
$.when(
$.post( scriptUrl, to_send, function( response ){ } )
).done( function() {
alert('Short description added');
console.log( response );
location.reload();
} );
}
})();