#!/usr/bin/env python3
# coding: utf8
import pywikibot
import math
import time
from pywikibot import pagegenerators

wikidata_site = pywikibot.Site('wikidata', 'wikidata')
repo = wikidata_site.data_repository()

fromRefProp = 'P143'
fromRefValue = 'Q145'
fromRefHash = '2c21824512f5fa181a016ae2cd0ac7aef14c6048'
editSummary= 'remove nonsense reference [[Property:{}]]: [[{}]]'
reportIncrement = 10
limit = None
timeout = None

dataset_query = """SELECT DISTINCT ?item WHERE {{ ?item ?any [ prov:wasDerivedFrom wdref:{} ] }}""".format(fromRefHash)
inputdata = [  ]
for itemkey in pagegenerators.WikidataSPARQLPageGenerator(dataset_query, site=wikidata_site):
    inputdata.append(itemkey.title())
print('Found {} items to process'.format(len(inputdata)))
print()

def deleteRefs(Qitem): # adapted from https://github.com/Pascalco/DeltaBot/blob/master/fixClaims/fixClaims.py#L223 on 2017-03-06
    commands = {} # define a command structure to be filled; this is the "data" parameter of the API at https://www.mediawiki.org/wiki/Wikibase/API#wbeditentity
    commands['claims'] = [] # we only work on claims in this script; this list takes all claim commands

    if not Qitem.claims:
        return 0

    for hostProp in Qitem.claims:
        for claim in Qitem.claims[hostProp]:
            claimHasChanged = False
            claimJSON = claim.toJSON()
            if 'references' not in claimJSON: # no references found
                continue

            for i, reference in enumerate(claimJSON['references']):
                if claimJSON['references'][i]['hash'] != fromRefHash:
                    continue
                
                claimHasChanged = True
                claimJSON['references'].pop(i)
                
            if claimHasChanged==True:
                commands['claims'].append(claimJSON)

    if len(commands['claims']) > 0: # all commands are executed here
        try:
            Qitem.editEntity(commands, summary=editSummary.format(
                fromRefProp,
                fromRefValue
            ))
            if timeout != None:
                time.sleep(timeout)
            pass
        except Exception as e:
            print(e)
            time.sleep(60) # take a 1 minute break, in order to drastically reduce load in case something goes wrong
    return int(len(commands['claims'])) # return number of moved claims, inferred from command structure size

now = time.time()
digits = math.floor(math.log(len(inputdata), 10)) + 1
for i, item in enumerate(inputdata):
    Qitem = pywikibot.ItemPage(repo, item)

    if Qitem.isRedirectPage():
        continue

    Qitem.get()

    if not Qitem.claims:
        print('{}: no claims found; skip'.format(item))

    num = deleteRefs(Qitem)
    if (len(inputdata)-(i+1))%reportIncrement==0:
        print('{timestmp} (UTC), {title:9}: remove refs from {num:d} claims; {curr:{digits}d}/{total:{digits}d} item(s) to go; processing at {rate:.1f} items/min'.format(
            timestmp = time.strftime('%Y-%m-%d %H:%M:%S'),
            title = Qitem.title(),
            num = num,
            curr = len(inputdata)-(i+1),
            total = len(inputdata),
            rate = reportIncrement / ( (time.time() - now) / 60 ),
            digits = digits
        ))
        now = time.time()

    if limit!=None and i+1 >= limit:
        print()
        print('Limit of {} items reached; stop here'.format(limit))
        break

print('All done, job finished')
Found 918 items to process

