Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Não pode escolher mais do que 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.

afc_undated.py 7.6 KiB

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