Browse Source

Cleanup; support shutoff.

tags/v0.1^2
Ben Kurtovic 12 years ago
parent
commit
588d3fb696
1 changed files with 34 additions and 9 deletions
  1. +34
    -9
      earwigbot/tasks/wikiproject_tagger.py

+ 34
- 9
earwigbot/tasks/wikiproject_tagger.py View File

@@ -105,6 +105,7 @@ class WikiProjectTagger(Task):
self.logger.error(log) self.logger.error(log)
return return


site = self.bot.wiki.get_site(name=kwargs.get("site"))
banner = kwargs["banner"] banner = kwargs["banner"]
summary = kwargs.get("summary", "Adding WikiProject banner $3.") summary = kwargs.get("summary", "Adding WikiProject banner $3.")
append = kwargs.get("append") append = kwargs.get("append")
@@ -115,11 +116,16 @@ class WikiProjectTagger(Task):
if not names: if not names:
return return
job = _Job(banner, names, summary, append, autoassess, nocreate) job = _Job(banner, names, summary, append, autoassess, nocreate)
site = self.bot.wiki.get_site(name=kwargs.get("site"))


try:
self.run_job(kwargs, site, job, recursive)
except _ShutoffEnabled:
return

def run_job(self, kwargs, site, job, recursive):
if "category" in kwargs: if "category" in kwargs:
title = kwargs["category"] title = kwargs["category"]
title = self.guess_namespace(title, constants.NS_CATEGORY)
title = self.guess_namespace(site, title, constants.NS_CATEGORY)
self.process_category(site.get_page(title), job, recursive) self.process_category(site.get_page(title), job, recursive)


if "file" in kwargs: if "file" in kwargs:
@@ -135,7 +141,7 @@ class WikiProjectTagger(Task):
else: else:
self.process_page(page, job) self.process_page(page, job)


def guess_namespace(self, title, assumed):
def guess_namespace(self, site, title, assumed):
prefix = title.split(":", 1)[0] prefix = title.split(":", 1)[0]
if prefix == title: if prefix == title:
return u":".join((site.namespace_id_to_name(assumed), title)) return u":".join((site.namespace_id_to_name(assumed), title))
@@ -146,12 +152,12 @@ class WikiProjectTagger(Task):
return title return title


def get_names(self, site, banner): def get_names(self, site, banner):
title = self.guess_namespace(banner, constants.NS_TEMPLATE)
title = self.guess_namespace(site, banner, constants.NS_TEMPLATE)
if title == banner: if title == banner:
banner = banner.split(":", 1)[1] banner = banner.split(":", 1)[1]
page = site.get_page(title) page = site.get_page(title)
if page.exists != page.PAGE_EXISTS: if page.exists != page.PAGE_EXISTS:
self.logger.error("Banner [[{0}]] does not exist".format(title))
self.logger.error(u"Banner [[{0}]] does not exist".format(title))
return banner, None return banner, None


names = [banner] if banner == title else [banner, title] names = [banner] if banner == title else [banner, title]
@@ -162,12 +168,12 @@ class WikiProjectTagger(Task):
if backlink["ns"] == constants.NS_TEMPLATE: if backlink["ns"] == constants.NS_TEMPLATE:
names.append(backlink["title"].split(":", 1)[1]) names.append(backlink["title"].split(":", 1)[1])


log = "Found {0} aliases for banner [[{1}]]".format(len(names), title)
log = u"Found {0} aliases for banner [[{1}]]".format(len(names), title)
self.logger.debug(log) self.logger.debug(log)
return banner, names return banner, names


def process_category(self, page, job, recursive): def process_category(self, page, job, recursive):
self.logger.info("Processing category: [[{0]]".format(page.title))
self.logger.info(u"Processing category: [[{0]]".format(page.title))
for member in page.get_members(): for member in page.get_members():
if member.namespace == constants.NS_CATEGORY: if member.namespace == constants.NS_CATEGORY:
if recursive is True: if recursive is True:
@@ -178,16 +184,21 @@ class WikiProjectTagger(Task):
self.process_page(member, job) self.process_page(member, job)


def process_page(self, page, job): def process_page(self, page, job):
if job.counter % 10 == 0: # Do a shutoff check every ten pages
if self.shutoff_enabled(page.site):
raise _ShutoffEnabled()
job.counter += 1

if not page.is_talkpage: if not page.is_talkpage:
page = page.toggle_talk() page = page.toggle_talk()
try: try:
code = page.parse() code = page.parse()
except exceptions.PageNotFoundError: except exceptions.PageNotFoundError:
if job.nocreate: if job.nocreate:
log = "Skipping nonexistent page: [[{0}]]".format(page.title)
log = u"Skipping nonexistent page: [[{0}]]".format(page.title)
self.logger.info(log) self.logger.info(log)
else: else:
log = "Tagging new page: [[{0}]]".format(page.title)
log = u"Tagging new page: [[{0}]]".format(page.title)
self.logger.info(log) self.logger.info(log)
banner = "{{" + job.banner + job.append + "}}" banner = "{{" + job.banner + job.append + "}}"
summary = job.summary.replace("$3", banner) summary = job.summary.replace("$3", banner)
@@ -202,11 +213,18 @@ class WikiProjectTagger(Task):


text = unicode(code) text = unicode(code)
if page.get() != text: if page.get() != text:
self.logger.info(u"Tagging page: [[{0}]]".format(page.title))
summary = job.summary.replace("$3", banner) summary = job.summary.replace("$3", banner)
page.edit(text, self.make_summary(summary)) page.edit(text, self.make_summary(summary))




class _Job(object): class _Job(object):
"""Represents a single wikiproject-tagging task.

Stores information on the banner to add, the edit summary to use, whether
or not to autoassess and create new pages from scratch, and a counter of
the number of pages edited.
"""
def __init__(self, banner, names, summary, append, autoassess, nocreate): def __init__(self, banner, names, summary, append, autoassess, nocreate):
self.banner = banner self.banner = banner
self.names = names self.names = names
@@ -214,3 +232,10 @@ class _Job(object):
self.append = append self.append = append
self.autoassess = autoassess self.autoassess = autoassess
self.nocreate = nocreate self.nocreate = nocreate
self.counter = 0


class _ShutoffEnabled(Exception):
"""Raised by process_page() if shutoff is enabled. Caught by run(), which
will then stop the task."""
pass

Loading…
Cancel
Save