diff --git a/README.md b/README.md index da9ed3f..9567182 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ Running - Install all dependencies listed above. -- Create an SQL database ... +- Create an SQL database based on `schema.sql` on the same server as your + MediaWiki database. - Create an earwigbot instance in `.earwigbot` (run `earwigbot .earwigbot`). In `.earwigbot/config.yml`, fill out the connection info for the database by diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..99b5d0d --- /dev/null +++ b/schema.sql @@ -0,0 +1,9 @@ +CREATE DATABASE `sXXXXX__tif`; + +DROP TABLE IF EXISTS `cache`; +CREATE TABLE `cache` ( + `cache_id` INT(10) UNSIGNED NOT NULL, + `cache_views` INT UNSIGNED NOT NULL, + `cache_time` TIMESTAMP NOT NULL, + PRIMARY KEY (`cache_id`) +) ENGINE=InnoDB; diff --git a/tif/calc.py b/tif/calc.py index f23d691..cc47dfb 100644 --- a/tif/calc.py +++ b/tif/calc.py @@ -15,13 +15,18 @@ def _get_db(bot): args["autoreconnect"] = True return connect(**args) -def _get_transclusions(page): - # TODO - yield page +def _compute_stats(page, db): + with db.cursor() as cursor: + query = """SELECT COUNT(*) FROM templatelinks WHERE tl_title = ? + AND tl_namespace = 10 AND tl_from_namespace = 0""" + cursor.execute(query, (page.title.replace(" ", "_"),)) + transclusions = cursor.fetchall()[0][0] -def _get_view_average(page, db, cache_info): - # TODO - return 0.0 + # TODO + tif = 0.0 + cache_time = None + + return tif, transclusions, cache_time def _format_time(cache_time): formatter = lambda n, w: "{0} {1}{2}".format(n, w, "" if n == 1 else "s") @@ -36,27 +41,19 @@ def calculate_tif(title): bot = Bot(".earwigbot") db = _get_db(bot) site = bot.wiki.get_site() - template = site.get_page(title) - result = {"title": title, "page": template} + page = site.get_page(title) + result = {"title": title, "page": page} - if template.exists != template.PAGE_EXISTS: + if page.exists != page.PAGE_EXISTS: result["error"] = "no page" return result - tif = 0.0 - transclusions = 0 - cache_info = {"cache": False, "cache_time_raw": None} - for page in _get_transclusions(template): - tif += _get_view_average(page, db, cache_info) - transclusions += 1 - - if cache_info["cache"]: - ctime = cache_info["cache_time"] - cache_info["cache_time"] = ctime.strftime("%b %d, %Y %H:%M:%S UTC") - cache_info["cache_ago"] = _format_time(ctime) + tif, transclusions, cache_time = _compute_stats(page, db) result["tif"] = tif result["transclusions"] = transclusions - result["protection"] = template.protection - result.update(cache_info) + result["protection"] = page.protection + if cache_time: + result["cache_time"] = cache_time.strftime("%b %d, %Y %H:%M:%S UTC") + result["cache_ago"] = _format_time(cache_time) return result