Skip to content

Instantly share code, notes, and snippets.

@NagariaHussain
Last active December 11, 2024 04:22
Show Gist options
  • Save NagariaHussain/a8189fe5cb330268218a2ec664799d20 to your computer and use it in GitHub Desktop.
Save NagariaHussain/a8189fe5cb330268218a2ec664799d20 to your computer and use it in GitHub Desktop.
old_wiki_site = "https://frappeframework.com"
new_wiki_space_name = "1u8fslkdg6" # on this site
old_site_wiki_space_name = "Unversioned docs" # on old site
migration_settings = frappe.get_doc("Temp Wiki Migration")
api_credentials = {
"key": migration_settings.api_key,
"secret": migration_settings.get_password("api_secret")
}
old_site = FrappeClient(url=old_wiki_site, api_key=api_credentials["key"], api_secret=api_credentials["secret"])
def clone_wiki_pages():
pages = old_site.get_list(
"Wiki Page",
fields=["*"],
limit_page_length=1000,
)
# clone the pages
for page in pages:
page["route"] = page["route"].replace("docs", "framework")
page["doctype"] = "Wiki Page"
try:
frappe.get_doc(page).insert()
except:
print("Unable to migrate: ", page.name, page.title)
def get_new_wiki_page_name(old_name):
old_route = old_site.get_value("Wiki Page", "route", {"name": old_name}) # route is unique, title may not be
new_route = old_route["route"].replace("docs", "framework")
return frappe.db.get_value("Wiki Page", {"route": new_route}, "name")
# clone_wiki_pages()
# clone the space
old_wiki_space_doc = old_site.get_doc("Wiki Space", old_site_wiki_space_name)
new_space_doc = frappe.get_doc("Wiki Space", new_wiki_space_name)
for row in old_wiki_space_doc["wiki_sidebars"]:
old_name = row["wiki_page"]
row["wiki_page"] = get_new_wiki_page_name(old_name)
new_space_doc.append("wiki_sidebars", {
"parent_label": row["parent_label"],
"wiki_page": row["wiki_page"],
"hide_on_sidebar": row["hide_on_sidebar"]
})
new_space_doc.save()
# copy navbar items
for row in old_wiki_space_doc["navbar_items"]:
new_space_doc.append("navbar_items", {
"label": row["label"],
"url": row["url"],
"right": row["right"],
"parent_label": row["parent_label"]
})
new_space_doc.save()
# for migrating files
file_names = [...]
for file_name in file_names:
response = old_site.session.get(old_wiki_site + f"/files/{file_name}")
frappe.get_doc({
"doctype": "File",
"file_name": file_name,
"content": response.content
}).save()
# for cloning multiple wiki spaces together
spaces = [
("jrq61gs42b", "erpnext/v14/user/manual/de"),
("jrj0blrlpa", "erpnext/v13/user/manual/en"),
("jrauacovd2", "erpnext/v12/user/manual/en"),
("jql5ttjkir", "erpnext/v14/user/manual/en"),
("16ec307d6b", "erpnext/v13/user/manual/de"),
("4b2fd9919c", "erpnext/v13/user/videos/learn"),
("fd244d0d20", "erpnext/v14/user/manual/es"),
("004401dbe2", "erpnext/v12/user/videos/learn"),
("ccab26c6d7", "erpnext/v13/user/manual/es"),
("c4599ec7d3", "erpnext/v12/user/manual/de"),
]
for old_site_wiki_space_name, route in spaces:
new_space_doc = frappe.new_doc("Wiki Space")
new_space_doc.route = route
old_wiki_space_doc = old_site.get_doc("Wiki Space", old_site_wiki_space_name)
for row in old_wiki_space_doc["wiki_sidebars"]:
if row.get("wiki_page"):
old_name = row["wiki_page"]
row["wiki_page"] = get_new_wiki_page_name(old_name)
if not row.get("wiki_page"):
print("issue with: ", old_name)
continue
new_space_doc.append("wiki_sidebars", {
"parent_label": row["parent_label"],
"wiki_page": row["wiki_page"],
"hide_on_sidebar": row["hide_on_sidebar"]
})
new_space_doc.save()
# --------
# Migrating file attachments
wiki_page_files = old_site.get_list(
"File",
fields=["file_name", "attached_to_name", "file_url"],
filters={
"attached_to_doctype": "Wiki Page"
}, limit_page_length=1000)
for wiki_file in wiki_page_files:
file_url = wiki_file["file_url"]
file_name = wiki_file["file_name"]
response = old_site.session.get(old_wiki_site + file_url)
frappe.get_doc({
"doctype": "File",
"file_name": file_name,
"content": response.content,
"attached_to_doctype": "Wiki Page",
"attached_to_name": get_new_wiki_page_name(wiki_file["attached_to_name"])
}).save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment