User:JimTittsler/CopyPagesExample

From WikiEducator
Jump to: navigation, search

#!/usr/bin/python

# copy a page and its subpages
import mwclient

user = 'JimTittslerBot'
password = 'plugh'

source = 'OLPC Tuvalu'
destination = 'OLPC Kosrae'
source_template = '{{OLPCTuvalu}}'
destination_template = '{{OLPCKosrae}}'
source_redirect = '#REDIRECT [[OLPC Tuvalu'
destination_redirect = '#REDIRECT [[OLPC Kosrae'
summary_message = 'started from corresponding OLPC Tuvalu page'

site = mwclient.Site('WikiEducator.org', path='/')
site.login(user, password)

def process_page(text):
    """ alter or delete wikitext during copy"""
    text = text.replace(source_template, destination_template)
    text = text.replace(source_redirect, destination_redirect)
    return text

for page in site.allpages(prefix=source):
    newpage_name = page.name.replace(source, destination)
    print page.name,'=>',newpage_name
    text = page.edit(readonly=True)

    newpage = site.Pages[newpage_name]
    #newtext = newpage.edit()
    newpage.save(process_page(text), summary=summary_message)