Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Python:I6.py

From Botwiki
Jump to: navigation, search
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys, re, traceback
sys.path.append(os.environ['HOME']+'/pywikipedia')
import wikipedia, catlib, pagegenerators
 
 
def bufline(ch=u'*', clr=10, L=40):
    wikipedia.output(ch*L,colors=[clr for i in range(L)])
 
 
def colorize(text):
    colors = [None for i in range(len(text))]
 
    RX = re.compile(r'{{[^}]*}}',re.DOTALL)
    for match in RX.finditer(text):
        for i in range(match.start(),match.end()):
            colors[i] = 14
 
    wikipedia.output(text, colors=colors)
 
 
def halt_brackets(txt, lvl, open, close):
    txt = list(txt)
    ret = u''
    while True:
        ch = txt.pop(0)
        ret += ch
        if ch==open:
            lvl += 1
        elif ch==close:
            lvl -= 1
        if len(txt)==0 or lvl==0:
            return ret, ''.join(txt)
 
 
def delink(pg, image):
    wikipedia.output(u'Delinking from [[%s]]...' % (pg.title()))
    old_txt = pg.get()
    new_txt = u''
 
    R = r'^(?P<pre>.*?)(?P<img>(?:Image:)?%s)(?P<post>.*)$' % re.sub('[ _]','[ _]',image.titleWithoutNamespace().replace('.',r'\.'))
    #print R
    rx = re.compile(R,re.IGNORECASE)
 
    for L in old_txt.split('\n'):
        M = rx.search(L)
        newL = ''
        if M:
            #print M.groups()
            pre = M.group('pre')
            img = M.group('img')
            post = M.group('post')
            if pre == '':
                wikipedia.output(u'Image inside a <gallery>')
                newL = u'<!--Removed deleted image: ' + img + post + u'-->'
            elif re.search(r'{{ *(?:audio|listen) *\| *(?: *filename *= *)?$',pre):
                wikipedia.output(u'Media file inside {{audio}} or {{listen}}')
                rest1, rest2 = halt_brackets(post,2,'{','}')
                newL = re.sub(r'{{ *((?:audio|listen) *(?:\| *filename *= *|\|))$',r'<!--Removed deleted file: {{\1',pre) + img + rest1 + '-->' + rest2
            elif re.search(r'\[\[ *$',pre):
                wikipedia.output(u'Regular image inside brackets')
                rest1, rest2 = halt_brackets(post,2,'[',']')
                newL = re.sub(r'\[\[ *$','',pre) + u'<!--Removed deleted image: [[' + img + rest1 + '-->' + rest2
            elif re.search('= *$',pre):
                wikipedia.output(u'Image inside infobox, no brackets')
                newL = pre + u'<!--Removed deleted image: ' + img + u'-->' + post
            elif re.search(r'\[\[[^\]\[]*$',pre):
                pass #We caught a suffix, eg. [[Image:spam eggs.jpg]] while searching for [[Image:eggs.jpg]]
            else:
                return False #It's there, but we can't remove it
        if newL:
            new_txt += newL + '\n'
        else:
            new_txt += L + '\n'
    if old_txt != new_txt:
        wikipedia.showDiff(old_txt,new_txt)
        pg.put(
                new_txt,
                comment = u'Removed image (%s) deleted per [[WP:CSD#I6]]' % image.titleWithoutNamespace(),
                minorEdit = True,
                )
        return True
    else:
        return False
 
 
def delink_and_delete(img):
    wikipedia.output(u'Delinking image...')
 
    for pg in img.usingPages():
        if pg.namespace()==0:
            rm = delink(pg,img)
            if not rm:
                wikipedia.output(u'Failed to delink. Aborting.')
                return
 
    reason = u'Image missing fair use rationale ([[WP:CSD#I6]])'
    if img.usingPages():
        reason += u' (removed from: %s)' % u', '.join([u'[[%s]]' % pg.title() for pg in img.usingPages()])
    wikipedia.output(u'Deleting image...')
    img.delete(
            reason = reason,
            prompt = False,
            )
    talk = img.toggleTalkPage()
    if talk.exists():
        wikipedia.output(u'Deleting talk page...')
        talk.delete(
                reason = u'Talk page of a deleted image [[WP:CSD#G8]]',
                prompt = False,
                )
 
 
def main(day, startAt):
    Site = wikipedia.getSite()
    cat = catlib.Category(Site,'Category:Images with no fair use rationale as of ' + day)
 
    for pg in cat.articles():
        if startAt and pg.title() < 'Image:'+startAt:
            continue
        try:
            img = wikipedia.ImagePage(Site,pg.title())
            bufline(ch=u'=', clr=11, L=70)
            wikipedia.output(u'[[%s]]\n\tImage page description follows (%d bytes):' % (img.title(),len(img.get())))
            bufline(ch=u'=', clr=12, L=70)
            colorize(img.get())
            bufline(ch=u'=', clr=12, L=70)
            wikipedia.output(u'Used in: ' + ', '.join(['[[%s]]' % p.title() for p in img.usingPages()]))
            bufline(ch=u'=', clr=12, L=70)
 
            ch = wikipedia.inputChoice('Delete image?',['y','n'],['y','n'])
 
            if ch=='y':
                delink_and_delete(img)
 
        except wikipedia.Error:
            traceback.print_exc()
 
 
if __name__ == '__main__':
    try:
        day = sys.argv[1]
        try:
            startAt = sys.argv[2]
        except:
            startAt = None
        main(day, startAt)
    finally:
        wikipedia.stopme()
Personal tools
Share