#!/usr/bin/python -tt from __future__ import print_function # Remove me if Python >= 3.0 from getpass import getpass import mwclient import sys, os def add_to_category(page, new_category): ''' Add categorization to a page, by way of appending a newline and then the declared category. If page is already in category, do nothing. ''' buf = page.edit() newcat_string = '[[Category:%s]]' % (new_category) if buf.find(newcat_string) < 0: retval = page.save(buf + newcat_string) if retval['result'] == 'Success': return True else: return False def get_pages_by_category(category): ''' Return a standard list of Page objects belonging to a category. ''' mycat = site.categories.get(category) return [page for page in mycat.members()] if __name__ == '__main__': username = raw_input('Enter FAS username: ') password = getpass('Enter FAS password: ') print ('Logging into MediaWiki... ', end='') try: site = mwclient.Site('fedoraproject.org') site.login(username, password) except: print ('Username or password incorrect.') sys.exit(-1) print ('OK!') category = raw_input('Enter category of pages to recategorize: ') print ('Getting pages in category %s... ' % (category), end='') pages = get_pages_by_category(category) if pages == []: print ('None found.') sys.exit(-2) print ('OK!') newcat = raw_input ('Enter new category: ') for page in pages: try: add_to_category(page, newcat) print ('Added %s to category %s' % (page.page_title, newcat)) except: print ('Could not act on page %s' % (page.page_title))