User:Aeroid/wikidata useful.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.
/*
USAGE : Add the line
importScript("User:Magnus Manske/wikidata useful.js");// [[User:Magnus Manske/wikidata useful.js]]
to your [[Special:Mypage/common.js]] page

based on User:Magnus Manske/wikidata useful.js and User:JonnyJD/consistency check.js
*/

/************************************************************************************************************************
The section below contains the "Wikidata Usefuls" link definitions. Add new ones or edit existing ones as you see fit.
*************************************************************************************************************************/

/*
You can add your own links, just for yourself, if you add code on your common.js page ''before'' the importScript, like so:

var wd_useful_thisis_add = [
				[ 'village' , 'P31:Q532' ] , // instance of: village
				[ 'PD-100' , 'P6216:Q19652|P459:Q29940705' ] // copyright status:public domain, with qualifier determination method:100 years after author's death
				// You can add more (item only!) qualifiers by adding them with "|"
] ;
*/

var wd_useful_thisis_langs = {} ;
wd_useful_thisis_langs['en'] = [
	[
		{
			pre : 'a ' ,
			options : [
				[ '<b>human</b>' , 'P31:Q5' ] ,
				[ '<b>fictional character </b>' , 'P31:Q95074' ] ,
				[ 'man' , 'P21:Q6581097' , 'P31:Q5' ] , // , 'P107:Q215627'
				[ 'woman' , 'P21:Q6581072' , 'P31:Q5' ] , // , 'P107:Q215627' ,
				[ 'intersex' , 'P21:Q1097630' ] ,
				[ 'transgender male' , 'P21:Q15145783 ' ] ,
				[ 'transgender female ' , 'P21:Q15145782' ] ,
				[ 'duo ' , 'P31:Q10648343' ] ,
			]
		} ,
		
		{
			pre : 'Jobs:' ,
			options : [
				[ 'actor' , 'P106:Q33999' ] ,
				[ 'artist' , 'P106:Q483501' ] ,
				[ 'director' , 'P106:Q2526255' ] ,
				[ 'economist' , 'P106:Q188094' ] ,
				[ 'lawyer' , 'P106:Q40348' ] ,
				[ 'writer' , 'P106:Q36180' ] ,
				[ 'painter' , 'P106:Q1028181' ] ,
				[ 'politician' , 'P106:Q82955' ] ,
				[ 'photographer' , 'P106:Q33231' ] ,
				[ 'musician' , 'P106:Q639669' ] ,
				[ 'journalist' , 'P106:Q1930187' ] ,
				[ 'mathematician' , 'P106:Q170790' ] ,
				[ 'sculptor' , 'P106:Q1281618' ] ,
				[ 'scientist' , 'P106:Q901' ] ,
				[ 'singer' , 'P106:Q177220' ] ,
				[ 'sports journalist' , 'P106:Q13219447' ] ,
			]
		} ,
		
		{
			pre : 'Sport:' ,
			options : [
				[ 'sportsperson' , 'P106:Q2066131' ] ,
				[ 'baseball' , 'P106:Q10871364' ] ,
				[ 'basketball' , 'P106:Q3665646' ] ,
				[ 'bicycle racer ' , 'P106:Q2309784' ] ,
				[ 'cricket' , 'P106:Q12299841' ] ,
				[ 'football' , 'P106:Q937857' ] ,
				[ 'track cyclist' , 'P106:Q15117395' ] ,
				[ 'softball' , 'P106:Q13388586' ] ,
				[ 'volley' , 'P106:Q15117302' ] ,
			]
		} ,
		
		{
			pre : 'a ' ,
			options : [
				[ 'species' , 'P105:Q7432' , 'P31:Q16521' ] ,
				[ 'genus' , 'P105:Q34740' , 'P31:Q16521' ] ,
				[ 'family' , 'P105:Q35409' , 'P31:Q16521' ] ,
				[ 'order' , 'P105:Q36602' , 'P31:Q16521' ] ,
				[ 'class' , 'P105:Q37517' , 'P31:Q16521' ] ,
			]
		} ,
		
		{
			pre : 'a ' ,
			options : [
//				[ '<b>location</b>' , 'P107:Q618123' ] ,
				[ 'mountain' , 'P31:Q8502' ] ,
				[ 'mountain range' , 'P31:Q46831' ] ,
				[ 'river' , 'P31:Q4022' ] ,
				[ 'lake' , 'P31:Q23397' ] ,
				[ 'railway station' , 'P31:Q55488' ] ,
				[ 'bridge' , 'P31:Q12280' ] ,
			]
		} ,
		
		{
			pre : 'a ' ,
			options : [
//				[ '<b>work</b>' , 'P107:Q386724' ] ,
				[ 'movie' , 'P31:Q11424' ] ,
				[ 'album' , 'P31:Q482994' ] ,
				[ 'song' , 'P31:Q7366' ] ,
				[ 'book' , 'P31:Q571' ] ,
				[ 'novel' , 'P31:Q571' , 'P136:Q8261' ] ,
				[ 'fictional character' , 'P31:Q95074' ] ,
				[ 'video game' , 'P31:Q7889' ] ,
				[ 'TV series' , 'P31:Q5398426' ] ,
				[ 'play' , 'P31:Q25379' ] ,
			]
		} ,

		{
			pre : 'an ' ,
			options : [
//				[ '<b>organization</b>' , 'P107:Q43229' ] ,
				[ 'band' , 'P31:Q215380' ] ,
				[ 'rock band' , 'P31:Q5741069' ] ,
				[ 'Category' , 'P31:Q4167836' ] ,
				[ 'Template' , 'P31:Q11266439' ] ,
				[ 'List' , 'P31:Q13406463' ] ,
				[ 'Disambig' , 'P31:Q4167410' ] ,
			]
		} ,
/*		
		{
			pre : 'an ' ,
			options : [
				[ '<b>event</b>' , 'P107:Q1656682' ] ,
				[ '<b>term</b>' , 'P107:Q1969448' ] ,
				[ '<b>disambiguation</b>' , 'P107:Q11651459' ] ,
			]
		} ,
*/		
	]
] ;

wd_useful_thisis_langs['fr'] = [
	[
		{
			pre : 'Sexe : ' ,
			options : [
				[ '<b>être humain</b>' , 'P31:Q5' ] ,
				[ '<b>pers. de fiction </b>' , 'P31:Q95074' ] ,
				[ 'homme' , 'P21:Q6581097' ] ,//'P107:Q215627'
				[ 'femme' , 'P21:Q6581072' ] ,//'P107:Q215627'
			]
		} ,
		
		{
			pre : 'Profession : ' ,
			options : [
				[ 'acteur' , 'P106:Q33999' ] ,
				[ 'artiste' , 'P106:Q483501' ] ,
				[ 'aviateur' , 'P106:Q2095549' ] ,
				[ 'avocat' , 'P106:Q40348' ] ,
				[ 'chanteur' , 'P106:Q177220' ] ,
				[ 'compositeur' , 'P106:Q36834' ] ,
				[ 'dramaturge' , 'P106:Q214917' ] ,
				[ 'économiste' , 'P106:Q188094' ] ,
				[ 'écrivain' , 'P106:Q36180' ] ,
				[ 'explorateur' , 'P106:Q11900058' ] ,
				[ 'historien' , 'P106:Q201788' ] ,
				[ 'ingénieur' , 'P106:Q81096' ] ,
				[ 'inventeur' , 'P106:Q205375' ] ,
				[ 'journaliste' , 'P106:Q1930187' ] ,
				[ 'journaliste sportif' , 'P106:Q13219447' ] ,
				[ 'librettiste' , 'p106:Q8178443' ] ,
				[ 'mannequin' , 'p106:Q4610556' ] ,
				[ 'mathématicien' , 'P106:Q170790' ] ,
				[ 'militaire' , 'p106:Q4991371' ] ,
				[ 'musicien' , 'P106:Q639669' ] ,
				[ 'officier' , 'P106:Q189290' ] ,
				[ 'peintre' , 'P106:Q1028181' ] ,
				[ 'photographe' , 'P106:Q33231' ] ,
				[ 'poète' , 'P106:Q49757' ] ,
				[ 'politicien' , 'P106:Q82955' ] ,
				[ 'prêtre catholique' , 'P106:Q250867' ] ,
				[ 'réalisateur' , 'P106:Q2526255' ] ,
				[ 'sculpteur' , 'P106:Q1281618' ] ,
			]
		} ,

		{
			pre : 'Fonction : ' ,
			options : [
				[ 'député français' , 'P39:Q3044918' ] ,
				[ 'sénateur français' , 'P39:Q14828018' ] ,
				[ 'maire français' , 'P39:Q382617' ] ,
				[ 'évêque' , 'P39:Q29182' ] ,
				[ 'archevêque' , 'P39:Q49476' ] ,
				[ 'cardinal' , 'P39:Q45722' ] ,
				[ 'pape' , 'P39:Q19546' ] ,
			]
		} ,

		{
			pre : 'Sport :' ,
			options : [
				[ 'sportif' , 'P106:Q2066131' ] ,
				[ 'basket-ball' , 'P106:Q3665646' ] ,
				[ 'football' , 'P106:Q937857' ] ,
				[ 'cricket' , 'P106:Q5185059' ] ,
				[ 'cycliste ' , 'P106:Q2309784' ] ,
				[ 'cycliste sur piste' , 'P106:Q15117395' ] ,
				[ 'baseball' , 'P106:Q10871364' ] ,
				[ 'rugby' , 'p106:q13415036' ] ,
				[ 'softball' , 'P106:Q13388586' ] ,
				[ 'volley' , 'P106:Q15117302' ] ,
			]
		} ,
		
		{
			pre : 'Une : ' ,
			options : [
				[ 'espèce' , 'P105:Q7432' ] ,//'P107:Q1969448'
				[ 'genre' , 'P105:Q34740' ] ,
				[ 'famille' , 'P105:Q35409' ] ,
				[ 'ordre' , 'P105:Q36602' ] ,
				[ 'classe' , 'P105:Q37517' ] ,
			]
		} ,
		
		{
			pre : 'Lieu : ' ,
			options : [
			    //[ '<b>lieu</b>' , 'P107:Q618123' ] ,
				[ 'cordillière' , 'P31:Q46831' ] ,
				[ 'lac' , 'P31:Q23397' ] ,
				[ 'montagne' , 'P31:Q8502' ] ,
				[ 'rivière ' , 'P31:Q4022' ] ,
				[ ' château' , 'P31:Q751876' ] ,
				[ 'château fort' , 'P31:Q23413' ] ,
				[ 'manoir ' , 'P31:Q879050' ] ,
				[ ' abbaye' , 'P31:Q160742' ] ,
				[ 'basilique' , 'P31:Q163687' ] ,
				[ 'cathédrale' , 'P31:Q2977' ] ,
				[ 'chapelle' , 'P31:Q108325' ] ,
				[ 'église ' , 'P31:Q16970' ] ,
				[ ' gare ferroviaire' , 'P31:Q55488' ] ,
				[ 'pont' , 'P31:Q12280' ] ,
			]
		} ,
		
		{
			pre : 'Œuvre : ' ,
			options : [
				//[ '<b>œuvre</b>' , 'P107:Q386724' ] ,
				[ 'film' , 'P31:Q11424' ] ,
				[ 'album' , 'P31:Q482994' ] ,
				[ 'chanson' , 'P31:Q7366' ] ,
				[ 'livre' , 'P31:Q571' ] ,
				[ 'roman' , 'P31:Q571' , 'P136:Q8261' ] ,
				[ 'jeu de cartes' , 'P31:Q142714' ] ,
				[ 'jeu de société' , 'P31:Q131436' ] ,
				[ 'jeu vidéo' , 'P31:Q7889' ] ,
				[ 'série TV' , 'P31:Q5398426' ] ,
				[ 'théâtre' , 'P31:Q25379' ] ,
			]
		} ,

		{
			pre : 'Un : ' ,
			options : [
				//[ '<b>organisation</b>' , 'P107:Q43229' ] ,
				[ 'groupe de musique' , 'P31:Q215380' ] ,
				[ 'groupe de rock' , 'P31:Q5741069' ] ,
			]
		} ,
		
		{
			pre : 'Wiki : ' ,
			options : [
				[ 'catégorie' , 'P31:Q4167836' ] ,
				[ 'modèle' , 'P31:Q11266439' ] ,
				[ 'liste' , 'P31:Q13406463' ] ,
				[ 'homonymie' , 'P31:Q4167410' ] ,
			]
		} ,
		
	]
] ;

