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

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על 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 )	{
		// Find a link for item
		wdApi.get( {
			action: 'wbgetentities',
			props: 'sitelinks',
			ids: templateId
		} ).done( function ( data ) {
			if ( data.success ) {
				for ( var i in data.entities ) {
					if ( !i.match( /^Q/ ) ) {
						continue;
					}

					var sitelinks = data.entities[ i ].sitelinks;
					if ( !sitelinks ) {
						continue;
					}
					
					if ( sitelinks[ site ] ) {
						mw.notify('הצלחה: ' + sitelinks[ site ].title, 'warn' );
						return;
					}
				}
			}

			mw.notify( 'לא נמצאה תבנית', 'error' );
		} );
	}

	/*
	 * Recursive infobox search
	 */
	function getMainInfobox( entityId ) {
		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 ) {
			var templateUrl = ( ( ( ( ( data || {} ).results || {} ).bindings || [] )[ 0 ] || {} ).tpl || {} ).value;
			if ( !templateUrl ) {
				mw.notify( 'לא נמצאה תבנית', 'error' );
				return;
			}
			var templateId = templateUrl.replace( 'http://www.wikidata.org/entity/', '' );
			getMainInfoboxSitelink( templateId );
		});
	}

	var init = function () {
		wdApi = new mw.ForeignApi( '//www.wikidata.org/w/api.php' );

		if ( mw.config.get( 'wgAction' ) !== 'view' ||
			mw.config.get( 'wgNamespaceNumber' ) ||
			mw.config.get( 'wgIsMainPage' )
		) {
			return;
		}
		if ( mw.config.get( 'wgWikibaseItemId' ) === null ) {
			mw.notify( 'אין פריט ויקינתונים', 'warn' );
			return;
		}

		if ( mw.util.$content === null ) {
			mw.util.$content = $( '#mw-content-text' );
		}

		getMainInfobox( mw.config.get( 'wgWikibaseItemId' ) );
	};

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

}( mediaWiki, jQuery ) );