MediaWiki:Gadget-EasyQuery.js

From Wikibase Personal data
Revision as of 23:28, 17 February 2020 by Podehaye (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After saving, 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: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
// Modified from [https://www.wikidata.org/wiki/MediaWiki:Gadget-EasyQuery.js]

$( function () {
    if ( 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' ] ).then( function () {
        var searchIcon = new OO.ui.IconWidget( {
          icon: 'ellipsis',
          iconTitle: title,
          $element: $( '<a>' ).attr( {
            href: 'https://query.personaldata.io/' + url,
            target: '_blank',
            style: 'background-size: 20px 20px; 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.personaldata.io/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 = '#%23defaultView%3AGraph%0APREFIX%20pdio%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fentity%2F%3E%0APREFIX%20pdiot%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fprop%2Fdirect%2F%3E%0APREFIX%20pdiop%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fprop%2F%3E%0APREFIX%20pdiops%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fprop%2Fstatement%2F%3E%0APREFIX%20pdiopq%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fprop%2Fqualifier%2F%3E%0A%0ASELECT%20%3Fnode%20%3FnodeLabel%20%3FchildNode%20%3FchildNodeLabel%20%3Frgb%20WHERE%20%7B%0A%20%20%7B%0A%20%20%20%20BIND%28pdio%3A'
  + id + '%20AS%20%3Fnode%29%0A%20%20%20%20%3Fnode%20%3Fp%20%3Fi.%0A%20%20%20%20%3FchildNode%20%3Fx%20%3Fp.%0A%20%20%20%20%3FchildNode%20rdf%3Atype%20wikibase%3AProperty.%0A%20%20%20%20FILTER%28STRSTARTS%28STR%28%3Fi%29%2C%20%22https%3A%2F%2Fwiki.personaldata.io%2Fentity%2FQ%22%29%29%0A%20%20%20%20FILTER%28STRSTARTS%28STR%28%3FchildNode%29%2C%20%22https%3A%2F%2Fwiki.personaldata.io%2Fentity%2FP%22%29%29%0A%20%20%7D%0A%20%20UNION%0A%20%20%7B%0A%20%20%20%20BIND%28%22EFFBD8%22%20AS%20%3Frgb%29%0A%20%20%20%20pdio%3A'
  + id + '%20%3Fp%20%3FchildNode.%0A%20%20%20%20%3Fnode%20%3Fx%20%3Fp.%0A%20%20%20%20%3Fnode%20rdf%3Atype%20wikibase%3AProperty.%0A%20%20%20%20FILTER%28STRSTARTS%28STR%28%3FchildNode%29%2C%20%22https%3A%2F%2Fwiki.personaldata.io%2Fentity%2FQ%22%29%29%0A%20%20%7D%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22' + mw.config.get( 'wgUserLanguage' ) + '%22.%20%7D%0A%7D';
    createPopupAndAddIcon( $title.find( '.wikibase-title-id' ), url, 'Click to see graph', 'Entity Graph');

    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 = 'pdio:' + qid.replace("Item:","");
          }
          if ( qid ) {
          	
            var url = '#PREFIX%20pdio%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fentity%2F%3E%0APREFIX%20pdiot%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fprop%2Fdirect%2F%3E%0APREFIX%20pdiop%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fprop%2F%3E%0APREFIX%20pdiops%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fprop%2Fstatement%2F%3E%0APREFIX%20pdiopq%3A%20%3Chttps%3A%2F%2Fwiki.personaldata.io%2Fprop%2Fqualifier%2F%3E%0A%0ASELECT%20%3Fitem%20%3FitemLabel%20WHERE%20%7B%0A%20%20%3Fitem%20pdiot%3A' + pidTemp + '%20' + qid + '.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22' + mw.config.get( 'wgUserLanguage' ) + '%22%20.%20%0A%20%20%7D%0A%20%20%0A%7D%0ALIMIT%20100';
            createPopupAndAddIcon( $( this ).siblings( '.wikibase-snakview-indicators' ), url, 'More "' + pLabelTemp + '" "'+ qLabel + '"', '"' + pLabelTemp + '" "'+ qLabel + '"' );
          }
        } );
      } );
    } );
  } );