Ensure release notes from older version still load without 404 errors.
authorJonathan S. Katz <jonathan.katz@excoventures.com>
Wed, 13 Feb 2019 20:22:49 +0000 (15:22 -0500)
committerJonathan S. Katz <jonathan.katz@excoventures.com>
Fri, 8 Mar 2019 19:19:33 +0000 (11:19 -0800)
An upcoming change in PostgreSQL keeps only release notes in their
current versions. So e.g., PostgreSQL 11 only has the release notes
for 11.0, 11.1, 11.2 etc.

Thus, for PostgreSQL 9.4 on up, any URLs such as /docs/10/release-9-6-8.html
would return a 404. Additonal, URLs with /current/ and /devel/ would break
as well.

This patch prevents these 404s from occuring by redirecting users accessing
release notes in the 9.4 and above documentation to the older versions
of the release notes by redirecting them to the notes in the specific
version. So in the above example, a user would be redirect to
/docs/9.6/release-9-6-8.html

As we do not have documentation on the website past 7.2, any release
notes from an older version are redirected to the release notes tree
in 7.2

pgweb/docs/views.py

index 62f0abb3ce6ebacfbf3fab663065a1ba7d680224..54690eecd9ed2aee5e3064b5f0bcbe1b44134119 100644 (file)
@@ -5,8 +5,9 @@ from pgweb.util.decorators import login_required, allow_frames, content_sources
 from django.db.models import Q
 from django.conf import settings
 
-from decimal import Decimal
+from decimal import Decimal, ROUND_DOWN
 import os
+import re
 
 from pgweb.util.contexts import render_pgweb
 from pgweb.util.helpers import template_to_string
@@ -54,6 +55,34 @@ def docpage(request, version, filename):
         return HttpResponsePermanentRedirect("/docs/{0}/{1}.html".format(int(ver), filename))
 
     fullname = "%s.%s" % (filename, extension)
+
+    # Before looking up the documentation, we need to make a check for release
+    # notes. Based on a change, from PostgreSQL 9.4 and up, release notes are
+    # only available for the current version (e.g. 11 only has 11.0, 11.1, 11.2)
+    # This checks to see if there is a mismatch (e.g. ver = 9.4, fullname = release-9-3-2.html)
+    # and perform a redirect to the older version
+    if fullname.startswith('release-') and ver >= Decimal("9.4"):
+        # figure out which version to redirect to. Note that the oldest version
+        # of the docs loaded is 7.2
+        release_version = re.sub(r'release-((\d+)(-\d+)?)(-\d+)?.html',
+            r'\1', fullname).replace('-', '.')
+        # convert to Decimal for ease of manipulation
+        release_version = Decimal(release_version)
+        # if the version is greater than 10, truncate the number
+        if release_version >= Decimal('10'):
+            release_version = release_version.quantize(Decimal('1'), rounding=ROUND_DOWN)
+        # only proceed if the release version of the documents does not match
+        # the calculated release version
+        if release_version != ver:
+            url = "/docs/"
+            if release_version >= Decimal('10'):
+                url += "{}/{}".format(int(release_version), fullname)
+            elif release_version < Decimal('7.2'):
+                url += "7.2/{}".format(fullname)
+            else:
+                url += "{}/{}".format(release_version, fullname)
+            return HttpResponsePermanentRedirect(url)
+
     page = get_object_or_404(DocPage, version=ver, file=fullname)
     versions = DocPage.objects.extra(
         where=["file=%s OR file IN (SELECT file2 FROM docsalias WHERE file1=%s) OR file IN (SELECT file1 FROM docsalias WHERE file2=%s)"],