changeset 5349:dad2f9e6f901

backport from 1.9 of maint.mkpagepacks and packages for creating attachments in underlay pages for pagepacks. (This changest is based on b8b4e75c9ca1, 9730c2936db7, c9d372df60a9, 6525ddcac8cd of 1.9)
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sat, 05 Dec 2009 14:50:15 +0100
parents a458025ab6cb
children 175ebfeff12a
files MoinMoin/packages.py MoinMoin/script/maint/mkpagepacks.py
diffstat 2 files changed, 46 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/packages.py	Sat Dec 05 11:41:37 2009 +0100
+++ b/MoinMoin/packages.py	Sat Dec 05 14:50:15 2009 +0100
@@ -327,6 +327,30 @@
         else:
             self.msg += u"action rename page: not enough rights - nothing done \n"
 
+    def do_replaceunderlayattachment(self, zipname, filename, pagename, author=u"Scripting Subsystem", comment=u""):
+        """
+        overwrite underlay attachments
+
+        @param pagename: Page where the file is attached. Or in 2.0, the file itself.
+        @param zipname: Filename of the attachment from the zip file
+        @param filename: Filename of the attachment (just applicable for MoinMoin < 2.0)
+        """
+        if self.request.user.may.write(pagename):
+            _ = self.request.getText
+            filename = wikiutil.taintfilename(filename)
+            zipname = wikiutil.taintfilename(zipname)
+            page = PageEditor(self.request, pagename, do_editor_backup=0, uid_override=author)
+            pagedir = page.getPagePath(use_underlay=1, check_create=1)
+            attachments = os.path.join(pagedir, 'attachments')
+            if not os.path.exists(attachments):
+                os.mkdir(attachments)
+            target = os.path.join(attachments, filename)
+            self._extractToFile(zipname, target)
+            if os.path.exists(target):
+                os.chmod(target, config.umask )
+        else:
+            self.msg += u"action replace underlay attachment: not enough rights - nothing done \n"
+
     def do_deletepage(self, pagename, comment="Deleted by the scripting subsystem."):
         """ Marks a page as deleted (like the DeletePage action).
 
--- a/MoinMoin/script/maint/mkpagepacks.py	Sat Dec 05 11:41:37 2009 +0100
+++ b/MoinMoin/script/maint/mkpagepacks.py	Sat Dec 05 14:50:15 2009 +0100
@@ -13,7 +13,9 @@
 
 from MoinMoin.support.python_compatibility import set
 from MoinMoin import wikidicts, wikiutil
+from MoinMoin.action.AttachFile import _get_files
 from MoinMoin.Page import Page
+from MoinMoin.action import AttachFile
 from MoinMoin.packages import packLine, MOIN_PACKAGE_FILE
 from MoinMoin.script import MoinScript
 
@@ -93,25 +95,34 @@
             os.remove(filename)
         except OSError:
             pass
+        # page SystemPagesSetup needs no packing!
+        existing_pages = [pagename for pagename in pagelist if Page(request, pagename).exists() and pagename != 'SystemPagesSetup']
+        if not existing_pages:
+            return
+
         zf = zipfile.ZipFile(filename, "w", COMPRESSION_LEVEL)
 
         cnt = 0
         script = [packLine(['MoinMoinPackage', '1']), ]
 
-        for pagename in pagelist:
+        for pagename in existing_pages:
             pagename = pagename.strip()
             page = Page(request, pagename)
-            if page.exists():
+            files = _get_files(request, pagename)
+            for attname in files:
                 cnt += 1
-                script.append(packLine([function, str(cnt), pagename]))
-                timestamp = wikiutil.version2timestamp(page.mtime_usecs())
-                zi = zipfile.ZipInfo(filename=str(cnt), date_time=datetime.fromtimestamp(timestamp).timetuple()[:6])
-                zi.compress_type = COMPRESSION_LEVEL
-                zf.writestr(zi, page.get_raw_body().encode("utf-8"))
-            else:
-                #import sys
-                #print >>sys.stderr, "Could not find the page %s." % pagename.encode("utf-8")
-                pass
+                zipname = "%d" % cnt
+                script.append(packLine(["ReplaceUnderlayAttachment", zipname, attname, pagename]))
+                attpath = AttachFile.getFilename(request, pagename, attname)
+                zf.write(attpath, zipname)
+
+            cnt += 1
+            zipname = "%d" % cnt
+            script.append(packLine([function, zipname, pagename]))
+            timestamp = wikiutil.version2timestamp(page.mtime_usecs())
+            zi = zipfile.ZipInfo(filename=str(cnt), date_time=datetime.fromtimestamp(timestamp).timetuple()[:6])
+            zi.compress_type = COMPRESSION_LEVEL
+            zf.writestr(zi, page.get_raw_body().encode("utf-8"))
 
         script += [packLine(['Print', 'Installed MoinMaster page bundle %s.' % os.path.basename(filename)])]