Skip to content

Instantly share code, notes, and snippets.

@tmtmtmtm
Last active October 9, 2020 13:50
Show Gist options
  • Save tmtmtmtm/ea81cbae866c4883930985eb469a766d to your computer and use it in GitHub Desktop.
Save tmtmtmtm/ea81cbae866c4883930985eb469a766d to your computer and use it in GitHub Desktop.
Add missing P39s based on Wikipedia Categories

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/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment