Увага: Після публікування слід очистити кеш браузера, щоб побачити зміни.

  • Firefox / Safari: тримайте Shift, коли натискаєте Оновити, або натисніть Ctrl-F5 чи Ctrl-Shift-R (⌘-R на Apple Mac)
  • Google Chrome: натисніть Ctrl-Shift-R (⌘-Shift-R на Apple Mac)
  • Internet Explorer / Edge: тримайте Ctrl, коли натискаєте Оновити, або натисніть Ctrl-F5
  • Opera: натисніть Ctrl-F5
function hello ( ) {

	var wdeConfig = {
		project: 'ukwiki',
		language: 'uk',
		languages: [ 'uk', 'en' ],
		references: {
			P212: [ {
				snaktype: 'value',
				property: 'P143',
				datavalue: {
					type: 'wikibase-entityid',
					value: { id: 'Q199698' }
				}
			} ]
		},
		units: {
			"1": { label: '', description: '', search: [ '' ] },
		}
	};

	var api;
	var wdApi;

	var wdeBaseRevId;
	var wdePropertyIds = [];
	var wdeProperties = [];
	var wdeTypesMapping = {
		'commonsMedia': 'string',
		'external-id': 'string',
		'url': 'string',
		'wikibase-item': 'wikibase-entityid'
	};
	var wdeWindowManager;

	var wdeErrorDialog = function ( title, message ) {
		var errorDialog = new OO.ui.MessageDialog();
		wdeWindowManager.addWindows( [ errorDialog ] );
		wdeWindowManager.openWindow( errorDialog, {
			title: title,
			message: message
		} );
	};

	var wdeClaimGuid = function ( entityId ) {
		var getRandomHex = function ( min, max ) {
			return ( Math.floor( Math.random() * ( max - min + 1 ) ) + min ).toString( 16 );
		};

		var template = 'xx-x-x-x-xxx';
		var guid = '';
		for ( var i = 0; i < template.length; i++ ) {
			if ( template.charAt( i ) === '-' ) {
				guid += '-';
				continue;
			}

			var hex;
			if ( i === 3 ) {
				hex = getRandomHex( 16384, 20479 );
			} else if ( i === 4 ) {
				hex = getRandomHex( 32768, 49151 );
			} else {
				hex = getRandomHex( 0, 65535 );
			}

			while ( hex.length < 4 ) {
				hex = '0' +  hex;
			}

			guid += hex;
		}

		return entityId + '$' + guid;
	};

	var wdeCreateClaims = function ( propertyId, values, revIds ) {
		var value = values.shift();
		var revIds = revIds || [];
		if ( !value ) {
			wdeAddTags( propertyId, revIds );
			return;
		}
		var datatype = wdeProperties[propertyId].datatype;
		var mainsnak = value.match( '^(novalue|somevalue)$' ) ? {
			snaktype: value,
			property: propertyId
		} : {
			snaktype: 'value',
			property: propertyId,
			datavalue: {
				type: wdeTypesMapping[datatype] ? wdeTypesMapping[datatype] : datatype,
				value: JSON.parse( value )
			}
		};
		var claim = {
			type: 'statement',
			mainsnak: mainsnak,
			id: wdeClaimGuid( mw.config.get( 'wgWikibaseItemId' ) ),
			references: [ { snaks: wdeConfig.references } ],
			rank: 'normal'
		};
		wdApi.postWithToken( 'csrf', {
			action: 'wbsetclaim',
			claim: JSON.stringify( claim ),
			baserevid: wdeBaseRevId
		} ).done( function ( claimData ) {
			if ( claimData.success ) {
				mw.notify( 'Значення властивості ' + propertyId + ' успішно додано на вікідані' );

				wdeBaseRevId = claimData.pageinfo.lastrevid;
				revIds.push( claimData.pageinfo.lastrevid );
				wdeCreateClaims( propertyId, values, revIds );
			} else {
				wdeErrorDialog( 'Помилка при збереженні', JSON.stringify( claimData ) );
			}
		} );
	};

	var wdeAddTags = function ( propertyId, revIds ) {
		wdApi.postWithToken( 'csrf', {
			action: 'tag',
			add: 'InfoboxExport gadget',
			revid: revIds
		} ).done( function ( data ) {
			var success = false;
			if ( data.tag ) {
				success = true;
				for ( var i in data.tag ) {
					if ( data.tag[i].status !== 'success' ) {
						success = false;
						break;
					}
				}
			}
			if ( success ) {
				mw.notify( 'Мітки правок на вікіданих успішно встановлені' );

				$( '.no-wikidata[data-wikidata-property-id=' + propertyId + ']' )
					.removeClass( 'no-wikidata' )
					.off( 'dblclick', wdeClickEvent );
			} else {
				console.log( 'Помилка при додаванні мітки', data );
			}
		} );
	};

	var wdeLoadProperties = function () {
		if ( !wdePropertyIds.length ) {
			return;
		}
		
		var units = [];
		var userLanguage = mw.user.options.get( 'language' );		
		wdApi.get( {
			action: 'wbgetentities',
			languages: wdeConfig.languages,
			props: [ 'labels', 'datatype', 'claims' ],
			ids: wdePropertyIds
		} ).done( function ( data ) {
			if ( !data.success ) {
				return;	
			}
			
			for ( var propertyId in data.entities ) {
				if ( propertyId == -1 ) {
					return;
				}
				var entity = data.entities[propertyId];
				var label = entity.labels[userLanguage] ? entity.labels[userLanguage].value : entity.labels.en.value;
				wdeProperties[propertyId] = {
					'datatype': entity.datatype,
					'label': label.charAt(0).toUpperCase() + label.slice(1),
					'units': []
				};
				var constraints = [];
				if ( entity.claims ) {
					if ( entity.claims.P2302 ) {
						for ( var i in entity.claims.P2302 ) {
							var claim = entity.claims.P2302[i];
							if ( claim.mainsnak &&
								claim.mainsnak.datavalue &&
								claim.mainsnak.datavalue.value &&
								claim.mainsnak.datavalue.value.id
							) {
								switch ( claim.mainsnak.datavalue.value.id ) {
									case 'Q19474404':
										constraints.push( 'unique' );
										break;
								}
							}
						}
					}

					if ( entity.claims.P2237 ) {
						for ( var i in entity.claims.P2237 ) {
							var claim = entity.claims.P2237[i];
							if ( claim.mainsnak &&
								claim.mainsnak.datavalue &&
								claim.mainsnak.datavalue.value &&
								claim.mainsnak.datavalue.value.id &&
								claim.mainsnak.datavalue.value.id !== 'Q21027105'
							) {
								wdeProperties[propertyId].units.push ( claim.mainsnak.datavalue.value.id );
								units.push( claim.mainsnak.datavalue.value.id );
							}
						}
					}
				}
				if ( wdeProperties[propertyId].units.length === 0 ) {
					wdeProperties[propertyId].units.push( '1' );
				}
				wdeProperties[propertyId].constraints = $.unique( constraints );
			}
			
			var languages = wdeConfig.languages;
			languages.push( userLanguage );
			wdApi.get( {
				action: 'wbgetentities',
				languages: languages,
				props: [ 'labels', 'descriptions', 'aliases', 'claims' ],
				ids: $.unique( units )
			} ).done( function ( unitData ) {
				if ( !unitData.success ) {
					return;
				}
				
				for ( var unitId in unitData.entities ) {
					var unit = unitData.entities[unitId];
					var unitSearch = wdeConfig.units[unitId] ? wdeConfig.units[unitId].search : [];
					if ( !wdeConfig.units[unitId] ) {
						wdeConfig.units[unitId] = {};
					}

					// Метка
					if ( unit.labels ) {
						wdeConfig.units[unitId].label = unit.labels[userLanguage] ||
							unit.labels[wdeConfig.language] ||
							unit.labels.en ||
							unit.labels[Object.keys( unit.labels )[0]];

						if ( unit.labels[wdeConfig.language] ) {
							unitSearch.push( unit.labels[wdeConfig.language].value );
						}
					}

					// Описание
					if ( unit.descriptions && unit.descriptions[wdeConfig.language] ) {
						wdeConfig.units[unitId].description = unit.descriptions[userLanguage] ||
							unit.descriptions[wdeConfig.language] ||
							unit.descriptions.en ||
							unit.descriptions[Object.keys( unit.labels )[0]];
					}

					// Алиасы
					if ( unit.aliases && unit.aliases[wdeConfig.language] ) {
						for ( var i in unit.aliases[wdeConfig.language] ) {
							unitSearch.push( unit.aliases[wdeConfig.language][i].value );
						}
					}
					
					if ( unit.claims && unit.claims.P558 ) {
						for ( var i in unit.claims.P558 ) {
							var claim = unit.claims.P558[i];
							if ( claim.mainsnak &&
								claim.mainsnak.datavalue &&
								claim.mainsnak.datavalue.value
							) {
								unitSearch.push( claim.mainsnak.datavalue.value );
							}
						}
					}
					wdeConfig.units[unitId].search = unitSearch;
				}
			} );
		} );
	};

	var wdePrepareDialog = function ( $field, propertyId ) {
		if ( wdeProperties[propertyId].constraints.indexOf( 'qualifier' ) !== -1 ) {
			mw.notify( 'Неможливо визначити обов\'язковий кваліфікатор', { type: 'error' } );
			return;	
		}
		
		var values = [];
		var datatype = wdeProperties[propertyId].datatype;

		var $content = $field.clone();
		$content.find( 'sup.reference' ).remove();
		$content.find( '[style*="display:none"]' ).remove();

		switch ( datatype ) {
			case 'commonsMedia':
				var $imgs = $content.find( 'img' );
				$imgs.each( function () {
					var $img = $( this );
					var src = $img.attr( 'src' );
					if ( !src.match( /upload.wikimedia.org\/wikipedia\/commons/ ) ) {
						return;
					}
					var srcParts = src.split( '/' );
					var value = srcParts.pop();
					if ( value.match( /^\d+px-/ ) ) {
						value = srcParts.pop();
					}
					value = decodeURIComponent( value );
					value = value.replace( /_/g, ' ' );

					values.push( {
						api: value,
						label: $( '<code>' + value + '</code><br><br>' + this.outerHTML )
					} );
				} );
				break;

			case 'external-id':
			case 'string':
				var strings = $content.text().trim().split( /[,;]/ );
				for ( var i in strings ) {
					var s = strings[i].trim();
					if ( s ) {
						values.push( {
							api: s,
							label: $( '<code>' + s + '</code>' )
						} );
					}
				}
				break;

			case 'monolingualtext':
				var $items = $content.find( '[lang]' );
				$items.each( function () {
					var $item = $( this );
					values.push( {
						api: {
							text: $item.text().trim(),
							language: $item.attr( 'lang' ).trim()
						}
					} );
				} );
				if ( !values.length ) {
					var text = $content.text().trim();
					if ( text ) {
						var $items = mw.util.$content.find( '[lang]' );
						$items.each( function () {
							$item = $( this );
							if ( $item.text().trim() == text ) {
								values.push( {
									api: {
										text: text,
										language: $item.attr( 'lang' ).trim()
									}
								} );
							}
						} );
					}
				}
				for ( var i in values ) {
					values[i].label = $( '<span>' )
						.append( $( '<span>' ).css( 'color', '#666' ).text( '(' + values[i].api.language + ') ' ) )
						.append( $( '<strong>' ).text( values[i].api.text ) );
				}
				break;

			case 'quantity':
				wdeProperties[propertyId].units.forEach( function( element ) {
					var unit = wdeConfig.units[element];
					unit.search.every( function( suffix ) {
						var value = { api: { unit : '1' } };
						var text = $content.text().replace(/\u00a0/g, ' ').split( '(' )[0].trim();
						if ( suffix ) {
							if ( !text.match( new RegExp( '[\\d\\s]' + suffix + '\\.?$' ) ) ) {
								return true;
							}
							text = text.replace( new RegExp( '\\s*' + suffix + '\\.?$' ), '' );
							value.api.unit = 'http://www.wikidata.org/entity/' + element;
						}

						var multiplier = 1;
						if ( text.includes( 'тис' ) ) {
							multiplier = 1000;
						} else if ( text.includes( 'млн' ) ) {
							multiplier = 1000000;
						} else if ( text.includes( 'млрд' ) ) {
							multiplier = 1000000000;
						} else if ( text.includes( 'трлн' ) ) {
							multiplier = 1000000000000;
						} else {
							var match = text.match ('·10(\\d+)');
							if (match) {
								multiplier = '10e'+match[1];
								text = text.replace (new RegExp('·10(\\d+)'), '');
							}
						}
						var decimals = text.replace( /,/g, '.' ).replace( /−/g, '-' ).split( '±' );
						var amount = multiplier*parseFloat( decimals[0].replace( /[^0-9.\+\-]/g, '' ) );
						if ( isNaN( amount ) ) {
							return true;
						}
						value.api.amount = amount.toString();
						value.label = $( '<span>' ).append( $( '<strong>' ).text( value.api.amount ) );
		
						if ( decimals.length > 1 ) {
							var parts = value.api.amount.split( '.' );
							var fract = parts.length > 1 ? parts[1].length : 0;
							var bound = multiplier*parseFloat ( decimals[1].replace( /[^0-9.\+\-]/g, '' ) );
							if ( !isNaN( bound) ) {
								value.api.lowerBound = ( amount - bound ).toFixed( fract );
								value.api.upperBound = ( amount + bound ).toFixed( fract );
								value.label.append( $( '<span>' ).text( ' ± ' + bound.toString() ) );
							}
						}
						
						if ( suffix ) {
							value.label.append( $( '<strong>' ).text( ' ' + ( unit.label ? unit.label.value : '' ) ) )
								.append( $( '<small>' ).text( ' (' + element  + ( unit.description ? ': ' + unit.description.value : '' ) + ')' ) );
						}
						
						values.push( value );
						return false;
					} );
				} );
				break;
				
			case 'time':
				var centuries = [ 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII',
					'VIII', 'IX', 'X', 'XI', 'XII', 'XIII', 'XIV', 'XV',
					'XVI', 'XVII', 'XVIII', 'XIX', 'XX', 'XXI', 'XXII' ];
				var months = [ 'січень', 'лютий', 'березень', 'квітень', 'травень', 'червень',
					'липень', 'серпень', 'вересень', 'жовтень', 'листопад', 'грудень' ];
				var monthsGen = [ 'січня', 'лютого', 'березня', 'квітня', 'травня', 'червня',
					'липня', 'серпня', 'вересня', 'жовтня', 'листопада', 'грудня' ];
				var date = $content.text().toLowerCase().trim().replace( /\s+(року?|р\.?)$/, '' );
				var isoDate;
				var precision;
				var dateParts;
				if ( dateParts = date.match( /^([ivx]{1,5})(\століття?)?$/ ) ) {
					// век
					date = dateParts[1].toUpperCase() + ' століття';
					var century = centuries.indexOf( dateParts[1].toUpperCase() ) + 1;
					isoDate = '+' + ( century < 10 ? '0' + century : century )
						+ '00-00-00T00:00:00Z';
					precision = 7;
				} else if ( date.match( /^\d{3,4}$/ ) ) {
						isoDate = '+' + ( date < 1000 ? '0' + date : date ) + '-00-00T00:00:00Z';
					precision = 9;
				} else if ( dateParts = date.match( new RegExp( '^(' + months.join( '|' ) + ')\\s+([12]\\d{3})$' ) ) ) {
						dateParts[1] = months.indexOf( dateParts[1] ) + 1;
					isoDate = '+' + dateParts[2]
						+ '-' + ( dateParts[1] < 10 ? '0' + dateParts[1] : dateParts[1] )
						+ '-01T00:00:00Z';
					precision = 10;
				} else if ( dateParts = date.match( new RegExp( '^(\\d{1,2})\\s+(' + monthsGen.join( '|' ) + ')\\s+([12]\\d{3})$' ) ) ) {
							dateParts[1] = parseInt( dateParts[1], 10 );
					dateParts[2] = monthsGen.indexOf( dateParts[2] ) + 1;
					isoDate = '+' + dateParts[3]
						+ '-' + ( dateParts[2] < 10 ? '0' + dateParts[2] : dateParts[2] )
						+ '-' + ( dateParts[1] < 10 ? '0' + dateParts[1] : dateParts[1] )
						+ 'T00:00:00Z';
					precision = 11;
				} else if ( dateParts = date.match( new RegExp( '^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})$' ) ) ) {
						dateParts[1] = parseInt( dateParts[1], 10 );
					dateParts[2] = parseInt( dateParts[2], 10 );
					if ( dateParts[3] < 100 ) {
						dateParts[3] += 1900;
					}
					isoDate = '+' + dateParts[3]
						+ '-' + ( dateParts[2] < 10 ? '0' + dateParts[2] : dateParts[2] )
						+ '-' + ( dateParts[1] < 10 ? '0' + dateParts[1] : dateParts[1] )
						+ 'T00:00:00Z';
					precision = 11;
				} else if ( date.match( /^(зараз|наш)\s+час/ ) ) {
					isoDate = 'novalue';
				} else if ( date.match( /^(\?|невідомо)$/ ) ) {
					isoDate = 'somevalue';
				}

				if ( isoDate ) {
					if ( isoDate.match( /^(novalue|somevalue)$/ ) ) {
						values.push( {
							api: isoDate,
							label: $( '<span>' )
								.append( $( '<span>' ).css( 'color', '#666' ).text( 'Значение ' ) )
								.append( $( '<strong>' ).text( isoDate === 'novalue' ? 'отсутствует' : 'неизвестно' ) )
						} );
					} else {
						var calendar = {
							name: 'Григоріанський',
							id: 1985727
						};
						if ( isoDate < '+1582-10-15T00:00:00Z' ) {
							calendar = {
								name: 'Юліанський',
								id: 1985786
							};
						}

						values.push( {
							api: {
								time: isoDate,
								timezone: 0,
								before: 0,
								after: 0,
								precision: precision,
								calendarmodel: 'http://www.wikidata.org/entity/Q' + calendar.id
							},
							label: $( '<span>' )
								.append( $( '<strong>' ).text( date ) )
								.append( $( '<span>' ).css( 'color', '#666' ).text( ' (' + calendar.name + ') ' ) )
								.append( $( '<code>' ).text( isoDate ) )
						} );
					}
				}
				break;

			case 'wikibase-item':
				var titles = {};
				var $links = $content.find( 'a[title][class!=image][class!=new]' );

					if ( !$links.length && $content.text().trim() ) {
					titles[wdeConfig.language] = [ $content.text().trim() ];
					var parts = $content.text().split( ',' );
					for ( var i in parts ) {
						if ( parts[i].trim() ) {
							titles[wdeConfig.language].push( parts[i].trim() );
						}
					}
					titles[wdeConfig.language] = $.unique( titles[wdeConfig.language] );
				}

				$links.each( function () {
					var $link = $( this );
					var title = decodeURIComponent( $link.attr( 'href' ) )
						.replace( /^.*\/wiki\//, '' );
					if ( title ) {
						var code = wdeConfig.language;
						if ( $link.hasClass( 'extiw' ) ) {
							var m = $link.attr( 'href' ).match( /^https:\/\/([a-z\-]+)\./ );
							if ( !m || !m[1] ) {
								return;
							}
							code = m[1];
						}
						if ( !titles[code] ) {
							titles[code] = [];
						}
						titles[code].push( title );
					}
				} );

				var processTitles = function ( titles ) {
					var userLanguage = mw.user.options.get( 'language' );
					var languages = wdeConfig.languages;
					languages.push( userLanguage );

					var sites = [];
					var siteTitles = [];

					for ( var code in titles ) {
						for ( var i in titles[code] ) {
							sites.push( code + 'wiki' );
							siteTitles.push( titles[code][i] );
							if ( languages.indexOf( code ) !== -1 ) {
								languages.push( code );
							}
						}
					}

					wdApi.get( {
						action: 'wbgetentities',
						sites: sites,
						titles: siteTitles,
						languages: languages,
						props: [ 'info', 'labels', 'descriptions' ]
					} ).done( function ( data ) {
						if ( data.success ) {
							for ( var entityId in data.entities ) {
								if ( !entityId.match( /^Q/ ) ) {
									continue;
								}

								var entity = data.entities[entityId];
								var label = entity.labels[userLanguage] ||
									entity.labels.en ||
									entity.labels[Object.keys( entity.labels )[0]];
								var description = entity.descriptions[userLanguage] ||
									entity.descriptions.en ||
									entity.descriptions[Object.keys( entity.descriptions )[0]];

								values.push( {
									api: { id: entityId },
									label: $( '<span>' )
										.append( $( '<strong>' ).text( label ? label.value : entityId ) )
										.append( label ? ' (' + entityId + ')' : '' )
										.append( description ? ' — ' + description.value : '' )
								} );
							}
							wdeDialog( $field, propertyId, values );
						} else {
							wdeErrorDialog( 'Помилка при пошуку елемента за назвою статті', JSON.stringify( claimData ) );
						}
					} );
				};

				for ( var code in titles ) {
					for ( var i in titles[code] ) {
						var title = titles[code][i]
							.replace( /_/g, ' ' )
							.trim();
						title = title.charAt(0).toUpperCase() + title.substr( 1, title.length - 1 );
						if ( title ) {
							titles[code][i] = title;
						} else {
							titles[code].splice(i, 1);
						}
					}
				}

				if ( titles.ru ) {
					api.get( {
						action: 'query',
						redirects: 1,
						titles: titles.ru
					} ).done( function ( data ) {
						if ( data.query ) {
							for ( var i in data.query.redirects ) {
								var redirect = data.query.redirects[i];
								var pos = titles.ru.indexOf( redirect.from );
								while ( pos !== -1 ) {
									titles.ru.splice( pos + 1, 0, redirect.to );
									pos = titles.ru.indexOf( redirect.from, pos + 1 ) ;
								}
							}
							processTitles( titles );
						} else {
							wdeErrorDialog( 'Помилка при обробці перенаправлень', JSON.stringify( data ) );
						}
					} );
				} else {
					processTitles( titles );
				}

				return;

			case 'url':
				var $links = $content.find( 'a' );
				$links.each( function () {
					var $link = $( this );
					var url = $link.attr( 'href' );
					values.push( {
						api: url,
						label: $( '<code>' + url + '</code>' )
					} );
				} );
				break;

			default:
				console.log( datatype, $content );
		}

		values = $.unique( values );
		wdeDialog( $field, propertyId, values );
	};

	var wdeClickEvent = function ( e ) {
		var $field = $( this );
		var propertyId = $field.attr( 'data-wikidata-property-id' );
		wdePrepareDialog( $field, propertyId );
	};

	var wdeDialog = function ( $field, propertyId, values ) {
		var fieldset;

		if ( !values || !values.length ) {
			mw.notify( 'Неможливо визначити значення властивості', { type: 'error' } );
			return;
		}

		function ProcessDialog( config ) {
			ProcessDialog.super.call( this, config );
		}
		OO.inheritClass( ProcessDialog, OO.ui.ProcessDialog );
		ProcessDialog.static.title = $( '<span>' )
			.attr( 'title', 'Версія ' )
			.text( 'Експорт на вікідані' );
		ProcessDialog.static.actions = [
			{ action: 'export', label: 'Експорт', flags: [ 'primary', 'constructive' ] },
			{ label: 'Відміна', flags: 'safe' }
		];
		ProcessDialog.prototype.initialize = function () {
			ProcessDialog.super.prototype.initialize.apply( this, arguments );
			this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );

			this.content.$element
				.append( $( '<p>' ).text( 'Експортувати значення властивості на вікідані?' ) )
				.append( $( '<p>' )
					.append( $( '<strong>' ).text( wdeProperties[propertyId].label ) )
					.append( $( '<span>' ).text( ' (' + propertyId + '):' ) )
				);

			fieldset = new OO.ui.FieldsetLayout();
			for ( var i in values ) {
				var checkbox = new OO.ui.CheckboxInputWidget( {
					value: JSON.stringify( values[i].api ),
					selected: ( wdeProperties[propertyId].constraints.indexOf( 'unique' ) !== -1 ? i == 0 : true )
				} );
				fieldset.addItems( [
					new OO.ui.FieldLayout( checkbox, {
						label: values[i].label,
						align: 'inline'
					} )
				] );
			}
			this.content.$element.append( fieldset.$element );

			this.$body.append( this.content.$element );
		};
		ProcessDialog.prototype.getActionProcess = function ( action ) {
			var dialog = this;
			if ( action === 'export' ) {
				return new OO.ui.Process( function () {
					var values = [];
					var fields = fieldset.getItems();
					for ( var i in fields ) {
						var checkbox = fields[i].getField();
						if ( checkbox.isSelected() ) {
							values.push( checkbox.getValue() );
						}
					}

					wdeCreateClaims( propertyId, values );
					dialog.close( { action: action } );
				}, this );
			}
			return ProcessDialog.super.prototype.getActionProcess.call( this, action );
		};
		var windowManager = new OO.ui.WindowManager();
		$( 'body' ).append( windowManager.$element );
		var processDialog = new ProcessDialog();
		windowManager.addWindows( [ processDialog ] );
		windowManager.openWindow( processDialog );
	};

	var wdeInit = function() {
		if ( mw.config.get( 'wgWikibaseItemId' ) === null ||
			mw.config.get( 'wgAction' ) !== 'view' ||
			mw.config.get( 'wgNamespaceNumber' )
		) {
			return;
		}

		api = new mw.Api();
		wdApi = new mw.ForeignApi( '//www.wikidata.org/w/api.php' );

		wdeWindowManager = new OO.ui.WindowManager();
		$( 'body' ).append( wdeWindowManager.$element );

		wdApi.get( {
			action: 'wbgetentities',
			props: [ 'info', 'claims' ],
			ids: mw.config.get( 'wgWikibaseItemId' )
		} ).done( function ( data ) {
			if ( data.success ) {
				var claims;
				for ( var i in data.entities ) {
					if ( i == -1 ) {
						return;
					}

					claims = data.entities[i].claims;
					wdeBaseRevId = data.entities[i].lastrevid;
					break;
				}
				if ( !claims ) {
					return;
				}

				var $fields = $( '.infobox .no-wikidata' );
				$fields.each( function() {
					var $field = $( this );
					var propertyId = $field.attr( 'data-wikidata-property-id' );

					if ( claims[propertyId] ) {
						$field.removeClass( 'no-wikidata' ).addClass( 'check-wikidata' );
					} else if ( $field.find( '.wikidata-claim' ).length ) {
						$field.removeClass( 'no-wikidata' );
					} else {
						var $img = $field.find( '.image img[src*="/wikipedia/' + wdeConfig.language + '/"]' );
						if ( $img.width() >= 80 ) {
								$field.removeClass( 'no-wikidata' );
						} else {
							wdePropertyIds.push( propertyId );
						}
					}
				} );
				$fields = $( '.infobox .no-wikidata' );
				mw.util.addCSS( '\
					.infobox .no-wikidata {\
						display: block !important;\
						background: #fdc;\
						padding: 5px 0;\
					}' );

				wdeLoadProperties();

				$fields.on( 'dblclick', wdeClickEvent );
			}
		} );
	};

	$.when(
		$.ready,
		mw.loader.using( [
			'mediawiki.api',
			'mediawiki.ForeignApi',
			'mediawiki.util',
			'oojs-ui-core',
			'oojs-ui-widgets',
			'oojs-ui-windows'
		] )
	).done( wdeInit );
}

$( document ).ready(function () {
	hello();
});