Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

733 rader
30 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 re
  24. from os.path import expanduser
  25. from threading import Lock
  26. from time import sleep
  27. import oursql
  28. from earwigbot import exceptions
  29. from earwigbot import wiki
  30. from earwigbot.tasks import Task
  31. class AFCStatistics(Task):
  32. """A task to generate statistics for WikiProject Articles for Creation.
  33. Statistics are stored in a MySQL database ("u_earwig_afc_statistics")
  34. accessed with oursql. Statistics are synchronied with the live database
  35. every four minutes and saved once an hour, on the hour, to self.pagename.
  36. In the live bot, this is "Template:AFC statistics".
  37. """
  38. name = "afc_statistics"
  39. number = 2
  40. # Chart status number constants:
  41. CHART_NONE = 0
  42. CHART_PEND = 1
  43. CHART_DRAFT = 2
  44. CHART_REVIEW = 3
  45. CHART_ACCEPT = 4
  46. CHART_DECLINE = 5
  47. CHART_MISPLACE = 6
  48. def setup(self):
  49. self.cfg = cfg = self.config.tasks.get(self.name, {})
  50. # Set some wiki-related attributes:
  51. self.pagename = cfg.get("page", "Template:AFC statistics")
  52. self.pending_cat = cfg.get("pending", "Pending AfC submissions")
  53. self.ignore_list = cfg.get("ignoreList", [])
  54. default_summary = "Updating statistics for [[WP:WPAFC|WikiProject Articles for creation]]."
  55. self.summary = self.make_summary(cfg.get("summary", default_summary))
  56. # Templates used in chart generation:
  57. templates = cfg.get("templates", {})
  58. self.tl_header = templates.get("header", "AFC statistics/header")
  59. self.tl_row = templates.get("row", "#invoke:AfC|row")
  60. self.tl_footer = templates.get("footer", "AFC statistics/footer")
  61. # Connection data for our SQL database:
  62. kwargs = cfg.get("sql", {})
  63. kwargs["read_default_file"] = expanduser("~/.my.cnf")
  64. self.conn_data = kwargs
  65. self.db_access_lock = Lock()
  66. def run(self, **kwargs):
  67. """Entry point for a task event.
  68. Depending on the kwargs passed, we will either synchronize our local
  69. statistics database with the site (self.sync()) or save it to the wiki
  70. (self.save()). We will additionally create an SQL connection with our
  71. local database.
  72. """
  73. action = kwargs.get("action")
  74. if not self.db_access_lock.acquire(False): # Non-blocking
  75. if action == "sync":
  76. self.logger.info("A sync is already ongoing; aborting")
  77. return
  78. self.logger.info("Waiting for database access lock")
  79. self.db_access_lock.acquire()
  80. try:
  81. self.site = self.bot.wiki.get_site()
  82. self.conn = oursql.connect(**self.conn_data)
  83. self.revision_cache = {}
  84. try:
  85. if action == "save":
  86. self.save(kwargs)
  87. elif action == "sync":
  88. self.sync(kwargs)
  89. elif action == "update":
  90. self.update(kwargs)
  91. finally:
  92. self.conn.close()
  93. finally:
  94. self.db_access_lock.release()
  95. def save(self, kwargs):
  96. """Save our local statistics to the wiki.
  97. After checking for emergency shutoff, the statistics chart is compiled,
  98. and then saved to self.pagename using self.summary iff it has changed
  99. since last save.
  100. """
  101. self.logger.info("Saving chart")
  102. if kwargs.get("fromIRC"):
  103. summary = self.summary + " (!earwigbot)"
  104. else:
  105. if self.shutoff_enabled():
  106. return
  107. summary = self.summary
  108. statistics = self.compile_charts()
  109. page = self.site.get_page(self.pagename)
  110. text = page.get()
  111. newtext = re.sub(u"<!-- stat begin -->(.*?)<!-- stat end -->",
  112. "<!-- stat begin -->\n" + statistics + "\n<!-- stat end -->",
  113. text, flags=re.DOTALL)
  114. if newtext == text:
  115. self.logger.info("Chart unchanged; not saving")
  116. return # Don't edit the page if we're not adding anything
  117. newtext = re.sub("<!-- sig begin -->(.*?)<!-- sig end -->",
  118. "<!-- sig begin -->~~~ at ~~~~~<!-- sig end -->",
  119. newtext)
  120. page.edit(newtext, summary, minor=True, bot=True)
  121. self.logger.info(u"Chart saved to [[{0}]]".format(page.title))
  122. def compile_charts(self):
  123. """Compile and return all statistics information from our local db."""
  124. stats = ""
  125. with self.conn.cursor() as cursor:
  126. cursor.execute("SELECT * FROM chart")
  127. for chart in cursor:
  128. stats += self.compile_chart(chart) + "\n"
  129. return stats[:-1] # Drop the last newline
  130. def compile_chart(self, chart_info):
  131. """Compile and return a single statistics chart."""
  132. chart_id, chart_title, special_title = chart_info
  133. chart = self.tl_header + "|" + chart_title
  134. if special_title:
  135. chart += "|" + special_title
  136. chart = "{{" + chart + "}}"
  137. query = "SELECT * FROM page JOIN row ON page_id = row_id WHERE row_chart = ?"
  138. with self.conn.cursor(oursql.DictCursor) as cursor:
  139. cursor.execute(query, (chart_id,))
  140. for page in cursor:
  141. chart += "\n" + self.compile_chart_row(page)
  142. chart += "\n{{" + self.tl_footer + "}}"
  143. return chart
  144. def compile_chart_row(self, page):
  145. """Compile and return a single chart row.
  146. 'page' is a dict of page information, taken as a row from the page
  147. table, where keys are column names and values are their cell contents.
  148. """
  149. row = u"{0}|s={page_status}|t={page_title}|z={page_size}|"
  150. if page["page_special_oldid"]:
  151. row += "sr={page_special_user}|sd={page_special_time}|si={page_special_oldid}|"
  152. row += "mr={page_modify_user}|md={page_modify_time}|mi={page_modify_oldid}"
  153. page["page_special_time"] = self.format_time(page["page_special_time"])
  154. page["page_modify_time"] = self.format_time(page["page_modify_time"])
  155. if page["page_notes"]:
  156. row += "|n=1{page_notes}"
  157. return "{{" + row.format(self.tl_row, **page) + "}}"
  158. def format_time(self, dt):
  159. """Format a datetime into the standard MediaWiki timestamp format."""
  160. return dt.strftime("%H:%M, %d %b %Y")
  161. def sync(self, kwargs):
  162. """Synchronize our local statistics database with the site.
  163. Syncing involves, in order, updating tracked submissions that have
  164. been changed since last sync (self.update_tracked()), adding pending
  165. submissions that are not tracked (self.add_untracked()), and removing
  166. old submissions from the database (self.delete_old()).
  167. The sync will be canceled if SQL replication lag is greater than 600
  168. seconds, because this will lead to potential problems and outdated
  169. data, not to mention putting demand on an already overloaded server.
  170. Giving sync the kwarg "ignore_replag" will go around this restriction.
  171. """
  172. self.logger.info("Starting sync")
  173. replag = self.site.get_replag()
  174. self.logger.debug("Server replag is {0}".format(replag))
  175. if replag > 600 and not kwargs.get("ignore_replag"):
  176. msg = "Sync canceled as replag ({0} secs) is greater than ten minutes"
  177. self.logger.warn(msg.format(replag))
  178. return
  179. with self.conn.cursor() as cursor:
  180. self.update_tracked(cursor)
  181. self.add_untracked(cursor)
  182. self.delete_old(cursor)
  183. self.logger.info("Sync completed")
  184. def update_tracked(self, cursor):
  185. """Update tracked submissions that have been changed since last sync.
  186. This is done by iterating through every page in our database and
  187. comparing our stored latest revision ID with the actual latest revision
  188. ID from an SQL query. If they differ, we will update our information
  189. about the page (self.update_page()).
  190. If the page does not exist, we will remove it from our database with
  191. self.untrack_page().
  192. """
  193. self.logger.debug("Updating tracked submissions")
  194. query = """SELECT s.page_id, s.page_title, s.page_modify_oldid,
  195. r.page_latest, r.page_title, r.page_namespace
  196. FROM page AS s
  197. LEFT JOIN {0}_p.page AS r ON s.page_id = r.page_id
  198. WHERE s.page_modify_oldid != r.page_latest"""
  199. cursor.execute(query.format(self.site.name))
  200. for pageid, title, oldid, real_oldid, real_title, real_ns in cursor:
  201. if not real_oldid:
  202. self.untrack_page(cursor, pageid)
  203. continue
  204. msg = u"Updating page [[{0}]] (id: {1}) @ {2}"
  205. self.logger.debug(msg.format(title, pageid, oldid))
  206. self.logger.debug(" {0} -> {1}".format(oldid, real_oldid))
  207. real_title = real_title.decode("utf8").replace("_", " ")
  208. ns = self.site.namespace_id_to_name(real_ns)
  209. if ns:
  210. real_title = u":".join((ns, real_title))
  211. try:
  212. self.update_page(cursor, pageid, real_title)
  213. except Exception:
  214. e = u"Error updating page [[{0}]] (id: {1})"
  215. self.logger.exception(e.format(real_title, pageid))
  216. def add_untracked(self, cursor):
  217. """Add pending submissions that are not yet tracked.
  218. This is done by compiling a list of all currently tracked submissions
  219. and iterating through all members of self.pending_cat via SQL. If a
  220. page in the pending category is not tracked and is not in
  221. self.ignore_list, we will track it with self.track_page().
  222. """
  223. self.logger.debug("Adding untracked pending submissions")
  224. query = """SELECT r.page_id, r.page_title, r.page_namespace
  225. FROM {0}_p.page AS r
  226. INNER JOIN {0}_p.categorylinks AS c ON r.page_id = c.cl_from
  227. LEFT JOIN page AS s ON r.page_id = s.page_id
  228. WHERE s.page_id IS NULL AND c.cl_to = ?"""
  229. cursor.execute(query.format(self.site.name),
  230. (self.pending_cat.replace(" ", "_"),))
  231. for pageid, title, ns in cursor:
  232. title = title.decode("utf8").replace("_", " ")
  233. ns = self.site.namespace_id_to_name(ns)
  234. if ns:
  235. title = u":".join((ns, title))
  236. if title in self.ignore_list:
  237. continue
  238. msg = u"Tracking page [[{0}]] (id: {1})".format(title, pageid)
  239. self.logger.debug(msg)
  240. try:
  241. self.track_page(cursor, pageid, title)
  242. except Exception:
  243. e = u"Error tracking page [[{0}]] (id: {1})"
  244. self.logger.exception(e.format(title, pageid))
  245. def delete_old(self, cursor):
  246. """Remove old submissions from the database.
  247. "Old" is defined as a submission that has been declined or accepted
  248. more than 36 hours ago. Pending submissions cannot be "old".
  249. """
  250. self.logger.debug("Removing old submissions from chart")
  251. query = """DELETE FROM page, row USING page JOIN row
  252. ON page_id = row_id WHERE row_chart IN (?, ?)
  253. AND ADDTIME(page_special_time, '36:00:00') < NOW()"""
  254. cursor.execute(query, (self.CHART_ACCEPT, self.CHART_DECLINE))
  255. def update(self, kwargs):
  256. """Update a page by name, regardless of whether anything has changed.
  257. Mainly intended as a command to be used via IRC, e.g.:
  258. !tasks start afc_statistics action=update page=Foobar
  259. """
  260. title = kwargs.get("page")
  261. if not title:
  262. return
  263. title = title.replace("_", " ").decode("utf8")
  264. query = "SELECT page_id, page_modify_oldid FROM page WHERE page_title = ?"
  265. with self.conn.cursor() as cursor:
  266. cursor.execute(query, (title,))
  267. try:
  268. pageid, oldid = cursor.fetchall()[0]
  269. except IndexError:
  270. msg = u"Page [[{0}]] not found in database".format(title)
  271. self.logger.error(msg)
  272. msg = u"Updating page [[{0}]] (id: {1}) @ {2}"
  273. self.logger.info(msg.format(title, pageid, oldid))
  274. self.update_page(cursor, pageid, title)
  275. def untrack_page(self, cursor, pageid):
  276. """Remove a page, given by ID, from our database."""
  277. self.logger.debug("Untracking page (id: {0})".format(pageid))
  278. query = """DELETE FROM page, row USING page JOIN row
  279. ON page_id = row_id WHERE page_id = ?"""
  280. cursor.execute(query, (pageid,))
  281. def track_page(self, cursor, pageid, title):
  282. """Update hook for when page is not in our database.
  283. A variety of SQL queries are used to gather information about the page,
  284. which is then saved to our database.
  285. """
  286. content = self.get_content(title)
  287. if content is None:
  288. msg = u"Could not get page content for [[{0}]]".format(title)
  289. self.logger.error(msg)
  290. return
  291. namespace = self.site.get_page(title).namespace
  292. status, chart = self.get_status_and_chart(content, namespace)
  293. if chart == self.CHART_NONE:
  294. msg = u"Could not find a status for [[{0}]]".format(title)
  295. self.logger.warn(msg)
  296. return
  297. size = self.get_size(content)
  298. m_user, m_time, m_id = self.get_modify(pageid)
  299. s_user, s_time, s_id = self.get_special(pageid, chart)
  300. notes = self.get_notes(chart, content, m_time, s_user)
  301. query1 = "INSERT INTO row VALUES (?, ?)"
  302. query2 = "INSERT INTO page VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  303. cursor.execute(query1, (pageid, chart))
  304. cursor.execute(query2, (pageid, status, title, size, notes, m_user,
  305. m_time, m_id, s_user, s_time, s_id))
  306. def update_page(self, cursor, pageid, title):
  307. """Update hook for when page is already in our database.
  308. A variety of SQL queries are used to gather information about the page,
  309. which is compared against our stored information. Differing information
  310. is then updated.
  311. """
  312. content = self.get_content(title)
  313. if content is None:
  314. msg = u"Could not get page content for [[{0}]]".format(title)
  315. self.logger.error(msg)
  316. return
  317. namespace = self.site.get_page(title).namespace
  318. status, chart = self.get_status_and_chart(content, namespace)
  319. if chart == self.CHART_NONE:
  320. self.untrack_page(cursor, pageid)
  321. return
  322. query = "SELECT * FROM page JOIN row ON page_id = row_id WHERE page_id = ?"
  323. with self.conn.cursor(oursql.DictCursor) as dict_cursor:
  324. dict_cursor.execute(query, (pageid,))
  325. result = dict_cursor.fetchall()[0]
  326. size = self.get_size(content)
  327. m_user, m_time, m_id = self.get_modify(pageid)
  328. if title != result["page_title"]:
  329. self.update_page_title(cursor, result, pageid, title)
  330. if m_id != result["page_modify_oldid"]:
  331. self.update_page_modify(cursor, result, pageid, size, m_user,
  332. m_time, m_id)
  333. if status != result["page_status"]:
  334. special = self.update_page_status(cursor, result, pageid, status,
  335. chart)
  336. s_user = special[0]
  337. else:
  338. s_user = result["page_special_user"]
  339. notes = self.get_notes(chart, content, m_time, s_user)
  340. if notes != result["page_notes"]:
  341. self.update_page_notes(cursor, result, pageid, notes)
  342. def update_page_title(self, cursor, result, pageid, title):
  343. """Update the title of a page in our database."""
  344. query = "UPDATE page SET page_title = ? WHERE page_id = ?"
  345. cursor.execute(query, (title, pageid))
  346. msg = u" {0}: title: {1} -> {2}"
  347. self.logger.debug(msg.format(pageid, result["page_title"], title))
  348. def update_page_modify(self, cursor, result, pageid, size, m_user, m_time, m_id):
  349. """Update the last modified information of a page in our database."""
  350. query = """UPDATE page SET page_size = ?, page_modify_user = ?,
  351. page_modify_time = ?, page_modify_oldid = ?
  352. WHERE page_id = ?"""
  353. cursor.execute(query, (size, m_user, m_time, m_id, pageid))
  354. msg = u" {0}: modify: {1} / {2} / {3} -> {4} / {5} / {6}"
  355. msg = msg.format(pageid, result["page_modify_user"],
  356. result["page_modify_time"],
  357. result["page_modify_oldid"], m_user, m_time, m_id)
  358. self.logger.debug(msg)
  359. def update_page_status(self, cursor, result, pageid, status, chart):
  360. """Update the status and "specialed" information of a page."""
  361. query1 = """UPDATE page JOIN row ON page_id = row_id
  362. SET page_status = ?, row_chart = ? WHERE page_id = ?"""
  363. query2 = """UPDATE page SET page_special_user = ?,
  364. page_special_time = ?, page_special_oldid = ?
  365. WHERE page_id = ?"""
  366. cursor.execute(query1, (status, chart, pageid))
  367. msg = " {0}: status: {1} ({2}) -> {3} ({4})"
  368. self.logger.debug(msg.format(pageid, result["page_status"],
  369. result["row_chart"], status, chart))
  370. s_user, s_time, s_id = self.get_special(pageid, chart)
  371. if s_id != result["page_special_oldid"]:
  372. cursor.execute(query2, (s_user, s_time, s_id, pageid))
  373. msg = u" {0}: special: {1} / {2} / {3} -> {4} / {5} / {6}"
  374. msg = msg.format(pageid, result["page_special_user"],
  375. result["page_special_time"],
  376. result["page_special_oldid"], s_user, s_time, s_id)
  377. self.logger.debug(msg)
  378. return s_user, s_time, s_id
  379. def update_page_notes(self, cursor, result, pageid, notes):
  380. """Update the notes (or warnings) of a page in our database."""
  381. query = "UPDATE page SET page_notes = ? WHERE page_id = ?"
  382. cursor.execute(query, (notes, pageid))
  383. msg = " {0}: notes: {1} -> {2}"
  384. self.logger.debug(msg.format(pageid, result["page_notes"], notes))
  385. def get_content(self, title):
  386. """Get the current content of a page by title from the API.
  387. The page's current revision ID is retrieved from SQL, and then
  388. an API query is made to get its content. This is the only API query
  389. used in the task's code.
  390. """
  391. query = "SELECT page_latest FROM page WHERE page_title = ? AND page_namespace = ?"
  392. try:
  393. namespace, base = title.split(":", 1)
  394. except ValueError:
  395. base = title
  396. ns = wiki.NS_MAIN
  397. else:
  398. try:
  399. ns = self.site.namespace_name_to_id(namespace)
  400. except exceptions.NamespaceNotFoundError:
  401. base = title
  402. ns = wiki.NS_MAIN
  403. result = self.site.sql_query(query, (base.replace(" ", "_"), ns))
  404. try:
  405. revid = int(list(result)[0][0])
  406. except IndexError:
  407. return None
  408. return self.get_revision_content(revid)
  409. def get_revision_content(self, revid, tries=1):
  410. """Get the content of a revision by ID from the API."""
  411. if revid in self.revision_cache:
  412. return self.revision_cache[revid]
  413. res = self.site.api_query(action="query", prop="revisions",
  414. revids=revid, rvprop="content")
  415. try:
  416. content = res["query"]["pages"].values()[0]["revisions"][0]["*"]
  417. except KeyError:
  418. if tries > 0:
  419. sleep(5)
  420. return self.get_revision_content(revid, tries=tries - 1)
  421. self.revision_cache[revid] = content
  422. return content
  423. def get_status_and_chart(self, content, namespace):
  424. """Determine the status and chart number of an AFC submission.
  425. The methodology used here is the same one I've been using for years
  426. (see also commands.afc_report), but with the new draft system taken
  427. into account. The order here is important: if there is more than one
  428. {{AFC submission}} template on a page, we need to know which one to
  429. use (revision history search to find the most recent isn't a viable
  430. idea :P).
  431. """
  432. statuses = self.get_statuses(content)
  433. if "R" in statuses:
  434. status, chart = "r", self.CHART_REVIEW
  435. elif "H" in statuses:
  436. status, chart = "p", self.CHART_DRAFT
  437. elif "P" in statuses:
  438. status, chart = "p", self.CHART_PEND
  439. elif "T" in statuses:
  440. status, chart = None, self.CHART_NONE
  441. elif "D" in statuses:
  442. status, chart = "d", self.CHART_DECLINE
  443. else:
  444. status, chart = None, self.CHART_NONE
  445. if namespace == wiki.NS_MAIN:
  446. if not statuses:
  447. status, chart = "a", self.CHART_ACCEPT
  448. else:
  449. status, chart = None, self.CHART_MISPLACE
  450. return status, chart
  451. def get_statuses(self, content):
  452. """Return a list of all AFC submission statuses in a page's text."""
  453. re_has_templates = "\{\{[aA][fF][cC] submission\s*(\}\}|\||/)"
  454. re_template = "\{\{[aA][fF][cC] submission\s*(.*?)\}\}"
  455. re_remove_embed = "(\{\{[aA][fF][cC] submission\s*(.*?))\{\{(.*?)\}\}(.*?)\}\}"
  456. valid = ["R", "H", "P", "T", "D"]
  457. subtemps = {
  458. "/reviewing": "R",
  459. "/onhold": "H",
  460. "/pending": "P",
  461. "/draft": "T",
  462. "/declined": "D"
  463. }
  464. statuses = []
  465. while re.search(re_has_templates, content):
  466. status = "P"
  467. match = re.search(re_template, content, re.S)
  468. if not match:
  469. return statuses
  470. temp = match.group(1)
  471. limit = 0
  472. while "{{" in temp and limit < 50:
  473. content = re.sub(re_remove_embed, "\\1\\4}}", content, 1, re.S)
  474. match = re.search(re_template, content, re.S)
  475. temp = match.group(1)
  476. limit += 1
  477. params = temp.split("|")
  478. try:
  479. subtemp, params = params[0].strip(), params[1:]
  480. except IndexError:
  481. status = "P"
  482. params = []
  483. else:
  484. if subtemp:
  485. status = subtemps.get(subtemp)
  486. params = []
  487. for param in params:
  488. param = param.strip().upper()
  489. if "=" in param:
  490. key, value = param.split("=", 1)
  491. if key.strip() == "1":
  492. status = value if value in valid else "P"
  493. break
  494. else:
  495. status = param if param in valid else "P"
  496. break
  497. statuses.append(status)
  498. content = re.sub(re_template, "", content, 1, re.S)
  499. return statuses
  500. def get_size(self, content):
  501. """Return a page's size in a short, pretty format."""
  502. return "{0} kB".format(round(len(content) / 1000.0, 1))
  503. def get_modify(self, pageid):
  504. """Return information about a page's last edit ("modification").
  505. This consists of the most recent editor, modification time, and the
  506. lastest revision ID.
  507. """
  508. query = """SELECT rev_user_text, rev_timestamp, rev_id FROM revision
  509. JOIN page ON rev_id = page_latest WHERE page_id = ?"""
  510. result = self.site.sql_query(query, (pageid,))
  511. m_user, m_time, m_id = list(result)[0]
  512. timestamp = datetime.strptime(m_time, "%Y%m%d%H%M%S")
  513. return m_user.decode("utf8"), timestamp, m_id
  514. def get_special(self, pageid, chart):
  515. """Return information about a page's "special" edit.
  516. I tend to use the term "special" as a verb a lot, which is bound to
  517. cause confusion. It is merely a short way of saying "the edit in which
  518. a declined submission was declined, an accepted submission was
  519. accepted, a submission in review was set as such, a pending submission
  520. was submitted, and a "misplaced" submission was created."
  521. This "information" consists of the special edit's editor, its time, and
  522. its revision ID. If the page's status is not something that involves
  523. "special"-ing, we will return None for all three. The same will be
  524. returned if we cannot determine when the page was "special"-ed, or if
  525. it was "special"-ed more than 100 edits ago.
  526. """
  527. if chart == self.CHART_NONE:
  528. return None, None, None
  529. elif chart == self.CHART_MISPLACE:
  530. return self.get_create(pageid)
  531. elif chart == self.CHART_ACCEPT:
  532. search_for = None
  533. search_not = ["R", "H", "P", "T", "D"]
  534. elif chart == self.CHART_DRAFT:
  535. search_for = "H"
  536. search_not = []
  537. elif chart == self.CHART_PEND:
  538. search_for = "P"
  539. search_not = []
  540. elif chart == self.CHART_REVIEW:
  541. search_for = "R"
  542. search_not = []
  543. elif chart == self.CHART_DECLINE:
  544. search_for = "D"
  545. search_not = ["R", "H", "P", "T"]
  546. query = """SELECT rev_user_text, rev_timestamp, rev_id
  547. FROM revision WHERE rev_page = ? ORDER BY rev_id DESC"""
  548. result = self.site.sql_query(query, (pageid,))
  549. counter = 0
  550. last = (None, None, None)
  551. for user, ts, revid in result:
  552. counter += 1
  553. if counter > 50:
  554. msg = "Exceeded 50 content lookups while determining special for page (id: {0}, chart: {1})"
  555. self.logger.warn(msg.format(pageid, chart))
  556. return None, None, None
  557. try:
  558. content = self.get_revision_content(revid)
  559. except exceptions.APIError:
  560. msg = "API error interrupted SQL query in get_special() for page (id: {0}, chart: {1})"
  561. self.logger.exception(msg.format(pageid, chart))
  562. return None, None, None
  563. statuses = self.get_statuses(content)
  564. matches = [s in statuses for s in search_not]
  565. if search_for:
  566. if search_for not in statuses or any(matches):
  567. return last
  568. else:
  569. if any(matches):
  570. return last
  571. timestamp = datetime.strptime(ts, "%Y%m%d%H%M%S")
  572. last = (user.decode("utf8"), timestamp, revid)
  573. return last
  574. def get_create(self, pageid):
  575. """Return information about a page's first edit ("creation").
  576. This consists of the page creator, creation time, and the earliest
  577. revision ID.
  578. """
  579. query = """SELECT rev_user_text, rev_timestamp, rev_id
  580. FROM revision WHERE rev_id =
  581. (SELECT MIN(rev_id) FROM revision WHERE rev_page = ?)"""
  582. result = self.site.sql_query(query, (pageid,))
  583. c_user, c_time, c_id = list(result)[0]
  584. timestamp = datetime.strptime(c_time, "%Y%m%d%H%M%S")
  585. return c_user.decode("utf8"), timestamp, c_id
  586. def get_notes(self, chart, content, m_time, s_user):
  587. """Return any special notes or warnings about this page.
  588. copyvio: submission is a suspected copyright violation
  589. unsourced: submission lacks references completely
  590. no-inline: submission has no inline citations
  591. short: submission is less than a kilobyte in length
  592. resubmit: submission was resubmitted after a previous decline
  593. old: submission has not been touched in > 4 days
  594. blocked: submitter is currently blocked
  595. """
  596. notes = ""
  597. ignored_charts = [self.CHART_NONE, self.CHART_ACCEPT, self.CHART_DECLINE]
  598. if chart in ignored_charts:
  599. return notes
  600. copyvios = self.config.tasks.get("afc_copyvios", {})
  601. regex = "\{\{\s*" + copyvios.get("template", "AfC suspected copyvio")
  602. if re.search(regex, content):
  603. notes += "|nc=1" # Submission is a suspected copyvio
  604. if not re.search("\<ref\s*(.*?)\>(.*?)\</ref\>", content, re.I | re.S):
  605. regex = "(https?:)|\[//(?!{0})([^ \]\\t\\n\\r\\f\\v]+?)"
  606. sitedomain = re.escape(self.site.domain)
  607. if re.search(regex.format(sitedomain), content, re.I | re.S):
  608. notes += "|ni=1" # Submission has no inline citations
  609. else:
  610. notes += "|nu=1" # Submission is completely unsourced
  611. if len(content) < 1000:
  612. notes += "|ns=1" # Submission is short
  613. statuses = self.get_statuses(content)
  614. if "D" in statuses and chart != self.CHART_MISPLACE:
  615. notes += "|nr=1" # Submission was resubmitted
  616. time_since_modify = (datetime.utcnow() - m_time).total_seconds()
  617. max_time = 4 * 24 * 60 * 60
  618. if time_since_modify > max_time:
  619. notes += "|no=1" # Submission hasn't been touched in over 4 days
  620. if chart in [self.CHART_PEND, self.CHART_DRAFT] and s_user:
  621. submitter = self.site.get_user(s_user)
  622. try:
  623. if submitter.blockinfo:
  624. notes += "|nb=1" # Submitter is blocked
  625. except exceptions.UserNotFoundError: # Likely an IP
  626. pass
  627. return notes