לדלג לתוכן

משתמש:IKhitron/infobox1.js

מתוך ויקיפדיה, האנציקלופדיה החופשית

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
/* Thanks to User:Putnik */
/* jshint multistr: true */
/* global mediaWiki */
/* global jQuery */

( function ( mw, $ ) {
	var wdApi;
	var site = mw.config.get( 'wgDBname' );

	/*
	 * Find links for inboxes for the found elements
	 */
	function getMainInfoboxSitelink( templateId, entityID, name )	{
		$('#' + name + ' .third').css({background:'green'}).text('phase5');
		// Find a link for item
		wdApi.get( {
			action: 'wbgetentities',
			props: 'sitelinks',
			ids: templateId
		} ).done( function ( data ) {
			$('#' + name + ' .third').css({background:'green'}).text('phase7');
			if ( data.success ) {
				$('#' + name + ' .third').css({background:'green'}).text('phase9');
				for ( var i in data.entities ) {
					if ( !i.match( /^Q/ ) ) {
						continue;
					}

					var sitelinks = data.entities[ i ].sitelinks;
					if ( !sitelinks ) {
						continue;
					}
					$('#' + name + ' .third').css({background:'green'}).text('phase11');
					
					if ( sitelinks[ site ] ) {
						$('#' + name + ' .third').css({background:'yellow'}).text(sitelinks[ site ].title);
						return;
					}
				}
			}
			$('#' + name + ' .third').css({background:'red'}).text('none1');
		} ).fail(function(data) {
				$('#' + name + ' .third').css({background:'red'}).text('none3');
		});
	}

	/*
	 * Recursive infobox search
	 */
	function getMainInfobox( entityId, name ) {
		var query = 'SELECT DISTINCT ?tpl\
			WHERE {\
				{\
					SELECT DISTINCT ?tpl ?tplClass ?supTpl ?depth (count(?midTpl) as ?specific)\
					WHERE {\
						{\
							SELECT DISTINCT ?tpl ?tplClass (count(?mid) as ?depth)\
							WHERE {\
								?tpl wdt:P31 wd:Q19887878 .\
								?tpl wdt:P1423 ?tplClass .\
								wd:' + entityId + ' wdt:P31|wdt:P106 ?entityClass .\
								?entityClass wdt:P279* ?mid .\
								?mid wdt:P279* ?tplClass .\
							}\
							GROUP BY ?tpl ?tplClass\
						}\
						OPTIONAL{\
						  ?tpl wdt:P279* ?midTpl .\
						  ?midTpl wdt:P279* ?supTpl .\
						}\
					}\
					GROUP BY ?tpl ?tplClass ?supTpl ?depth\
				}\
				FILTER EXISTS {\
					?page schema:about ?tpl .\
					?page schema:isPartOf <https:' + mw.config.get( 'wgServer' ) + '/> .\
				}\
			}\
			ORDER BY ASC(?depth) DESC(?specific)\
			LIMIT 1';
		var url = 'https://query.wikidata.org/sparql?format=json&query=' + mw.util.rawurlencode( query );
		$.post( url, function( data ) {
		$('#' + name + ' .third').css({background:'green'}).text('phase');
			var templateUrl = ( ( ( ( ( data || {} ).results || {} ).bindings || [] )[ 0 ] || {} ).tpl || {} ).value;
			if ( !templateUrl ) {
				$('#' + name + ' .third').css({background:'red'}).text('none');
				return;
			}
			var templateId = templateUrl.replace( 'http://www.wikidata.org/entity/', '' );
			getMainInfoboxSitelink( templateId, entityId, name );
		});
	}

	var init = function () {
		wdApi = new mw.ForeignApi( '//www.wikidata.org/w/api.php' );
	let set1 = $('.second');
	let length = set1.length;
	let text,name;
	set1.each( function( index, el ) {
		text1 = $(el).text();
		name = $(el).parent().attr('id');
		$('#' + name + ' .third').css({background:'green'}).text('phase1');
		$( el ).text(getMainInfobox(text1, name));
	});
	};

	$.when(
		$.ready,
		mw.loader.using( [
			'mediawiki.api',
			'mediawiki.ForeignApi',
			'mediawiki.page.ready',
			'mediawiki.util',
		] )
	).done( init );

}( mediaWiki, jQuery ) );