Permalink
Browse files

Check for actual categories to remove. Otherwise, don't make edits

  • Loading branch information...
harej committed Aug 17, 2015
1 parent 807cf36 commit f345ef6900e022ef2a9fcf4705b35d4e3eb3a7aa
Showing with 21 additions and 16 deletions.
  1. +21 −16 category_migration.py
@@ -23,28 +23,33 @@ def main():
pairs = [row[0].decode('utf-8') for row in wptools.query('wiki', q, None)]
for pair in pairs:
# Load WikiProject page+category pairs
# Load WikiProject page
project_page = pwb.Page(bot, 'Wikipedia:' + pair)
project_cat = pwb.Page(bot, 'Category:' + pair)
# Preserve only categories that aren't in the style "X WikiProjects"
preserve = [c for c in pwb.textlib.getCategoryLinks(project_page.text) \
if str(c)[-15:] != ' WikiProjects]]']
project_page.text = pwb.textlib.replaceCategoryLinks(project_page.text, preserve)
# List categories to add to project category
page_cats = [c for c in pwb.textlib.getCategoryLinks(project_page.text) \
if str(c)[-15:] == ' WikiProjects]]']
cat_cats = [c for c in pwb.textlib.getCategoryLinks(project_cat.text) \
if str(c)[-15:] == ' WikiProjects]]']
to_add = list(set(page_cats) - set(cat_cats))
project_cat.text = pwb.textlib.replaceCategoryLinks(project_cat.text, to_add, addOnly=True)
# Saving pages
summary = "WikiProject category migration. See [[User:Harej bot/WikiProject category migration]]."
project_page.save(summary, minor=False)
project_cat.save(summary, minor=False)
# Check for presence of removable categories; otherwise, don't bother
if preserve != pwb.textlib.getCategoryLinks(project_page.text):
project_page.text = pwb.textlib.replaceCategoryLinks(project_page.text, preserve)
# Load WikiProject category
project_cat = pwb.Page(bot, 'Category:' + pair)
# List categories to add to project category
page_cats = [c for c in pwb.textlib.getCategoryLinks(project_page.text) \
if str(c)[-15:] == ' WikiProjects]]']
cat_cats = [c for c in pwb.textlib.getCategoryLinks(project_cat.text) \
if str(c)[-15:] == ' WikiProjects]]']
to_add = list(set(page_cats) - set(cat_cats))
project_cat.text = pwb.textlib.replaceCategoryLinks(project_cat.text, to_add, addOnly=True)
# Saving pages
summary = "WikiProject category migration. See [[User:Harej bot/WikiProject category migration]]."
project_page.save(summary, minor=False)
project_cat.save(summary, minor=False)
if __name__ == "__main__":

0 comments on commit f345ef6

Please sign in to comment.