def get_mediawiki_users(host, user, password, dbname, tableprefix): print(host, user, password, dbname, tableprefix) db = MySQLdb.connect(passwd=password, user=user, host=host, db=dbname, use_unicode=True, charset="utf8") c = db.cursor() c.execute( "SELECT user_name,user_real_name,user_email,user_password FROM %suser" % tableprefix) users = {} def _escape(field): if isinstance(field, unicode): return field.replace(":", r"\:") else: return unicode(field, "utf-8").replace(":", r"\:") for row in c.fetchall(): login = names.clean_user(unicode(row[0], "utf-8")) users[login] = { "login": login, "pwhash": _escape(row[3]), "name": _escape(row[1]), "email": _escape(row[2]), "groups": "user", } return users
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 get_mediawiki_users(host, user, password, dbname, tableprefix): print(host, user, password, dbname, tableprefix) db = MySQLdb.connect(passwd=password, user=user, host=host, db=dbname, use_unicode=True, charset="utf8") c = db.cursor() c.execute("SELECT user_name,user_real_name,user_email,user_password FROM %suser" % tableprefix) users = {} def _escape(field): if isinstance(field, unicode): return field.replace(":", r"\:") else: return unicode(field, "utf-8").replace(":", r"\:") for row in c.fetchall(): login = names.clean_user(unicode(row[0], "utf-8")) users[login] = { "login": login, "pwhash": _escape(row[3]), "name": _escape(row[1]), "email": _escape(row[2]), "groups": "user", } return users
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)