query-05a01ac2ee12171a34a374b25809cd89

rq turtle/ttl

Using mwapi to base a query on articles in a Wikipedia category

Use the MediaWiki API to get the articles from a specific category in a specific Wikipedia language version.

Then use this results as usual in a query, in this example a simple query for the gender.

By User:Ainali with the help of User:Dipsacus fullonum 2021-02

SELECT ?item ?itemLabel ?genderLabel WHERE { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "fr.wikipedia.org"; # Set the project here wikibase:api "Generator"; mwapi:generator "categorymembers"; # Selects the content from a category mwapi:gcmtitle "Catégorie:Sociologue français";. # Specifies the category (Born in 2001) ?item wikibase:apiOutputItem mwapi:item. } FILTER BOUND (?item) # Safeguard to not get a timeout from unbound items when using ?item below ?item wdt:P21 ?gender . # Example retrieval of a value SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Use at

PREFIX mwapi: <http://wikiba.se/ontology#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX bd: <http://www.bigdata.com/rdf#>
#Using mwapi to base a query on articles in a Wikipedia category
# Use the MediaWiki API to get the articles from a specific category in a specific Wikipedia language version.
# Then use this results as usual in a query, in this example a simple query for the gender.
# By User:Ainali with the help of User:Dipsacus fullonum  2021-02
SELECT ?item ?itemLabel ?genderLabel WHERE {
  SERVICE wikibase:mwapi {
     bd:serviceParam wikibase:endpoint "fr.wikipedia.org";     # Set the project here
                     wikibase:api "Generator";
                     mwapi:generator "categorymembers";        # Selects the content from a category
                     mwapi:gcmtitle "Catégorie:Sociologue français";.    # Specifies the category (Born in 2001)
     ?item wikibase:apiOutputItem mwapi:item.
  } 
  FILTER BOUND (?item)                                         # Safeguard to not get a timeout from unbound items when using ?item below
  ?item wdt:P21 ?gender .                                      # Example retrieval of a value
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }    
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?gender") v1("?item"):::projected c12(["mwapi:item"]):::iri c8(["categorymembers"]):::literal c10(["Catégorie:Sociologue français"]):::literal c4(["fr.wikipedia.org"]):::literal c2(["bd:serviceParam"]):::iri c16(["#91;AUTO_LANGUAGE#93;,en"]):::literal c6(["Generator"]):::literal f0[["bound(?item)"]] f0 --> v1 subgraph s1["http://wikiba.se/ontology#mwapi"] style s1 stroke-width:4px; c2 --"mwapi:endpoint"--> c4 c2 --"mwapi:api"--> c6 c2 --"mwapi:generator"--> c8 c2 --"mwapi:gcmtitle"--> c10 v1 --"mwapi:apiOutputItem"--> c12 end v1 --"wdt:P21"--> v2 subgraph s2["http://wikiba.se/ontology#label"] style s2 stroke-width:4px; c2 --"mwapi:language"--> c16 end