2019-05-01 18:08:58 (UTC), Q5145498 : remove refs from 1 claims; 910/918 item(s) to go; processing at 75.7 items/min
2019-05-01 18:09:08 (UTC), Q5238011 : remove refs from 1 claims; 900/918 item(s) to go; processing at 60.2 items/min
2019-05-01 18:09:18 (UTC), Q4706513 : remove refs from 1 claims; 890/918 item(s) to go; processing at 58.3 items/min
2019-05-01 18:09:28 (UTC), Q4758271 : remove refs from 1 claims; 880/918 item(s) to go; processing at 57.6 items/min
2019-05-01 18:09:39 (UTC), Q7518329 : remove refs from 1 claims; 870/918 item(s) to go; processing at 59.1 items/min
2019-05-01 18:09:49 (UTC), Q7613309 : remove refs from 1 claims; 860/918 item(s) to go; processing at 57.0 items/min
2019-05-01 18:09:59 (UTC), Q4765755 : remove refs from 1 claims; 850/918 item(s) to go; processing at 59.4 items/min
2019-05-01 18:10:10 (UTC), Q5438937 : remove refs from 1 claims; 840/918 item(s) to go; processing at 56.8 items/min
2019-05-01 18:10:20 (UTC), Q4707592 : remove refs from 1 claims; 830/918 item(s) to go; processing at 60.4 items/min
2019-05-01 18:10:30 (UTC), Q4909896 : remove refs from 1 claims; 820/918 item(s) to go; processing at 58.1 items/min
2019-05-01 18:10:40 (UTC), Q4977699 : remove refs from 1 claims; 810/918 item(s) to go; processing at 60.1 items/min
2019-05-01 18:10:50 (UTC), Q15491123: remove refs from 1 claims; 800/918 item(s) to go; processing at 58.6 items/min
2019-05-01 18:11:00 (UTC), Q8021682 : remove refs from 1 claims; 790/918 item(s) to go; processing at 58.8 items/min
2019-05-01 18:11:10 (UTC), Q4685333 : remove refs from 1 claims; 780/918 item(s) to go; processing at 59.9 items/min
2019-05-01 18:11:20 (UTC), Q6239521 : remove refs from 1 claims; 770/918 item(s) to go; processing at 59.9 items/min
2019-05-01 18:11:30 (UTC), Q4773189 : remove refs from 1 claims; 760/918 item(s) to go; processing at 60.1 items/min
2019-05-01 18:11:40 (UTC), Q7609923 : remove refs from 1 claims; 750/918 item(s) to go; processing at 60.0 items/min
2019-05-01 18:11:51 (UTC), Q5238022 : remove refs from 1 claims; 740/918 item(s) to go; processing at 59.5 items/min
2019-05-01 18:12:01 (UTC), Q5540957 : remove refs from 1 claims; 730/918 item(s) to go; processing at 59.5 items/min
2019-05-01 18:12:11 (UTC), Q5728170 : remove refs from 1 claims; 720/918 item(s) to go; processing at 60.0 items/min
2019-05-01 18:12:21 (UTC), Q7089821 : remove refs from 1 claims; 710/918 item(s) to go; processing at 59.7 items/min
2019-05-01 18:12:31 (UTC), Q5548240 : remove refs from 1 claims; 700/918 item(s) to go; processing at 58.2 items/min
2019-05-01 18:12:41 (UTC), Q6260144 : remove refs from 1 claims; 690/918 item(s) to go; processing at 59.1 items/min
2019-05-01 18:12:51 (UTC), Q6845987 : remove refs from 1 claims; 680/918 item(s) to go; processing at 59.9 items/min
2019-05-01 18:13:01 (UTC), Q7146159 : remove refs from 1 claims; 670/918 item(s) to go; processing at 59.5 items/min
2019-05-01 18:13:12 (UTC), Q4756727 : remove refs from 1 claims; 660/918 item(s) to go; processing at 55.0 items/min
2019-05-01 18:13:22 (UTC), Q6152967 : remove refs from 1 claims; 650/918 item(s) to go; processing at 58.4 items/min
2019-05-01 18:13:32 (UTC), Q16729981: remove refs from 1 claims; 640/918 item(s) to go; processing at 60.8 items/min
2019-05-01 18:13:42 (UTC), Q7803038 : remove refs from 1 claims; 630/918 item(s) to go; processing at 59.3 items/min
2019-05-01 18:13:52 (UTC), Q7027843 : remove refs from 1 claims; 620/918 item(s) to go; processing at 60.1 items/min
2019-05-01 18:14:03 (UTC), Q6260998 : remove refs from 1 claims; 610/918 item(s) to go; processing at 59.3 items/min
2019-05-01 18:14:13 (UTC), Q16734171: remove refs from 1 claims; 600/918 item(s) to go; processing at 57.9 items/min
2019-05-01 18:14:23 (UTC), Q5263145 : remove refs from 1 claims; 590/918 item(s) to go; processing at 58.9 items/min
2019-05-01 18:14:33 (UTC), Q16950038: remove refs from 1 claims; 580/918 item(s) to go; processing at 61.1 items/min
2019-05-01 18:14:43 (UTC), Q16206402: remove refs from 1 claims; 570/918 item(s) to go; processing at 59.7 items/min
2019-05-01 18:14:53 (UTC), Q4858880 : remove refs from 1 claims; 560/918 item(s) to go; processing at 57.0 items/min
2019-05-01 18:15:04 (UTC), Q5362951 : remove refs from 1 claims; 550/918 item(s) to go; processing at 59.8 items/min
2019-05-01 18:15:14 (UTC), Q6660062 : remove refs from 1 claims; 540/918 item(s) to go; processing at 59.2 items/min
2019-05-01 18:15:24 (UTC), Q6513302 : remove refs from 1 claims; 530/918 item(s) to go; processing at 59.3 items/min
2019-05-01 18:15:34 (UTC), Q6848442 : remove refs from 1 claims; 520/918 item(s) to go; processing at 59.9 items/min
2019-05-01 18:15:44 (UTC), Q6274724 : remove refs from 1 claims; 510/918 item(s) to go; processing at 59.1 items/min
2019-05-01 18:15:54 (UTC), Q4912170 : remove refs from 1 claims; 500/918 item(s) to go; processing at 59.8 items/min
2019-05-01 18:16:04 (UTC), Q4737516 : remove refs from 1 claims; 490/918 item(s) to go; processing at 60.0 items/min
2019-05-01 18:16:14 (UTC), Q5980358 : remove refs from 1 claims; 480/918 item(s) to go; processing at 59.2 items/min
2019-05-01 18:16:24 (UTC), Q5239962 : remove refs from 1 claims; 470/918 item(s) to go; processing at 59.9 items/min
2019-05-01 18:16:34 (UTC), Q5232613 : remove refs from 1 claims; 460/918 item(s) to go; processing at 60.1 items/min
2019-05-01 18:16:44 (UTC), Q6181692 : remove refs from 1 claims; 450/918 item(s) to go; processing at 59.2 items/min
2019-05-01 18:16:55 (UTC), Q6210468 : remove refs from 1 claims; 440/918 item(s) to go; processing at 58.5 items/min
2019-05-01 18:17:05 (UTC), Q7364113 : remove refs from 1 claims; 430/918 item(s) to go; processing at 60.0 items/min
2019-05-01 18:17:15 (UTC), Q6181658 : remove refs from 1 claims; 420/918 item(s) to go; processing at 59.2 items/min
2019-05-01 18:17:25 (UTC), Q17020978: remove refs from 1 claims; 410/918 item(s) to go; processing at 59.5 items/min
2019-05-01 18:17:35 (UTC), Q14946828: remove refs from 1 claims; 400/918 item(s) to go; processing at 56.1 items/min
2019-05-01 18:17:47 (UTC), Q7117320 : remove refs from 1 claims; 390/918 item(s) to go; processing at 52.9 items/min
2019-05-01 18:17:57 (UTC), Q5119176 : remove refs from 1 claims; 380/918 item(s) to go; processing at 58.9 items/min
2019-05-01 18:18:07 (UTC), Q7626677 : remove refs from 1 claims; 370/918 item(s) to go; processing at 59.6 items/min
2019-05-01 18:18:17 (UTC), Q5372433 : remove refs from 1 claims; 360/918 item(s) to go; processing at 59.5 items/min
2019-05-01 18:18:27 (UTC), Q5982235 : remove refs from 1 claims; 350/918 item(s) to go; processing at 60.2 items/min
2019-05-01 18:18:37 (UTC), Q5112859 : remove refs from 1 claims; 340/918 item(s) to go; processing at 58.0 items/min
2019-05-01 18:18:47 (UTC), Q6110990 : remove refs from 1 claims; 330/918 item(s) to go; processing at 59.7 items/min
2019-05-01 18:18:58 (UTC), Q7609401 : remove refs from 1 claims; 320/918 item(s) to go; processing at 59.5 items/min
2019-05-01 18:19:08 (UTC), Q4767447 : remove refs from 1 claims; 310/918 item(s) to go; processing at 60.0 items/min
2019-05-01 18:19:18 (UTC), Q6792850 : remove refs from 1 claims; 300/918 item(s) to go; processing at 60.2 items/min
2019-05-01 18:19:28 (UTC), Q4717499 : remove refs from 1 claims; 290/918 item(s) to go; processing at 60.0 items/min
2019-05-01 18:19:38 (UTC), Q5179315 : remove refs from 1 claims; 280/918 item(s) to go; processing at 57.9 items/min
2019-05-01 18:19:48 (UTC), Q7518484 : remove refs from 1 claims; 270/918 item(s) to go; processing at 59.5 items/min
2019-05-01 18:19:58 (UTC), Q16226133: remove refs from 1 claims; 260/918 item(s) to go; processing at 58.8 items/min
2019-05-01 18:20:08 (UTC), Q4773345 : remove refs from 1 claims; 250/918 item(s) to go; processing at 59.7 items/min
2019-05-01 18:20:18 (UTC), Q6384548 : remove refs from 1 claims; 240/918 item(s) to go; processing at 60.3 items/min
2019-05-01 18:20:28 (UTC), Q7152907 : remove refs from 1 claims; 230/918 item(s) to go; processing at 59.2 items/min
2019-05-01 18:20:38 (UTC), Q6390537 : remove refs from 1 claims; 220/918 item(s) to go; processing at 60.0 items/min
2019-05-01 18:20:48 (UTC), Q5542294 : remove refs from 1 claims; 210/918 item(s) to go; processing at 59.8 items/min
2019-05-01 18:20:58 (UTC), Q5263557 : remove refs from 1 claims; 200/918 item(s) to go; processing at 59.7 items/min
2019-05-01 18:21:08 (UTC), Q5085518 : remove refs from 1 claims; 190/918 item(s) to go; processing at 59.5 items/min
2019-05-01 18:21:18 (UTC), Q5129243 : remove refs from 1 claims; 180/918 item(s) to go; processing at 60.1 items/min
2019-05-01 18:21:29 (UTC), Q5563799 : remove refs from 1 claims; 170/918 item(s) to go; processing at 58.4 items/min
2019-05-01 18:21:39 (UTC), Q5240778 : remove refs from 1 claims; 160/918 item(s) to go; processing at 58.7 items/min
2019-05-01 18:21:49 (UTC), Q6205936 : remove refs from 1 claims; 150/918 item(s) to go; processing at 60.1 items/min
2019-05-01 18:21:59 (UTC), Q5249946 : remove refs from 1 claims; 140/918 item(s) to go; processing at 60.2 items/min
2019-05-01 18:22:09 (UTC), Q4766657 : remove refs from 1 claims; 130/918 item(s) to go; processing at 57.0 items/min
2019-05-01 18:22:20 (UTC), Q6174181 : remove refs from 1 claims; 120/918 item(s) to go; processing at 59.4 items/min
2019-05-01 18:22:30 (UTC), Q6969758 : remove refs from 1 claims; 110/918 item(s) to go; processing at 60.3 items/min
2019-05-01 18:22:40 (UTC), Q7151123 : remove refs from 1 claims; 100/918 item(s) to go; processing at 58.8 items/min
2019-05-01 18:22:50 (UTC), Q7827598 : remove refs from 1 claims;  90/918 item(s) to go; processing at 60.7 items/min
2019-05-01 18:23:00 (UTC), Q6759862 : remove refs from 1 claims;  80/918 item(s) to go; processing at 59.0 items/min
2019-05-01 18:23:10 (UTC), Q5232574 : remove refs from 1 claims;  70/918 item(s) to go; processing at 59.9 items/min
2019-05-01 18:23:20 (UTC), Q4708046 : remove refs from 1 claims;  60/918 item(s) to go; processing at 60.4 items/min
2019-05-01 18:23:30 (UTC), Q7149357 : remove refs from 1 claims;  50/918 item(s) to go; processing at 60.5 items/min
2019-05-01 18:23:40 (UTC), Q7615067 : remove refs from 1 claims;  40/918 item(s) to go; processing at 59.5 items/min
2019-05-01 18:23:50 (UTC), Q8462137 : remove refs from 1 claims;  30/918 item(s) to go; processing at 57.1 items/min
2019-05-01 18:24:00 (UTC), Q16199476: remove refs from 1 claims;  20/918 item(s) to go; processing at 60.8 items/min
2019-05-01 18:24:10 (UTC), Q4761011 : remove refs from 1 claims;  10/918 item(s) to go; processing at 59.3 items/min
2019-05-01 18:24:20 (UTC), Q7352223 : remove refs from 1 claims;   0/918 item(s) to go; processing at 58.6 items/min
All done, job finished