2019-02-15 10:47:50 +00:00
|
|
|
from collections import deque
|
|
|
|
|
|
|
|
from cfg import config
|
2019-02-19 13:16:22 +00:00
|
|
|
from db_util import get_page_id
|
2019-02-02 15:07:19 +00:00
|
|
|
|
|
|
|
def prepare_dijkstra(connection):
|
|
|
|
cursor = connection.cursor()
|
2019-02-21 16:14:17 +00:00
|
|
|
if(config["use_sqlite"]):
|
|
|
|
cursor.execute('''INSERT OR IGNORE INTO dijkstra_helper(page)
|
|
|
|
SELECT rowid FROM pages
|
|
|
|
''')
|
|
|
|
else:
|
|
|
|
cursor.execute('''INSERT IGNORE INTO dijkstra_helper(page)
|
|
|
|
SELECT page_id FROM pages
|
|
|
|
''')
|
|
|
|
|
2019-02-02 15:07:19 +00:00
|
|
|
|
2019-02-15 10:47:50 +00:00
|
|
|
if(config["use_sqlite"]):
|
|
|
|
cursor.execute("UPDATE dijkstra_helper SET value=1e1000")
|
|
|
|
else:
|
|
|
|
cursor.execute("UPDATE dijkstra_helper SET value=2147483647")
|
2019-02-02 15:07:19 +00:00
|
|
|
connection.commit()
|
|
|
|
|
2019-02-19 13:16:22 +00:00
|
|
|
def dijkstra_one(page, value, connection):
|
2019-02-02 15:07:19 +00:00
|
|
|
cursor = connection.cursor()
|
2019-02-21 16:14:17 +00:00
|
|
|
if(isinstance(page, tuple)):
|
2019-02-02 15:07:19 +00:00
|
|
|
# Idk why this happens.
|
|
|
|
title = title[0]
|
2019-02-19 13:16:22 +00:00
|
|
|
cursor.execute('''SELECT page
|
2019-02-02 15:07:19 +00:00
|
|
|
FROM dijkstra_helper
|
2019-02-19 13:16:22 +00:00
|
|
|
LEFT JOIN links ON links.destination=dijkstra_helper.page
|
2019-02-21 16:14:17 +00:00
|
|
|
WHERE links.source=%s
|
|
|
|
AND dijkstra_helper.value>%s''', (page, value + 1))
|
2019-02-02 15:07:19 +00:00
|
|
|
# This is the list of nodes that have to be updated
|
|
|
|
result = cursor.fetchall()
|
|
|
|
|
|
|
|
cursor.execute('''UPDATE dijkstra_helper
|
2019-02-21 16:14:17 +00:00
|
|
|
SET value=%s
|
|
|
|
WHERE page IN (
|
2019-02-02 15:07:19 +00:00
|
|
|
SELECT destination
|
|
|
|
FROM links
|
2019-02-21 16:14:17 +00:00
|
|
|
WHERE source=%s)
|
|
|
|
AND dijkstra_helper.value>%s''', (value + 1, page, value + 1))
|
2019-02-02 15:07:19 +00:00
|
|
|
connection.commit()
|
|
|
|
return result
|
|
|
|
|
|
|
|
def recursive_dijkstra(titles, value, connection):
|
|
|
|
if(not titles):
|
|
|
|
return
|
|
|
|
|
2019-02-15 10:47:50 +00:00
|
|
|
todos = deque()
|
2019-02-02 15:07:19 +00:00
|
|
|
for title in titles:
|
2019-02-15 10:47:50 +00:00
|
|
|
todos.extend(dijkstra_one(title, value, connection))
|
|
|
|
|
|
|
|
recursive_dijkstra(todos, value + 1, connection)
|
2019-02-02 15:07:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dijkstra(title, connection):
|
2019-02-19 13:16:22 +00:00
|
|
|
page = get_page_id(title, connection)
|
2019-02-02 15:07:19 +00:00
|
|
|
cursor = connection.cursor()
|
2019-02-21 16:14:17 +00:00
|
|
|
cursor.execute("UPDATE dijkstra_helper SET value=0 WHERE page=%s", (page,))
|
2019-02-02 15:07:19 +00:00
|
|
|
|
2019-02-19 13:16:22 +00:00
|
|
|
todos = dijkstra_one(page, 1, connection)
|
2019-02-02 15:07:19 +00:00
|
|
|
recursive_dijkstra(todos, 2, connection)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|