Attachment 'packages_20060707.patch'
Download 1 # HG changeset patch
2 # User ReimarBauer <R.Bauer@fz-juelich.de>
3 # Node ID 5142cfdccbac1f5ef80d75c97ff5f31964707798
4 # Parent b47c307ed9d555d4831b7d1275203795ddf1dfca
5 added funtionality of AddAtachment, DelAttachment, RenamePage and comments for
6 Addrevision and ignoring of empty lines by len(line) == 0 may be it would be better here to check if there are the wanted colums.
7
8 diff -r b47c307ed9d5 -r 5142cfdccbac MoinMoin/packages.py
9 --- a/MoinMoin/packages.py Thu Jul 06 15:10:16 2006 +0200
10 +++ b/MoinMoin/packages.py Fri Jul 07 00:28:44 2006 +0200
11 @@ -6,16 +6,106 @@
12 @license: GNU GPL, see COPYING for details.
13 """
14
15 -import os
16 +import os, codecs
17 import sys
18 import zipfile
19
20 -from MoinMoin import config, wikiutil, caching
21 +from MoinMoin import config, wikiutil, caching, user
22 from MoinMoin.Page import Page
23 from MoinMoin.PageEditor import PageEditor
24 +from MoinMoin.util import filesys
25 +from MoinMoin.logfile import editlog, eventlog
26
27 MOIN_PACKAGE_FILE = 'MOIN_PACKAGE'
28 MAX_VERSION = 1
29 +
30 +def renamepage(self,pagename,newpagename,author=u"Scripting Subsystem", comment=u""):
31 +
32 + _ = self.request.getText
33 +
34 + newpage = PageEditor(self.request, newpagename)
35 +
36 + # Check whether a page with the new name already exists
37 + if newpage.exists(includeDeleted=0):
38 + self.msg = u'Could not rename page because new page %(newpagename)s already exists\n' % {
39 + 'newpagename': newpagename}
40 + return
41 +
42 + page = PageEditor(self.request, pagename, do_editor_backup=0, uid_override=author)
43 + path = page.getPagePath(check_create=0)
44 + rev = page.current_rev()
45 +
46 + # Get old page text
47 + savetext = page.get_raw_body()
48 + # add comment
49 + savetext = u"## page was renamed from %s\n%s" % (pagename, savetext)
50 +
51 + oldpath = page.getPagePath(check_create=0)
52 + newpath = newpage.getPagePath(check_create=0)
53 +
54 +
55 + # Rename page
56 +
57 + # NOTE: might fail if another process created newpagename just
58 + # NOW, while you read this comment. Rename is atomic for files -
59 + # but for directories, rename will fail if the directory
60 + # exists. We should have global edit-lock to avoid this.
61 + # See http://docs.python.org/lib/os-file-dir.html
62 + try:
63 + os.rename(oldpath, newpath)
64 +
65 + self.newpage = newpage
66 + self.msg = u'%(pagename)s renamed to %(newpagename)s\n' % {
67 + "pagename":pagename,
68 + "newpagename":newpagename}
69 +
70 + action = 'SAVENEW'
71 +
72 + revstr = '%08d' % rev
73 + newpagedir = Page(self.request, newpagename).getPagePath("", check_create=1)
74 +
75 + revdir = os.path.join(newpagedir, 'revisions')
76 + oldpagefile = os.path.join(revdir, revstr)
77 +
78 + rev += 1
79 + revstr = '%08d' % rev
80 + newpagedir = Page(self.request, newpagename).getPagePath("", check_create=1)
81 + revdir = os.path.join(newpagedir, 'revisions')
82 + newpagefile = os.path.join(revdir, revstr)
83 +
84 + f = open(newpagefile,'w')
85 + f.write(savetext)
86 + f.close()
87 + os.chmod(newpagefile, 0666 & config.umask)
88 +
89 + cfn = os.path.join(newpagedir,'current')
90 + f = open(cfn, 'w')
91 + f.write('%08d\n' % rev)
92 + f.close()
93 + os.chmod(cfn, 0666 & config.umask)
94 +
95 + clfn = os.path.join(newpagedir,'current-locked')
96 + f = open(clfn, 'w')
97 + f.write(revstr+'\n')
98 + f.close()
99 + os.chmod(clfn, 0666 & config.umask)
100 +
101 + edit_logfile_append(self,newpagename,newpath,rev,action,logname='edit-log',
102 + comment=u'Renamed from %(pagename)s' % {"pagename": pagename},author=author)
103 + event_logfile(self,newpagename,newpagefile)
104 +
105 + return
106 +
107 + except OSError, err:
108 + # Try to understand what happened. Maybe its better to check
109 + # the error code, but I just reused the available code above...
110 + if newpage.exists(includeDeleted=0):
111 + self.msg = u'Could not rename page because new page %(newpagename)s already exists\n' % {
112 + 'newpagename': newpagename}
113 + return
114 + else:
115 + self.msg = u'Could not rename page because of file systemerror: %s.' % unicode(err)
116 +
117
118 # Exceptions
119 class PackageException(Exception):
120 @@ -38,11 +128,33 @@ class ScriptExit(Exception):
121 class ScriptExit(Exception):
122 """ Raised by the script commands when the script should quit. """
123
124 +def event_logfile(self,pagename,pagefile):
125 + # add event log entry
126 +
127 + filename = self.request.rootpage.getPagePath('event-log', isfile=1)
128 + eventtype = 'SAVENEW'
129 + mtime_usecs = wikiutil.timestamp2version(os.path.getmtime(pagefile))
130 + elog = eventlog.EventLog(self.request)
131 + elog.add(self.request, eventtype, {'pagename': pagename}, 1, mtime_usecs)
132 +
133 +def edit_logfile_append(self,pagename,pagefile,rev,action,logname='edit-log',comment=u'',author=u"Scripting Subsystem"):
134 + glog = editlog.EditLog(self.request, uid_override=author)
135 + pagelog = Page(self.request, pagename).getPagePath(logname, use_underlay=0, isfile=1)
136 + llog = editlog.EditLog(self.request, filename=pagelog,
137 + uid_override=author)
138 + mtime_usecs = wikiutil.timestamp2version(os.path.getmtime(pagefile))
139 + host = '::1'
140 + extra = u''
141 + glog.add(self.request, mtime_usecs, rev, action, pagename, host, comment)
142 + llog.add(self.request, mtime_usecs, rev, action, pagename, host, extra, comment)
143 + event_logfile(self,pagename,pagefile)
144 +
145 +
146 # Parsing and (un)quoting for script files
147 def packLine(list, separator="|"):
148 """ Packs a list into a string that is separated by `separator`. """
149 return '|'.join([x.replace('\\', '\\\\').replace(separator, '\\' + separator) for x in list])
150 -
151 +
152 def unpackLine(string, separator="|"):
153 """ Unpacks a string that was packed by packLine. """
154 result = []
155 @@ -95,6 +207,59 @@ class ScriptEngine:
156 #Satisfy pylint
157 self.msg = getattr(self, "msg", "")
158 self.request = getattr(self, "request", None)
159 + def do_addattachment(self,filename,pagename,author=u"Scripting Subsystem", comment=u""):
160 + """
161 + Installs an attachment
162 +
163 + @param pagename: Page where the file is attached. Or in 2.0, the file itself.
164 + @param filename: Filename of the attachment (just applicable for MoinMoin < 2.0)
165 + """
166 + _ = self.request.getText
167 +
168 + attachments = Page(self.request, pagename).getPagePath("attachments", check_create=1)
169 + filename = wikiutil.taintfilename(filename)
170 + target = os.path.join(attachments,filename)
171 +
172 + page = PageEditor(self.request, pagename, do_editor_backup=0, uid_override=author)
173 + rev = page.current_rev()
174 + path = page.getPagePath(check_create=0)
175 +
176 + if not os.path.exists(target):
177 + self._extractToFile(filename, target)
178 + if os.path.exists(target):
179 + os.chmod(target, config.umask )
180 + action = 'ATTNEW'
181 + edit_logfile_append(self,pagename,path,rev,action,logname='edit-log',
182 + comment=u'%(filename)s' % {"filename":filename},author=author)
183 + self.msg += u"%(filename)s attached \n" % {"filename": filename}
184 + else:
185 + self.msg += u"%(filename)s not attached \n" % {"filename":filename}
186 +
187 + def do_delattachment(self,filename,pagename,author=u"Scripting Subsystem", comment=u""):
188 + """
189 + Removes an attachment
190 +
191 + @param pagename: Page where the file is attached. Or in 2.0, the file itself.
192 + @param filename: Filename of the attachment (just applicable for MoinMoin < 2.0)
193 + """
194 + _ = self.request.getText
195 +
196 + attachments = Page(self.request, pagename).getPagePath("attachments", check_create=1)
197 + filename = wikiutil.taintfilename(filename)
198 + target = os.path.join(attachments,filename)
199 +
200 + page = PageEditor(self.request, pagename, do_editor_backup=0, uid_override=author)
201 + rev = page.current_rev()
202 + path = page.getPagePath(check_create=0)
203 +
204 + if os.path.exists(target):
205 + os.remove(target)
206 + action = 'ATTDEL'
207 + edit_logfile_append(self,pagename,path,rev,action,logname='edit-log',
208 + comment=u'%(filename)s' % {"filename":filename},author=author)
209 + self.msg += u"%(filename)s removed \n" % {"filename":filename}
210 + else:
211 + self.msg += u"%(filename)s not exists \n" % {"filename":filename}
212
213 def do_print(self, *param):
214 """ Prints the parameters into output of the script. """
215 @@ -216,13 +381,24 @@ class ScriptEngine:
216 _ = self.request.getText
217 trivial = str2boolean(trivial)
218
219 +
220 + uid = user.getUserId(self.request, author)
221 + theuser = user.User(self.request, uid)
222 + self.request.user = theuser
223 +
224 page = PageEditor(self.request, pagename, do_editor_backup=0, uid_override=author)
225 try:
226 page.saveText(self.extract_file(filename).decode("utf-8"), 0, trivial=trivial, comment=comment)
227 + self.msg += u"%(pagename)s added \n" % {"pagename": pagename}
228 except PageEditor.Unchanged:
229 pass
230
231 page.clean_acl_cache()
232 +
233 +
234 + def do_renamepage(self,pagename,newpagename,author=u"Scripting Subsystem", comment=u""):
235 + renamepage(self,pagename,newpagename,author=u"Scripting Subsystem", comment=u"")
236 +
237
238 def do_deletepage(self, pagename, comment="Deleted by the scripting subsystem."):
239 """ Marks a page as deleted (like the DeletePage action).
240 @@ -301,7 +477,7 @@ class ScriptEngine:
241 self.goto -= 1
242 continue
243
244 - if line.startswith("#"):
245 + if line.startswith("#") or len(line) == 0:
246 continue
247 elements = unpackLine(line)
248 fnname = elements[0].strip().lower()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.