Given two small wikibase-cli scripts:
category-members.js
:
module.exports = (positionid) => {
return `
SELECT ?item (wd:${positionid} AS ?position) ?categoryPage
WHERE {
?categoryPage schema:isPartOf <https://en.wikipedia.org/>;
schema:about [ ^wdt:P910 wd:${positionid} ];
schema:name ?categoryName .
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:api "Generator";
wikibase:endpoint "en.wikipedia.org";
mwapi:generator "categorymembers";
mwapi:gcmtitle ?categoryName;
mwapi:gcmnamespace 0;
mwapi:gcmprop "id";
mwapi:gcmlimit "max".
?item wikibase:apiOutputItem mwapi:item.
}
?item wdt:P31 wd:Q5 .
FILTER NOT EXISTS { ?item p:P39/ps:P39 wd:${positionid} }
}
`
}
and add-p39s.js
:
module.exports = (id, target, source) => ({
id,
claims: {
P39: {
value: target,
references: {
P143: 'Q328',
P4656: source
},
}
}
})
Then for any given Wikidata position item (e.g. Q3695560 (Governor of Khyber Pakhtunkhwa)), we can look for anyone in the associated Category on English Wikipedia who doesn't yet have the relevant P39 ("position held") claim in Wikidata, and add those that are missing:
wd sparql category-members.js Q3695560 | jq -r '.[] | [.item, .position, .categoryPage] | join(" ")' | \
wd ee add-P39.js --batch --summary "add P39s based on category"
(These ones added as https://editgroups.toolforge.org/b/wikibase-cli/be0f4725efc03/)