Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

131 linhas
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import unicode_literals
  23. from earwigbot.tasks import Task
  24. from earwigbot.wiki import constants
  25. import mwparserfromhell
  26. class InfoboxStation(Task):
  27. """
  28. A task to replace ``{{Infobox China station}}`` and
  29. ``{{Infobox Japan station}}`` with ``{{Infobox station}}``.
  30. """
  31. name = "infobox_station"
  32. number = 20
  33. def setup(self):
  34. self.site = self.bot.wiki.get_site()
  35. self._targets = {
  36. "China": (
  37. ["Infobox China station", "Infobox china station"],
  38. "Infobox China station/sandbox",
  39. "Infobox China station/sandbox/cats",
  40. "Wikipedia:Templates for discussion/Log/2015 February 8#Template:Infobox China station"
  41. ),
  42. "Japan": (
  43. ["Infobox Japan station", "Infobox japan station"],
  44. "Infobox Japan station/sandbox",
  45. "Infobox Japan station/sandbox/cats",
  46. "Wikipedia:Templates for discussion/Log/2015 May 9#Template:Infobox Japan station"
  47. ),
  48. }
  49. self._replacement = "{{Infobox station}}"
  50. self.summary = self.make_summary(
  51. "Replacing {source} with {dest} per [[{discussion}|TfD]].")
  52. def run(self, **kwargs):
  53. for name, args in self._targets.items():
  54. if self.shutoff_enabled():
  55. return
  56. self._replace(name, args)
  57. def _replace(self, name, args):
  58. """
  59. Replace a template in all pages that transclude it.
  60. """
  61. self.logger.info("Replacing {0} infobox template".format(name))
  62. count = 0
  63. for title in self._get_transclusions(args[0][0]):
  64. count += 1
  65. if count % 10 == 0 and self.shutoff_enabled():
  66. return
  67. page = self.site.get_page(title)
  68. self._process_page(page, args)
  69. def _process_page(self, page, args):
  70. """
  71. Process a single page to replace a template.
  72. """
  73. self.logger.debug("Processing [[{0}]]".format(page.title))
  74. if not page.check_exclusion():
  75. self.logger.warn("Bot excluded from [[{0}]]".format(page.title))
  76. return
  77. code = mwparserfromhell.parse(page.get(), skip_style_tags=True)
  78. for tmpl in code.filter_templates():
  79. if tmpl.name.matches(args[0]):
  80. tmpl.name = "subst:" + args[2]
  81. self._add_cats(code, unicode(tmpl))
  82. tmpl.name = "subst:" + args[1]
  83. break
  84. if code == page.get():
  85. msg = "Couldn't figure out what to edit in [[{0}]]"
  86. self.logger.warn(msg.format(page.title))
  87. return
  88. summary = self.summary.format(
  89. source="{{" + args[0][0] + "}}", dest=self._replacement,
  90. discussion=args[3])
  91. page.edit(unicode(code), summary, minor=True)
  92. def _add_cats(self, code, cats):
  93. """Add category data (*cats*) to wikicode."""
  94. for link in code.ifilter_wikilinks():
  95. if link.title.lower().startswith("category:"):
  96. code.insert_before(link, cats + "\n")
  97. return
  98. for tmpl in code.filter_templates():
  99. if tmpl.name.lower().endswith("stub"):
  100. prev = code.get(code.index(tmpl) - 1)
  101. if prev.endswith("\n\n"):
  102. code.replace(prev, prev[:-1])
  103. code.insert_before(tmpl, cats + "\n\n")
  104. def _get_transclusions(self, tmpl):
  105. """
  106. Return a list of mainspace translusions of the given template.
  107. """
  108. query = """SELECT page_title
  109. FROM templatelinks
  110. LEFT JOIN page ON tl_from = page_id
  111. WHERE tl_namespace = ? AND tl_title = ? AND tl_from_namespace = ?"""
  112. results = self.site.sql_query(query, (
  113. constants.NS_TEMPLATE, tmpl.replace(" ", "_"), constants.NS_MAIN))
  114. return [title.decode("utf8").replace("_", " ") for (title,) in results]