Topic on Talk:Wikidata Query Service/User Manual/MWAPI

How I can read content of revision (wikitext) using MWAPI?

3
Summary by Zache

Solved and correct answer was updated to starting message

Zache (talkcontribs)

Hi, I tried to fetch revision like this, but i could not figure out how to access to actual content which should be under the key "*". Do you know how i should do that?

SOLVED: Example is now fixed based on answer below

SELECT * WHERE {
  BIND(wd:Q42 AS ?item)
  ?item wdt:P18 ?image.
  BIND(STRAFTER(wikibase:decodeUri(STR(?image)), "http://commons.wikimedia.org/wiki/Special:FilePath/") AS ?fileTitle)

  SERVICE wikibase:mwapi {
    bd:serviceParam wikibase:endpoint "commons.wikimedia.org";
                    wikibase:api "Generator";
                    wikibase:limit "once";
                    mwapi:generator "allpages";
                    mwapi:gapfrom ?fileTitle;
                    mwapi:gapnamespace 6; # NS_FILE
                    mwapi:gaplimit 1;
                    mwapi:prop "revisions";
                    mwapi:rvprop "content".
    ?contentmodel wikibase:apiOutput 'revisions/rev/@contentmodel'.
    ?contentformat wikibase:apiOutput 'revisions/rev/@contentformat'.
    ?content wikibase:apiOutput 'revisions/rev/text()' .
  }
}

Try it!

Dipsacus fullonum (talkcontribs)

There is no key "*". MWAPI request output in XML format from the API and uses the XPath query language to find the wanted elements in the XML output. The XML has the context as the text in a "rev" element that haves "revisions" as parent element, so you have to add the triple


  ?content wikibase:apiOutput 'revisions/rev/text()' .


to the "SERVICE wikibase:mwapi" call in your SPARQL query.

Zache (talkcontribs)

It worked! Thank you very much.