Mwclient: Replace text in all pages in a category

From Glitchdata
Jump to navigation Jump to search

What if we want to replace text on every page in an entire category tree? For this we use a simple recursive function which calls itself on sub-categories, as in this example, which replaces all "cc-by-2.0" license tags with "cc0" in a given category tree on Commons:




import mwclient

def replace_in_category(category):
    print 'Replacing in category ' + category.[[Page.page_title|page_title]]
    for page in category:
        if page.namespace == 14:  # 14 is the category namespace
            replace_in_category(page)
        else:
            print page.page_title
            text = page.text()
            text = text.replace('{{cc-by-2.0}}', '{{cc0}}')
            page.save(text, summary='Replacing license tag ({{cc-by-2.0}} -> {{cc0}})')
    print 'Done with category ' + cat.page_title

site = mwclient.Site(('https', 'commons.wikimedia.org'))
site.login('username', 'password')
replace_in_cat(site.Categories['Root category'])