Skip to content

Commit

Permalink
Trim second category (#33)
Browse files Browse the repository at this point in the history
Add postupload mechanism for trimming a given category from all files also in a second category.

Additionally allows for further filtering on filename to ensure only images from the batch are affected.

Task: [T177320](https://phabricator.wikimedia.org/T177320)
  • Loading branch information
lokal-profil authored Oct 5, 2017
1 parent d1d0dfc commit 884332d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
57 changes: 54 additions & 3 deletions batchupload/postUpload.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ def load_commons_site():
commons = pywikibot.Site('commons', 'commons')


def trim_parent_category(category):
def trim_parent_category(category, summary=None, verbose=True):
"""
Remove a category from any files already in one of its sub-categories.
@param category: the category name (with or without the Category:-prefix)
@param summary: override the default summary
@param verbose: if verbose output is desired. Default: True.
@return: the number of pages removed from the category
"""
summary = 'Removing parent category when image already in subcategory'
load_commons_site()
Expand All @@ -36,5 +39,53 @@ def trim_parent_category(category):
for page in uncat:
page.change_category(oldCat=cat, newCat=None, summary=summary)

pywikibot.output(
'Removed {num} pages from {cat}'.format(num=len(uncat), cat=category))
if verbose:
pywikibot.output(
'Removed {num} pages from {cat}'.format(
num=len(uncat), cat=category))

return len(uncat)


def trim_second_category(start_category, del_category, in_filename=None,
summary=None, verbose=True):
"""
Remove a category from any files in a second category.
@param start_category: the name of the category to start with (with or
without the Category:-prefix)
@param del_category: the name of the category to remove to start with (with
or without the Category:-prefix)
@param in_filename: string which must be part of the filename for action
to be taken. Default: None
@param summary: override the default summary
@param verbose: if verbose output is desired. Default: True.
@return: the number of pages removed from the category
"""
load_commons_site()
if not start_category.startswith('Category:'):
start_category = 'Category:{0}'.format(start_category)
if not del_category.startswith('Category:'):
del_category = 'Category:{0}'.format(del_category)
summary = summary or \
'Removing {del_category} for image already in {start_category}'.format(
del_category=del_category, start_category=start_category)

start_cat = pywikibot.Category(commons, start_category)
del_cat = pywikibot.Category(commons, del_category)

counter = 0
for page in start_cat.articles(namespaces=6, content=True):
if in_filename and in_filename not in page.title():
continue
page_cats = set(page.categories())
if del_cat in page_cats:
page.change_category(oldCat=del_cat, newCat=None, summary=summary)
counter += 1

if verbose:
pywikibot.output(
'{start_cat}: Removed {num} pages from {del_cat}'.format(
num=counter, del_cat=del_category, start_cat=start_category))

return counter
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from setuptools import setup
version = '0.1.7'
version = '0.1.8'
repo = 'BatchUploadTools'

setup(
Expand Down

0 comments on commit 884332d

Please sign in to comment.