1.1 --- a/actions/ExportPDF.py Fri Nov 01 15:21:36 2013 +0100
1.2 +++ b/actions/ExportPDF.py Fri Nov 01 17:07:09 2013 +0100
1.3 @@ -6,7 +6,8 @@
1.4 @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.5 """
1.6
1.7 -from MoinMoin.action import ActionBase
1.8 +from MoinMoin import caching
1.9 +from MoinMoin.action import ActionBase, cache
1.10 from MoinMoin.wikiutil import escape
1.11 from MoinSupport import ActionSupport, escattr, getFormatterClass, formatText, get_send_headers
1.12 from os.path import join
1.13 @@ -147,18 +148,36 @@
1.14
1.15 _ = self._
1.16 form = self.get_form()
1.17 + request = self.request
1.18
1.19 paper_size = form.get("paper-size", [""])[0]
1.20
1.21 if not paper_size in self._get_paper_sizes() or []:
1.22 return 0, _("A paper size must be chosen.")
1.23
1.24 + # See if the revision is cached.
1.25 +
1.26 + cache_key = cache.key(request, content="%s-%s" % (self.page.rev, paper_size))
1.27 + cache_entry = caching.CacheEntry(request, self.page, cache_key, scope="item")
1.28 +
1.29 + # Open any available cache entry and read it.
1.30 +
1.31 + if cache_entry.exists():
1.32 + cache_entry.open()
1.33 + try:
1.34 + self._write_pdf(cache_entry.read())
1.35 + return 1, None
1.36 + finally:
1.37 + cache_entry.close()
1.38 +
1.39 + # Otherwise, prepare the PDF.
1.40 +
1.41 if self.mode == "docbook":
1.42 - return self._export_using_docbook(paper_size)
1.43 + return self._export_using_docbook(paper_size, cache_entry)
1.44 elif self.mode == "wkhtmltopdf":
1.45 - return self._export_using_wkhtmltopdf(paper_size)
1.46 + return self._export_using_wkhtmltopdf(paper_size, cache_entry)
1.47 elif self.mode == "htmldoc":
1.48 - return self._export_using_htmldoc(paper_size)
1.49 + return self._export_using_htmldoc(paper_size, cache_entry)
1.50 else:
1.51 return 0, _("The action must be configured to use a particular PDF generation tool.")
1.52
1.53 @@ -233,7 +252,7 @@
1.54 self._write_pdf(out)
1.55 return 1, None
1.56
1.57 - def _export_using_wkhtmltopdf(self, paper_size):
1.58 + def _export_using_wkhtmltopdf(self, paper_size, cache_entry):
1.59
1.60 """
1.61 Send the page HTML to the processor, indicating the given 'paper_size'.
1.62 @@ -251,9 +270,9 @@
1.63 stdout=subprocess.PIPE,
1.64 stderr=subprocess.PIPE)
1.65
1.66 - return self._write_pdf_for_html(p, self._get_page_as_html())
1.67 + return self._write_pdf_for_html(p, self._get_page_as_html(), cache_entry)
1.68
1.69 - def _export_using_htmldoc(self, paper_size):
1.70 + def _export_using_htmldoc(self, paper_size, cache_entry):
1.71
1.72 """
1.73 Send the page HTML to the processor, indicating the given 'paper_size'.
1.74 @@ -272,9 +291,9 @@
1.75 stdout=subprocess.PIPE,
1.76 stderr=subprocess.PIPE)
1.77
1.78 - return self._write_pdf_for_html(p, self._get_page_as_html())
1.79 + return self._write_pdf_for_html(p, self._get_page_as_html(), cache_entry)
1.80
1.81 - def _export_using_docbook(self, paper_size):
1.82 + def _export_using_docbook(self, paper_size, cache_entry):
1.83
1.84 """
1.85 Send the page DocBook XML to the processor, indicating the given
1.86 @@ -320,10 +339,28 @@
1.87 if retcode != 0:
1.88 return 0, err
1.89
1.90 + self._write_to_cache(out, cache_entry)
1.91 self._write_pdf(out)
1.92 return 1, None
1.93
1.94 + def _write_to_cache(self, out, cache_entry):
1.95 +
1.96 + "Write the output 'out' to the given 'cache_entry'."
1.97 +
1.98 + cache_entry.open(mode="w")
1.99 + try:
1.100 + try:
1.101 + cache_entry.write(out)
1.102 + finally:
1.103 + cache_entry.close()
1.104 + except IOError:
1.105 + if cache_entry.exists():
1.106 + cache_entry.remove()
1.107 +
1.108 def _write_pdf(self, out):
1.109 +
1.110 + "Write the output 'out' to the request/response."
1.111 +
1.112 request = self.request
1.113
1.114 send_headers = get_send_headers(request)