User:Indic-TechCom/Script/insertWDImage.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/*
This script allows users to insert an image from Wikidata on their local wiki page
if the page's Wikidata item has an image.
@Author [[User:Jayprakash12345]]
OwnBy [[meta:Indic-TechCom]]
*/
WikiDataImageTrackCat = {
"enwiki": "Category:No local image but image on Wikidata",
"bnwiki": "বিষয়শ্রেণী:কোন স্থানীয় চিত্র নেই কিন্তু উইকিউপাত্তে আছে",
"knwiki": "ವರ್ಗ:No local image but image on Wikidata",
"mlwiki": "വർഗ്ഗം:No local image but image on Wikidata",
"orwiki": "ଶ୍ରେଣୀ:No local image but image on Wikidata",
"urwiki": "زمرہ:کوئی مقامی تصویر نہیں تاہم ویکی ڈیٹا پر موجود",
"bhwiki": "श्रेणी:विकिडेटा पर फोटो मौजूद बा जबकि इहाँ नइखे इस्तेमाल भइल"
};
$( function(){
function getImagefromWikiData(){
mw.loader.using( 'ext.wikiEditor' ).done( function(){
var editingTextBox = document.getElementById('wpTextbox1');
// Add icon to WikiEditor
$( editingTextBox ).wikiEditor( 'addToToolbar', {
'section': 'main',
'group': 'insert',
'tools': {
'InsertWikidataImage': {
label: 'Insert image from Wikidata',
type: 'button',
icon: '//upload.wikimedia.org/wikipedia/commons/thumb/7/7d/Oxygen15.04.1-insert-image.svg/22px-Oxygen15.04.1-insert-image.svg.png',
action: {
type: 'callback',
execute: function () {
showLoadingMsg("Getting Image from Wikidata" );
var startPostion = editingTextBox.selectionStart,
endPostion = editingTextBox.selectionStart,
wikidataItem = $("#t-wikibase").children().attr("href").split('/').pop(),
wdApi = new mw.ForeignApi('https://www.wikidata.org/w/api.php');
var wbparam = {
"action": "wbgetclaims",
"format": "json",
"entity": wikidataItem,
"property": "P18"
};
wdApi.get( wbparam ).done( function( wbdata ){
try {
imagefromWD = wbdata.claims.P18[0].mainsnak.datavalue.value;
editingTextBox.setRangeText( "[[File:" + imagefromWD + "|300px]]", startPostion, endPostion, 'select' );
$( "#wpSummary" ).val("Insert image using [[meta:Indic-TechCom/Tools/InsertWikiDataImage|InsertWikiDataImage]]");
} catch (e) {
mw.notify( "Somethin went wrong! Couldn't find image.", { type: "error"} );
}
}).fail( function(){
mw.notify( "Somethin went wrong! Wikitdata item is unreachable.", { type: "error"} );
}).always( function(){
showLoadingMsg( "" );
});
}
}
}
}
} );
} );
}
function showLoadingMsg( msgLabel ) {
var msg, loadingGif,
loadingId = 'LoadingForInsertWikidataImage';
// Always remove any existing message.
$( '#' + loadingId ).remove();
// Add the new message if required.
if ( msgLabel.length !== 0 ) {
msgBox = $( "<p>" )
.attr( "id", loadingId )
.css( "background-color", "#efefef" ).css( "border", "1px solid #ccc" )
.text( msgLabel );
loadingGif = $( "<img>" )
.attr( "src", '//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif' )
.attr( "alt", "Animated loading indicator" )
.css( "display", "inline-block" ).css( "margin", "0.3em" );
msgBox.prepend( loadingGif );
$( '#wpTextbox1' ).before( msgBox );
}
}
// Run only when user is in edit mode
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
mw.loader.using( 'mediawiki.api' ).done( function(){
var param = {
"action": "query",
"format": "json",
"prop": "categories",
"cllimit": "max",
"titles": mw.config.get('wgPageName')
},
api = new mw.Api();
api.get( param ).done( function( data ){
var categories = data.query.pages[Object.keys(data.query.pages)[0]].categories,
haveImageOnWikiData = false;
// if there is no category exit from execution
if (categories === undefined ){
return;
}
categories.forEach( function(item){
if( item.title === WikiDataImageTrackCat[mw.config.get("wgDBname")] ){
haveImageOnWikiData = true;
}
});
// If category found then only run further script
if( haveImageOnWikiData ){
getImagefromWikiData();
}
});
});
}
});