Jump to content

SPARQL to datanamespace project (autumn 2022)

From mediawiki.org

Description

[edit]

Web tool executes a SPARQL query and saves the result to wikipage. SPARQL query and target page is defined in the link which opens the tool. After the tool is opened, the tool shows the link to query.wikidata.org where the query can be previewed. The user can execute the query, and tool will save the result to wikipage. After the save tool will show if the saving was successful and the link to the wikipage in Tabular Data format.

Similar tools

[edit]

ListeriaBot is to show what the use case for the tool is.

Example usage
Complex usage example

Licence to be used and revision control

[edit]
  • Licence: (some open source licence, in example w:MIT Licence)
  • Revision control: (some public revision control such as Github)

Input

[edit]

Url parameters

[edit]
  • wikipage = target page in Beta Commons
  • sparql = SPARQL-query

Example SPARQL query

[edit]
#Population in Finland after 1960
SELECT  ?item
        ?itemLabel
        (YEAR(?date) as ?year)
        ?population 
WHERE
{
        BIND(wd:Q33 as ?item)
        ?item p:P1082 ?populationStatement .
        ?populationStatement    ps:P1082 ?population
        ; pq:P585 ?date .
        SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }               
  FILTER (YEAR(?date) >= 1960)
}
ORDER BY ?objectLabel ?year

Try it!

Open Query in the web interface
Execute the query and get result in JSON format

Output

[edit]

The result should be stored to Beta commons to the page defined in parameter wikipage in Tabular Data format.

Notes
  • In the commons data namespace, stored data needs to be formatted to a two-dimensional array.
  • Following datatypes should be handled when saving values as tabular data
    • Float and integers = number
    • Boolean = boolean
    • any other = string

Sample for tabular data JSON

[edit]
{
  "head" : {
    "vars" : [ "item", "itemLabel", "year", "population" ]
  },
  "results" : {
    "bindings" : [ {
      "item" : {
        "type" : "uri",
        "value" : "http://www.wikidata.org/entity/Q33"
      },
      "itemLabel" : {
        "xml:lang" : "en",
        "type" : "literal",
        "value" : "Finland"
      },
      "population" : {
        "datatype" : "http://www.w3.org/2001/XMLSchema#decimal",
        "type" : "literal",
        "value" : "4429634"
      },
      "year" : {
        "datatype" : "http://www.w3.org/2001/XMLSchema#integer",
        "type" : "literal",
        "value" : "1960"
      }
    }, {
      "item" : {
        "type" : "uri",
        "value" : "http://www.wikidata.org/entity/Q33"
      },
      "itemLabel" : {
        "xml:lang" : "en",
        "type" : "literal",
        "value" : "Finland"
      },
      "population" : {
        "datatype" : "http://www.w3.org/2001/XMLSchema#decimal",
        "type" : "literal",
        "value" : "4461005"
      },
      "year" : {
        "datatype" : "http://www.w3.org/2001/XMLSchema#integer",
        "type" : "literal",
        "value" : "1961"
      }
    }, {
      "item" : {
        "type" : "uri",
        "value" : "http://www.wikidata.org/entity/Q33"
      },
      "itemLabel" : {
        "xml:lang" : "en",
        "type" : "literal",
        "value" : "Finland"
      },
      "population" : {
        "datatype" : "http://www.w3.org/2001/XMLSchema#decimal",
        "type" : "literal",
        "value" : "4491443"
      },
      "year" : {
        "datatype" : "http://www.w3.org/2001/XMLSchema#integer",
        "type" : "literal",
        "value" : "1962"
      }
    } ]
  }
}

Output
{
    "license": "CC0-1.0",
    "description": {
        "en": "Result of SPARQL query"
    },
    "sources": "SPARQL Query: https://query.wikidata.org/#%23Population%20in%20Finland%20after%201960%0ASELECT%20%20%3Fitem%0A%20%20%20%20%20%20%20%20%3FitemLabel%0A%20%20%20%20%20%20%20%20%28YEAR%28%3Fdate%29%20as%20%3Fyear%29%0A%20%20%20%20%20%20%20%20%3Fpopulation%20%0AWHERE%0A%7B%0A%20%20%20%20%20%20%20%20BIND%28wd%3AQ33%20as%20%3Fitem%29%0A%20%20%20%20%20%20%20%20%3Fitem%20p%3AP1082%20%3FpopulationStatement%20.%0A%20%20%20%20%20%20%20%20%3FpopulationStatement%20%20%20%20ps%3AP1082%20%3Fpopulation%0A%20%20%20%20%20%20%20%20%3B%20pq%3AP585%20%3Fdate%20.%0A%20%20%20%20%20%20%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20%7D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20FILTER%20%28YEAR%28%3Fdate%29%20%3E%3D%201960%29%0A%7D%0AORDER%20BY%20%3FobjectLabel%20%3Fyear%0ALIMIT%203",
    "schema": {
        "fields": [
            {
                "name": "item",
                "type": "string",
                "title": {
                    "en": "item"
                }
            },
            {
                "name": "itemLabel",
                "type": "string",
                "title": {
                    "en": "itemLabel"
                }
            },
            {
                "name": "year",
                "type": "number",
                "title": {
                    "en": "year"
                }
            },
            {
                "name": "population",
                "type": "number",
                "title": {
                    "en": "population"
                }
            }
        ]
    },
    "data": [
        [
            "http://www.wikidata.org/entity/Q33",
            "Finland",
            1960,
            4429634
        ],
        [
            "http://www.wikidata.org/entity/Q33",
            "Finland",
            1961,
            4461005
        ],
        [
            "http://www.wikidata.org/entity/Q33",
            "Finland",
            1962,
            4491443
        ]
    ]
}

Editing Wikimedia Commons

[edit]

Beta Commons can be used for testing.

Mediawiki API-documentation

Query parameters

[edit]
Example url
https://tabular-data-from-sparql.netlify.app/?query=%7B%7B%7Bquery%7D%7D%7D&wikipage=Data:Tabular_data_from_SPARQL&endpoint=%2F%2Fquery.wikidata.org%2Fsparql%3Fquery%3Dlicence=CC0

Parameters

[edit]
query
SPARQL query
wikipage
Target wikipage
endpoint
allowed endpoints ("//query.wikidata.org/sparql/", "//commons-query.wikimedia.org/sparql", "//sophox.org/sparql/")
licence
allowed licences [1]
  • CC0
  • CC-BY*: (CC-BY, CC-BY-1.0, CC-BY-2.0 ...)
  • CC-BY-SA* (CC-BY-SA, CC-BY-SA-1.0, CC-BY-SA-2.0 ...)
  • ODbL-1.0

All parameters are mandatory. If they are empty disable saving of page and show the link to the help page (Github readme )