#!/usr/bin/python
# -*- coding: UTF-8 -*-
# licensed under CC-Zero: https://creativecommons.org/publicdomain/zero/1.0
# adapted from https://github.com/Pascalco/DeltaBot/blob/1dece3dc152323ee6e05e91d511c69819e86426f/rfd.py

import pywikibot
import re

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

page = pywikibot.Page(site, 'Wikidata:Requests for deletions')

cntDone = 0
cntNotDone = 0
force = False

content = re.findall(r'(?:(?<!=)==([^=]+)==(?!=))?([\s\S]+?(?=$|(?<!=)==[^=]+==(?!=)))', page.get()) # list of tuples of second order sections on the RfD page

for i in range(len(content)):
    #content[i] = map(unicode.strip, list(content[i]))
    content[i] = list(content[i])
    
    content[i][0] = content[i][0].strip()
    res = re.search(r'(Q\d+)', content[i][0])
    if res:
        entity = pywikibot.ItemPage(repo, res.group(1))
    else:
        res = re.search(r'(Lexeme:L\d+)', content[i][0])
        if res:
            entity = pywikibot.Page(repo, res.group(1))  # T189321
    if not res:
        continue
    
    content[i][1] = content[i][1].strip()
    
    if any(x in content[i][1] for x in ('{{done', '{{deleted', '{{not done', '{{notdone', '{{not deleted', '{{merged')):
        continue
        
    if entity.isRedirectPage() and entity.getRedirectTarget().exists():
        content[i][1] += ('\n:{{{{done}}}} Redirect created by [[User:{}|]], you can do it [[Special:MyLanguage/Help:Merge|yourself]] next time. —~~~~').format(entity.userName())
        cntDone += 1
    elif not entity.exists():
        for m in site.logevents(logtype='delete', page=entity, total=1):
            content[i][1] += '\n:{{{{deleted|admin={}}}}} —~~~~'.format(m.user())
        cntDone += 1
    else:
        if '{{on hold' not in content[i][1]:
            refs = list(entity.backlinks(follow_redirects=False, filter_redirects=False, namespaces=[0, 120, 146], total=12))
            numberOfRefs = len(refs)
            if entity in refs:
                numberOfRefs -= 1
            if numberOfRefs > 0:
                force = True
                content[i][1] += '\n:{{{{on hold}}}} This item is linked from {}{} other{}. —~~~~'.format(min(numberOfRefs, 10), '+' if numberOfRefs > 10 else '', 's' if numberOfRefs > 1 else '')
        cntNotDone += 1

text = ''
for section in content:
    if section[0].strip() != '':
        text += '== {} ==\n\n'.format(section[0].strip())
    text += section[1].strip()+'\n\n'

if cntDone > 0 or force:
    editSummary = 'marking {} requests as done ({} unactioned requests)'.format(cntDone, cntNotDone)
    page.put(text, summary=editSummary, minor=False)
    print(editSummary)
    
#statspage = pywikibot.Page(site, 'User:BeneBot*/RfD-stats')
#statspage.put(cntNotDone, summary='Updating stats: {}'.format(cntNotDone), minor=False)
Page [[wikidata:Wikidata:Requests for deletions]] saved
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-8c49c721cc9d> in <module>()
     66 if cntDone > 0 or force:
     67     page.put(text, summary='marking {} requests as done ({} unactioned requests)'.format(cntDone, cntNotDone), minor=False)
---> 68     print(comment)
     69 
     70 #statspage = pywikibot.Page(site, 'User:BeneBot*/RfD-stats')

NameError: name 'comment' is not defined