Fixes RDF export of subobjects

Partial integration of the patch https://gerrit.wikimedia.org/r/#/c/70047/4
from Dbeeson
I agree its a lot of code. And I don't understand it myself yet. Lets wait to hear from the original developer, otherwise I will go through it and try to improve it + write tests.