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.

185 lines
7.6 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. from datetime import datetime
  23. import mwparserfromhell
  24. from earwigbot.tasks import Task
  25. from earwigbot.wiki.constants import *
  26. class AFCUndated(Task):
  27. """A task to clear [[Category:Undated AfC submissions]]."""
  28. name = "afc_undated"
  29. number = 5
  30. def setup(self):
  31. cfg = self.config.tasks.get(self.name, {})
  32. self.category = cfg.get("category", "Undated AfC submissions")
  33. default_summary = "Adding timestamp to undated [[WP:AFC|Articles for creation]] submission."
  34. self.summary = self.make_summary(cfg.get("summary", default_summary))
  35. self.namespaces = {
  36. "submission": [NS_USER, NS_PROJECT, NS_PROJECT_TALK],
  37. "talk": [NS_TALK, NS_FILE_TALK, NS_TEMPLATE_TALK, NS_HELP_TALK,
  38. NS_CATEGORY_TALK]
  39. }
  40. self.aliases = {
  41. "submission": ["AFC submission"],
  42. "talk": ["WikiProject Articles for creation"]
  43. }
  44. def run(self, **kwargs):
  45. try:
  46. self.statistics = self.bot.tasks.get("afc_statistics")
  47. except KeyError:
  48. err = "Requires afc_statistics task (from earwigbot_plugins)"
  49. self.logger.error(err)
  50. return
  51. self.site = self.bot.wiki.get_site()
  52. category = self.site.get_category(self.category)
  53. logmsg = u"Undated category [[{0}]] has {1} members"
  54. self.logger.info(logmsg.format(category.title, category.size))
  55. if category.size:
  56. self.build_aliases()
  57. counter = 0
  58. for page in category:
  59. if not counter % 10:
  60. if self.shutoff_enabled():
  61. return
  62. self.process_page(page)
  63. counter += 1
  64. def build_aliases(self):
  65. """Build template name aliases for the AFC templates."""
  66. for key in self.aliases:
  67. base = self.aliases[key][0]
  68. aliases = [base, "Template:" + base]
  69. result = self.site.api_query(
  70. action="query", list="backlinks", bllimit=50,
  71. blfilterredir="redirects", bltitle=aliases[1])
  72. for data in result["query"]["backlinks"]:
  73. redir = self.site.get_page(data["title"])
  74. aliases.append(redir.title)
  75. if redir.namespace == NS_TEMPLATE:
  76. aliases.append(redir.title.split(":", 1)[1])
  77. self.aliases[key] = aliases
  78. def process_page(self, page):
  79. """Date the necessary templates inside a page object."""
  80. if not page.check_exclusion():
  81. msg = u"Skipping [[{0}]]; bot excluded from editing"
  82. self.logger.info(msg.format(page.title))
  83. return
  84. is_sub = page.namespace in self.namespaces["submission"]
  85. is_talk = page.namespace in self.namespaces["talk"]
  86. if is_sub:
  87. aliases = self.aliases["subission"]
  88. timestamps = {}
  89. elif is_talk:
  90. aliases = self.aliases["talk"]
  91. timestamp, reviewer = self.get_talkdata(page)
  92. else:
  93. msg = u"[[{0}]] is undated, but in a namespace I don't know how to process"
  94. self.logger.warn(msg.format(page.title))
  95. return
  96. if not timestamp:
  97. return
  98. code = mwparserfromhell.parse(page.get())
  99. changes = 0
  100. for template in code.filter_templates():
  101. if template.name.matches(aliases) and not template.has("ts"):
  102. if is_sub:
  103. status = self.get_status(template)
  104. if status in timestamps:
  105. timestamp = timestamps[status]
  106. else:
  107. timestamp = self.get_timestamp(page, status)
  108. timestamps[status] = timestamp
  109. template.add("ts", timestamp)
  110. if is_talk and not template.has("reviewer"):
  111. template.add("reviewer", reviewer)
  112. changes += 1
  113. if changes:
  114. msg = u"Dating [[{0}]]: {1}x {2}"
  115. self.logger.info(msg.format(page.title, changes, aliases[0]))
  116. page.edit(unicode(code), self.summary)
  117. else:
  118. msg = u"[[{0}]] is undated, but I can't figure out what to replace"
  119. self.logger.warn(msg.format(page.title))
  120. def get_status(self, template):
  121. """Get the status code that corresponds to a given template."""
  122. valid = ["P", "R", "T", "D"]
  123. if template.has(1):
  124. status = template.get(1).value.strip().upper()
  125. if status in valid:
  126. return status
  127. return "P"
  128. def get_timestamp(self, page, chart):
  129. """Get the timestamp associated with a particular submission."""
  130. log = u"[[{0}]]: Getting timestamp for state {1}"
  131. self.logger.debug(log.format(page.title, chart))
  132. search = self.statistics.search_history
  133. user, ts, revid = search(page.pageid, chart, chart, [])
  134. if not ts:
  135. log = u"Couldn't find timestamp in [[{0}]] with state {1}"
  136. self.logger.warn(log.format(page.title, chart))
  137. return None
  138. return ts.strftime("%Y%m%d%H%M%S")
  139. def get_talkdata(self, page):
  140. """Get the timestamp and reviewer associated with a talkpage.
  141. This is the mover for a normal article submission, and the uploader for
  142. a file page.
  143. """
  144. subject = page.toggle_talk()
  145. if subject.namespace == NS_FILE:
  146. return self.get_filedata(subject)
  147. self.logger.debug(u"[[{0}]]: Getting talkdata".format(page.title))
  148. chart = self.statistics.CHART_ACCEPT
  149. user, ts, revid = self.statistics.get_special(subject.pageid, chart)
  150. if not ts:
  151. log = u"Couldn't get talkdata for [[{0}]]"
  152. self.logger.warn(log.format(page.title))
  153. return None, None
  154. return ts.strftime("%Y%m%d%H%M%S"), user
  155. def get_filedata(self, page):
  156. """Get the timestamp and reviewer associated with a file talkpage."""
  157. self.logger.debug(u"[[{0}]]: Getting filedata".format(page.title))
  158. result = self.site.api_query(action="query", prop="imageinfo",
  159. titles=page.title)
  160. data = result["query"]["pages"].values()[0]
  161. if "imageinfo" not in data:
  162. log = u"Couldn't get filedata for [[{0}]]"
  163. self.logger.warn(log.format(page.title))
  164. return None, None
  165. info = data["imageinfo"][0]
  166. ts = datetime.strptime(info["timestamp"], "%Y-%m-%dT%H:%M:%SZ")
  167. return ts.strftime("%Y%m%d%H%M%S"), info["user"]