A Python robot that edits Wikipedia and interacts with people over IRC 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.

536 linhas
23 KiB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. import logging
  4. import re
  5. from os.path import expanduser
  6. from threading import Lock
  7. import oursql
  8. from classes import BaseTask
  9. import config
  10. import wiki
  11. # Chart status number constants:
  12. CHART_NONE = 0
  13. CHART_PEND = 1
  14. CHART_DRAFT = 2
  15. CHART_REVIEW = 3
  16. CHART_ACCEPT = 4
  17. CHART_DECLINE = 5
  18. class Task(BaseTask):
  19. """A task to generate statistics for WikiProject Articles for Creation.
  20. Statistics are stored in a MySQL database ("u_earwig_afc_statistics")
  21. accessed with oursql. Statistics are updated live while watching the recent
  22. changes IRC feed and saved once an hour, on the hour, to self.pagename.
  23. In the live bot, this is "Template:AFC statistics".
  24. """
  25. name = "afc_statistics"
  26. number = 2
  27. def __init__(self):
  28. self.cfg = cfg = config.tasks.get(self.name, {})
  29. # Set some wiki-related attributes:
  30. self.pagename = cfg.get("page", "Template:AFC statistics")
  31. self.pending_cat = cfg.get("pending", "Pending AfC submissions")
  32. self.ignore_list = cfg.get("ignore_list", [])
  33. default_summary = "Updating statistics for [[WP:WPAFC|WikiProject Articles for creation]]."
  34. self.summary = self.make_summary(cfg.get("summary", default_summary))
  35. # Templates used in chart generation:
  36. templates = cfg.get("templates", {})
  37. self.tl_header = templates.get("header", "AFC statistics/header")
  38. self.tl_row = templates.get("row", "AFC statistics/row")
  39. self.tl_footer = templates.get("footer", "AFC statistics/footer")
  40. # Connection data for our SQL database:
  41. kwargs = cfg.get("sql", {})
  42. kwargs["read_default_file"] = expanduser("~/.my.cnf")
  43. self.conn_data = kwargs
  44. self.db_access_lock = Lock()
  45. def run(self, **kwargs):
  46. """Entry point for a task event.
  47. Depending on the kwargs passed, we will either synchronize our local
  48. statistics database with the site (self.sync()) or save it to the wiki
  49. (self.save()). We will additionally create an SQL connection with our
  50. local database.
  51. """
  52. self.site = wiki.get_site()
  53. self.conn = oursql.connect(**self.conn_data)
  54. action = kwargs.get("action")
  55. try:
  56. if action == "save":
  57. self.save(**kwargs)
  58. elif action == "sync":
  59. self.sync(**kwargs)
  60. finally:
  61. self.conn.close()
  62. def save(self, **kwargs):
  63. """Save our local statistics to the wiki.
  64. After checking for emergency shutoff, the statistics chart is compiled,
  65. and then saved to self.pagename using self.summary iff it has changed
  66. since last save.
  67. """
  68. self.logger.info("Saving chart")
  69. if kwargs.get("fromIRC"):
  70. summary = " ".join((self.summary, "(!earwigbot)"))
  71. else:
  72. if self.shutoff_enabled():
  73. return
  74. summary = self.summary
  75. statistics = self.compile_charts()
  76. page = self.site.get_page(self.pagename)
  77. text = page.get()
  78. newtext = re.sub("(<!-- stat begin -->)(.*?)(<!-- stat end -->)",
  79. statistics.join(("\\1\n", "\n\\3")), text,
  80. flags=re.DOTALL)
  81. if newtext == text:
  82. self.logger.info("Chart unchanged; not saving")
  83. return # Don't edit the page if we're not adding anything
  84. newtext = re.sub("(<!-- sig begin -->)(.*?)(<!-- sig end -->)",
  85. "\\1~~~ at ~~~~~\\3", newtext)
  86. page.edit(newtext, summary, minor=True, bot=True)
  87. self.logger.info("Chart saved to [[{0}]]".format(page.title()))
  88. def compile_charts(self):
  89. """Compile and return all statistics information from our local db."""
  90. stats = ""
  91. with self.conn.cursor() as cursor, self.db_access_lock:
  92. cursor.execute("SELECT * FROM chart")
  93. for chart in cursor:
  94. stats += self.compile_chart(chart) + "\n"
  95. return stats[:-1] # Drop the last newline
  96. def compile_chart(self, chart_info):
  97. """Compile and return a single statistics chart."""
  98. chart_id, chart_title, special_title = chart_info
  99. chart = "|".join((self.tl_header, chart_title))
  100. if special_title:
  101. chart += "".join(("|", special_title))
  102. chart = "".join(("{{", chart, "}}"))
  103. query = "SELECT * FROM page JOIN row ON page_id = row_id WHERE row_chart = ?"
  104. with self.conn.cursor(oursql.DictCursor) as cursor:
  105. cursor.execute(query, (chart_id,))
  106. for page in cursor:
  107. chart += "\n" + self.compile_chart_row(page)
  108. chart += "".join(("\n{{", self.tl_footer, "}}"))
  109. return chart
  110. def compile_chart_row(self, page):
  111. """Compile and return a single chart row.
  112. 'page' is a dict of page information, taken as a row from the page
  113. table, where keys are column names and values are their cell contents.
  114. """
  115. row = "{0}|s={page_status}|t={page_title}|h={page_short}|z={page_size}|"
  116. row += "cr={page_create_user}|cd={page_create_time}|ci={page_create_oldid}|"
  117. row += "mr={page_modify_user}|md={page_modify_time}|mi={page_modify_oldid}|"
  118. page["page_create_time"] = self.format_time(page["page_create_time"])
  119. page["page_modify_time"] = self.format_time(page["page_modify_time"])
  120. if page["page_special_user"]:
  121. row += "sr={page_special_user}|sd={page_special_time}|si={page_special_oldid}|"
  122. page["page_special_time"] = self.format_time(page["page_special_time"])
  123. if page["page_notes"]:
  124. row += "n=1{page_notes}"
  125. return "".join(("{{", row.format(self.tl_row, **page), "}}"))
  126. def format_time(self, timestamp):
  127. """Format a datetime into the standard MediaWiki timestamp format."""
  128. return timestamp.strftime("%H:%M, %d %B %Y")
  129. def sync(self, **kwargs):
  130. """Synchronize our local statistics database with the site.
  131. Syncing involves, in order, updating tracked submissions that have
  132. been changed since last sync (self.update_tracked()), adding pending
  133. submissions that are not tracked (self.add_untracked()), and removing
  134. old submissions from the database (self.delete_old()).
  135. The sync will be canceled if SQL replication lag is greater than 600
  136. seconds, because this will lead to potential problems and outdated
  137. data, not to mention putting demand on an already overloaded server.
  138. Giving sync the kwarg "ignore_replag" will go around this restriction.
  139. """
  140. self.logger.info("Starting sync")
  141. replag = self.site.get_replag()
  142. self.logger.debug("Server replag is {0}".format(replag))
  143. if replag > 600 and not kwargs.get("ignore_replag"):
  144. msg = "Sync canceled as replag ({0} secs) is greater than ten minutes."
  145. self.logger.warn(msg.format(replag))
  146. with self.conn.cursor() as cursor, self.db_access_lock:
  147. self.update_tracked(cursor)
  148. self.add_untracked(cursor)
  149. self.delete_old(cursor)
  150. self.logger.info("Sync completed")
  151. def update_tracked(self, cursor):
  152. """Update tracked submissions that have been changed since last sync.
  153. This is done by iterating through every page in our database and
  154. comparing our stored latest revision ID with the actual latest revision
  155. ID from an SQL query. If they differ, we will update our information
  156. about the page (self.update_page()).
  157. If the page does not exist, we will remove it from our database with
  158. self.untrack_page().
  159. """
  160. self.logger.debug("Updating tracked submissions")
  161. query1 = "SELECT page_id, page_title, page_modify_oldid FROM page"
  162. query2 = """SELECT page_latest, page_title, page_namespace FROM page
  163. WHERE page_id = ?"""
  164. cursor.execute(query1)
  165. for pageid, title, oldid in cursor:
  166. msg = "Updating tracked page: [[{0}]] (id: {1}) @ {2}"
  167. self.logger.debug(msg.format(pageid, title, oldid))
  168. result = list(self.site.sql_query(query2, (pageid,)))
  169. try:
  170. real_oldid = result[0][0]
  171. except IndexError: # Page doesn't exist!
  172. self.untrack_page(cursor, pageid)
  173. continue
  174. if real_oldid != oldid:
  175. body = result[0][1].replace("_", " ")
  176. ns = self.site.namespace_id_to_name(result[0][2])
  177. real_title = ":".join(ns, body)
  178. self.update_page(cursor, pageid, real_title)
  179. def add_untracked(self, cursor):
  180. """Add pending submissions that are not yet tracked.
  181. This is done by compiling a list of all currently tracked submissions
  182. and iterating through all members of self.pending_cat via SQL. If a
  183. page in the pending category is not tracked and is not in
  184. self.ignore_list, we will track it with self.track_page().
  185. """
  186. self.logger.debug("Adding untracked pending submissions")
  187. cursor.execute("SELECT page_id FROM page")
  188. tracked = [i[0] for i in cursor.fetchall()]
  189. category = self.site.get_category(self.pending_cat)
  190. pending = category.members(use_sql=True)
  191. for title, pageid in pending:
  192. if title in self.ignore_list:
  193. continue
  194. if pageid not in tracked:
  195. self.track_page(cursor, pageid, title)
  196. def delete_old(self, cursor):
  197. """Remove old submissions from the database.
  198. "Old" is defined as a submission that has been declined or accepted
  199. more than 36 hours ago. Pending submissions cannot be "old".
  200. """
  201. self.logger.debug("Removing old submissions from chart")
  202. query = """DELETE FROM page, row USING page JOIN row
  203. ON page_id = row_id WHERE row_chart IN ?
  204. AND ADDTIME(page_special_time, '36:00:00') < NOW()"""
  205. old_charts = (CHART_ACCEPT, CHART_DECLINE)
  206. cursor.execute(query, (old_charts,))
  207. def untrack_page(self, cursor, pageid):
  208. """Remove a page, given by ID, from our database."""
  209. self.logger.debug("Untracking page (id: {0})".format(pageid))
  210. query = """DELETE FROM page, row USING page JOIN row
  211. ON page_id = row_id WHERE page_id = ?"""
  212. cursor.execute(query, (pageid,))
  213. def track_page(self, cursor, pageid, title):
  214. """Update hook for when page is not in our database.
  215. A variety of SQL queries are used to gather information about the page,
  216. which are then saved to our database.
  217. """
  218. msg = "Tracking page [[{0}]] (id: {1})".format(title, pageid)
  219. self.logger.debug(msg)
  220. content = self.get_content(title)
  221. status, chart = self.get_status_and_chart(content)
  222. if not status:
  223. msg = "Could not find a status for [[{0}]]".format(title)
  224. self.logger.error(msg)
  225. return
  226. short = self.get_short_title(title)
  227. size = len(content)
  228. notes = self.get_notes(pageid)
  229. c_user, c_time, c_id = self.get_create(pageid)
  230. m_user, m_time, m_id = self.get_modify(pageid)
  231. s_user, s_time, s_id = self.get_special(pageid, chart)
  232. query1 = "INSERT INTO row VALUES ?"
  233. query2 = "INSERT INTO page VALUES ?"
  234. cursor.execute(query1, ((pageid, chart),))
  235. cursor.execute(query2, ((pageid, status, title, short, size, notes,
  236. c_user, c_time, c_id, m_user, m_time, m_id,
  237. s_user, s_time, s_id),))
  238. def update_page(self, cursor, pageid, title):
  239. """Update hook for when page is already in our database.
  240. A variety of SQL queries are used to gather information about the page,
  241. which is compared against our stored information. Differing information
  242. is then updated.
  243. If our page is now a redirect, we will determine the namespace it was
  244. moved to. If it was moved to the mainspace or template space, we will
  245. set the sub's status as accepted. If it was to the Project: or Project
  246. talk: namespaces, we'll merely update our stored title (this is likely
  247. to occur if a submission was moved from the userspace to the project
  248. space). If it was moved to another namespace, something unusual has
  249. happened, and we'll untrack the submission.
  250. """
  251. msg = "Updating page [[{0}]] (id: {1})".format(title, pageid)
  252. self.logger.debug(msg)
  253. content = self.get_content(title)
  254. try:
  255. redirect_regex = wiki.Page.re_redirect
  256. target_title = re.findall(redirect_regex, content, flags=re.I)[0]
  257. except IndexError:
  258. status, chart = self.get_status_and_chart(content)
  259. if not status:
  260. self.untrack_page(cursor, pageid)
  261. return
  262. else:
  263. target_ns = self.site.get_page(target_title).namespace()
  264. if target_ns in [wiki.NS_MAIN, wiki.NS_TEMPLATE]:
  265. status, chart = "accept", CHART_ACCEPT
  266. elif target_ns in [wiki.NS_PROJECT, wiki.NS_PROJECT_TALK]:
  267. title = target_title
  268. content = self.get_content(title)
  269. status, chart = self.get_status_and_chart(content)
  270. if not status:
  271. self.untrack_page(cursor, pageid)
  272. return
  273. else:
  274. msg = "Page has moved to namespace {0}".format(target_ns)
  275. self.logger.debug(msg)
  276. self.untrack_page(cursor, pageid)
  277. return
  278. size = len(content)
  279. notes = self.get_notes(pageid)
  280. m_user, m_time, m_id = self.get_modify(pageid)
  281. query = "SELECT * FROM page JOIN row ON page_id = row_id WHERE page_id = ?"
  282. with self.conn.cursor(oursql.DictCursor) as dict_cursor:
  283. dict_cursor.execute(query, (pageid,))
  284. result = dict_cursor.fetchall()[0]
  285. if title != result["page_title"]:
  286. self.update_page_title(cursor, result, pageid, title)
  287. if m_id != result["page_modify_oldid"]:
  288. self.update_page_modify(cursor, result, pageid, size, m_user, m_time, m_id)
  289. if status != result["page_status"]:
  290. self.update_page_status(cursor, result, pageid, status, chart, page)
  291. if notes != result["page_notes"]:
  292. self.update_page_notes(cursor, result, pageid, notes)
  293. def update_page_title(self, cursor, result, pageid, title):
  294. """Update the title and short_title of a page in our database."""
  295. query = "UPDATE page SET page_title = ?, page_short = ? WHERE page_id = ?"
  296. short = self.get_short_title(title)
  297. cursor.execute(query, (title, short, pageid))
  298. msg = "{0}: title: {1} -> {2}"
  299. self.logger.debug(msg.format(pageid, result["page_title"], title))
  300. def update_page_modify(self, cursor, result, pageid, size, m_user, m_time, m_id):
  301. """Update the last modified information of a page in our database."""
  302. query = """UPDATE page SET page_size = ?, page_modify_user = ?,
  303. page_modify_time = ?, page_modify_oldid = ?
  304. WHERE page_id = ?"""
  305. cursor.execute(query, (size, m_user, m_time, m_id, pageid))
  306. msg = "{0}: modify: {1} / {2} / {3} -> {4} / {5} / {6}"
  307. msg = msg.format(pageid, result["page_modify_user"],
  308. result["page_modify_time"],
  309. result["page_modify_oldid"], m_user, m_time, m_id)
  310. self.logger.debug(msg)
  311. def update_page_status(self, cursor, result, pageid, status, chart, page):
  312. """Update the status and "specialed" information of a page."""
  313. query1 = """UPDATE page JOIN row ON page_id = row_id
  314. SET page_status = ?, row_chart = ? WHERE page_id = ?"""
  315. query2 = """UPDATE page SET page_special_user = ?,
  316. page_special_time = ?, page_special_oldid = ?
  317. WHERE page_id = ?"""
  318. cursor.execute(query1, (status, chart, pageid))
  319. msg = "{0}: status: {1} ({2}) -> {3} ({4})"
  320. self.logger.debug(msg.format(pageid, result["page_status"],
  321. result["row_chart"], status, chart))
  322. s_user, s_time, s_id = self.get_special(pageid, chart)
  323. if s_id != result["page_special_oldid"]:
  324. cursor.execute(query2, (s_user, s_time, s_id, pageid))
  325. msg = "{0}: special: {1} / {2} / {3} -> {4} / {5} / {6}"
  326. msg = msg.format(pageid, result["page_special_user"],
  327. result["page_special_time"],
  328. result["page_special_oldid"], m_user, m_time, m_id)
  329. self.logger.debug(msg)
  330. def update_page_notes(self, cursor, result, pageid, notes):
  331. """Update the notes (or warnings) of a page in our database."""
  332. query = "UPDATE page SET page_notes = ? WHERE page_id = ?"
  333. cursor.execute(query, (notes, pageid))
  334. msg = "{0}: notes: {1} -> {2}"
  335. self.logger.debug(msg.format(pageid, result["page_notes"], notes))
  336. def get_content(self, title):
  337. """Get the current content of a page by title from SQL.
  338. The page's current revision ID is retrieved from SQL, and then
  339. site.get_revid_content() is called.
  340. The reason a more conventional method (i.e. site.get_page.get()) is
  341. avoided is that due to replication lag, a discrepancy between the live
  342. database (which the API uses) and the replicated database (which SQL
  343. uses) can lead to incorrect and very confusing data, such as missing
  344. pages that are supposed to exist, if both are used interchangeably.
  345. """
  346. query = "SELECT page_latest FROM page WHERE page_title = ? AND page_namespace = ?"
  347. namespace, base = title.split(":", 1)
  348. try:
  349. ns = self.site.namespace_name_to_id(namespace)
  350. except wiki.NamespaceNotFoundError:
  351. base = title
  352. ns = wiki.NS_MAIN
  353. result = self.site.sql_query(query, (base, ns))
  354. revid = list(result)[0]
  355. return self.site.get_revid_content(revid)
  356. def get_status_and_chart(self, content):
  357. """Determine the status and chart number of an AFC submission.
  358. The methodology used here is the same one I've been using for years
  359. (see also commands.afc_report), but with the new draft system taken
  360. into account. The order here is important: if there is more than one
  361. {{AFC submission}} template on a page, we need to know which one to
  362. use (revision history search to find the most recent isn't a viable
  363. idea :P).
  364. """
  365. if re.search("\{\{afc submission\|r\|(.*?)\}\}", content, re.I):
  366. return "review", CHART_REVIEW
  367. elif re.search("\{\{afc submission\|h\|(.*?)\}\}", content, re.I):
  368. return "pend", CHART_DRAFT
  369. elif re.search("\{\{afc submission\|\|(.*?)\}\}", content, re.I):
  370. return "pend", CHART_PEND
  371. elif re.search("\{\{afc submission\|t\|(.*?)\}\}", content, re.I):
  372. return None, CHART_NONE
  373. elif re.search("\{\{afc submission\|d\|(.*?)\}\}", content, re.I):
  374. return "decline", CHART_DECLINE
  375. return None, CHART_NONE
  376. def get_short_title(self, title):
  377. """Shorten a title so we can display it in a chart using less space.
  378. Basically, this just means removing the "Wikipedia talk:Articles for
  379. creation" part from the beginning. If it is longer than 50 characters,
  380. we'll shorten it down to 47 and add an poor-man's ellipsis at the end.
  381. """
  382. short = re.sub("Wikipedia(\s*talk)?\:Articles\sfor\screation\/", "", title)
  383. if len(short) > 50:
  384. short = "".join((short[:47], "..."))
  385. return short
  386. def get_create(self, pageid):
  387. """Return information about a page's first edit ("creation").
  388. This consists of the page creator, creation time, and the earliest
  389. revision ID.
  390. """
  391. query = """SELECT rev_user_text, rev_timestamp, rev_id
  392. FROM revision WHERE rev_id =
  393. (SELECT MIN(rev_id) FROM revision WHERE rev_page = ?)"""
  394. result = self.site.sql_query(query, (pageid,))
  395. c_user, c_time, c_id = list(result)[0]
  396. return c_user, datetime.strptime(c_time, "%Y%m%d%H%M%S"), c_id
  397. def get_modify(self, pageid):
  398. """Return information about a page's last edit ("modification").
  399. This consists of the most recent editor, modification time, and the
  400. lastest revision ID.
  401. """
  402. query = """SELECT rev_user_text, rev_timestamp, rev_id FROM revision
  403. JOIN page ON rev_id = page_latest WHERE page_id = ?"""
  404. result = self.site.sql_query(query, (pageid,))
  405. m_user, m_time, m_id = list(result)[0]
  406. return m_user, datetime.strptime(m_time, "%Y%m%d%H%M%S"), m_id
  407. def get_special(self, pageid, chart):
  408. """Return information about a page's "special" edit.
  409. I tend to use the term "special" as a verb a lot, which is bound to
  410. cause confusion. It is merely a short way of saying "the edit in which
  411. a declined submission was declined, an accepted submission was
  412. accepted, a submission in review was set as such, and a pending draft
  413. was submitted."
  414. This "information" consists of the special edit's editor, its time, and
  415. its revision ID. If the page's status is not something that involves
  416. "special"-ing, we will return None for all three. The same will be
  417. returned if we cannot determine when the page was "special"-ed, or if
  418. it was "special"-ed more than 100 edits ago.
  419. """
  420. if chart in [CHART_NONE, CHART_PEND]:
  421. return None, None, None
  422. elif chart == CHART_ACCEPT:
  423. return self.get_create(pageid)
  424. elif chart == CHART_DRAFT:
  425. search = "(?!\{\{afc submission\|h\|(.*?)\}\})"
  426. elif chart == CHART_REVIEW:
  427. search = "(?!\{\{afc submission\|r\|(.*?)\}\})"
  428. elif chart == CHART_DECLINE:
  429. search = "(?!\{\{afc submission\|d\|(.*?)\}\})"
  430. query = """SELECT rev_user_text, rev_timestamp, rev_id
  431. FROM revision WHERE rev_page = ? ORDER BY rev_id DESC"""
  432. result = self.site.sql_query(query, (pageid,))
  433. counter = 0
  434. for user, ts, revid in result:
  435. counter += 1
  436. if counter > 100:
  437. break
  438. content = self.site.get_revid_content(revid)
  439. if re.search(search, content, re.I):
  440. return user, datetime.strptime(ts, "%Y%m%d%H%M%S"), revid
  441. return None, None, None
  442. def get_notes(self, pageid):
  443. """Return any special notes or warnings about this page.
  444. Currently unimplemented, so always returns None.
  445. """
  446. return None