User:Ainali/common-properties.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* This script adds an icon for a popup after an item that runs a query to show 
   which properties most often use the item as a value.

   To use it, add the following line to your common.js:
   importScript( 'User:Ainali/common-properties.js' );
   
   This script was created by adapting MediaWiki:Gadget-EasyQuery.js 
*/
$( function () {

		if ( !((mw.config.get( 'wgNamespaceNumber' ) == 0 ) || ( mw.config.get( 'wgNamespaceNumber' ) == 120 ))) {
			return;
		}

		function createPopupAndAddIcon( element, url, title, label ) {
			mw.loader.using( [ 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui.styles.icons-interactions', 'oojs-ui.styles.icons-user' ] ).then( function () {
				var searchIcon = new OO.ui.IconWidget( {
					icon: 'watchlist',
					iconTitle: title,
					$element: $( '<a>' ).attr( {
						href: 'https://query.wikidata.org/' + url,
						target: '_blank',
						style: 'background-size: 16px 16px; opacity: 0.5;'
					} )
				} );

				var $content = $( '<div>' );
				var popup = new OO.ui.PopupWidget( {
					$content: $content,
					width: 500,
					head: true,
					padded: false,
					label: label,
					align: 'force-right'
				} );

				$( document ).keydown( function( e ) {
					// ESCAPE key pressed
					if ( e.keyCode === 27 ) {
						popup.onCloseButtonClick();
					}
				} );

				searchIcon.$element.click( function () {
					$content.html(
						$( '<iframe scrolling="yes" frameborder="0">' )
						.attr( {
							src: 'https://query.wikidata.org/embed.html' + url,
							width: '500',
							height: '400'
						} )
					);
					popup.$element.attr( 'style', 'position:absolute; z-index:100;' );
					popup.toggle( true );
					return false;
				} );
				$( element ).append( searchIcon.$element, popup.$element );
			} );
		}

		// Title lable popup
		var $title = $( '.wikibase-title' ),
			id = $title.find( '.wikibase-title-id' ).text().replace( /[()]/g, '' ),
			url = '#SELECT%20%3Fproperty%20%3FpropertyLabel%20%28COUNT%28%3FpropertyRel%29%20AS%20%3Fcount%29%20WHERE%20%7B%0A%3Fitem%20%3FpropertyRel%20wd:' 
			+ id + 
			'.%0A%3Fproperty%20wikibase%3AdirectClaim%20%3FpropertyRel.%0ASERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22' 
			+ mw.config.get( 'wgUserLanguage' ) 
			+ '%22.%20%7D%0A%7D%20GROUP%20BY%20%3Fproperty%20%3FpropertyLabel%0AORDER%20BY%20DESC%28%3Fcount%29';
		createPopupAndAddIcon( $title.find( '.wikibase-title-id' ), url, 'Click to see properties that commonly have this item as value', 'Most common properties that have this item as value');

		mw.hook( 'wikibase.entityPage.entityLoaded' ).add( function ( entityData ) {
			// Statement popups
			$( '.wikibase-statementgroupview' ).each( function () {
				var pid = this.id,
					pLabel = $( this ).find( '.wikibase-statementgroupview-property-label' ).text();

				$( '.wikibase-statementgroupview#' + pid )
				.find( '.wikibase-statementview-mainsnak-container' )
				.find( '.wikibase-snakview-value' )
				.each( function () {
					var pidTemp, pLabelTemp, qid, qLabel, type, value,
					pElement = $( this ).parents( '.wikibase-snakview' )
					.find( '.wikibase-snakview-property' ).find( 'a' );
					if ( pElement.length ) {
						pidTemp = pElement.attr( 'title' ).split( ':' )[ 1 ];
						pLabelTemp = pElement.text();
					} else {
						pidTemp = pid;
						pLabelTemp = pLabel;
					}

					qid = $( this ).find( 'a' ).attr( 'title' );
					qLabel = $( this ).find( 'a' ).text();
					if ( !qid ) {
						if ( $( this ).find( 'a' ).length === 0 ) {
							if ( $( this ).find( '.wb-monolingualtext-value' ).length ) {
								qLabel = $( this ).find( '.wb-monolingualtext-value' ).html();
								qid = null;
							} else {
								qLabel = $( this ).html();
								try {
									datavalue = entityData.claims[ pidTemp ][ 0 ].mainsnak.datavalue
									type = datavalue.type;
								} catch( e ) {
									type = null;
								}
								if ( type === 'time' ) {
									time = datavalue.value.time;
									qid = '"' + time + '"^^xsd:dateTime';
								} else if ( type === 'quantity' ) {
									amount = datavalue.value.amount;
									qid = amount;
								} else {
									qLabel = $( this ).text();
									qid = '"' + qLabel + '"';
								}
							}
						}
					} else {
						qid = 'wd:' + qid;
					}
					if ( qid ) {
						// console.log(qid);
						var url = '#SELECT%20%3Fproperty%20%3FpropertyLabel%20%28COUNT%28%3FpropertyRel%29%20AS%20%3Fcount%29%20%3Furl%20WHERE%20%7B%0A%3Fitem%20%3FpropertyRel%20' 
						+ qid + 
						'.%0A%3Fproperty%20wikibase%3AdirectClaim%20%3FpropertyRel.%0ASERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22' 
						+ mw.config.get( 'wgUserLanguage' ) + 
						'%22.%20%7D%0ABIND(URI(CONCAT("https://query.wikidata.org/embed.html#SELECT%20%3Fitem%20%3FitemLabel%20WHERE%20%7B%20%3Fitem%20",REPLACE(STR(?propertyRel),"http://www.wikidata.org/prop/direct/","wdt:"),"%20' 
						+ qid + 
						'%20.%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%5C%22' 
						+ mw.config.get( 'wgUserLanguage' ) + 
						'%5C%22.%20%7D%20%7D"%20))%20AS%20?url)%20%7D%20GROUP%20BY%20%3Fproperty%20%3FpropertyLabel%3Furl%20%20ORDER%20BY%20DESC%28%3Fcount%29';
						createPopupAndAddIcon( $( this ).siblings( '.wikibase-snakview-indicators' ), url, 'More "' + qLabel + '"', 'Most common properties that use "' + qLabel + '" as a value.' );
					}
				} );
			} );
		} );
	} );