Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  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. import mwparserfromhell
  23. from earwigbot.tasks import Task
  24. from earwigbot.wiki.constants import *
  25. class AFCUndated(Task):
  26. """A task to clear [[Category:Undated AfC submissions]]."""
  27. name = "afc_undated"
  28. number = 5
  29. def setup(self):
  30. cfg = self.config.tasks.get(self.name, {})
  31. self.category = cfg.get("category", "Undated AfC submissions")
  32. default_summary = "Adding timestamp to undated [[WP:AFC|Articles for creation]] submission."
  33. self.summary = self.make_summary(cfg.get("summary", default_summary))
  34. self.namespaces = {
  35. "submission": [NS_USER, NS_PROJECT, NS_PROJECT_TALK],
  36. "talk": [NS_TALK, NS_FILE_TALK, NS_TEMPLATE_TALK, NS_HELP_TALK,
  37. NS_CATEGORY_TALK]
  38. }
  39. self.aliases = {"submission": ["AFC submission"], "talk": ["WPAFC"]}
  40. def run(self, **kwargs):
  41. self.site = self.bot.wiki.get_site()
  42. category = self.site.get_category(self.category)
  43. logmsg = u"Undated category [[{0}]] has {1} members"
  44. self.logger.info(logmsg.format(category.title, category.size))
  45. if category.size:
  46. self.build_aliases()
  47. counter = 0
  48. for page in category:
  49. if counter % 10:
  50. if self.shutoff_enabled():
  51. return
  52. self.process_page(page)
  53. counter += 1
  54. def build_aliases(self):
  55. """Build template name aliases for the AFC templates."""
  56. pass
  57. def process_page(self, page):
  58. """Date the necessary templates inside a page object."""
  59. is_sub = page.namespace in self.namespaces["submission"]
  60. is_talk = page.namespace in self.namespaces["talk"]
  61. if is_sub:
  62. aliases = self.aliases["subission"]
  63. timestamp = self.get_timestamp(page)
  64. elif is_talk:
  65. aliases = self.aliases["talk"]
  66. timestamp, reviewer = self.get_talkdata(page)
  67. else:
  68. msg = u"[[{0}]] is undated, but in a namespace we don't know how to process"
  69. self.logger.warn(msg.format(page.title))
  70. return
  71. code = mwparserfromhell.parse(page.get())
  72. changes = 0
  73. for template in code.filter_templates():
  74. if template.name.matches(aliases) and not template.has("ts"):
  75. template.add("ts", timestamp)
  76. if is_talk and not template.has("reviewer"):
  77. template.add("reviewer", reviewer)
  78. changes += 1
  79. if changes:
  80. msg = u"Dating [[{0}]]: {1}x {2}"
  81. self.logger.info(msg.format(page.title, changes, aliases[0]))
  82. page.edit(unicode(code), self.summary)
  83. else:
  84. msg = u"[[{0}]] is undated, but I can't figure out what to replace"
  85. self.logger.warn(msg.format(page.title))
  86. def get_timestamp(self, page):
  87. """Get the timestamp associated with a particular submission."""
  88. return timestamp
  89. def get_talkdata(self, page):
  90. """Get the timestamp and reviewer associated with a talkpage.
  91. This is the mover for a normal article submission, and the uploader for
  92. a file page.
  93. """
  94. return timestamp, reviewer