changeset 1720:8a0c277a20f7

merged main
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 07 Jan 2007 22:55:26 +0100
parents 3244350dc5ad (current diff) 1d0663c1c44f (diff)
children 65bf1618c196
files
diffstat 2 files changed, 107 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/PackagePages.py	Sun Jan 07 22:55:02 2007 +0100
+++ b/MoinMoin/action/PackagePages.py	Sun Jan 07 22:55:26 2007 +0100
@@ -19,6 +19,8 @@
 from MoinMoin.Page import Page
 from MoinMoin.action.AttachFile import _addLogEntry
 from MoinMoin.packages import MOIN_PACKAGE_FILE, packLine, unpackLine
+from MoinMoin.action import AttachFile
+from MoinMoin.action.AttachFile import _get_files
 
 class ActionError(Exception): pass
 
@@ -89,7 +91,6 @@
         # get directory, and possibly create it
         attach_dir = Page(self.request, self.page.page_name).getPagePath("attachments", check_create=1)
         fpath = os.path.join(attach_dir, target).encode(config.charset)
-        #print fpath
         if os.path.exists(fpath):
             raise ActionError(_("Attachment '%(target)s' (remote name '%(filename)s') already exists.") % {
                 'target': wikiutil.escape(target), 'filename': wikiutil.escape(target)})
@@ -102,11 +103,18 @@
 
         for page in pages:
             cnt += 1
+            files = _get_files(self.request, page.page_name)
             script.append(packLine(["AddRevision", str(cnt), page.page_name, user.getUserIdentification(self.request), "Created by the PackagePages action."]))
+
             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"))
+            for attname in files:
+                if attname != packagename:
+                    script.append(packLine(["AddAttachment", attname, page.page_name, user.getUserIdentification(self.request), "Created by the PackagePages action."]))
+                    filename =  AttachFile.getFilename(self.request, page.page_name, attname)
+                    zf.write(filename.encode("cp437"), attname.encode("cp437"))
 
         script += [packLine(['Print', 'Thank you for using PackagePages!'])]
 
@@ -150,7 +158,7 @@
     <tr>
         <td class="label"><label>%(list_label)s</label></td>
         <td class="content">
-            <input type="text" name="pagelist" maxlength="200">
+            <input type="text" name="pagelist" maxlength="200" value=%(pagename)s>
         </td>
     </tr>
     <tr>
--- a/MoinMoin/packages.py	Sun Jan 07 22:55:02 2007 +0100
+++ b/MoinMoin/packages.py	Sun Jan 07 22:55:26 2007 +0100
@@ -6,17 +6,20 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-import os
+import os, codecs
 import sys
 import zipfile
 
-from MoinMoin import config, wikiutil, caching
+from MoinMoin import config, wikiutil, caching, user
 from MoinMoin.Page import Page
 from MoinMoin.PageEditor import PageEditor
+from MoinMoin.util import filesys
+from MoinMoin.logfile import editlog, eventlog
 
 MOIN_PACKAGE_FILE = 'MOIN_PACKAGE'
 MAX_VERSION = 1
 
+
 # Exceptions
 class PackageException(Exception):
     """ Raised if the package is broken. """
@@ -38,6 +41,26 @@
 class ScriptExit(Exception):
     """ Raised by the script commands when the script should quit. """
 
+def event_logfile(self, pagename, pagefile):
+    # add event log entry
+    filename = self.request.rootpage.getPagePath('event-log', isfile=1)
+    eventtype = 'SAVENEW'
+    mtime_usecs = wikiutil.timestamp2version(os.path.getmtime(pagefile))
+    elog = eventlog.EventLog(self.request)
+    elog.add(self.request, eventtype, {'pagename': pagename}, 1, mtime_usecs)
+
+def edit_logfile_append(self, pagename, pagefile, rev, action, logname='edit-log', comment=u'', author=u"Scripting Subsystem"):
+    glog = editlog.EditLog(self.request, uid_override=author)
+    pagelog = Page(self.request, pagename).getPagePath(logname, use_underlay=0, isfile=1)
+    llog = editlog.EditLog(self.request, filename=pagelog,
+                               uid_override=author)
+    mtime_usecs = wikiutil.timestamp2version(os.path.getmtime(pagefile))
+    host = '::1'
+    extra = u''
+    glog.add(self.request, mtime_usecs, rev, action, pagename, host, comment)
+    llog.add(self.request, mtime_usecs, rev, action, pagename, host, extra, comment)
+    event_logfile(self, pagename, pagefile)
+
 # Parsing and (un)quoting for script files
 def packLine(list, separator="|"):
     """ Packs a list into a string that is separated by `separator`. """
