Collision avoidance for attachments in same namespace

This commit is contained in:
Lukas P 2016-01-06 16:55:45 +01:00
parent eb469fe356
commit ac22cb54c2
4 changed files with 28 additions and 13 deletions

View file

@ -29,5 +29,5 @@ class DokuWiki:
cmd = ['./doku.php', method ] + args cmd = ['./doku.php', method ] + args
res = subprocess.Popen(cmd, stdin = None, stdout = subprocess.PIPE, stderr = sys.stderr, close_fds = True).communicate() res = subprocess.Popen(cmd, stdin = None, stdout = subprocess.PIPE, stderr = sys.stderr, close_fds = True).communicate()
self.callcache[key] = unicode(res[0].decode('utf-8')) self.callcache[key] = unicode(res[0].decode('utf-8'))
print "%s->%s" % (cmd, self.callcache[key]) #print "%s->%s" % (cmd, self.callcache[key])
return self.callcache[key] return self.callcache[key]

View file

@ -20,6 +20,7 @@ from os import listdir, mkdir
from os.path import isdir, basename from os.path import isdir, basename
from doku import DokuWiki from doku import DokuWiki
from moinformat import moin2doku from moinformat import moin2doku
import random
USEC = 1000000 USEC = 1000000
@ -63,7 +64,7 @@ def writefile(filename, content, overwrite=False):
# page = MoinMoin Page oject # page = MoinMoin Page oject
# ns = DokuWiki namespace where attachments to copy # ns = DokuWiki namespace where attachments to copy
def copy_attachments(page, ns): def copy_attachments(page, ns,randomID):
srcdir = page.getPagePath('attachments', check_create = 0) srcdir = page.getPagePath('attachments', check_create = 0)
if not isdir(srcdir): if not isdir(srcdir):
return return
@ -75,7 +76,7 @@ def copy_attachments(page, ns):
attachments = listdir(srcdir) attachments = listdir(srcdir)
for attachment in attachments: for attachment in attachments:
src = os.path.join(srcdir, attachment) src = os.path.join(srcdir, attachment)
dst = os.path.join(output_dir, 'media', dw.mediaFN(dw.cleanID("%s/%s" % (ns, attachment)))) dst = os.path.join(output_dir, 'media', dw.mediaFN(dw.cleanID("%s/%s" % (ns, str(randomID)+attachment))))
copyfile(src, dst) copyfile(src, dst)
copystat(src, dst) copystat(src, dst)
@ -157,8 +158,10 @@ def convert_editlog(page, output = None, overwrite = False):
def convertfile(page, output = None, overwrite = False): def convertfile(page, output = None, overwrite = False):
pagedir = page.getPagePath() pagedir = page.getPagePath()
pagename = wikiname(pagedir) pagename = wikiname(pagedir)
if not output: if not output:
output = pagename output = pagename.split("/")
if page.isUnderlayPage(): if page.isUnderlayPage():
print "underlay: %s" % page.request.cfg.data_underlay_dir print "underlay: %s" % page.request.cfg.data_underlay_dir
@ -174,6 +177,9 @@ def convertfile(page, output = None, overwrite = False):
else: else:
revs = [current_rev] revs = [current_rev]
# Generate random ID Number for collision avoidance when attachments in Namespace have the same name
randomID = random.randint(101,999)
for rev in revs: for rev in revs:
page = Page(request, pagename, rev = rev) page = Page(request, pagename, rev = rev)
pagefile, realrev, exists = page.get_rev(rev = rev); pagefile, realrev, exists = page.get_rev(rev = rev);
@ -200,7 +206,7 @@ def convertfile(page, output = None, overwrite = False):
else: else:
out_file = os.path.join(output_dir, 'attic', dw.wikiFN(output, str(mtime))) out_file = os.path.join(output_dir, 'attic', dw.wikiFN(output, str(mtime)))
content = moin2doku(pagename, page.get_raw_body()) content = moin2doku(pagename, page.get_raw_body(),randomID)
if len(content) == 0: if len(content) == 0:
# raise Exception, "No content" # raise Exception, "No content"
print "NO CONTENT: exists: %s,%s" % (exists, os.path.exists(pagefile)) print "NO CONTENT: exists: %s,%s" % (exists, os.path.exists(pagefile))
@ -209,7 +215,7 @@ def convertfile(page, output = None, overwrite = False):
copystat(pagefile, out_file) copystat(pagefile, out_file)
ID = dw.cleanID(output) ID = dw.cleanID(output)
copy_attachments(page, dw.getNS(ID)) copy_attachments(page, dw.getNS(ID),randomID)
# convert edit-log, it's always present even if current page is not # convert edit-log, it's always present even if current page is not
convert_editlog(page, output = output, overwrite = overwrite) convert_editlog(page, output = output, overwrite = overwrite)

