Bikarhêner:Balyozxane/skrîpt/py/addcontrolauth.py

Ji Wîkîpediya, ensîklopediya azad.
#!/usr/bin/python3
import pywikibot
import re
from pywikibot import pagegenerators
from pywikibot.bot import AutomaticTWSummaryBot, ConfigParserBot, SingleSiteBot
from pywikibot.exceptions import LockedPageError
from kucosmetics import CANCEL, CosmeticChangesToolkit

TESTING = True

def reorder_templates_and_remove_categories(text):

    # Find and remove Koord or Coord templates
    koord_templates = find_koord_templates(text)
    text = remove_koord_templates(text)

    # Find and remove other templates
    template_regex = r'{{\s*([^\}]+\-şitil|[Şş]iti?l|[Kk]urt|[Ss]tub|[Şş]itlek|[^\}]+\-şitil\-[^\}]+)\s*}}'
    templates = re.findall(template_regex, text)
    text = re.sub(template_regex, '', text)


    # Find and remove other templates
    template_sitil_regex = r'{{\s*([Şş]itil-[^\}]+)\s*}}'
    template_sitil = re.findall(template_sitil_regex, text)
    text = re.sub(template_sitil_regex, '', text)


    # Find and remove categories
    category_regex = r'\[\[Kategorî:[^\]]+\]\]'
    categories = re.findall(category_regex, text)
    text = re.sub(category_regex, '', text)

    # Find and remove DEFAULTSORT
    defaultsort_regex = r'{{\s*(DEFAULTSORT:[^}]+|Salê kat bike heke sal hebe)\s*}}'
    defaultsort = re.findall(defaultsort_regex, text)
    defaultsort = defaultsort[0] if defaultsort else ""  # Use an empty string if no DEFAULTSORT is found
    text = re.sub(defaultsort_regex, '', text)

    return text, koord_templates, templates, categories, defaultsort, template_sitil

def find_koord_templates(text):
    # Find Koord or Coord templates
    koord_regex = r'{{\s*([Kk]oord|[Cc]oord)\s*\|\s*([^}]+display\s*=\s*title)\s*}}'
    return [match[1] for match in re.findall(koord_regex, text)]


def remove_koord_templates(text):
    # Remove Koord or Coord templates
    koord_regex = r'{{\s*([Kk]oord|[Cc]oord)\s*\|\s*[^}]+display\s*=\s*title\s*}}'
    return re.sub(koord_regex, '', text)


def append_templates_and_categories(text, koord_templates, templates, categories, defaultsort, template_sitil):
    # Append other text, Koord or Coord templates, DEFAULTSORT, and categories in the specified order
    updated_text = text.rstrip('\n\n')
    
    if not re.search(r'\{\{\s*([kK]ontrola otorîtey?ê?|[aA]uthority control|Kontrola otorîte)\s*\}\}', text, re.IGNORECASE):
        updated_text += "\n{{Kontrola otorîteyê}}"

    if template_sitil and template_sitil != "":
        updated_text +=  "\n"+ '\n'.join('{{' + template_s + '}}' for template_s in template_sitil)

    if templates and templates != "":
        updated_text += "\n"+'\n'.join('{{' + template + '}}' for template in templates)

    if koord_templates and koord_templates != "":
        updated_text += "\n"+'\n'.join('{{Koord|' + koord_template + '}}' for koord_template in koord_templates)

    if defaultsort and defaultsort != "":
        updated_text += '\n' + '{{' + defaultsort + '}}'

    if categories and categories != "":
        updated_text += '\n\n' + '\n'.join(categories)

    # Remove empty lines at the end of the page
    updated_text = re.sub(r'\n\n\n*', '\n\n', updated_text)

    return updated_text

class AppendTextBot(
    SingleSiteBot,
    ConfigParserBot,
    AutomaticTWSummaryBot,
):
    summary_key = 'basic-changing'
    use_redirects = False

    update_options = {
        'async': False,
        'showdiff': False,
        'ignore': CANCEL.MATCH,
    }

    def do_kozmetik(self, old_text):
        kozmetik_cebu = ""
        cc_toolkit = CosmeticChangesToolkit(self.current_page,
                                            ignore=self.opt.ignore)
        new_text, summaries = cc_toolkit.change(old_text)
        applies_summaries = ', '.join(summaries.values())
        if new_text is not False and new_text != old_text:
            kozmetik_cebu = "; paqijiyên kozmetîk"
            if applies_summaries:
                kozmetik_cebu += f' ({applies_summaries}.)'

        return new_text, kozmetik_cebu

    def treat_page(self) -> None:
        page = self.current_page

        try:
            page_text = page.text
        except LockedPageError:
            pywikibot.warning('Page {} is locked?!'
                              .format(page.title(as_link=True)))
            return  # Skip locked page

        if not page.exists():
            print("Page does not exist.")
            return

        # Get the categories of the current page
        categories = [category.title() for category in page.categories()]

        # Check if the page is in the category "Rûpelên cudakirinê"
        if 'Kategorî:Rûpelên cudakirinê' in categories:
            if TESTING:
                print(f"Skipping page '{page}' as it is in the category 'Rûpelên cudakirinê'")
            return

        if re.search(r'\{\{\s*(kontrola otorîteyê|Authority control|Kontrola otorîte)\s*}}', page_text, re.IGNORECASE):
            if TESTING:
                pywikibot.output("Template already exists. Skipping page.")
            return

        # Find and remove templates, categories, etc.
        text, koord_templates, templates, categories, defaultsort, template_sitil = reorder_templates_and_remove_categories(page_text)

        # Append other text, templates, DEFAULTSORT, and categories in the specified order
        updated_text = append_templates_and_categories(text, koord_templates, templates, categories, defaultsort, template_sitil)

        cleaned_new_text, kozmetik_cebu = self.do_kozmetik(updated_text)

        summary = f"Bot: +{{{{[[Şablon:Kontrola otorîteyê|Kontrola otorîteyê]]}}}} (bnr. [[Taybet:PermanentLink/1295521#Şablona_Kontrola_otorîteyê|gotûbêjê]]){kozmetik_cebu}"

        self.put_current(
            cleaned_new_text,
            summary=summary,
            asynchronous=self.opt['async'],
            show_diff=self.opt['showdiff']
        )


def main(*args: str) -> None:
    local_args = pywikibot.handle_args(args)
    gen_factory = pagegenerators.GeneratorFactory()
    local_args = gen_factory.handle_args(local_args)

    options = {}

    # Parse your own command line arguments
    for arg in local_args:
        arg, _, value = arg.partition(':')
        option = arg[1:]
        if option in ('-always', '-async', '-showdiff'):
            options[option[1:]] = True
        elif option == '-ignore':
            value = value.upper()
            try:
                options['ignore'] = getattr(CANCEL, value)
            except AttributeError:
                raise ValueError(f'Unknown ignore mode {value!r}!')

        # take the remaining options as booleans.
        # You will get a hint if they aren't pre-defined in your bot class
        else:
            options[option] = True

    gen = gen_factory.getCombinedGenerator(preload=True)

    if not pywikibot.bot.suggest_help(missing_generator=not gen):
        bot = AppendTextBot(generator=gen, **options)
        bot.run()


if __name__ == '__main__':
    main()