#url /skg-if-wikidata/v1
#type api
#base https://w3id.org/skg-if/sandbox/wikidata
#title SKG-IF API for Wikidata
#description SKG-IF compliant API exposing Wikidata research product data as JSON-LD, following the <a href="https://skg-if.github.io/interoperability-framework/" target="_blank">Scholarly Knowledge Graph Interoperability Framework specification</a>.
#html_meta_description SKG-IF API for Wikidata: access research product metadata via the Scholarly Knowledge Graph Interoperability Framework.
#version 0.1.0
#contacts <a href="mailto:wikidata@lists.wikimedia.org" target="_blank">wikidata@lists.wikimedia.org</a>
#license This document is licensed with a <a href="https://creativecommons.org/licenses/by/4.0/legalcode" target="_blank">Creative Commons Attribution 4.0 International License</a>, while the REST API itself has been created using <a href="https://github.com/opencitations/ramose" target="_blank">RAMOSE, the Restful API Manager Over SPARQL Endpoints</a>, which is licensed with an <a href="https://opensource.org/licenses/ISC" target="_blank">ISC license</a>.
#endpoint https://query.wikidata.org/sparql
#method get
#addon ramose.skg_if
#disable_params require,filter,sort,format,json

#url /products/{local_identifier}
#type operation
#local_identifier str(http://www\.wikidata\.org/entity/Q\d+)
#method get
#description Returns a single research product from Wikidata identified by its entity URI.
#call /products/http://www.wikidata.org/entity/Q139571902
#format skg_if,to_skg_if,application/ld+json
#default_format skg_if
#sparql PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?local_identifier ?title ?title_lang ?product_type
       ?manifestation_version ?manifestation_dates_type ?manifestation_dates_value
       ?manifestation_licence
WHERE {
  BIND(<[[local_identifier]]> AS ?local_identifier)
  ?local_identifier rdfs:label ?title_raw . FILTER(LANG(?title_raw) = "en")
  BIND(STR(?title_raw) AS ?title)
  BIND(LANG(?title_raw) AS ?title_lang)
  OPTIONAL {
    ?local_identifier wdt:P31 ?_instance_type .
    VALUES (?_instance_type ?_mapped_type) {
      (wd:Q13442814 "literature")
      (wd:Q18918145 "literature")
      (wd:Q7318358  "literature")
      (wd:Q7397     "research software")
      (wd:Q341      "research software")
      (wd:Q506883   "research software")
      (wd:Q1172284  "research data")
    }
  }
  BIND(COALESCE(?_mapped_type, "other") AS ?product_type)
  OPTIONAL {
    {
      SELECT ?manifestation_version ?manifestation_dates_value WHERE {
        {
          <[[local_identifier]]> p:P348 ?version_stmt .
          ?version_stmt ps:P348 ?manifestation_version ;
              pq:P577 ?release_date .
          BIND(REPLACE(STR(?release_date), "T00:00:00Z$", "") AS ?manifestation_dates_value)
        } UNION {
          <[[local_identifier]]> wdt:P577 ?pub_date .
          BIND(REPLACE(STR(?pub_date), "T00:00:00Z$", "") AS ?manifestation_dates_value)
        }
      }
      ORDER BY DESC(?manifestation_dates_value)
      LIMIT 1
    }
    BIND("publication" AS ?manifestation_dates_type)
  }
  OPTIONAL {
    ?local_identifier wdt:P275 ?licence_entity .
    BIND(STR(?licence_entity) AS ?manifestation_licence)
  }
}

@@join ?local_identifier ?local_identifier type=left
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT ?local_identifier ?identifier_scheme ?identifier_value
WHERE {
  BIND(<[[local_identifier]]> AS ?local_identifier)
  { ?local_identifier wdt:P356 ?v . BIND("doi" AS ?identifier_scheme) BIND(?v AS ?identifier_value) }
  UNION { ?local_identifier wdt:P4901 ?v . BIND("url" AS ?identifier_scheme) BIND(CONCAT("https://zenodo.org/record/", ?v) AS ?identifier_value) }
  UNION { ?local_identifier wdt:P6138 ?v . BIND("url" AS ?identifier_scheme) BIND(CONCAT("https://archive.softwareheritage.org/", ?v) AS ?identifier_value) }
}

@@join ?local_identifier ?local_identifier type=left
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?local_identifier ?contribution_by_name
       ?contribution_by_identifier_scheme ?contribution_by_identifier_value
       ?contribution_by_local_identifier
       ?contribution_role ?_contribution_key
WHERE {
  BIND(<[[local_identifier]]> AS ?local_identifier)
  {
    { ?local_identifier wdt:P178 ?contributor . }
    UNION
    { ?local_identifier wdt:P50 ?contributor . }
  }
  ?contributor rdfs:label ?contribution_by_name . FILTER(LANG(?contribution_by_name) = "en")
  OPTIONAL {
    ?contributor wdt:P496 ?contribution_by_identifier_value .
    BIND("orcid" AS ?contribution_by_identifier_scheme)
  }
  BIND(STR(?contributor) AS ?contribution_by_local_identifier)
  BIND(CONCAT(STR(?local_identifier), "/contributor/", STRAFTER(STR(?contributor), "http://www.wikidata.org/entity/")) AS ?_contribution_key)
  BIND("author" AS ?contribution_role)
}

@@join ?local_identifier ?local_identifier type=left
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?local_identifier ?topic_term ?topic_label ?topic_label_lang
WHERE {
  BIND(<[[local_identifier]]> AS ?local_identifier)
  ?local_identifier wdt:P921 ?topic .
  BIND(STR(?topic) AS ?topic_term)
  BIND("en" AS ?topic_label_lang)
  ?topic rdfs:label ?topic_label . FILTER(LANG(?topic_label) = "en")
}