View file

@ -18,9 +18,11 @@ from text_dokuwiki import Formatter
import sys import sys
import StringIO import StringIO
def moin2doku(pagename, text): def moin2doku(pagename, text, randomID=None):
parser = Parser(text, request) parser = Parser(text, request)
formatter.setRandomID(randomID)
# this needed for macros # this needed for macros
request.formatter = formatter request.formatter = formatter

View file

@ -31,10 +31,13 @@ class Formatter(FormatterBase):
self.in_pre = 0 self.in_pre = 0
self.in_table = 0 self.in_table = 0
self._text = None # XXX does not work with links in headings!!!!! self._text = None # XXX does not work with links in headings!!!!!
self.randomID= None
self.list_depth = 0 self.list_depth = 0
self.list_type = ' ' self.list_type = ' '
def setRandomID(self,ID):
self.randomID = str(ID)
def _escape(self, text, extra_mapping={"'": "'", '"': """}): def _escape(self, text, extra_mapping={"'": "'", '"': """}):
return saxutils.escape(text, extra_mapping) return saxutils.escape(text, extra_mapping)
@ -78,13 +81,16 @@ class Formatter(FormatterBase):
return ('[[%s|' % (self._escape(url)), ']]')[not on] return ('[[%s|' % (self._escape(url)), ']]')[not on]
def attachment_link(self, on, url=None, querystr=None, **kw): def attachment_link(self, on, url=None, querystr=None, **kw):
return '{{ %s | %s }}' % (url, querystr) if on:
return '{{ %s | }}' % (self.randomID+url)
else:
return ' }}'
def attachment_image(self, url, **kw): def attachment_image(self, url, **kw):
return '{{%s|}}' % (url,) return '{{%s|}}' % (self.randomID+url,)
def attachment_drawing(self, url, text, **kw): def attachment_drawing(self, url, text, **kw):
return '{{%s|%s}}' % (url, text) return '{{%s|%s}}' % (self.randomID+url, text)
def text(self, text, **kw): def text(self, text, **kw):
self._did_para = 0 self._did_para = 0
@ -291,7 +297,7 @@ class Formatter(FormatterBase):
def showAttachedFiles(args): def showAttachedFiles(args):
args = args.split(',') args = args.split(',')
return self.attachment_link(1,url=args[0].strip(),querystr=args[1].strip()) return '{{ %s | %s }}' % (self.randomID+args[0].strip(), args[1].strip())
# function which will just do what parent class would # function which will just do what parent class would
def inherit(args): def inherit(args):
@ -303,7 +309,8 @@ class Formatter(FormatterBase):
'MailTo' : email, 'MailTo' : email,
'GetText' : args, 'GetText' : args,
'ShowSmileys' : inherit, 'ShowSmileys' : inherit,
'ShowAttachedFiles' : showAttachedFiles 'ShowAttachedFiles' : showAttachedFiles,
'Include' : inherit
}[name] }[name]
except KeyError: except KeyError:
lookup = '/* UndefinedMacro: %s(%s) */' % (name, args) lookup = '/* UndefinedMacro: %s(%s) */' % (name, args)