wd_useful_thisis_langs['it'] = [
	[
		{
			pre : 'Sesso : ' ,
			options : [
				[ '<b>essere umano</b>' , 'P31:Q5' ] ,
				[ '<b>pers. immaginario </b>' , 'P31:Q95074' ] ,
				[ 'maschio' , 'P21:Q6581097' ] ,//'P107:Q215627'
				[ 'femmina' , 'P21:Q6581072' ] ,//'P107:Q215627'
				[ 'intersessuato' , 'P21:Q1097630' ] ,
				[ 'transessuale uomo' , 'P21:Q15145783) ' ] ,
				[ 'transessuale femmina' , 'P21:Q15145782' ] ,
			]
		} ,
 
		{
			pre : 'Professione : ' ,
			options : [
				[ 'attore' , 'P106:Q33999' ] ,
				[ 'artista' , 'P106:Q483501' ] ,
				[ 'aviatore' , 'P106:Q2095549' ] ,
				[ 'avvocato' , 'P106:Q40348' ] ,
				[ 'cantante' , 'P106:Q177220' ] ,
				[ 'compositore' , 'P106:Q36834' ] ,
				[ 'drammaturgo' , 'P106:Q214917' ] ,
				[ 'economista' , 'P106:Q188094' ] ,
				[ 'esploratore' , 'P106:Q11900058' ] ,
				[ 'fotografo' , 'P106:Q33231' ] ,
				[ 'giornalista' , 'P106:Q1930187' ] ,
				[ 'giornalista sportivo' , 'P106:Q13219447' ] ,
				[ 'ingegnere' , 'P106:Q81096' ] ,
				[ 'inventore' , 'P106:Q205375' ] ,
				[ 'librettista' , 'p106:Q8178443' ] ,
				[ 'matematico' , 'P106:Q170790' ] ,
				[ 'modella' , 'p106:Q4610556' ] ,
				[ 'militare' , 'p106:Q4991371' ] ,
				[ 'musicista' , 'P106:Q639669' ] ,
				[ 'pittore' , 'P106:Q1028181' ] ,
				[ 'poeta' , 'P106:Q49757' ] ,
				[ 'prete cattolico' , 'P106:Q250867' ] ,
				[ 'politico' , 'P106:Q82955' ] ,
				[ 'regista cinematografico' , 'P106:Q2526255' ] ,
				[ 'regista teatrale' , 'P106:Q3387717' ] ,
				[ 'regista televisivo' , 'P106:Q2059704' ] ,
				[ 'scrittore' , 'P106:Q36180' ] ,
				[ 'scultore' , 'P106:Q1281618' ] ,
				[ 'storico' , 'P106:Q201788' ] ,
				[ 'ufficiale militare' , 'P106:Q189290' ] ,
			]
		} ,
 
		{
			pre : 'Funzione : ' ,
			options : [
				[ 'deputato' , 'P39:Q1055894' ] ,
				[ 'senatore' , 'P39:Q1672074' ] ,
				[ 'sindaco' , 'P39:Q30185' ] ,
				[ 'vescovo' , 'P39:Q29182' ] ,
				[ 'arcivescovo' , 'P39:Q49476' ] ,
				[ 'cardinale' , 'P39:Q45722' ] ,
				[ 'papa' , 'P39:Q19546' ] ,
			]
		} ,
 
		{
			pre : 'Sport :' ,
			options : [
				[ 'atleta' , 'P106:Q2066131' ] ,
				[ 'baseball' , 'P106:Q10871364' ] ,
				[ 'calciatore' , 'P106:Q937857' ] ,
				[ 'cestista' , 'P106:Q3665646' ] ,
				[ 'ciclista' , 'P106:Q2309784' ] ,
				[ 'ciclista su pista' , 'P106:Q15117395' ] ,
				[ 'cricket' , 'P106:Q5185059' ] ,
				[ 'pallavolista' , 'P106:Q15117302' ] ,
				[ 'rugbysta' , 'p106:q13415036' ] ,
				[ 'softball' , 'P106:Q13388586' ] ,
			]
		} ,
 
		{
			pre : 'Un : ' ,
			options : [
				[ 'specie' , 'P105:Q7432' ] ,//'P107:Q1969448'
				[ 'genere' , 'P105:Q34740' ] ,
				[ 'famiglia' , 'P105:Q35409' ] ,
				[ 'ordine' , 'P105:Q36602' ] ,
				[ 'classe' , 'P105:Q37517' ] ,
			]
		} ,
 
		{
			pre : 'Luogo : ' ,
			options : [
			    //[ '<b>lieu</b>' , 'P107:Q618123' ] ,
				[ 'catena montuosa' , 'P31:Q46831' ] ,
				[ 'lago' , 'P31:Q23397' ] ,
				[ 'montagna' , 'P31:Q8502' ] ,
				[ 'fiume' , 'P31:Q4022' ] ,
				[ 'castello' , 'P31:Q751876' ] ,
				[ 'forte' , 'P31:Q23413' ] ,
				[ 'maniero' , 'P31:Q879050' ] ,
				[ 'abbazia' , 'P31:Q160742' ] ,
				[ 'basilica' , 'P31:Q163687' ] ,
				[ 'cattedrale' , 'P31:Q2977' ] ,
				[ 'cappella' , 'P31:Q108325' ] ,
				[ 'chiesa ' , 'P31:Q16970' ] ,
				[ 'stazione ferroviria' , 'P31:Q55488' ] ,
				[ 'ponte' , 'P31:Q12280' ] ,
			]
		} ,
 
		{
			pre : 'Un : ' ,
			options : [
				//[ '<b>œuvre</b>' , 'P107:Q386724' ] ,
				[ 'film' , 'P31:Q11424' ] ,
				[ 'album' , 'P31:Q482994' ] ,
				[ 'canzone' , 'P31:Q7366' ] ,
				[ 'libro' , 'P31:Q571' ] ,
				[ 'romanzo' , 'P31:Q571' , 'P136:Q8261' ] ,
				[ 'gioco di carte' , 'P31:Q142714' ] ,
				[ 'gioco di società' , 'P31:Q131436' ] ,
				[ 'videogioco' , 'P31:Q7889' ] ,
				[ 'serie tv' , 'P31:Q5398426' ] ,
				[ 'opera teatrale' , 'P31:Q25379' ] ,
			]
		} ,
 
		{
			pre : 'Un : ' ,
			options : [
				//[ '<b>organisation</b>' , 'P107:Q43229' ] ,
				[ 'gruppo musicale' , 'P31: Q215380' ] ,
				[ 'gruppo rock' , 'P31: Q5741069' ] ,
			]
		} ,
 
		{
			pre : 'Wiki : ' ,
			options : [
				[ 'categoria' , 'P31:Q4167836' ] ,
				[ 'template' , 'P31:Q11266439' ] ,
				[ 'liste' , 'P31:Q13406463' ] ,
				[ 'Disambigua' , 'P31:Q4167410' ] ,
			]
		} ,
 
	]
] ;

wd_useful_thisis_langs['nb'] = [
	[
		{
			pre : 'en/et ' ,
			options : [
				[ '<b>menneske</b>' , 'P31:Q5' ] ,
				[ '<b>rollefigur </b>' , 'P31:Q95074' ] ,
				[ 'mann' , 'P21:Q6581097' , 'P31:Q5' ] , // , 'P107:Q215627'
				[ 'kvinne' , 'P21:Q6581072' , 'P31:Q5' ] , // , 'P107:Q215627' ,
				[ 'duo ' , 'P31:Q10648343' ] ,
			]
		} ,
		
		{
			pre : 'Yrker:' ,
			options : [
				[ 'skuespiller' , 'P106:Q33999' ] ,
				[ 'artist' , 'P106:Q483501' ] ,
				[ 'regissør' , 'P106:Q2526255' ] ,
				[ 'økonom' , 'P106:Q188094' ] ,
				[ 'advokat' , 'P106:Q40348' ] ,
				[ 'forfatter' , 'P106:Q482980' ] ,
				[ 'maler' , 'P106:Q1028181' ] ,
				[ 'politiker' , 'P106:Q82955' ] ,
				[ 'fotograf' , 'P106:Q33231' ] ,
				[ 'musiker' , 'P106:Q639669' ] ,
				[ 'journalist' , 'P106:Q1930187' ] ,
				[ 'matematiker' , 'P106:Q170790' ] ,
				[ 'skulptør' , 'P106:Q1281618' ] ,
				[ 'sanger' , 'P106:Q177220' ] ,
				[ 'sportsjournalist' , 'P106:Q13219447' ] ,
			]
		} ,
		
		{
			pre : 'Sport:' ,
			options : [
				[ 'idrettsutøver' , 'P106:Q2066131' ] ,
//				[ 'baseball' , 'P106:Q10871364' ] ,
				[ 'basketball' , 'P106:Q3665646' ] ,
				[ 'sykler' , 'P106:Q2309784' ] ,
//				[ 'cricket' , 'P106:Q12299841' ] ,
				[ 'fotball' , 'P106:Q937857' ] ,
				[ 'håndball' , 'P106:Q13365117' ],
				[ 'ishockey' , 'P106:Q11774891' ],
				[ 'golf' , 'P106:Q13156709' ],
//				[ 'track cyclist' , 'P106:Q15117395' ] ,
//				[ 'softball' , 'P106:Q13388586' ] ,
//				[ 'volley' , 'P106:Q15117302' ] ,
				[ 'langrenn' , 'P106:Q13382608' ] ,
				[ 'skiskytter' , 'P106:Q16029547' ],
				[ 'alpinist' , 'P106:Q4144610' ],
				[ 'skihopper', 'P106:Q13382603' ],
				[ 'skøyter' , 'P106:Q10866633' ] ,
				[ 'friidrett', 'P106:Q11513337' ] ,
			]
		} ,
		
		{
			pre : 'en ' ,
			options : [
				[ 'art' , 'P105:Q7432' ] , // , 'P107:Q1969448'
				[ 'slekt' , 'P105:Q34740' ] ,
				[ 'familie' , 'P105:Q35409' ] ,
				[ 'orden' , 'P105:Q36602' ] ,
				[ 'klasse' , 'P105:Q37517' ] ,
			]
		} ,
		
		{
			pre : 'en/et ' ,
			options : [
//				[ '<b>location</b>' , 'P107:Q618123' ] ,
				[ 'fjell' , 'P31:Q8502' ] ,
				[ 'fjellkjede' , 'P31:Q46831' ] ,
				[ 'elv' , 'P31:Q4022' ] ,
				[ 'innsjø' , 'P31:Q23397' ] ,
				[ 'jernbanestasjon' , 'P31:Q55488' ] ,
				[ 'bro' , 'P31:Q12280' ] ,
			]
		} ,
		
		{
			pre : 'en/ei/et ' ,
			options : [
//				[ '<b>work</b>' , 'P107:Q386724' ] ,
				[ 'film' , 'P31:Q11424' ] ,
				[ 'album' , 'P31:Q482994' ] ,
				[ 'sang' , 'P31:Q7366' ] ,
				[ 'bok' , 'P31:Q571' ] ,
				[ 'roman' , 'P31:Q571' , 'P136:Q8261' ] ,
				[ 'rollefigur' , 'P31:Q95074' ] ,
				[ 'videospill' , 'P31:Q7889' ] ,
				[ 'TV-serie' , 'P31:Q5398426' ] ,
				[ 'skuespill' , 'P31:Q25379' ] ,
			]
		} ,

		{
			pre : 'en/et ' ,
			options : [
//				[ '<b>organization</b>' , 'P107:Q43229' ] ,
				[ 'band' , 'P31:Q215380' ] ,
				[ 'rockeband' , 'P31:Q5741069' ] ,
				[ 'Kategori' , 'P31:Q4167836' ] ,
				[ 'Mal' , 'P31:Q11266439' ] ,
				[ 'Liste' , 'P31:Q13406463' ] ,
				[ 'Peker' , 'P31:Q4167410' ] ,
			]
		} ,
/*		
		{
			pre : 'an ' ,
			options : [
				[ '<b>event</b>' , 'P107:Q1656682' ] ,
				[ '<b>term</b>' , 'P107:Q1969448' ] ,
				[ '<b>disambiguation</b>' , 'P107:Q11651459' ] ,
			]
		} ,
*/		
	]
] ;

