]> git.ipfire.org Git - ipfire.org.git/commitdiff
wiki: Render HTML for faster pages
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 6 Dec 2018 11:25:47 +0000 (11:25 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 6 Dec 2018 11:25:47 +0000 (11:25 +0000)
Also adds a command to rebake all pages

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/base.py
src/backend/wiki.py

index c1088282e6e6a9025686b0a02f14ebb5f574201d..70182ca6dc003f992c176ba045704b5756e62dbf 100644 (file)
@@ -89,6 +89,7 @@ class Backend(object):
        def run_task(self, task, *args, **kwargs):
                tasks = {
                        "cleanup-messages"  : self.messages.queue.cleanup,
+                       "rebake-wiki"       : self.wiki.rebake,
                        "scan-files"        : self.releases.scan_files,
                        "send-all-messages" : self.messages.queue.send_all,
                        "update-blog-feeds" : self.blog.update_feeds,
index 3c8b04a5d02775d84e59911699fb0d3d08855d5a..ab98b9a370424d55829c3a8b8c9aaba75619fea8 100644 (file)
@@ -6,6 +6,7 @@ import io
 import logging
 import os.path
 import re
+import tornado.gen
 import urllib.parse
 
 from . import misc
@@ -65,6 +66,9 @@ class Wiki(misc.Object):
                page = self._get_page("INSERT INTO wiki(page, author_uid, markdown, changes, address) \
                        VALUES(%s, %s, %s, %s, %s) RETURNING *", page, author.uid, content or None, changes, address)
 
+               # Render markdown
+               page.bake()
+
                # Send email to all watchers
                page._send_watcher_emails(excludes=[author])
 
@@ -78,6 +82,19 @@ class Wiki(misc.Object):
                # Just creates a blank last version of the page
                self.create_page(page, author=author, content=None, **kwargs)
 
+       @tornado.gen.coroutine
+       def rebake(self):
+               """
+                       Re-renders all wiki pages
+               """
+               pages = self._get_pages("SELECT * FROM wiki ORDER BY timestamp DESC")
+
+               for page in pages:
+                       logging.info("Baking %s from %s" % (page.page, page.timestamp))
+
+                       with self.db.transaction():
+                               page.bake()
+
        def make_breadcrumbs(self, url):
                # Split and strip all empty elements (double slashes)
                parts = list(e for e in url.split("/") if e)
@@ -319,6 +336,14 @@ class Page(misc.Object):
        def html(self):
                return self.data.html or self._render(self.markdown)
 
+       def bake(self):
+               """
+                       Renders the page's markdown to HTML and stores
+                       it in the database.
+               """
+               self.db.execute("UPDATE wiki SET html = %s WHERE id = %s",
+                       self._render(self.markdown), self.id)
+
        @property
        def timestamp(self):
                return self.data.timestamp