def _convert_page(self, page): """ Convert the supplied mediawiki page to a Dokuwiki page """ print("Converting %d revisions of page '%s'..." % (len(page["revisions"]), page['title'])) # Sanitise the mediawiki pagename to something matching the dokuwiki pagename convention full_title = make_dokuwiki_pagename(page['title']) # Mediawiki pagenames can contain namespace :s, convert these to dokuwiki / paths on the filesystem (becoming : namespaces in dokuwiki) subdir, pagename = os.path.split(full_title.replace(':', '/')) pagedir = os.path.join(self.pages, subdir) metadir = os.path.join(self.meta, subdir) atticdir = os.path.join(self.attic, subdir) for d in pagedir, metadir, atticdir: ensure_directory_exists(d) # Walk through the list of revisions revisions = list(reversed(page["revisions"])) # order as oldest first for revision in revisions: is_current = (revision == revisions[-1]) is_first = (revision == revisions[0]) content = wikicontent.convert_pagecontent(full_title, revision["*"]) timestamp = get_timestamp(revision) comment = revision.get("comment", "").replace("\t", " ").split("\n")[0] # path to the .changes metafile changespath = os.path.join(metadir, "%s.changes" % pagename) # for current revision, create 'pages' .txt if is_current: txtpath = os.path.join(pagedir, "%s.txt" % pagename) with codecs.open(txtpath, "w", "utf-8") as f: f.write(content) os.utime(txtpath, (timestamp, timestamp)) # create gzipped attic revision atticname = "%s.%s.txt.gz" % (pagename, timestamp) atticpath = os.path.join(atticdir, atticname) with gzip.open(atticpath, "wb") as f: f.write(content.encode("utf-8")) os.utime(atticpath, (timestamp, timestamp)) # append entry to page's 'changes' metadata index with codecs.open(changespath, "w" if is_first else "a", "utf-8") as f: changes_title = full_title.replace("/", ":") fields = (str(timestamp), "::1", "C" if is_first else "E", changes_title, names.clean_user(revision["user"]), comment) print(u"\t".join(fields), file=f)
def run_test(testdir): """ Run the test contained in the directory 'testdir' Return True on success """ print("Running %s..." % testdir) mw = _readfile(testdir, "mediawiki.txt").strip() # ignore leading/trailing whitespace, too annoying dw = _readfile(testdir, "dokuwiki.txt").strip() # ignore leading/trailing whitespace, too annoying notes = _readfile(testdir, "notes.txt") # use directory name as the page name pagename = os.path.split(testdir)[1] if len(mw) == 0: print("WARNING: No mediawiki input!!!") try: converted = wikicontent.convert_pagecontent(pagename, mw).strip() if converted == dw: return True except: print("CONVERSION ERROR") traceback.print_exc() print(DELIMITER) if len(notes): print("Test notes:") print(notes) return False print("OUTPUT MISMATCH") if len(notes): print("Test notes:") print(notes) print(DELIMITER) print("Input Mediawiki:") print(mw) print(DELIMITER) print("Expected Output:") print(DELIMITER) print(dw) print(DELIMITER) print("Actual Output:") print(converted) print(DELIMITER) return False
def _convert_page(self, page): """ Convert the supplied mediawiki page to a Dokuwiki page """ print("Converting %d revisions of page '%s'..." % (len(page["revisions"]), page['title'])) # Sanitise the mediawiki pagename to something matching the dokuwiki pagename convention full_title = unicode(make_dokuwiki_pagename(page['title']), 'utf-8') # Mediawiki pagenames can contain namespace :s, convert these to dokuwiki / paths on the filesystem (becoming : namespaces in dokuwiki) subdir, pagename = os.path.split(full_title.replace(':','/')) pagedir = os.path.join(self.pages, subdir) metadir = os.path.join(self.meta, subdir) atticdir = os.path.join(self.attic, subdir) for d in pagedir, metadir, atticdir: ensure_directory_exists(d) # Walk through the list of revisions revisions = list(reversed(page["revisions"])) # order as oldest first for revision in revisions: is_current = (revision == revisions[-1]) is_first = (revision == revisions[0]) content = wikicontent.convert_pagecontent(full_title, revision["*"]) timestamp = get_timestamp(revision) comment = revision.get("comment", "").replace("\t", " ").split("\n")[0] # path to the .changes metafile changespath = os.path.join(metadir, "%s.changes"%pagename) # for current revision, create 'pages' .txt if is_current: txtpath = os.path.join(pagedir, "%s.txt"%pagename) with codecs.open(txtpath, "w", "utf-8") as f: f.write(content) os.utime(txtpath, (timestamp,timestamp)) # create gzipped attic revision atticname = "%s.%s.txt.gz" % (pagename, timestamp) atticpath = os.path.join(atticdir, atticname).encode("utf-8") with gzip.open(atticpath, "wb") as f: f.write(content.encode("utf-8")) os.utime(atticpath, (timestamp,timestamp)) # append entry to page's 'changes' metadata index with codecs.open(changespath, "w" if is_first else "a", "utf-8") as f: changes_title = full_title.replace("/", ":") fields = (str(timestamp), "::1", "C" if is_first else "E", changes_title, names.clean_user(revision["user"]), comment) print(u"\t".join(fields), file=f)