import pywikibot from pywikibot import pagegenerators from pywikibot.page import Page import re import time import threading from pywikibot.exceptions import ArticleExistsConflictError

failedMoves = ""

def isValid(page: Page) -> bool:

   if page.title() == "Bajszpilowy wyjůntek" or page.title() == "Bajszpilowy wyjůntek2" or page.title() == "Bajszpilowy wyjůntek3":
       return False
   cont = page.text
   codes = [
       r'\) - wieś',
       r'\) - wjeś',
       r'\) - miasto',
       r'\) - mjasto',
       r'\) - dźelńica',
       r'\) - dzielnica',
       r" - wieś",
       r" - wjeś",
       r" - miasto",
       r" - mjasto",
       r" - dźelńica",
       r" - dzielnica",
       r'\) - \[\[wieś',
       r'\) - \[\[wjeś',
       r'\) - \[\[miasto',
       r'\) - \[\[mjasto',
       r'\) - \[\[dźelńica',
       r'\) - \[\[dzielnica',
       r" - \[\[wieś",
       r" - \[\[wjeś",
       r" - \[\[miasto",
       r" - \[\[mjasto",
       r" - \[\[dźelńica",
       r" - \[\[dzielnica",
       r'\) – wieś',
       r'\) – wjeś',
       r'\) – miasto',
       r'\) – mjasto',
       r'\) – dźelńica',
       r'\) – dzielnica',
       r" – wieś",
       r" – wjeś",
       r" – miasto",
       r" – mjasto",
       r" – dźelńica",
       r" – dzielnica",
       r'\) – \[\[wieś',
       r'\) – \[\[wjeś',
       r'\) – \[\[miasto',
       r'\) – \[\[mjasto',
       r'\) – \[\[dźelńica',
       r'\) – \[\[dzielnica',
       r" – \[\[wieś",
       r" – \[\[wjeś",
       r" – \[\[miasto",
       r" – \[\[mjasto",
       r" – \[\[dźelńica",
       r" – \[\[dzielnica"
   ]
   pattern = re.compile('|'.join(codes))
   return bool(re.search(pattern, cont))

def remove_parentheses(string):

   # Use regular expression to match parentheses and their content
   pattern = r"\([^()]*\)"
   # Find all occurrences of the pattern in the string
   matches = re.findall(pattern, string)
   # Remove the matched parentheses and their content from the string
   for match in matches:
       string = string.replace(match, "")
   return string.rstrip()

def remove_except_parentheses(string):

   pattern = r'\([^()]+\)'  # Match parentheses and their content
   
   match = re.search(pattern, string)  # Find the first occurrence
   
   if match:
       return match.group()[1:-1].rstrip()  # Remove the parentheses
   else:
       return None  # No parentheses found

def split_replace_concatenate(string, split_substring, replace_substring):

   parts = string.split(split_substring, 1)
   if len(parts) > 1:
       parts[0] = parts[0].replace(replace_substring, "")
   return split_substring.join(parts)


def fixContent(page: Page) -> None:

   oldName = page.title()
   print("Old name: "+oldName)
   oldOrgName = remove_parentheses(oldName)
   plName = page.data_item().getSitelink("plwiki")
   plOrgName = remove_parentheses(plName)
   if(oldOrgName == plOrgName):
       print("    Pomijom bo brak zmian\n")
       return
   klamraSzl = remove_except_parentheses(oldName)
   print("Old city name: "+oldOrgName)
   print("New city name: "+plOrgName)
   content = page.text
   content = content.replace(oldOrgName, plOrgName)
   content = content.replace("Kategoryjo:"+plOrgName, "Kategoryjo:"+oldOrgName)
   content = content.replace("Kategoria:"+plOrgName, "Kategoria:"+oldOrgName)
   content = content.replace("Category:"+plOrgName, "Category:"+oldOrgName)
   content = content.replace("Kategoryjo:Gmin "+plOrgName, "Kategoryjo:Gmin "+oldOrgName)
   content = content.replace("Kategoria:Gmin "+plOrgName, "Kategoria:Gmin "+oldOrgName)
   content = content.replace("Category:Gmin "+plOrgName, "Category:Gmin "+oldOrgName)
   content = content.replace("Kategoryjo:Krys "+plOrgName, "Kategoryjo:Krys "+oldOrgName)
   content = content.replace("Kategoria:Krys "+plOrgName, "Kategoria:Krys "+oldOrgName)
   content = content.replace("Category:Krys "+plOrgName, "Category:Krys "+oldOrgName)
   page.text = content
   # page.save("Půmjana mjana we inhalće artikla na polske podug kůnsynsusu",minor=False)
   
   pageName = plOrgName
   if klamraSzl is not None:
       pageName = plOrgName + " (" + klamraSzl + ")"
   print("New article name: "+plOrgName+"\n")
   try:
       pass
       # page.move(pageName, reason="Půmjana mjana na polske podug kůnsynsusu", movetalk=True, noredirect=False)
   except ArticleExistsConflictError:
       failedMoves = failedMoves + "\n" + pageName


if __name__ == '__main__':

   site = pywikibot.Site()
   cat = pywikibot.Category(site,'Kujawsko-půmorske wojewůdztwo‎‎‎')
   gen = pagegenerators.CategorizedPageGenerator(cat, recurse=True, total=10)
   for page in gen:
       # Do something with the page
       if isValid(page):
           fixContent(page)
           time.sleep(0.5)
       else:
           print("    Pomijom bo invalid: "+page.title()+"\n")
   print("Ńyudane przećepki: "+failedMoves)