@@ -95,6 +118,55 @@
         #Satisfy pylint
         self.msg = getattr(self, "msg", "")
         self.request = getattr(self, "request", None)
+    def do_addattachment(self, filename, pagename, author=u"Scripting Subsystem", comment=u""):
+        """
+        Installs an attachment
+
+        @param pagename: Page where the file is attached. Or in 2.0, the file itself.
+        @param filename: Filename of the attachment (just applicable for MoinMoin < 2.0)
+        """
+        _ = self.request.getText
+
+        attachments = Page(self.request, pagename).getPagePath("attachments", check_create=1)
+        filename = wikiutil.taintfilename(filename)
+        target = os.path.join(attachments, filename)
+        page = PageEditor(self.request, pagename, do_editor_backup=0, uid_override=author)
+        rev = page.current_rev()
+        path = page.getPagePath(check_create=0)
+        if not os.path.exists(target):
+           self._extractToFile(filename, target)
+           if os.path.exists(target):
+              os.chmod(target, config.umask )
+              action = 'ATTNEW'
+              edit_logfile_append(self, pagename, path, rev, action, logname='edit-log',
+                      comment=u'%(filename)s' % {"filename":filename}, author=author)
+              self.msg += u"%(filename)s attached \n" % {"filename": filename}
+        else:
+           self.msg += u"%(filename)s not attached \n" % {"filename":filename}
+
+    def do_delattachment(self, filename, pagename, author=u"Scripting Subsystem", comment=u""):
+        """
+        Removes an attachment
+
+        @param pagename: Page where the file is attached. Or in 2.0, the file itself.
+        @param filename: Filename of the attachment (just applicable for MoinMoin < 2.0)
+        """
+        _ = self.request.getText
+
+        attachments = Page(self.request, pagename).getPagePath("attachments", check_create=1)
+        filename = wikiutil.taintfilename(filename)
+        target = os.path.join(attachments, filename)
+        page = PageEditor(self.request, pagename, do_editor_backup=0, uid_override=author)
+        rev = page.current_rev()
+        path = page.getPagePath(check_create=0)
+        if os.path.exists(target):
+              os.remove(target)
+              action = 'ATTDEL'
+              edit_logfile_append(self, pagename, path, rev, action, logname='edit-log',
+                      comment=u'%(filename)s' % {"filename":filename}, author=author)
+              self.msg += u"%(filename)s removed \n" % {"filename":filename}
+        else:
+           self.msg += u"%(filename)s not exists \n" % {"filename":filename}
 
     def do_print(self, *param):
         """ Prints the parameters into output of the script. """
@@ -215,14 +287,34 @@
         """
         _ = self.request.getText
         trivial = str2boolean(trivial)
-
+        uid = user.getUserId(self.request, author)
+        theuser = user.User(self.request, uid)
+        self.request.user = theuser
         page = PageEditor(self.request, pagename, do_editor_backup=0, uid_override=author)
         try:
             page.saveText(self.extract_file(filename).decode("utf-8"), 0, trivial=trivial, comment=comment)
+            self.msg += u"%(pagename)s added \n" % {"pagename": pagename}
         except PageEditor.Unchanged:
             pass
+        page.clean_acl_cache()
 
-        page.clean_acl_cache()
+    def do_renamepage(self, pagename, newpagename, author=u"Scripting Subsystem", comment=u"Renamed by the scripting subsystem."):
+        """ Renames a page.
+
+        @param pagename: name of the target page
+        @param newpagename: name of the new page
+        @param author:   user name of the editor (optional)
+        @param comment:  comment related to this revision (optional)
+        """
+        _ = self.request.getText
+        page = PageEditor(self.request, pagename, do_editor_backup=0, uid_override=author)
+        if not page.exists():
+            raise RuntimeScriptException(_("The page %s does not exist.") % pagename)
+        newpage = PageEditor(self.request, newpagename)
+        page.renamePage(newpage.page_name, comment=u"Renamed from '%s'" % (pagename))
+        self.msg += u'%(pagename)s renamed to %(newpagename)s\n' % {
+                    "pagename": pagename,
+                    "newpagename": newpagename}
 
     def do_deletepage(self, pagename, comment="Deleted by the scripting subsystem."):
         """ Marks a page as deleted (like the DeletePage action).
@@ -298,7 +390,7 @@
                 self.goto -= 1
                 continue
 
-            if line.startswith("#"):
+            if line.startswith("#") or len(line) == 0:
                 continue
             elements = unpackLine(line)
             fnname = elements[0].strip().lower()
@@ -445,4 +537,3 @@
 
 if __name__ == '__main__':
     main()
-