/************************************************************************************************************************
The section below contains the "Wikidata Usefuls" country list. Add new ones or edit existing ones as you see fit.
*************************************************************************************************************************/

var wd_useful_countries = {
	"---" : "",
	"Afghanistan" : "Q889",
	"Albania" : "Q222",
	"Algeria" : "Q262",
	"Andorra" : "Q228",
	"Angola" : "Q916",
	"Antigua and Barbuda" : "Q781",
	"Argentina" : "Q414",
	"Armenia" : "Q399",
	"Australia" : "Q408",
	"Austria" : "Q40",
	"Azerbaijan" : "Q227",
	"Bahamas" : "Q778",
	"Bahrain" : "Q398",
	"Bangladesh" : "Q902",
	"Barbados" : "Q244",
	"Belarus" : "Q184",
	"Belgium" : "Q31",
	"Belize" : "Q242",
	"Benin" : "Q962",
	"Bhutan" : "Q917",
	"Bolivia" : "Q750",
	"Bosnia and Herzegovina" : "Q225",
	"Botswana" : "Q963",
	"Brazil" : "Q155",
	"Brunei" : "Q921",
	"Bulgaria" : "Q219",
	"Burkina Faso" : "Q965",
	"Burundi" : "Q967",
	"Cambodia" : "Q424",
	"Cameroon" : "Q1009",
	"Canada" : "Q16",
	"Cape Verde" : "Q1011",
	"Central African Republic" : "Q929",
	"Chad" : "Q657",
	"Chile" : "Q298",
	"China" : "Q148",
	"Colombia" : "Q739",
	"Comoros" : "Q970",
	"Costa Rica" : "Q800",
	"Côte d'Ivoire" : "Q1008",
	"Croatia" : "Q224",
	"Cuba" : "Q241",
	"Cyprus" : "Q229",
	"Czech Republic" : "Q213",
	"Democratic Republic of the Congo" : "Q974",
	"Denmark" : "Q35",
	"Djibouti" : "Q977",
	"Dominica" : "Q784",
	"Dominican Republic" : "Q786",
	"East Timor" : "Q574",
	"Ecuador" : "Q736",
	"Egypt" : "Q79",
	"El Salvador" : "Q792",
	"Equatorial Guinea" : "Q983",
	"Eritrea" : "Q986",
	"Estonia" : "Q191",
	"Ethiopia" : "Q115",
	"Federated States of Micronesia" : "Q702",
	"Fiji" : "Q712",
	"Finland" : "Q33",
	"France" : "Q142",
	"Gabon" : "Q1000",
	"Gambia" : "Q1005",
	"Georgia" : "Q230",
	"Germany" : "Q183",
	"Ghana" : "Q117",
	"Greece" : "Q41",
	"Grenada" : "Q769",
	"Guatemala" : "Q774",
	"Guinea" : "Q1006",
	"Guinea-Bissau" : "Q1007",
	"Guyana" : "Q734",
	"Haiti" : "Q790",
	"Honduras" : "Q783",
	"Hungary" : "Q28",
	"Iceland" : "Q189",
	"India" : "Q668",
	"Indonesia" : "Q252",
	"Iran" : "Q794",
	"Iraq" : "Q796",
	"Ireland" : "Q27",
	"Israel" : "Q801",
	"Italy" : "Q38",
	"Jamaica" : "Q766",
	"Japan" : "Q17",
	"Jordan" : "Q810",
	"Kazakhstan" : "Q232",
	"Kenya" : "Q114",
	"Kiribati" : "Q710",
	"Kuwait" : "Q817",
	"Kyrgyzstan" : "Q813",
	"Laos" : "Q819",
	"Latvia" : "Q211",
	"Lebanon" : "Q822",
	"Lesotho" : "Q1013",
	"Liberia" : "Q1014",
	"Libya" : "Q1016",
	"Liechtenstein" : "Q347",
	"Lithuania" : "Q37",
	"Luxembourg" : "Q32",
	"Macedonia" : "Q221",
	"Madagascar" : "Q1019",
	"Malawi" : "Q1020",
	"Malaysia" : "Q833",
	"Maldives" : "Q826",
	"Mali" : "Q912",
	"Malta" : "Q233",
	"Marshall Islands" : "Q709",
	"Mauritania" : "Q1025",
	"Mauritius" : "Q1027",
	"Mexico" : "Q96",
	"Moldova" : "Q217",
	"Mongolia" : "Q711",
	"Montenegro" : "Q236",
	"Monaco" : "Q235",
	"Morocco" : "Q1028",
	"Mozambique" : "Q1029",
	"Myanmar" : "Q836",
	"Namibia" : "Q1030",
	"Nauru" : "Q697",
	"Nepal" : "Q837",
	"Netherlands" : "Q55",
	"New Zealand" : "Q664",
	"Nicaragua" : "Q811",
	"Niger" : "Q1032",
	"Nigeria" : "Q1033",
	"North Korea" : "Q423",
	"Norway" : "Q20",
	"Oman" : "Q842",
	"Pakistan" : "Q843",
	"Palestine": "Q219060",
	"Palau" : "Q695",
	"Panama" : "Q804",
	"Papua New Guinea" : "Q691",
	"Paraguay" : "Q733",
	"Peru" : "Q419",
	"Philippines" : "Q928",
	"Poland" : "Q36",
	"Portugal" : "Q45",
	"Qatar" : "Q846",
	"Republic of the Congo" : "Q971",
	"Romania" : "Q218",
	"Russia" : "Q159",
	"Rwanda" : "Q1037",
	"Saint Kitts and Nevis" : "Q763",
	"Saint Lucia" : "Q760",
	"Saint Vincent and the Grenadines" : "Q757",
	"Samoa" : "Q683",
	"San Marino" : "Q238",
	"São Tomé and Príncipe" : "Q1039",
	"Saudi Arabia" : "Q851",
	"Senegal" : "Q1041",
	"Serbia" : "Q403",
	"Seychelles" : "Q1042",
	"Sierra Leone" : "Q1044",
	"Singapore" : "Q334",
	"Slovakia" : "Q214",
	"Slovenia" : "Q215",
	"Solomon Islands" : "Q685",
	"Somalia" : "Q1045",
	"South Africa" : "Q258",
	"South Korea" : "Q884",
	"South Sudan" : "Q958",
	"Spain" : "Q29",
	"Sri Lanka" : "Q854",
	"Sudan" : "Q1049",
	"Suriname" : "Q730",
	"Swaziland" : "Q1050",
	"Sweden" : "Q34",
	"Switzerland" : "Q39",
	"Syria" : "Q858",
	"Taiwan (Republic of China)" : "Q865",
	"Tajikistan" : "Q863",
	"Tanzania" : "Q924",
	"Thailand" : "Q869",
	"Togo" : "Q945",
	"Tonga" : "Q678",
	"Trinidad and Tobago" : "Q754",
	"Tunisia" : "Q948",
	"Turkey" : "Q43",
	"Turkmenistan" : "Q874",
	"Tuvalu" : "Q672",
	"Uganda" : "Q1036",
	"Ukraine" : "Q212",
	"United Arab Emirates" : "Q878",
	"United Kingdom" : "Q145",
	"United States" : "Q30",
	"Uruguay" : "Q77",
	"Uzbekistan" : "Q265",
	"Vanuatu" : "Q686",
	"Vatican City" : "Q237",
	"Venezuela" : "Q717",
	"Vietnam" : "Q881",
	"Yemen" : "Q805",
	"Zambia" : "Q953",
	"Zimbabwe" : "Q954",
	/* Some former countries that are often encountered */
	"Czechoslovakia" : "Q33946",
	"East Germany" : "Q16957",
	"Soviet Union" : "Q15180",
	"Yugoslavia" : "Q36704"
} ;

/************************************************************************************************************************
DO NOT EDIT BELOW THIS LINE UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!
*************************************************************************************************************************/

if ( typeof ( $ ) === 'undefined' ) $ = $ ;

var wd_useful_thisis ;
var user_lang = mw.config.get( 'wgUserLanguage' );
if ( undefined === user_lang || undefined === wd_useful_thisis_langs[user_lang] ) wd_useful_thisis = wd_useful_thisis_langs['en'] ; // Fallback
else wd_useful_thisis = wd_useful_thisis_langs[user_lang] ;

function ucFirst(string) {
	return string.substring(0, 1).toUpperCase() + string.substring(1);
}

