User:Bovlb/notability.js

From Wikidata
Jump to navigation Jump to search

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.
// Adds a small notability indicator to the top right of an item showing 
// how well the item satisfies the three notability criteria.
// Left to right, the three columns indicate sitelinks, 
// identifiers and references, and structural need.
// The middle column shows identifiers at the top and references at the bottom.
// The box shows whether any of the three criteria is met.
// As a half-hearted attempt to deal with lag, the indicator is hidden if
// nothing is known about the item.  
// The indicator will (briefly) show as a black square during loading.
//
// The exact query used here is a work in progress.
// Obviously such simple mechanical determination is simply a starting point.
//
// For each criterion, it is assessed at weak and strong levels, so the result
// is tri-state (red/amber/green).  The gap between amber and green is intended
// to represent the reasonable variation found in community interpretation of 
// the criteria.
//
// To see an English-language explanation of the test applied, see:
//     [[User:Bovlb/notability.js/doc]].
// To play with the SPARQL, see:
//     [[User:Bovlb/queries#Notability]]

/*jshint esversion: 6 */
// <nowiki>
(function(mw, $, wb) {
    "use strict";

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

    var lang = mw.config.get('wgUserLanguage');
    var messages, entityid = mw.config.get('wbEntityId'), api = new mw.Api();

    var html = `
    <table id="notability" 
        style="table-layout: fixed; border-spacing: 1pt; width: 25px; height: 25px; border: solid 3px; ">
      <tr>
        <td rowspan="2" id="notability1" />
        <td id="notability2a" />
        <td rowspan="2" id="notability3" />
      </tr>
      <tr>
        <td id="notability2b" />
      </tr>
    `;

    var sparql = `
    SELECT * WHERE {
        VALUES ?x { wd:${entityid} }
      
        BIND(exists {
            ?x schema:version ?version .
          } AS ?exists)
            
          # N1: It contains at least one valid sitelink ...
          BIND(EXISTS { # ?notability1_strong
            ?sitelink schema:about ?x .
          } AS ?notability1_strong)
        
          BIND(?notability1_strong as ?notability1_weak) # FUTURE
          
          # N2a: It refers to an instance of a clearly identifiable conceptual or material entity
          BIND(EXISTS { # ?notability2a_weak
            {
              ?x ?p ?statement .
              ?prop wikibase:claim ?p .
              ?prop wikibase:propertyType <http://wikiba.se/ontology#ExternalId> . 
            } UNION {
              ?x p:P856 ?statement . # official website
            }
          } AS ?notability2a_weak)
        
          BIND(EXISTS { # ?notability2a_strong
            {
              ?x ?p ?statement .
              ?prop wikibase:claim ?p .
              ?prop wikibase:propertyType <http://wikiba.se/ontology#ExternalId> . 
            } MINUS {
              ?prop wdt:P279*/wdt:P31 wd:Q105388954 . # Wikidata property to identify online accounts 
            }
          } AS ?notability2a_strong)
        
          #N2b: The entity must be notable, in the sense that it can be described using serious and publicly available references.
          BIND(EXISTS { # ?notability2b_weak
            {
              ?x ?p ?statement .
              ?statement prov:wasDerivedFrom ?ref . 
            } UNION {
              ?x ?p ?statement .
              ?prop wikibase:claim ?p .
              ?prop wdt:Q31 wd:Q62589316 . # Wikidata property for an identifier that suggests notability
            } UNION {
              ?x p:P973 ?statement . # described at URL
            }
          } AS ?notability2b_weak)
        
          BIND(EXISTS { # ?notability2b_strong
            {
              {
                ?x ?p ?statement .
                ?statement prov:wasDerivedFrom ?ref . 
              } MINUS {
                ?ref ?pr ?source .
                ?prop wikibase:reference ?pr .
                ?prop wdt:P31 wd:Q113558322 . # Wikidata property that suggests a reference is weak
              }
            } UNION {
              ?x ?p ?statement .
              ?prop wikibase:claim ?p .
              ?prop wdt:Q31 wd:Q62589316 . # Wikidata property for an identifier that suggests notability
            } UNION {
              ?x p:P973 ?statement . # described at URL
            }
          } AS ?notability2b_strong)
        
          # N3: It fulfills a structural need, for example: it is needed to make statements made in other items more useful.
          BIND(EXISTS { # ?notability3_strong
            ?statement ?ps ?x .
            ?other ?p ?statement .
            ?prop wikibase:claim ?p .
            FILTER(?other != ?x)
          } AS ?notability3_strong)
            
          BIND(?notability3_strong as ?notability3_weak) # FUTURE
          
          BIND((?notability1_strong || (?notability2a_strong && ?notability2b_strong) || ?notability3_strong) AS ?notability_strong)
          BIND((?notability1_weak || (?notability2a_weak && ?notability2b_weak) || ?notability3_weak) AS ?notability_weak)
      }
    `;


    function init() {
        $('.mw-indicators').append(html);

        var api = new mw.Api({
            ajax: {
                url: 'https://query.wikidata.org/bigdata/namespace/wdq/sparql?',
                dataType: 'json',
                cache: true
            }
        });
        
        api.get({
		    query: sparql
        }).done(function(data) {
            function color(weak, strong) {
                return strong.value == "true" ? "green" :
                    weak.value == "true" ? "#FFBF00" // amber yellow
                    : "red"
            }

            for (var k in data.results.bindings) {
                var page = data.results.bindings[k];
                if(page.exists.value == "true") {
                    $('#notability1').css('background-color', 
                        color(page.notability1_weak, page.notability1_strong));
                    $('#notability2a').css('background-color', 
                        color(page.notability2a_weak, page.notability2a_strong));
                    $('#notability2b').css('background-color', 
                        color(page.notability2b_weak, page.notability2b_strong));
                    $('#notability3').css('background-color', 
                        color(page.notability3_weak, page.notability3_strong));
                    $('#notability').css('border-color', 
                        color(page.notability_weak, page.notability_strong));
                } else {
                    $('#notability').css('display', 'none');
                }
            }
        });
    }

    $(init);

}(mediaWiki, jQuery, wikibase));
// </nowiki>