changeset 41:ef9cf617d76f

Correctly handle caching for included pages.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 07 Jan 2013 17:20:50 -0800
parents 81333391792d
children f6721f283a56
files wikked/cache.py wikked/wiki.py
diffstat 2 files changed, 38 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/wikked/cache.py	Mon Jan 07 15:31:17 2013 -0800
+++ b/wikked/cache.py	Mon Jan 07 17:20:50 2013 -0800
@@ -11,6 +11,10 @@
     def __init__(self, root):
         self.cache_dir = root
 
+    def isValid(self, url, time):
+        path, valid = self._getCachePathAndValidity(url, time)
+        return valid
+
     def read(self, url, time):
         path, valid = self._getCachePathAndValidity(url, time)
         if valid:
@@ -35,7 +39,7 @@
         if not os.path.isfile(path):
             return None
         return os.path.getmtime(path)
-        
+
     def _getCachePath(self, url):
         return os.path.join(self.cache_dir, url)
 
--- a/wikked/wiki.py	Mon Jan 07 15:31:17 2013 -0800
+++ b/wikked/wiki.py	Mon Jan 07 17:20:50 2013 -0800
@@ -27,6 +27,7 @@
         self.url = url
         self.ext = ext
         self.out_links = []
+        self.included_pages = []
         self.meta = {}
 
     @property
@@ -70,7 +71,10 @@
                 ctx.meta[meta_name] = True
             if meta_name == 'include':
                 # TODO: handle self-includes or cyclic includes.
-                included_page = self.wiki.getPage(meta_value)
+                abs_included_url = Page.get_absolute_url(ctx.urldir, meta_value)
+                abs_included_url = Page.title_to_url(abs_included_url)
+                included_page = self.wiki.getPage(abs_included_url)
+                ctx.included_pages.append(abs_included_url)
                 return included_page.formatted_text
             return ''
 
@@ -95,15 +99,7 @@
         return text
 
     def _formatWikiLink(self, ctx, display, url):
-        if url.startswith('/'):
-            # Absolute page URL.
-            abs_url = url[1:]
-        else:
-            # Relative page URL. Let's normalize all `..` in it,
-            # which could also replace forward slashes by backslashes
-            # on Windows, so we need to convert that back.
-            raw_abs_url = os.path.join(ctx.urldir, url)
-            abs_url = os.path.normpath(raw_abs_url).replace('\\', '/')
+        abs_url = Page.get_absolute_url(ctx.urldir, url)
         slug = Page.title_to_url(abs_url)
         ctx.out_links.append(slug)
 
@@ -201,8 +197,19 @@
         cache_key = self.url + '.info.cache'
         cached_meta = self._getCached(cache_key)
         if cached_meta is not None:
-            self._meta = cached_meta
-            return
+            # We have a valid cache for our content, but if we are including
+            # other pages, we need to check if they have changed since last
+            # time.
+            base_url = os.path.dirname(self.url)
+            for included_url in cached_meta['included_pages']:
+                included_path = self.wiki.fs.getPhysicalPagePath(included_url)
+                included_time = os.path.getmtime(included_path)
+                included_cache_key = included_url + '.info.cache'
+                if not self.wiki.cache.isValid(included_cache_key, included_time):
+                    break
+            else:
+                self._meta = cached_meta
+                return
 
         self._meta = self.wiki.fs.getPage(self.url)
 
@@ -219,9 +226,8 @@
             if name in ctx.meta:
                 self._meta[name] = ctx.meta[name]
 
-        self._meta['out_links'] = []
-        for l in ctx.out_links:
-            self._meta['out_links'].append(l)
+        self._meta['out_links'] = ctx.out_links
+        self._meta['included_pages'] = ctx.included_pages
 
         self._putCached(cache_key, self._meta)
 
@@ -250,6 +256,18 @@
             return m.group(0).upper()
         return re.sub(r'^.|\s\S', upperChar, url.lower().replace('-', ' '))
 
+    @staticmethod
+    def get_absolute_url(base_url, url):
+        if url.startswith('/'):
+            # Absolute page URL.
+            return url[1:]
+        else:
+            # Relative page URL. Let's normalize all `..` in it,
+            # which could also replace forward slashes by backslashes
+            # on Windows, so we need to convert that back.
+            raw_abs_url = os.path.join(base_url, url)
+            return os.path.normpath(raw_abs_url).replace('\\', '/')
+
 
 class Wiki(object):
     def __init__(self, root=None, logger=None):