var wd_useful = {

	q_prefix : 'Q' ,
	p_prefix : 'P' ,
	id_cnt : 0 ,
	api : '/w/api.php' ,
	link2data : [] ,
	bad_images : [ 'Blue morpho butterfly 300x271.jpg' ] , // Replace all underscores with spaces!!!
	langpref : ['de','nds','en'] ,
	
	countries : {} ,
	
	addPropsFromLink : function ( o ) {
		var self = this ;
		var lid = $(o).attr('lid') ;
//		console.log ( self.link2data[lid] ) ;
		var is_running = self.queue.length > 0 ;
		$.each ( self.link2data[lid]||[] , function ( k , v ) {
			let qualifiers = v.split ( /\|/ ) ;
			v = qualifiers.shift() ;
			var pq = v.split ( ':' ) ;
			if ( pq.length != 2 ) {
				console.log ( "PROBLEM : " + v ) ;
				return ;
			}
			self.queue.push ( { mode:'set' , prop:pq[0] , target_entity:pq[1] , qualifiers:qualifiers } ) ;
		} ) ;
		if ( !is_running ) {
			self.processed = 0 ;
			self.processNextQueueItem () ;
		}
		return false ;
	} ,

	init : function () {
		var self = this ;
		mw.loader.using( 'jquery.ui', function () {	
			var q = mw.config.get('wgPageName').toUpperCase() ;
			self.q = q ;
			self.queue = [] ;
			if ( typeof wd_useful_thisis_add != 'undefined' ) {
				wd_useful_thisis[0].push ( { pre:'' , options:wd_useful_thisis_add } ) ;
			}	
			self.setTitleTool();
			var h = "<div id='wd_useful_dialog'>" ;
			h += "<div>" ;
			
			var lid = 0 ;
			var maxw = 320 ;
			$.each ( wd_useful_thisis , function ( k1 , v1 ) {
				h += "<div style='margin-bottom:5px'>" ;
				$.each ( v1 , function ( k2 , v2 ) {
					var indent = k2 == 0 ? 10 : 15 ;
					h += "<div style='max-width:"+maxw+"px;text-indent:-10px;margin-left:"+indent+"px;margin-bottom:3px'>" ;
					if ( v2.pre !== undefined ) h += v2.pre ;
					$.each ( v2.options , function ( k3 , v3 ) {
						var title = v3.shift() ;
						self.link2data[lid] = v3 ;
						var h2 = "<a href='#' lid='" + lid + "' onclick='wd_useful.addPropsFromLink(this);return false;'>" + title + "</a>" ;
						if ( k3+1 < v2.options.length ) h2 += '/' ;
						h += "<nobr>" + h2 + '</nobr>' ;
						h += '<wbr/>' ;
						lid++ ;
					} ) ;
					if ( v2.post !== undefined ) h += v2.post ;
					h += "</div>" ;
				} ) ;
				h += "</div>" ;
			} ) ;
	
			h += "<hr/>" ;
			h += "<a href='#' onclick='$(\"#country\").val(\"Q30\");return false'>U.S.</a>" ;
			h += "|<a href='#' onclick='$(\"#country\").val(\"Q145\");return false'>U.K.</a>" ;
			h += "|<a href='#' onclick='$(\"#country\").val(\"Q183\");return false'>Germany</a>" ;
			h += "|<a href='#' onclick='$(\"#country\").val(\"Q142\");return false'>France</a>" ;
			h += "|<a href='#' onclick='$(\"#country\").val(\"Q29\");return false'>Spain</a>" ;
			h += "|<a href='#' onclick='$(\"#country\").val(\"Q38\");return false'>Italy</a>" ;
			h += "|<a href='#' onclick='$(\"#country\").val(\"Q55\");return false'>Netherlands</a>" ;
			h += "<br/><select id='country'></select>" ;
			h += "<br/>" ;
			h += "<a href='#' onclick='wd_useful.useCountry(17);return false'>Country</a> | " ;
			h += "<a href='#' onclick='wd_useful.useCountry(27);return false'>C. of citizenship | </a>" ;
			h += "<a href='#' onclick='wd_useful.useCountry(495);return false'>C. of origin</a>" ;
			h += "<hr/>" ;
			h += "<div><a href='#' onclick='wd_useful.duplicateStatements();return false'>Duplicate statements to en category</a></div>" ;
			h += "<div><a href='#' onclick='wd_useful.duplicateStatementsFromItemList();return false'>Duplicate statements for items linked on Wikidata page</a></div>" ;
			h += "<div><a href='#' onclick='wd_useful.showImages();return false'>Show/add images used on Wikipedia</a></div>" ;
			h += "<div><a href='#' onclick='consistency_check.run();return false'>Check Consistency</a></div>" ;
			h += "<div><a href='#' onclick='consistency_check.reverse();return false'>Check Symmetry</a></div>" ;
			h += "</div>" ;

			h += "</div>" ;
			$('#mw-content-text').append ( h ) ;

			self.loadCountries () ;
			self.tryDesc() ;
			self.autoName() ;
			self.tryAutoAdd() ;
			self.autoRenameStatements () ;
			self.showQ() ;
			
			setTimeout ( function () {
				var main = $('#wb-item-'+self.q.toUpperCase()) ;
				$('#wd_useful_dialog').dialog ( {
					modal : false ,
					title : 'Wikidata Usefuls' ,
					width : 'auto' ,
					position : { my: "left top", at: "right top", of: $('#claims') } , // wb-ui-descriptionedittool
					open: function( event, ui ) {
						$('#wd_useful_dialog a').css ( { color:'#0b0080' } ) ;
						main.focus() ;
					}
				} ) ;
				$('#wd_useful_dialog a').blur();
			} , 300 ) ;
		} ) ;
	} ,
	
	showImages : function () {
		var self = this ;
		self.image_container_id = 'wd_wp_images' ;
		self.thumbsize = 128 ;
		var h = "<div id='" + self.image_container_id + "'></div>" ;
		$($('div.wikibase-listview').get(0)).prepend ( h ) ;
		
		self.images = {} ;
		
		var cnt = 0 ;
		var finish = function () {
			cnt-- ;
			if ( cnt > 0 ) return ; // Not done
			var found_any = false ;
			$.each ( self.images , function() { found_any = true } ) ;
			if ( found_any ) {
				$('#'+self.image_container_id).append ( '<i>All&nbsp;images&nbsp;loaded.</i>' ) ;
			} else {
				$('#'+self.image_container_id).append ( '<i>No images found.</i>' ) ;
			}
		} ;
		
		$.each ( (self.sitelinks||{}) , function ( wiki , v0 ) {
//		$.each ( ({enwiki:{title:"Peter_David"}}) , function ( wiki , v0 ) { // TESTING
			var lang = wiki.replace(/wiki$/,'') ;
			var title = v0.title ;
			cnt++ ;
			$.getJSON ( '//'+lang+'.wikipedia.org/w/api.php?callback=?' , {
				action:'query' ,
				generator:'images' ,
				titles:title ,
				gimlimit:500 ,
				prop:'imageinfo' ,
				iiprop:'url|size' ,
				iiurlwidth:self.thumbsize ,
				iiurlheight:self.thumbsize ,
				format:'json'
			} , function ( data ) {
				if ( data.query === undefined ) return finish() ;
				$.each ( (data.query.pages||{}) , function ( pid , page ) {
					if ( page.imagerepository != 'shared' ) return ; // Commons only
					if ( null != page.title.match ( /\.svg/i ) ) return ;
					if ( null != page.title.replace(/_/g,' ').match ( /\bstub\b/i ) ) return ;
					self.addImage ( page.title , page.imageinfo[0] ) ;
				} ) ;
				finish() ;
			} ) ;
		} ) ;
	} ,
	
	addImage : function ( file , imageinfo ) {
		var self = this ;
		var title = file.replace(/^[^:]+:/i,'').replace(/_/g,' ') ;
		if ( undefined !== self.images[title] ) return ; // Had that
		if ( -1 != $.inArray ( title , self.bad_images ) ) return ; // Don't want that
		if ( (imageinfo.width||999)<150 && (imageinfo.height||999)<150 ) return ; // Too small
		self.images[title] = imageinfo ;
		var left = Math.floor ( (self.thumbsize-imageinfo.thumbwidth)/2 ) ;
		var top = Math.floor ( (self.thumbsize-imageinfo.thumbheight)/2 ) ;
		var h = "<div style='margin:2px;display:inline-block;text-align:center' nowrap>" ;
		h += "<a href='" + imageinfo.descriptionurl + "' target='_blank'>" ;
		h += "<img alt='Loading...' border=0 style='padding-top:"+top+"px;padding-left:"+left+"px;padding;right:"+left+"px;padding-bottom:"+top+"px' src='" + imageinfo.thumburl + "' title='" + title.replace(/['"]/g,'') + "' /></a><br/>" ;
		h += "<a href='#' onclick='wd_useful.asImage(\"" + escape(title) + "\",\"image\");return false'>Image</a>/" ;
		h += "<a href='#' onclick='wd_useful.asImage(\"" + escape(title) + "\",\"logo\");return false'>logo</a>/" ;
		h += "<a href='#' onclick='wd_useful.asImage(\"" + escape(title) + "\",\"map\");return false'>map</a>/" ;
		h += "<a href='#' onclick='wd_useful.asImage(\"" + escape(title) + "\",\"signature\");return false' title='Signature'>sig</a>/" ;
		h += "<a href='#' onclick='wd_useful.asImage(\"" + escape(title) + "\",\"coa\");return false' title='Coat of Arms'>CoA</a>" ;
		h += "</div>" ;
		$('#'+self.image_container_id).append ( h ) ;
	} ,
	
	asImage : function ( title , type ) {
		var self = this ;
		title = unescape ( title ) ;
//		console.log ( title + " as " + type ) ;
		var p ;
		if ( type == 'image' ) p = 18 ;
		else if ( type == 'map' ) p = 242 ;
		else if ( type == 'logo' ) p = 154 ;
		else if ( type == 'signature' ) p = 109 ;
		else if ( type == 'coa' ) p = 94 ;
		else {
			console.log ( "asImage : unknown type " + type ) ;
			return ;
		}
		var o = { mode:'set_string' , s:title , entity:self.q , prop:self.p_prefix+p } ;
		console.log ( o ) ;
		self.queue.push ( o ) ;
		self.processNextQueueItem () ;
	} ,
	
	autoRenameStatements : function () {
		var self = this ;
		entity = self.q_prefix + self.q.replace ( /\D/g , '' ) ;
		$.getJSON ( self.api , {
			action : 'wbgetentities' ,
			format : 'json' ,
			ids : entity ,
			props : 'info|claims|sitelinks'
		} , function ( data ) {
			if ( undefined === data.entities[entity] ) return ;
			self.sitelinks = data.entities[entity].sitelinks ;
			if ( undefined === data.entities[entity].claims ) return ;
			$.each ( data.entities[entity].claims , function ( p , v ) {
				if ( p=='P28'|p=='P11'||p=='P12'||p=='P28'||p=='P83'||p=='P34' ) { // replacing deprecated properties with "located in administrative unit"
					self.replaceClaimProperty ( { old_prop : p , new_prop : 'P131' , claim : v } ) ;
				}
			} ) ;
			self.processNextQueueItem () ;
		} ) ;
	} ,
	
	replaceClaimProperty : function ( data ) {
		var self = this ;
		$.each ( data.claim , function ( k , v ) {
			var nid = v.mainsnak.datavalue.value['numeric-id'] ;
//			console.log ( data.old_prop + " => " + data.new_prop + " : " + nid ) ;
			self.queue.push ( { mode:'set' , prop:data.new_prop , target_entity:self.q_prefix+nid } ) ;
			self.queue.push ( { mode:'remove' , prop:data.old_prop , target_entity:self.q_prefix+nid } ) ;
		} ) ;
	} ,
	
	showQ : function () {
		$('div.wikibase-snakview-value a').each ( function ( k , v ) {
			var url = $(v).attr('href') || '' ;
			var m = url.match ( /\/[qQ](\d+)$/ ) ;
			if ( m != null ) {
				var h = " <span style='color:#999999'><i>q" + m[1] + "</i></span>" ;
				$(v).after ( h ) ;
			}
		} ) ;
	} ,
	
	duplicateStatementsFromItemList : function () {
		var self = this ;
		var lang = 'en' ;
		var name = prompt("Enter a Wikidata page with item list : ", "");//'Wikidata:Tropical cyclones task force/standard/Atlantic' ;
		if ( name == null ) return ;
		self.dupCount = 0 ;
		self.processed = 0 ;
		self.claims = {} ;
		//http://wikidata.org/w/api.php?action=query&prop=links&titles=Wikidata:Tropical%20cyclones%20task%20force/standard/Atlantic&plnamespace=0&pllimit=500&format=jsonfm

		$.getJSON ( self.api , {
				action : 'wbgetentities' ,
				ids : self.q ,
				languages : lang ,
				format : 'json'
			} , function ( d0 ) {
			
				$.each ( d0.entities||[] , function ( k , v ) {
					self.claims = v.claims ;
				} ) ;
			
				$.getJSON ( '//www.wikidata.org/w/api.php?callback=?' , {
					action : 'query' ,
					prop : 'links' ,
					titles : name ,
					plnamespace : 0 ,
					pllimit : 500 ,
					format : 'json'
				} , function ( data ) {
					$.each ( data.query.pages , function ( k1 , v1 ) {
						$.each ( v1.links , function ( k , v ) {
							self.duplicateStatementsToEntity ( v.title ) ;
						} ) ;
					} ) ;
			} ) ;
		} ) ;

	} ,
	
	duplicateStatements : function () {
		var self = this ;
		var lang = 'en' ;
		var cat = prompt("Enter a en.wp category : ", "");//'Compositions_by_Pyotr_Ilyich_Tchaikovsky' ;
		if ( cat == null ) return ;
		self.dupCount = 0 ;
		self.processed = 0 ;
		self.claims = {} ;
		
		$.getJSON ( self.api , {
				action : 'wbgetentities' ,
				ids : self.q ,
				languages : lang ,
				format : 'json'
			} , function ( d0 ) {
			
				$.each ( d0.entities||[] , function ( k , v ) {
					self.claims = v.claims ;
				} ) ;
			
				$.getJSON ( '//'+lang+'.wikipedia.org/w/api.php?callback=?' , {
					action : 'query' ,
					list : 'categorymembers' ,
					cmtitle : 'Category:' + cat ,
					cmnamespace : 0 ,
					cmlimit : 500 ,
					format : 'json'
				} , function ( data ) {
					$.each ( data.query.categorymembers , function ( k , v ) {
						var t = v.title ;
						if ( t.match ( /^List[_ ]/ ) ) return ; // No lists!
						self.dupCount++ ;
						$.getJSON ( self.api , {
							action : 'wbgetentities' ,
							sites : lang+'wiki' ,
							titles : t ,
							languages : lang ,
							format : 'json'
						} , function ( d2 ) {
							$.each ( d2.entities||[] , function ( k2 , v2 ) {
//								self.dupCount++ ;
								self.duplicateStatementsToEntity ( k2 , v2 ) ;
							} ) ;
							self.dupCount-- ;
							if ( self.dupCount == 0 ) self.processNextQueueItem() ;
						} ) ;
//						if ( k > 5 ) return false ; // TESTING
					} ) ;
			} ) ;
		} ) ;
	} ,
	
	duplicateStatementsToEntity : function ( q , target ) {
		var self = this ;
		if ( q == self.q || q.replace(/\D/g,'')=='1' ) {
			return ;
		}
		
		$.each ( self.claims , function ( p , v ) {
			var nid = v[0].mainsnak.datavalue.value['numeric-id'] ;
			if ( nid === undefined ) return ;
			var o = { mode:'set' , prop:p , target_entity:self.q_prefix+nid , entity:q } ;
			self.queue.push ( o ) ;
		} ) ;

	} ,
	
	tryAutoAdd : function () {
		var self = this ;
		$('.wb-property-container-value .wb-value').each ( function ( k , v ) {
			var t = $(v).text() ;
			if ( t == 'Wikipedia disambiguation page' ) self.isDisambiguation() ;
		} ) ;
	} ,
	
	tryDesc : function () {
		var self = this ;
		var lang ;
		var title ;
		$.each ( ['en','de','nds','nl','fr','it','es','ru','pl','ja','pt','zh','el','sv','nl','ro','no','da','sv'] , function ( k , l ) {
			var t = $('.wikibase-sitelinkview-link-'+l+'wiki span.wikibase-sitelinkview-page a').text() ;
			if ( t == '' ) return ;
			lang = l ;
			title = t ;
			return false ;
		} ) ;
		if ( lang === undefined ) return ;
		$.getJSON ( '//'+lang+'.wikipedia.org/w/api.php?callback=?' , {
			action : 'query' ,
			prop : 'extracts' ,
//			exsentences : 5 ,
			exchars : 300 ,
			titles : title ,
			format : 'json'
		} , function ( data ) {
			$.each ( data.query.pages , function ( k , v ) {
				//$($('table.wb-sitelinks').get(2)).after ( v.extract ) ;
				$($('#wd_useful_dialog > div').get(0)).append ( "<div style='max-width:335px;margin-top:5px;border-top:1px solid black'>"+v.extract+"</div>" ) ;
				self.wdUsefulprocessExtract(v.extract) ;
			} ) ;
		} ) ;
	} ,
	
	useCountry : function ( v ) {
		var self = this ;
		var cq = $('#country').val().replace(/\D/g,'') ;
		var prop = v;
//		self.running = true ;
		self.queue.push ( { mode:'set' , prop:self.p_prefix+prop , target_entity:self.q_prefix+cq } ) ;
//		self.queue.push ( [ self.p_prefix+prop , self.q_prefix+cq ] ) ;
		self.processed = 0 ;
		self.processNextQueueItem () ;
	} ,
	
	loadCountries : function () {
		var self = this ;
		self.countries = wd_useful_countries ;
		//JSON.parse ( "{\"Angola\":\"Q916\",\"Botswana\":\"Q963\",\"Burkina Faso\":\"Q965\",\"Burundi\":\"Q967\",\"Comoros\":\"Q970\",\"Romania\":\"Q218\",\"Republic of the Congo\":\"Q971\",\"Democratic Republic of the Congo\":\"Q974\",\"Djibouti\":\"Q977\",\"Equatorial Guinea\":\"Q983\",\"Gabon\":\"Q1000\",\"Gambia\":\"Q1005\",\"Guinea\":\"Q1006\",\"Guinea-Bissau\":\"Q1007\",\"Côte d'Ivoire\":\"Q1008\",\"Cameroon\":\"Q1009\",\"Lesotho\":\"Q1013\",\"Liberia\":\"Q1014\",\"Madagascar\":\"Q1019\",\"Malawi\":\"Q1020\",\"Mauritania\":\"Q1025\",\"Mauritius\":\"Q1027\",\"Morocco\":\"Q1028\",\"Mozambique\":\"Q1029\",\"Namibia\":\"Q1030\",\"Niger\":\"Q1032\",\"Nigeria\":\"Q1033\",\"Uganda\":\"Q1036\",\"Rwanda\":\"Q1037\",\"Sao Tomé and Príncipe\":\"Q1039\",\"Scotland\":\"Q22\",\"Seychelles\":\"Q1042\",\"Sierra Leone\":\"Q1044\",\"Somalia\":\"Q1045\",\"Sudan\":\"Q1049\",\"Swaziland\":\"Q1050\",\"Soviet Union\":\"Q15180\",\"Mexico\":\"Q96\",\"Netherlands\":\"Q55\",\"Russian Empire\":\"Q34266\",\"Egypt\":\"Q79\",\"Canada\":\"Q16\",\"Timor-Leste\":\"Q574\",\"Chad\":\"Q657\",\"New Zealand\":\"Q664\",\"India\":\"Q668\",\"Tonga\":\"Q678\",\"Kiribati\":\"Q710\",\"Mongolia\":\"Q711\",\"Fiji\":\"Q712\",\"Suriname\":\"Q730\",\"Paraguay\":\"Q733\",\"Guyana\":\"Q734\",\"Ecuador\":\"Q736\",\"Colombia\":\"Q739\",\"Bolivia\":\"Q750\",\"Saint Vincent and the Grenadines\":\"Q757\",\"Saint Lucia\":\"Q760\",\"Saint Kitts and Nevis\":\"Q763\",\"Jamaica\":\"Q766\",\"Grenada\":\"Q769\",\"Honduras\":\"Q783\",\"Dominica\":\"Q784\",\"Dominican Republic\":\"Q786\",\"Iran\":\"Q794\",\"Iraq\":\"Q796\",\"Israel\":\"Q801\",\"Panama\":\"Q804\",\"Yemen\":\"Q805\",\"Jordan\":\"Q810\",\"Nicaragua\":\"Q811\",\"Kyrgyzstan\":\"Q813\",\"Kuwait\":\"Q817\",\"Laos\":\"Q819\",\"Lebanon\":\"Q822\",\"Myanmar\":\"Q836\",\"Nepal\":\"Q837\",\"Pakistan\":\"Q843\",\"Vietnam\":\"Q881\",\"South Korea\":\"Q884\",\"Afghanistan\":\"Q889\",\"Mali\":\"Q912\",\"Bhutan\":\"Q917\",\"Brunei\":\"Q921\",\"Tanzania\":\"Q924\",\"Philippines\":\"Q928\",\"Central African Republic\":\"Q929\",\"Togo\":\"Q945\",\"Tunisia\":\"Q948\",\"Turkey\":\"Q43\",\"Zambia\":\"Q953\",\"Zimbabwe\":\"Q954\",\"Benin\":\"Q962\",\"Japan\":\"Q17\",\"Norway\":\"Q20\",\"Republic of Ireland\":\"Q27\",\"Hungary\":\"Q28\",\"Spain\":\"Q29\",\"United States of America\":\"Q30\",\"Belgium\":\"Q31\",\"Luxembourg\":\"Q32\",\"Finland\":\"Q33\",\"Sweden\":\"Q34\",\"Denmark\":\"Q35\",\"Poland\":\"Q36\",\"Lithuania\":\"Q37\",\"Italy\":\"Q38\",\"Switzerland\":\"Q39\",\"Austria\":\"Q40\",\"Greece\":\"Q41\",\"Uruguay\":\"Q77\",\"Kenya\":\"Q114\",\"Ghana\":\"Q117\",\"France\":\"Q142\",\"United Kingdom\":\"Q145\",\"China\":\"Q148\",\"Brazil\":\"Q155\",\"Russia\":\"Q159\",\"Germany\":\"Q183\",\"Belarus\":\"Q184\",\"Iceland\":\"Q189\",\"Estonia\":\"Q191\",\"Latvia\":\"Q211\",\"Ukraine\":\"Q212\",\"Czech Republic\":\"Q213\",\"Slovakia\":\"Q214\",\"Moldova\":\"Q217\",\"Bulgaria\":\"Q219\",\"Croatia\":\"Q224\",\"Bosnia and Herzegovina\":\"Q225\",\"Cyprus\":\"Q229\",\"Georgia\":\"Q230\",\"Indonesia\":\"Q252\",\"South Africa\":\"Q258\",\"Uzbekistan\":\"Q265\",\"Chile\":\"Q298\",\"Singapore\":\"Q334\",\"Liechtenstein\":\"Q347\",\"Armenia\":\"Q399\",\"Serbia\":\"Q403\",\"Australia\":\"Q408\",\"Argentina\":\"Q414\",\"Cambodia\":\"Q424\",\"Federated States of Micronesia\":\"Q702\"}" ) ;
		self.showCountries() ;
/*		
		return ;
		$.getJSON ( self.api , {
			action : 'query' ,
			list : 'backlinks' ,
			bltitle : 'Q3624078' ,
			bllimit : 500 ,
			blnamespace : 0 ,
			format : 'json'
		} , function ( data ) {
			var qs = [] ;
			$.each ( data.query.backlinks , function ( k , v ) {
				qs.push ( v.title ) ;
			} ) ;
			var chunk = 50 ;
			self.loading_country_chunks = 0 ;
			for (i=0,j=qs.length; i<j; i+=chunk) {
				self.loading_country_chunks++ ;
				var tmp = qs.slice(i,i+chunk);
				self.addCountries ( tmp ) ;
			}
		} ) ;
*/
	} ,

	showCountries : function () {
		var self = this ;
		var h = '' ;
		var keys = [] ;
		$.each ( self.countries , function ( name , k ) { keys.push ( name ) } ) ;
		keys = keys.sort() ;
		$.each ( keys , function ( dummy , name ) {
			h += "<option value='" + self.countries[name] + "'>" + name + "</option>" ;
		} ) ;
		$('#country').html ( h ) ;
	} ,
	
	addCountries : function ( qs ) {
		var self = this ;
		$.getJSON ( self.api , {
			action : 'wbgetentities' ,
			ids : qs.join('|').toUpperCase() ,
			languages : 'en' ,
			format : 'json'
		} , function ( data ) {
			$.each ( data.entities , function ( k , v ) {
				var name = v.labels.en.value ;
				self.countries[name] = k ;
			} ) ;
			self.loading_country_chunks-- ;
			if ( self.loading_country_chunks > 0 ) return ;
			self.showCountries() ;
		} ) ;
	} ,

	isDisambiguation : function () {
		var self = this ;
//		self.queue.push ( { mode:'set' , prop:'P107' , target_entity:'Q11651459' } ) ;
		self.processed = 0 ;
		self.processNextQueueItem () ;
		return false ;
	} ,
	
	autoName : function () {
		var self = this ;
		
		var key = 'h1 input.wb-ui-propertyedittool-editablevalueinterface' ;
		var t = $($(key).get(0)) ;
		if ( t.val() != '' || t.tagName == 'span' ) return ; // Already has a title
		
		$.each ( ['en','de','fr','it','nl','sv','es'] , function ( k , v ) {
			var title = $($('td.wb-sitelinks-link-'+v + ' a').get(0)).text() ;
			if ( title == '' ) return ;
			title = title.replace ( /\s\(.+$/ , '' ) ;
			$($(key).get(0)).val(title).css({'background-color':'red'}) ;
			$($(key).get(0)).focus();
			
			return false ;
		} ) ;
	} ,
	
	processNextQueueItem : function () {
		var self = this ;
		if ( self.queue.length == 0 ) {
//			self.running = false ;
			if ( self.processed == 0 ) {
//				alert ( "No changes" ) ;
				return ;
			}
			// DONE
//			console.log ( "DONE" ) ;
//			location.reload();
			return ;
		}
		var q = self.queue.shift () ;
		if ( undefined === q.entity ) q.entity = self.q ;
		if ( q.mode == 'set' ) {
			var val = '{"entity-type":"item","numeric-id":' + q.target_entity.replace(/\D/g,'') + '}' ;
			self.tryCreateClaim ( q.entity , q.prop , val , (q.qualifiers||[]) ) ;
		} else if ( q.mode == 'set_string' ) {
			var val = JSON.stringify ( q.s ) ;
			self.tryCreateClaim ( q.entity , q.prop , val , (q.qualifiers||[]) ) ;
		} else if ( q.mode == 'remove' ) {
			var val = '{"entity-type":"item","numeric-id":' + q.target_entity.replace(/\D/g,'') + '}' ;
			self.tryRemoveClaim ( q.entity , q.prop , val ) ;
		} else {
			console.log ( q ) ;
		}
	} ,

	tryRemoveClaim : function ( entity , property , value , qualifiers ) {
		var self = this ;
		entity = self.q_prefix + entity.replace ( /\D/g , '' ) ;
		property = property.replace ( /\D/g , '' ) ;
		var value2 = JSON.parse ( value ) ;
		var nid = value2['numeric-id'] ;
		$.getJSON ( self.api , {
			action : 'wbgetentities' ,
			format : 'json' ,
			ids : entity ,
			props : 'info|claims'
		} , function ( data ) {
			if ( undefined !== data.entities[entity] ) {
				if ( undefined !== data.entities[entity].claims ) {
					if ( undefined !== data.entities[entity].claims[self.p_prefix+property] ) {
						var n = data.entities[entity].claims[self.p_prefix+property] ;
						var exists = false ;
						var id ;
						$.each ( n , function ( k , v ) {
							if ( v.mainsnak.datavalue.value['numeric-id'] == value.replace(/\D/g,'') ) {
								exists = true ;
								id = v.id ;
								return false ;
							}
						} ) ;
						if ( exists ) {
							self.removeClaim ( { revid:data.entities[entity].lastrevid , claim:id , entity:entity } ) ; // entity , property , value
//							console.log ( "p"+property+' exists for '+entity ) ;
							return ;
						}
					}
				}
			}
			console.log ( "Failed to remove " + entity + " / " + property + " : " + value ) ;
			self.processNextQueueItem() ;
		} ) ;
	} ,
		
	removeClaim : function ( o ) {
		var self = this ;
//		console.log ( "REMOVING" ) ;
//		console.log ( o ) ;
		$.post ( self.api , {
			action : 'query' ,
			prop : 'info' ,
			intoken : 'edit' ,
			titles : o.entity ,
			format : 'json'
		} , function ( data ) {
			var token , lastrevid ;
			$.each ( (data.query.pages||[]) , function ( k , v ) {
				token = v.edittoken ;
				lastrevid = v.lastrevid ;
			} ) ;
			
			if ( undefined === token ) {
				console.log ( "Cannot get edit token for " + entity ) ;
				self.processNextQueueItem() ;
				return ;
			}

			$.post ( self.api , {
				action : 'wbremoveclaims' ,
				claim : o.claim ,
				token : token ,
				baserevid : o.revid ,
				format : 'json'
			} , function ( data ) {
				var h = "<div style='color:red'>Removed " + JSON.stringify ( o ) + "</div>" ;
				$($('div.wikibase-listview').get(0)).append ( h ) ;

				self.processed++ ;
				self.processNextQueueItem() ;
			} , 'json' ) ;
			
			
			
		} , 'json' ) ;
	} ,

	tryCreateClaim : function ( entity , property , value , qualifiers ) {
		var self = this ;
		entity = self.q_prefix + entity.replace ( /\D/g , '' ) ;
		property = property.replace ( /\D/g , '' ) ;
		var value2 = JSON.parse ( value ) ;
		var nid = value2['numeric-id'] === undefined ? value2 : value2['numeric-id'] ;
		$.getJSON ( self.api , {
			action : 'wbgetentities' ,
			format : 'json' ,
			ids : entity ,
			props : 'info|claims'
		} , function ( data ) {
			if ( undefined !== data.entities[entity] ) {
				if ( undefined !== data.entities[entity].claims ) {
					if ( undefined !== data.entities[entity].claims[self.p_prefix+property] ) {
						var n = data.entities[entity].claims[self.p_prefix+property] ;
						var exists = false ;
						$.each ( n , function ( k , v ) {
							var the_val = v.mainsnak.datavalue.value['numeric-id'] === undefined ? v.mainsnak.datavalue.value : v.mainsnak.datavalue.value['numeric-id'] ;
							if ( the_val == nid ) {
								exists = true ;
								return false ;
							}
						} ) ;
						if ( exists ) {
							console.log ( "P"+property+' exists for '+entity ) ;
							self.processNextQueueItem() ;
							return ;
						}
					}
				}
			}
			self.createClaim ( entity , property , value , qualifiers ) ;
		} ) ;
	} ,
		
	createClaim : function ( entity , property , value , qualifiers ) {
//		console.log ( "Creating " + entity + " / " + property + " / " + value ) ;
		var self = this ;
		$.post ( self.api , {
			action : 'query' ,
			prop : 'info' ,
			intoken : 'edit' ,
			titles : entity ,
			format : 'json'
		} , function ( data ) {
			var token , lastrevid ;
			$.each ( (data.query.pages||[]) , function ( k , v ) {
				token = v.edittoken ;
				lastrevid = v.lastrevid ;
			} ) ;
			
			if ( undefined === token ) {
				console.log ( "Cannot get edit token for " + entity ) ;
				self.processNextQueueItem() ;
				return ;
			}
			
			property = property.replace(/\D/g,'') ;
			entity = entity.replace(/\D/g,'') ;
			var vo = JSON.parse ( value ) ;
//			console.log ( vo ) ;
			var is_item_target = vo['numeric-id'] !== undefined ;
			var value_id = is_item_target ? vo['numeric-id']+'' : vo ;
			
			var other_entity = ( entity.replace(/\D/g,'') != self.q.replace(/\D/g,'') ) ;
			
			let payload = {
				action : 'wbcreateclaim' ,
				entity : self.q_prefix+entity ,
				snaktype : 'value' ,
				property : self.p_prefix+property ,
				value : value ,
				token : token ,
				baserevid : lastrevid ,
				format : 'json'
			}  ;

			// Qualifiers => wbeditentiry
					console.log(qualifiers);
			if ( typeof qualifiers != 'undefined' && qualifiers.length > 0 ) {
				let qualifiers_out = [] ;
				$.each ( qualifiers , function ( dummy , qual ) {
					console.log(qual);
					let quals = qual.split(':');
					if ( quals.length != 2 ) return ;
					let qual_prop = quals[0] ;
					let qual_item = quals[1] ;
					qual = {
						snaktype:"value",
						property:qual_prop,
						datavalue:{
							value:{
								"entity-type": "item",
								id:qual_item
							},
							type: "wikibase-entityid"
						},
						datatype: "wikibase-item"
					} ;
					qualifiers_out.push(qual);
				} ) ;

				let data = {
						claims:[{
							mainsnak : {
								snaktype : 'value' ,
								property : self.p_prefix+property ,
								datavalue : {
									value : JSON.parse(value) ,
									type : 'wikibase-entityid'
								}
							},
							qualifiers : qualifiers_out ,
							type : 'statement' ,
							rank : 'normal'
						}]
					} ;
				console.log(data);

				payload = {
					action : 'wbeditentity' ,
					id : self.q_prefix+entity ,
					data : JSON.stringify(data),
					token : token ,
					baserevid : lastrevid ,
					format : 'json'
				} ;
				console.log(payload);
			}
			
			$.post ( self.api , payload , function ( data_ignored ) {
				var id = 'added_' + self.id_cnt ;
				var h = "<div id='" + id + "'>" ;
				h += "Added " ;
				h += "<span class='added_entity_P'>" + property + "</span>" ;
				h += " &Rarr; " ;
				h += "<span class='added_entity_Q'>" + value_id + "</span>" ;
				if ( other_entity ) h += " to entity <span class='added_entity_x'>" + entity + "</span>" ;
				h += ".</div>" ;
				$($('div.wikibase-listview').get(0)).append ( h ) ;
				self.updateEntity ( id , property , self.p_prefix ) ;
				if ( is_item_target ) self.updateEntity ( id , value_id , self.q_prefix ) ;
				if ( is_item_target && other_entity ) self.updateEntity ( id , entity.replace(/\D/g,'') , 'x' ) ;
				self.id_cnt++ ;
				self.processed++ ;
				self.processNextQueueItem() ;
			} , 'json' )   .fail(function(xhr, status, error) {
				alert(error)
    		});
		} , 'json' ) ;
	} ,
	
	updateEntity : function ( id , value , prefix ) {
		var self = this ;
		var q = prefix+value ;
		if ( prefix == 'x' ) q = self.q_prefix + value ;
		console.log ( q ) ;
		$.getJSON ( self.api , {
			action : 'wbgetentities' ,
			ids : q ,
			format : 'json'
		} , function ( data ) {
			var labels = data.entities[q].labels ;
			var title = q ;
			if ( undefined !== labels ) {
				$.each ( self.langpref , function ( k , v ) {
					if ( undefined === labels[v] ) return ;
					title = labels[v].value ;
					return false ;
				} ) ;
			}
			var h = "<a href='/wiki/" + q + "'>" + title + "</a>" ;
			$('#'+id+' span.added_entity_'+prefix).html ( h ) ;
		} ) ;
	} ,

	wdUsefulprocessExtract : function ( t ) {
		var months = {
			'january' : [ 'januar' , 'januari' , 'janvier' , 'jänner' , 'de enero de' , 'gennaio' , ' ianuarie'] ,
			'february' : [ 'februar' , 'februari' , 'février' , 'febbraio' , 'de febrero de', 'februarie' ] ,
			'march' : [ 'märz' , 'maart' , 'marzo' , 'mars' , 'de Marzo del' , 'martie'] ,
			'april' : [ 'avril' , 'de abril de' , 'aprile', 'aprilie' ] ,
			'may' : [ 'mei' , 'mai' , 'de mayo de' , 'maggio' , 'mai'] ,
			'june' : [ 'juni' , 'juin' , 'de junio de' , 'giugno', 'iunie' ] ,
			'july' : [ 'juli' , 'juillet' , 'de julio de' , 'luglio' , 'iulie'] ,
			'august' : [ 'augustus' , 'août', 'aout' , 'agosto' , 'de agosto de' , 'august' ] ,
			'september' : [ 'septembre' , 'setiembre' , 'de septiembre de' , 'settembre' , 'septembrie'] ,
			'october' : [ 'oktober' , 'octobre','października' , 'de octubre de' , 'ottobre', 'octombrie' ] ,
			'november' : [ 'novembre' , 'de noviembre de', 'noiembrie' ] ,
			'december' : [ 'dezember' , 'décembre' , 'diciembre','grudnia','de diciembre de','dicembre', 'decembrie','desember']
		} ;
		var m2n = {
			january:'01',
			february:'02',
			march:'03',
			april:'04',
			may:'05',
			june:'06',
			july:'07',
			august:'08',
			september:'09',
			october:'10',
			november:'11',
			december:'12'	
		} ;
		
		var found = false ;
		var h = '<div style="margin-top:5px;border-top:1px solid black">' ;
		h += "<div>Auto-detected birth/death dates:</div>" ;
		
		// Dates
		t = t.replace ( /<b>.+?<\/b>/ ) ;
		var m = t.match ( /\((.+?)\)/ ) ;
		if ( m == null  ) m = [ '' , t ] ; // Fallback
		if ( m != null ) {
			var s = m[1] ;
			s = s.toLowerCase() ;
			s = s.replace ( /&#160;/g , ' ' ) ; // Weird French template
			$.each ( months , function ( en , other ) {
				$.each ( other , function ( k , v ) {
					var r = new RegExp ( '\\b,*de\\s'+v+'\\sde,*\\b' , 'ig' ) ;
					s = s.replace ( r , en ) ;
					r = new RegExp ( '\\b'+v+'\\b' , 'ig' ) ;
					s = s.replace ( r , en ) ;
				} ) ; 
			} ) ;

			m = s.match ( /(\d{1,2})\.{0,1}\s+(january|february|march|april|may|june|july|august|september|october|november|december)\s+(\d{3,4})/g ) ;
			if ( m == null ) {
				m = s.match ( /\b(january|february|march|april|may|june|july|august|september|october|november|december)\s+(\d{1,2})\.{0,1}\,{0,1}\s+(\d{3,4})/g ) ;
			}
			
			var had_year = {} ;
			if ( m != null ) {
				$.each ( m , function ( k , v ) {
					var d = v.toLowerCase().replace(/,/g,'').replace(/\./g,'').split(' ') ;
					var day = '' , month = '' , year = '' ;
					$.each ( d , function ( k2 , v2 ) {
						if ( undefined !== m2n[v2] ) month = m2n[v2] ;
						else if ( v2.match(/^\d{3,4}$/) ) year = v2 ;
						else if ( v2.match(/^\d{1,2}$/) ) day = v2 ;
					} ) ;
					while ( year.length < 4 ) year = '0'+year ;
					while ( day.length < 2 ) day = '0'+day ;
					var time = "+0000000"+year+"-"+month+"-"+day+"T00:00:00Z" ;
					
					found = true ;
					var h2 = '' ;
					h2 += "<div>" + v ;
					h2 += " <a href='#' onclick='wd_useful.setDate(569,\""+time+"\",11);return false'>born</a>" ; // Born
					h2 += " <a href='#' onclick='wd_useful.setDate(570,\""+time+"\",11);return false'>died</a>" ; // Died
					h2 += "</div>" ;
					had_year[year] = h2 ;
				} ) ;
			}
			
			// try years only
			m = s.match ( /\b(\d{3,4})\b/g ) ;
			if ( m != null ) {
				$.each ( m , function ( k , year ) {
					year += '' ;
					while ( year.length < 4 ) year = '0'+year ;
					if ( undefined !== had_year[year] ) return ;
					var time = "+0000000"+year+"-01-01T00:00:00Z" ;
					found = true ;
					var h2 = '' ;
					h2 += "<div>" + year ;
					h2 += " <a href='#' onclick='wd_useful.setDate(569,\""+time+"\",9);return false'>born</a>" ; // Born
					h2 += " <a href='#' onclick='wd_useful.setDate(570,\""+time+"\",9);return false'>died</a>" ; // Died
					h2 += "</div>" ;
					had_year[year] = h2 ;
				} ) ;
			}

			var yk = [] ;
			$.each ( had_year , function ( k , v ) { yk.push ( k ) } ) ;
			yk.sort () ;
			$.each ( yk , function ( dummy , year ) { h += had_year[year] ;	})
			
		}

		h += "</div>" ;
		if ( found ) {
			$('#wd_useful_dialog > div').after ( h ) ;
			$('#wd_useful_dialog a').css ( { color:'#0b0080' } ) ;
		}
	} ,

	setDate : function (prop,date,prec) {
		var val = '{"time":"'+date+'","timezone": 0,"before": 0,"after": 0,"precision": '+prec+',"calendarmodel": "http://www.wikidata.org/entity/Q1985727"}' ;
//		console.log ( val ) ;
		wd_useful.createClaim ( wd_useful.q+'' , prop+'' , val ) ;
	} ,

	setTitleTool : function () {
		if ( $($('.wb-ui-propertyedittool-editablevalueinterface').get(0)).text() != '' ) return ; // Already has title
		$('td.wb-sitelinks-link').each ( function () {
			var o = $(this) ;
			var t = o.text() ;
			if ( t === undefined ) return ;
			t = t.replace(/\s*\(.+\)/,'')
			var h = " <a href='#' class='set-title-tool'>&uArr;</a>" ;
			o.parent().append ( "<td>"+h+"</td>" ) ;
			o.parent().find('a.set-title-tool').click ( function () {
				$('a.set-title-tool').remove() ;
				$($('input.wb-ui-propertyedittool-editablevalueinterface').get(0)).val(t).focus().click().keypress() ;
				return false ;
			}) ;
		})
	} ,

	mirrorClaimReverse : function ( v , p ) {
		var self = this ;
		self.mirror( self.q, p, v );
	} ,
	
	mirrorClaim : function ( t , p ) {
		var self = this ;
		self.mirror( t, p, self.q );
	} ,
	
	mirror : function ( t , p , v ) {
		var self = this ;
		var o = { mode:'set', entity:t, prop:p, target_entity:v } ;
		console.log ( o ) ;
		self.queue.push ( o ) ;
		self.processNextQueueItem () ;
	} ,

	the_end : ''
} ;

var consistency_check = {

	p_prefix : 'P' ,
	q_prefix : 'Q' ,
	
	running : false ,
	
	q : '',
	qclaims : [] ,
	instanceOfLabels : {} ,
	p31queue : [],
	checkLength : 0,
	cid_cnt : 0,
	
	outbound : "<span style='font-weight:bold;color:red'   title='Just outbound'>&rarr;</span>" ,
	inbound  : "<span style='font-weight:bold;color:red'   title='Just inbound' >&larr;</span>" ,
	bothways : "<span style='font-weight:bold;color:green' title='Both ways'    >&harr;</span>" ,

	reciproke : {
		'P47' : [ 'P47' ] , // shares border with
		'P26' : [ 'P26' ] , // spouse
		'P40' : [ 'P22', 'P25', 'P43', 'P44' ] , // child / mother, father, stepfather, stepmother
		'P22' : [ 'P40' ] , // father / child
		'P25' : [ 'P40' ],  // mother / child
		'P43' : [ 'P40' ],  // stepfather / child
		'P44' : [ 'P40' ],  // stepmother / child
		'P3373'  : [ 'P3373'], // sibling (, 'P9', 'P7')
		'P7'  : [ 'P3373' ], // brother, not used anymore
		'P9'  : [ 'P3373' ], // sister, not used anymore
		'P150': [ 'P131' ], // contains administr. subdivision
		'P131': [ 'P150' ], // located in the administrative territorial entity
		'P155': [ 'P156' ], // preceded by
		'P156': [ 'P155' ], // succeeded by
		'P184': [ 'P185' ], // doctoral advisor
		'P185': [ 'P184' ], // doctoral student
		'P190': [ 'P190' ], // twin city
		'P197': [ 'P197' ], // adjacent station
		'P200': [ 'P201' ], // lake inflow
		'P201': [ 'P200' ], // lake outflow
		'P358': [ 'P175' ], // discography -> artist
		'P361': [ 'P527' ], // part of
		'P527': [ 'P361' ], // has part
		'P398': [ 'P397' ], // child of astronomical body
		'P399': [ 'P399' ], // companion of (astronomical)
		'P451': [ 'P451' ], // cohabitant
		'P460': [ 'P460' ], // supposed to be the same as
		'P461': [ 'P461' ], // opposite of
		'P629': [ 'P747' ], // edition of
		'P747': [ 'P629' ], // edition
		'P1365': [ 'P1366'], // replaces
		'P1366': [ 'P1365'], // replaced by
		'P1889': [ 'P1889'], // different from
		'P2959': [ 'P2959'], // permanent duplicated item
		'P237': [ 'P1001'], // COA description
		'P1001': [ 'P217'], // only for COA descriptions!
	} ,
	
	dependencies : {
		'P7'   : [ 'P21', 'P31' ], // brother -> sex
		'P9'   : [ 'P21', 'P31' ], // sister -> sex
		'P22'  : [ 'P21', 'P31' ], // father -> sex
		'P25'  : [ 'P21', 'P31' ], // mother -> sex
		'P26'  : [ 'P21', 'P31' ], // spouse -> sex
		'P40'  : [ 'P21', 'P31' ], // child -> sex
		'P43'  : [ 'P21', 'P31' ], // stepfather -> sex
		'P44'  : [ 'P21', 'P31' ], // stepmother -> sex
		'P131' : [ 'P17', 'P31' ], // administrative unit -> country
		'P150' : [ 'P17', 'P31' ], // contains administr. division -> country, type of division
		'P190' : [ 'P31' ], // twin city -> instance of
		'P197' : [ 'P17', 'P31', 'P131', 'P625' ], // adjacent station
		'P399' : [ 'P31', 'P60' ] // companion (astronomical)
	},
	
	reverse : function () {
		var self = this ;
		//if ( self.running ) return ;
		//self.running = true ;
		self.q = mw.config.get('wgPageName') ;
		self.showDialog("<div id='symDialogId' style='float:left; padding-right:25px;'></div>");
		$(
			"<h2 class='wb-section-heading'>Symmetry</h2><small><i>Caution on P150/P131: e.g. a church might be located in (P131) a town, but it's not an administrative territorial entity (P150) of it! Use of P150 depends very much on the local administrative taxonmy.</i></small>"
			+"<table id='resultListReverse' class='wikitable sortable'><thead><tr class='hintergrundfarbe5'>"
			+"<th >Here</th>"
			+"<th > </th>"
			+"<th > </th>"
			+"<th >There</th>"
			+"<th >Value</th>"
			+"<th >Type</th>"
			+"<th >Source</th>"
			+"</tr></thead>" 
			+"</table></div>"
		).appendTo('#symDialogId');
		
		$.getJSON ( wd_useful.api , {
			action : 'wbgetentities' ,
			ids : self.q ,
			format : 'json' ,
			props : 'claims'
		} , function ( data ) {
			self.qclaims = data;
			if ( undefined !== self.qclaims.entities[self.q].claims ) {
				var check = [] ;
				var updt = [] ;
				$.each ( self.qclaims.entities[self.q].claims , function ( k , v ) { // all claims
					var p2 = self.reciproke[k];
					console.log("-     "+self.q+"." + k);
					if ( undefined !== p2 ) {
						$.each ( v , function ( k2 , v2 ) { // all entries per claim
							if ( undefined === v2.mainsnak.datavalue ) return ; // No value
							var q2 = self.q_prefix + v2.mainsnak.datavalue.value['numeric-id'] ;
							console.log("--    "+self.q+"." + k + "." + q2);
							var id = 'consistency_' + k + '_' + q2 ;
							var cid = 'conc_' + self.cid_cnt++ ;
							updt.push ( { i:cid , q:v2.mainsnak.datavalue.value['numeric-id'] , qprefix:self.q_prefix } ) ;
							check.push ( { prop:k , orig:self.q , target:q2 , any:p2 , id:id, q_prefix: self.q_prefix } ) ;
						} ) ;
					}
				} );
				$.each ( check , function ( k , v ) {
					self.checkReciproke ( v ) ;
				} ) ;
				$.each ( updt , function ( k , v ) { 
					wd_useful.updateEntity ( v.i , v.q , v.qprefix ) ;
				} ) ;
			}
		} ) ;

		//https://www.wikidata.org/w/api.php?action=query&list=backlinks&bltitle=q5907&bllimit=500&blfilterredir=nonredirects&blnamespace=0
		$.getJSON ( wd_useful.api , {
			action : 'query' ,
			bltitle : self.q ,
			format : 'json' ,
			list : 'backlinks' ,
			bllimit : 500 ,
			blfilterredir : 'nonredirects' ,
			blnamespace : 0 ,
		} , function ( data ) {
			var check = [] ;
			if ( undefined !== data.query.backlinks ) {
				$.each ( data.query.backlinks , function ( k , v ) { // all backlinks (pageid, ns, title)
					console.log("backlink: " + v['title']);
					check.push ( { orig:v['title'], id:'resultListReverse', q_prefix:self.q_prefix } ) ;
				} ) ;
			} else {
				$(
					"<div>No consistency checks for the claims of " + self.q + ".</div>"
				).appendTo('#symDialogId');
			}
			self.checkLength=check.length;
			$.each ( check , function ( k , v ) {
				self.checkReverseReciproke ( v ) ;
			} ) ;
			check={};
		} ) ;
	} ,
	
	checkReverseReciproke : function ( d ) {
		var self = this ;
		$.getJSON ( wd_useful.api , {
			action : 'wbgetentities' ,
			ids : d.orig ,
			format : 'json'
		} , function ( data ) {
			var foundOutbound = false ;
			var html_out = "";
			if ( undefined !== data.entities[d.orig].claims ) {
				console.log("-     backlink: " + d.orig);
				$.each ( data.entities[d.orig].claims , function ( k , v ) { // all claims of orig
					var p2 = self.reciproke[k];
					if ( undefined !== p2 ) { // not a reciproke property
						console.log("--    "+d.orig+".claim: " + k);
						$.each ( v , function ( k2 , v2 ) { // all values of claim of orig
							if ( undefined === v2.mainsnak.datavalue ) return ; // No value
							var q2 = d.q_prefix + v2.mainsnak.datavalue.value['numeric-id'] ;
							if ( q2 == self.q ) { // claim links to q
							
								console.log("---   "+d.orig+".claim.value: " + q2);
								$.each ( self.qclaims.entities[self.q].claims , function ( k3 , v3 ) { // all claims of q
									if ( k3 == p2 ) { // the matching reciproke property
										console.log("----  "+self.q+".claim: " + k3);
										$.each ( v3 , function ( k4 , v4 ) { // all entries per claim
											if ( undefined === v4.mainsnak.datavalue ) return ; // No value
											var q4 = self.q_prefix + v4.mainsnak.datavalue.value['numeric-id'] ;
											if ( q4 == d.orig ) {
												console.log("----- "+self.q+".claim.value: " + q4);
												foundOutbound = true; 
											}
										} ) ;
									}
								} ) ;
							
								var p31s = self.instanceOfLabel( data.entities[d.orig].claims['P31'] );
								var labels = data.entities[d.orig].labels ;
								self.addSymmetry(d.id, self.q, p2, d.orig, k, p31s, true, foundOutbound, labels, true);

								var sortableTables = $('table.sortable');
								if (sortableTables.length) {
									mw.loader.using('jquery.tablesorter', function () {
										sortableTables.tablesorter( {
											sortList: [ { 0: 'asc' }, { 5: 'asc' }, { 4: 'asc' } ] ,
										} );
									});
								}
							}
						} ) ;
					}
				} ) ;
			}
			if (self.checkLength < 2) {
				for ( k in self.p31queue ) {
					console.log("p31queue: " + k + " = " + self.p31queue[k]);
					if (self.p31queue[k]) self.replaceP31 ( k ) ;
				}
				self.p31queue={};
			}
			self.checkLength--;
		} ) ;
	} ,

	checkReciproke : function ( d ) { // 	check.push ( { prop:k , orig:self.q , target:q2 , any:p2 , id:id, q_prefix: self.q_prefix } ) ;
		var self = this ;
		$.getJSON ( wd_useful.api , {
			action : 'wbgetentities' ,
			ids : d.target ,
			format : 'json' ,
		} , function ( data ) {
			var foundInbound = false ;
			if ( undefined !== data.entities[d.target].claims ) {
				$.each ( data.entities[d.target].claims , function ( k , v ) {
					if ( -1 == $.inArray ( k , d.any ) ) return ;
					$.each ( v , function ( k2 , v2 ) {
						if ( undefined === v2.mainsnak.datavalue ) return ; // No value
						var q2 = d.q_prefix + v2.mainsnak.datavalue.value['numeric-id'] ;
						if ( q2 == d.orig ) foundInbound = true ;
					} ) ;
				} ) ;
			}
			var p31s = self.instanceOfLabel( data.entities[d.target].claims['P31'] );
			var labels = data.entities[d.target].labels ;
			self.addSymmetry('resultListReverse', d.orig, d.prop, d.target, d.any, p31s, foundInbound, true, labels, false);
		} ) ;
	} ,

	addSymmetry : function ( elementId, fromId, fromP, toId, toP, p31s, foundInbound, foundOutbound, labels, inboundCheck) {
		var self = this ;
		if ( foundOutbound && foundInbound && ! inboundCheck) return; // we only show complete links found searching outbound
		var html_out = "<tr><th class='label_"+ fromP +"'><a href='/wiki/Property:" + fromP + "'>"+ fromP + "</a></th>";
		var title = toId.toUpperCase() ;
		if ( foundOutbound == true ) {
			if ( foundInbound == true ) {
				html_out += "<td style='text-align:center'>"+ self.bothways +"</td>"
					+"<td></td>";
			} else {
				html_out += "<td style='text-align:center'>"+ self.outbound +"</td>"
					+"<td><a href='#' onclick='wd_useful.mirrorClaim(\""+toId+"\",\""+toP+"\");return false' "
					+"title='mirror claim "+fromId+"/"+fromP+"="+toId+" to "+toId+"/"+toP+"="+fromId+"'>mirror</a></td>";
			}
		} else {
			if ( foundInbound == true ) {
				html_out += "<td style='text-align:center'>"+ self.inbound +"</td>"
					+"<td><a href='#' onclick='wd_useful.mirrorClaimReverse(\""+toId+"\",\""+fromP+"\");return false' "
					+"title='mirror claim "+toId+"/"+toP+"="+fromId+" to "+fromId+"/"+fromP+"="+toId+"'>mirror</a></td>";
			} else {
				html_out += "<td style='text-align:center'>?</td>"
					+"<td></td>";
			}
		}
		html_out += "<td class='label_"+ toP +"'><a href='/wiki/Property:" + toP + "'>"+ toP + "</a></td>";
		if ( undefined !== labels ) {
			$.each ( wd_useful.langpref , function ( k , v ) {
				if ( undefined === labels[v] ) return ;
				title = labels[v].value ;
				return false ;
			} ) ;
		}
		html_out += "<td><a href='/wiki/" + toId.toUpperCase() + "'>" + title + "</a></td><td>" + p31s + "</td><td>"+((inboundCheck)?"inbound":"outbound")+" check</td></tr>";
		$(html_out).appendTo('#'+elementId);
	} ,

	replaceP31 : function ( typ ) {
		var self = this ;
		$.getJSON ( wd_useful.api , {
			action : 'wbgetentities' ,
			ids :  typ,
			format : 'json'
		} , function ( data ) {
			var labels = data.entities[typ].labels ;
			if ( undefined !== labels ) {
				$.each ( wd_useful.langpref , function ( k , v ) {
					if ( undefined === labels[v] ) return ;
					$.each ( $('#resultListReverse').find("span."+typ) , function ( k2 , v2 ) { 
						$(v2).html("<a href='/wiki/" + typ + "'>" + labels[v].value + "</a>");
					} ) ;
					return false ;
				} ) ;
			}
		} );
	} ,
	
	instanceOfLabel : function (instance_of) {
		var self = this ;
		if ( undefined !== instance_of ) { // all claims of orig
			var insttext;
			$.each ( instance_of , function ( k2 , v2 ) { // all values of claim of orig
				if ( undefined === v2.mainsnak.datavalue ) return ; // No value
				var typ = self.q_prefix + v2.mainsnak.datavalue.value['numeric-id'] ;
				var ititle = "<span class='"+ typ +"'>"+ typ +"</span>";
				if ( undefined === self.p31queue[typ] ) 
					self.p31queue[typ] = true;
				if ( undefined === insttext ) 
					insttext = ititle;	
				else
					insttext += " / " + ititle;	
			} ) ;
			return insttext;
		} else return "missing P31 (is instance of)";
	} ,

	run : function () {
		var self = this ;
		if ( self.running ) return ;
		self.running = true ;
		self.q = mw.config.get('wgPageName') ;

		$.getJSON ( wd_useful.api , {
			action : 'wbgetentities' ,
			ids : self.q ,
			format : 'json' ,
			props : 'claims'
		} , function ( data ) {
			var html_out = '' ;
			var something_to_check = false ;
			var dependencies;
			
			if ( undefined !== data.entities[self.q].claims ) {
				html_out += "<div style='float:left; padding-right:25px;'>";
				html_out += "<h2 class='wb-section-heading'>Dependencies</h2><table border=0>" ;
				$.each(data.entities[self.q].claims , function (k, v) {
					dependencies = true;
					if (undefined !== self.dependencies[k]) {
						something_to_check = true;
						html_out += "<tr><th class='label_" + k + "'>" + k + "</th><td>"
						$.each(self.dependencies[k], function(k2, v2) {
							if (!(v2 in data.entities[self.q].claims)) {
								if (dependencies) {
									dependencies = false;
									html_out += " <span style='color:red;'>missing:</span> "
								}
								html_out += v2 + " ";
							}		
						});
						if (dependencies) {
							html_out += " <span style='color:green;'>fine</span>"
						}
						html_out += "</td></tr>"
					}
				});
				html_out += "</table></div>";
			}
			
			if (!something_to_check) {
				html_out = "<div>No consistency checks for the claims of " + self.q + ".</div>";
			} 
			self.showDialog(html_out);
		} ) ;
	} ,
	
	showDialog : function ( html ) {
		var self = this ;
		mw.loader.using( ['jquery.ui'], function () {
			var summary;
			var dialogWidth = Math.min(document.body.clientWidth - 50, 1200);
			var dialogHeight = Math.min(document.body.clientHeight - 50, 800);
	
			if ( document.getElementById( "content" ) ) {
				summary = $(html).appendTo('#content');
			} else {
				summary = $(html).appendTo('#mw_content');
			}
			
			summary.dialog( {
				title: 'Consistency Check',
				autoOpen: true,
				modal: false,
				width: dialogWidth,
				height: dialogHeight,
				buttons: [],
				close: function( event, ui ) {/* self.running = false; */} // callbacks don't work on subsequent iterations....
			} );
		} );
	} ,
	
	the_end : ''
} ;

jQuery(document).ready ( function() {
	if ( mw.config.get('wgNamespaceNumber') != 0 ) return ;
	if ( mw.config.get('wgAction') != 'view' ) return ;
	if ( mw.config.get('wbIsEditView') == false ) return ;
	if ( mw.config.get('wgIsRedirect') == true ) return ;

//	setTimeout ( function(){wd_useful.init () ;} , 300 ) ;
//	consistency_check.init () ;
	wd_useful.init () ;
});