Gebruiker:HernoemBot/bulkcategorymove.py

Voor het hernoemen van een serie categorieën, inclusief het bijwerken van het label op Wikidata.

Maakt gebruik van category.py.

import pwb
import pywikibot
from pywikibot.backports import Tuple
import scripts.category
from scripts.category import CategoryMoveRobot

def main(*args: Tuple[str, ...]) -> None:
    summary = ''
    
    local_args = pywikibot.handle_args(args)
    for arg in local_args:
        option, _, value = arg[1:].partition(':')
        if option == 'summary':
            summary = value
    
    inputFile = open('input.txt', 'r', encoding='utf-8')
    site = pywikibot.Site()
    for line in inputFile:
        old_cat_title, new_cat_title = line.split('\t')

        # get Wikidata item ID before the move
        oldPage = pywikibot.Page(site, old_cat_title, ns=14)
        newPage = pywikibot.Page(site, new_cat_title, ns=14)
        if newPage.exists():
            itemId = None
        else:
            itemId = oldPage.properties().get('wikibase_item')

        # call category.py
        bot = CategoryMoveRobot(oldcat=old_cat_title,
                                newcat=new_cat_title,
                                batch=False,
                                comment=summary,
                                inplace=False,
                                delete_oldcat=True,
                                title_regex=False,
                                history=False,
                                pagesonly=False,
                                deletion_comment=False,
                                wikibase=True,
                                allow_split=False,
                                move_together=False,
                                keep_sortkey=True)
        try:
            bot.run()
        except Exception:
            pywikibot.error('Fatal error:', exc_info=True)

        # update Wikidata label
        if itemId:
            item = pywikibot.ItemPage(oldPage.site.data_repository(), itemId)
            newLabel = newPage.title(with_ns=True)
            item.editLabels({'nl': newLabel}, summary='Update [nl] label')

    inputFile.close()
    print('Done.')


if __name__ == '__main__':
    main()