A copyright violation detector running on Wikimedia Cloud Services https://tools.wmflabs.org/copyvios/
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.
 
 
 
 
 

113 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. from time import time
  3. from urlparse import urlparse
  4. from earwigbot import exceptions
  5. from .misc import open_sql_connection
  6. def get_site(bot, lang, project, name, all_projects):
  7. if project not in [proj[0] for proj in all_projects]:
  8. return None
  9. if project == "wikimedia" and name: # Special sites:
  10. try:
  11. return bot.wiki.get_site(name=name)
  12. except exceptions.SiteNotFoundError:
  13. try:
  14. return bot.wiki.add_site(lang=lang, project=project)
  15. except (exceptions.APIError, exceptions.LoginError):
  16. return None
  17. try:
  18. return bot.wiki.get_site(lang=lang, project=project)
  19. except exceptions.SiteNotFoundError:
  20. try:
  21. return bot.wiki.add_site(lang=lang, project=project)
  22. except (exceptions.APIError, exceptions.LoginError):
  23. return None
  24. def get_sites(bot):
  25. max_staleness = 60 * 60 * 24 * 7
  26. conn = open_sql_connection(bot, "globals")
  27. query1 = "SELECT update_time FROM updates WHERE update_service = ?"
  28. query2 = "SELECT lang_code, lang_name FROM language"
  29. query3 = "SELECT project_code, project_name FROM project"
  30. with conn.cursor() as cursor:
  31. cursor.execute(query1, ("sites",))
  32. try:
  33. time_since_update = int(time() - cursor.fetchall()[0][0])
  34. except IndexError:
  35. time_since_update = time()
  36. if time_since_update > max_staleness:
  37. _update_sites(bot.wiki.get_site(), cursor)
  38. cursor.execute(query2)
  39. langs = []
  40. for code, name in cursor.fetchall():
  41. if "\U" in name:
  42. name = name.decode("unicode_escape")
  43. langs.append((code, name))
  44. cursor.execute(query3)
  45. projects = cursor.fetchall()
  46. return langs, projects
  47. def _update_sites(site, cursor):
  48. matrix = site.api_query(action="sitematrix")["sitematrix"]
  49. del matrix["count"]
  50. languages, projects = set(), set()
  51. for site in matrix.itervalues():
  52. if isinstance(site, list): # Special sites
  53. bad_sites = ["closed", "private", "fishbowl"]
  54. for special in site:
  55. if all([key not in special for key in bad_sites]):
  56. full = urlparse(special["url"]).netloc
  57. if full.count(".") == 1: # No subdomain, so use "www"
  58. lang, project = "www", full.split(".")[0]
  59. else:
  60. lang, project = full.rsplit(".", 2)[:2]
  61. code = u"{0}::{1}".format(lang, special["dbname"])
  62. name = special["code"].capitalize()
  63. languages.add((code, u"{0} ({1})".format(lang, name)))
  64. projects.add((project, project.capitalize()))
  65. continue
  66. this = set()
  67. for web in site["site"]:
  68. if "closed" in web:
  69. continue
  70. project = "wikipedia" if web["code"] == u"wiki" else web["code"]
  71. this.add((project, project.capitalize()))
  72. if this:
  73. code = site["code"]
  74. if "\U" in site["name"].encode("unicode_escape"):
  75. name = site["name"].encode("unicode_escape")
  76. else:
  77. name = site["name"]
  78. languages.add((code, u"{0} ({1})".format(code, name)))
  79. projects |= this
  80. _save_site_updates(cursor, languages, projects)
  81. def _save_site_updates(cursor, languages, projects):
  82. query1 = "SELECT lang_code, lang_name FROM language"
  83. query2 = "DELETE FROM language WHERE lang_code = ? AND lang_name = ?"
  84. query3 = "INSERT INTO language VALUES (?, ?)"
  85. query4 = "SELECT project_code, project_name FROM project"
  86. query5 = "DELETE FROM project WHERE project_code = ? AND project_name = ?"
  87. query6 = "INSERT INTO project VALUES (?, ?)"
  88. query7 = "SELECT 1 FROM updates WHERE update_service = ?"
  89. query8 = "UPDATE updates SET update_time = ? WHERE update_service = ?"
  90. query9 = "INSERT INTO updates VALUES (?, ?)"
  91. _synchronize_sites_with_db(cursor, languages, query1, query2, query3)
  92. _synchronize_sites_with_db(cursor, projects, query4, query5, query6)
  93. cursor.execute(query7, ("sites",))
  94. if cursor.fetchall():
  95. cursor.execute(query8, (time(), "sites"))
  96. else:
  97. cursor.execute(query9, ("sites", time()))
  98. def _synchronize_sites_with_db(cursor, updates, q_list, q_rmv, q_update):
  99. removals = []
  100. cursor.execute(q_list)
  101. for site in cursor:
  102. updates.remove(site) if site in updates else removals.append(site)
  103. cursor.executemany(q_rmv, removals)
  104. cursor.executemany(q_update, updates)