A tool that evaluates high-risk Wikipedia templates https://tools.wmflabs.org/earwig-dev/tif
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.
 
 
 
 

63 lines
1.7 KiB

  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. from os.path import expanduser
  4. from earwigbot.bot import Bot
  5. from oursql import connect
  6. __all__ = ["calculate_tif"]
  7. def _get_db(bot):
  8. args = bot.config.wiki["_tifSQL"]
  9. args["read_default_file"] = expanduser("~/.my.cnf")
  10. args["autoping"] = True
  11. args["autoreconnect"] = True
  12. return connect(**args)
  13. def _get_transclusions(page):
  14. # TODO
  15. yield page
  16. def _get_view_average(page, db, cache_info):
  17. # TODO
  18. return 0.0
  19. def _format_time(cache_time):
  20. formatter = lambda n, w: "{0} {1}{2}".format(n, w, "" if n == 1 else "s")
  21. diff = datetime.utcnow() - cache_time
  22. if diff.seconds > 3600:
  23. return formatter(diff.seconds / 3600, "hour")
  24. if diff.seconds > 60:
  25. return formatter(diff.seconds / 60, "minute")
  26. return formatter(diff.seconds, "second")
  27. def calculate_tif(title):
  28. bot = Bot(".earwigbot")
  29. db = _get_db(bot)
  30. site = bot.wiki.get_site()
  31. template = site.get_page(title)
  32. result = {"title": title, "page": template}
  33. if template.exists != template.PAGE_EXISTS:
  34. result["error"] = "no page"
  35. return result
  36. tif = 0.0
  37. transclusions = 0
  38. cache_info = {"cache": False, "cache_time_raw": None}
  39. for page in _get_transclusions(template):
  40. tif += _get_view_average(page, db, cache_info)
  41. transclusions += 1
  42. if cache_info["cache"]:
  43. ctime = cache_info["cache_time"]
  44. cache_info["cache_time"] = ctime.strftime("%b %d, %Y %H:%M:%S UTC")
  45. cache_info["cache_ago"] = _format_time(ctime)
  46. result["tif"] = tif
  47. result["transclusions"] = transclusions
  48. result["protection"] = template.protection
  49. result.update(cache_info)
  50. return result