import pywikibot
import re
from pywikibot import pagegenerators
site = pywikibot.Site('commons', fam='commons')
parentcat = 'Tag des Denkmals 2016'
states = {'im Burgenland', 'in Kärnten', 'in Niederösterreich', 'in Oberösterreich', 'in Salzburg', 'in der Steiermark', 'in Tirol', 'in Vorarlberg', 'in Wien'}
cat = pywikibot.Category(site,'Category:Gravestones in Vienna') # source category
                                                                                                                                            
pages = pagegenerators.CategorizedPageGenerator(cat,0)
#pages =[pywikibot.Page(site, 'File:SchulmeisterOtto.jpg')]
count = 0

for page in pages:
    if page.isRedirectPage():
        print (page, 'is redirect')
        continue;
    if not page.isImage():
        print (page, 'isn\'t image')
        continue;
    oldpage = page.get()
    newpage = oldpage
    #|Description    ={{de|1=Herbert Prikopa-Wiener Zentralfriedhof (40-195)}}
    #m = re.search(r"Description\s*=\{\{de\|1=(.*)-(Wiener Zentralfriedhof|Friedhof Leopoldau|Ottakringer Friedhof|Döblinger Friedhof|Sieveringer Friedhof|Grinzinger Friedhof|Heiligenstädter Friedhof|Gersthofer Friedhof|Feuerhalle Simmering|Hernalser Friedhof|Dornbacher Friedhof|Neustifter Friedhof)\s*\(*", oldpage)
    m = regex.search(oldpage)
    if m:
        # group(0) is the complete RE 
        person=m.group(1)
        if m.group(2) == "Wiener Zentralfriedhof":       cemetery = "Zentralfriedhof, Vienna"
        elif m.group(2) == "Friedhof in Wien-Essling":   cemetery = "Esslinger Friedhof"
        elif m.group(2) == "Atzgersdorfer Friedhof":     cemetery = "Friedhof Atzgersdorf"
        elif m.group(2) == "Friedhof Leopoldau":         cemetery = "Leopoldauer Friedhof"
        elif m.group(2) == "Ober St. Veiter Friedhof":   cemetery = "Ober Sankt Veiter Friedhof"
        else:                                            cemetery= m.group(2)
        print (page, 'person=', person, 'cemetery=', cemetery)
        
        cats = page.categories()
        #for cat in cats: print (cat)
        
        #check for already sufficient subcats
        found = False
        for c in validcats:
            if c in cats:
                print ('found ' + c.title())
                found = True
                break
        if found: continue

        cemetery_cat = pywikibot.Page(site, 'Category:'+ cemetery)
        #print (cemetery_cat)
        if not cemetery_cat in cats:
            newpage = newpage + "\n" + "[[Category:" + cemetery + "]]"
            print (">> appending Category:", cemetery)
        #if not person in cats:
        #    person_cat = pywikibot.Page(site, 'Category:' + person)
        #    if person_cat.exists():
        #        newpage = newpage + "\n" + "[[Category:" + person + "]]"
        #        print (">> appending Category:", person)
    if (oldpage != newpage):
        print (page, 'changed')
        pywikibot.showDiff(oldpage, newpage)
        count = count+1
        page.put(newpage, "adding categories for cemetery")
    else:
        print (page, 'unchanged')
    #if (count >= 10): break