Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: develop
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 330 lines (288 sloc) 13.609 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import re

from earwigbot import exceptions
from earwigbot.tasks import Task
from earwigbot.wiki import constants

class WikiProjectTagger(Task):
    """A task to tag talk pages with WikiProject banners.

Usage: :command:`earwigbot -t wikiproject_tagger PATH
--banner BANNER (--category CAT | --file FILE) [--summary SUM]
[--append TEXT] [--autoassess] [--nocreate] [--recursive NUM]
[--site SITE]`

.. glossary::

``--banner BANNER``
the page name of the banner to add, without a namespace (unless the
namespace is something other than ``Template``) so
``--banner WikiProject Biography`` for ``{{WikiProject Biography}}``
``--category CAT`` or ``--file FILE``
determines which pages to tag; either all pages in a category (to
include subcategories as well, see ``--recursive``) or all
pages/categories in a file (utf-8 encoded and path relative to the
current directory)
``--summary SUM``
an optional edit summary to use; defaults to
``"Adding WikiProject banner {{BANNER}}."``
``--append TEXT``
optional text to append to the banner (after an autoassessment, if
any), like ``|importance=low``
``--autoassess``
try to assess each article's class automatically based on the class of
other banners on the same page
``--nocreate``
don't create new talk pages with just a banner if the page doesn't
already exist
``--recursive NUM``
recursively go through subcategories up to a maximum depth of ``NUM``,
or if ``NUM`` isn't provided, go infinitely (this can be dangerous)
``--site SITE``
the ID of the site to tag pages on, defaulting to the... default site

"""
    name = "wikiproject_tagger"

    # Regexes for template names that should always go above the banner, based
    # on [[Wikipedia:Talk page layout]]:
    TOP_TEMPS = [
        r"skip ?to ?(toc|talk|toctalk)$",

        r"ga ?nominee$",

        r"(user ?)?talk ?(header|page|page ?header)$",

        r"community ?article ?probation$",
        r"censor(-nudity)?$",
        r"blp(o| ?others?)?$",
        r"controvers(ial2?|y)$",

        r"(not ?(a ?)?)?forum$",
        r"tv(episode|series)talk$",
        r"recurring ?themes$",
        r"faq$",
        r"(round ?in ?)?circ(les|ular)$",

        r"ar(ti|it)cle ?(history|milestones)$",
        r"failed ?ga$",
        r"old ?prod( ?full)?$",
        r"(old|previous) ?afd$",

        r"((wikiproject|wp) ?)?bio(graph(y|ies))?$",
    ]

    def _upperfirst(self, text):
        """Try to uppercase the first letter of a string."""
        try:
            return text[0].upper() + text[1:]
        except IndexError:
            return text

    def run(self, **kwargs):
        """Main entry point for the bot task."""
        if "file" not in kwargs and "category" not in kwargs:
            log = "No pages to tag; I need either a 'category' or a 'file' passed as kwargs"
            self.logger.error(log)
            return
        if "banner" not in kwargs:
            log = "Needs a banner to add passed as the 'banner' kwarg"
            self.logger.error(log)
            return

        site = self.bot.wiki.get_site(name=kwargs.get("site"))
        banner = kwargs["banner"]
        summary = kwargs.get("summary", "Adding WikiProject banner $3.")
        append = kwargs.get("append")
        autoassess = kwargs.get("autoassess", False)
        nocreate = kwargs.get("nocreate", False)
        recursive = kwargs.get("recursive", 0)
        banner, names = self.get_names(site, banner)
        if not names:
            return
        job = _Job(banner, names, summary, append, autoassess, nocreate)

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

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

        if "file" in kwargs:
            with open(kwargs["file"], "r") as fileobj:
                for line in fileobj:
                    if line.strip():
                        line = line.decode("utf8")
                        if line.startswith("[[") and line.endswith("]]"):
                            line = line[2:-2]
                        page = site.get_page(line)
                        if page.namespace == constants.NS_CATEGORY:
                            self.process_category(page, job, recursive)
                        else:
                            self.process_page(page, job)

    def guess_namespace(self, site, title, assumed):
        """If the given *title* does not have an explicit namespace, guess it.

For example, when transcluding templates, the namespace is guessed to
be ``NS_TEMPLATE`` unless one is explicitly declared (so ``{{foo}}`` ->
``[[Template:Foo]]``, but ``{{:foo}}`` -> ``[[Foo]]``).
"""
        prefix = title.split(":", 1)[0]
        if prefix == title:
            return u":".join((site.namespace_id_to_name(assumed), title))
        try:
            site.namespace_name_to_id(prefix)
        except exceptions.NamespaceNotFoundError:
            return u":".join((site.namespace_id_to_name(assumed), title))
        return title

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

        if banner == title:
            names = [self._upperfirst(banner)]
        else:
            names = [self._upperfirst(banner), self._upperfirst(title)]
        result = site.api_query(action="query", list="backlinks", bllimit=500,
                                blfilterredir="redirects", bltitle=title)
        for backlink in result["query"]["backlinks"]:
            names.append(backlink["title"])
            if backlink["ns"] == constants.NS_TEMPLATE:
                names.append(backlink["title"].split(":", 1)[1])

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

    def process_category(self, page, job, recursive):
        """Try to tag all pages in the given category."""
        self.logger.info(u"Processing category: [[{0]]".format(page.title))
        for member in page.get_members():
            if member.namespace == constants.NS_CATEGORY:
                if recursive is True:
                    self.process_category(member, job, True)
                elif recursive:
                    self.process_category(member, job, recursive - 1)
            else:
                self.process_page(member, job)

    def process_page(self, page, job):
        """Try to tag a specific *page* using the *job* description."""
        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:
            page = page.toggle_talk()
        try:
            code = page.parse()
        except exceptions.PageNotFoundError:
            if job.nocreate:
                log = u"Skipping nonexistent page: [[{0}]]".format(page.title)
                self.logger.info(log)
            else:
                log = u"Tagging new page: [[{0}]]".format(page.title)
                self.logger.info(log)
                banner = "{{" + job.banner + job.append + "}}"
                summary = job.summary.replace("$3", banner)
                page.edit(banner, self.make_summary(summary))
            return
        except exceptions.InvalidPageError:
            log = u"Skipping invalid page: [[{0}]]".format(page.title)
            self.logger.error(log)
            return

        for template in code.ifilter_templates(recursive=True):
            name = self._upperfirst(template.name.strip())
            if name in job.names:
                log = u"Skipping page: [[{0}]]; already tagged with '{1}'"
                self.logger.info(log.format(page.title, name))
                return

        banner = self.make_banner(job, code)
        shell = self.get_banner_shell(code)
        if shell:
            if shell.has_param(1):
                shell.get(1).value.insert(0, banner + "\n")
            else:
                shell.add(1, banner)
        else:
            self.add_banner(code, banner)
        self.apply_genfixes(code)

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

    def make_banner(self, job, code):
        """Return banner text to add based on a *job* and a page's *code*."""
        banner = "{{" + job.banner
        if job.autoassess:
            classes = {"fa": 0, "fl": 0, "ga": 0, "a": 0, "b": 0, "start": 0,
                       "stub": 0, "list": 0, "dab": 0, "c": 0, "redirect": 0,
                       "book": 0, "template": 0, "category": 0}
            for template in code.ifilter_templates(recursive=True):
                if template.has_param("class"):
                    value = unicode(template.get("class").value).lower()
                    if value in classes:
                        classes[value] += 1
            values = tuple(classes.values())
            best = max(values)
            confidence = float(best) / sum(values)
            if confidence > 0.75:
                rank = tuple(classes.keys())[values.index(best)]
                if rank in ("fa", "fl", "ga"):
                    banner += "|class=" + rank.upper()
                else:
                    banner += "|class=" + self._upperfirst(rank)
        return banner + job.append + "}}"

    def get_banner_shell(self, code):
        """Return the banner shell template within *code*, else ``None``."""
        regex = r"^\{\{\s*((WikiProject|WP)[ _]?Banner[ _]?S(hell)?|W(BPS|PBS|PB)|Shell)"
        shells = code.filter_templates(matches=regex)
        if not shells:
            shells = code.filter_templates(matches=regex, recursive=True)
        if shells:
            log = u"Inserting banner into shell: {0}"
            self.logger.debug(log.format(shells[0].name))
            return shells[0]

    def add_banner(self, code, banner):
        """Add *banner* to *code*, following template order conventions."""
        index = 0
        for i, template in enumerate(code.ifilter_templates()):
            name = template.name.lower().replace("_", " ")
            for regex in self.TOP_TEMPS:
                if re.match(regex, name):
                    self.logger.info("Skipping top template: {0}".format(name))
                    index = i + 1

        self.logger.debug(u"Inserting banner at index {0}".format(index))
        code.insert(index, banner)

    def apply_genfixes(self, code):
        """Apply general fixes to *code*, such as template substitution."""
        regex = r"^\{\{\s*((un|no)?s(i((gn|ng)(ed3?)?|g))?|usu|tilde|forgot to sign|without signature)"
        for template in code.ifilter_templates(matches=regex):
            self.logger.debug("Applying genfix: substitute {{unsigned}}")
            template.name = "subst:unsigned"


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):
        self.banner = banner
        self.names = names
        self.summary = summary
        self.append = append
        self.autoassess = autoassess
        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
Something went wrong with that request. Please try again.