111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
import urllib.parse
|
|
import xml.etree.ElementTree as ET
|
|
import oracledb
|
|
import json
|
|
import re
|
|
|
|
def extract_siteorigin_html(content_text):
|
|
# Find all occurrences of siteorigin-panels block
|
|
# It might look like <!-- wp:siteorigin-panels/layout-block {"panelsData":{...}} /-->
|
|
html_parts = []
|
|
|
|
# regex to match <!-- wp:siteorigin-panels/layout-block {json} /-->
|
|
matches = re.finditer(r'<!--\s*wp:siteorigin-panels/layout-block\s+(.*?)\s*/?-->', content_text, re.DOTALL)
|
|
|
|
has_siteorigin = False
|
|
for match in matches:
|
|
has_siteorigin = True
|
|
json_str = match.group(1).strip()
|
|
if json_str.endswith('/'):
|
|
json_str = json_str[:-1].strip()
|
|
|
|
try:
|
|
data = json.loads(json_str)
|
|
if 'panelsData' in data and 'widgets' in data['panelsData']:
|
|
widgets = data['panelsData']['widgets']
|
|
for widget in widgets:
|
|
if 'text' in widget:
|
|
# Extract the HTML text from the widget
|
|
text = widget['text']
|
|
# The text might have unicode escapes like \u003c
|
|
# json.loads already decoded it!
|
|
html_parts.append(text)
|
|
except Exception as e:
|
|
print(f"Failed to parse JSON: {e}")
|
|
pass
|
|
|
|
if has_siteorigin:
|
|
return "\n".join(html_parts)
|
|
return content_text
|
|
|
|
def wrap_in_editorjs(html_content):
|
|
if not html_content.strip():
|
|
return ""
|
|
editor_json = {
|
|
"time": 1620000000,
|
|
"blocks": [
|
|
{
|
|
"type": "raw",
|
|
"data": {
|
|
"html": html_content
|
|
}
|
|
}
|
|
],
|
|
"version": "2.22.2"
|
|
}
|
|
return json.dumps(editor_json)
|
|
|
|
def main(xml_file_path):
|
|
print(f"Reading {xml_file_path} ...")
|
|
tree = ET.parse(xml_file_path)
|
|
root = tree.getroot()
|
|
|
|
ns = {
|
|
'wp': 'http://wordpress.org/export/1.2/',
|
|
'content': 'http://purl.org/rss/1.0/modules/content/'
|
|
}
|
|
|
|
dsn = "localhost:1521/sisvietnam"
|
|
conn = oracledb.connect(user="sisvietnam", password="sisvietnam", dsn=dsn)
|
|
cursor = conn.cursor()
|
|
|
|
count = 0
|
|
|
|
for item in root.findall('.//item'):
|
|
post_name_tag = item.find('wp:post_name', ns)
|
|
if post_name_tag is not None and post_name_tag.text:
|
|
slug = post_name_tag.text
|
|
else:
|
|
continue
|
|
|
|
slug = urllib.parse.unquote(slug)
|
|
if slug == "khoa-than-kinh-dot-quy-2":
|
|
slug = "khoa-than-kinh-dot-quy"
|
|
|
|
content_tag = item.find('content:encoded', ns)
|
|
content_text = content_tag.text if content_tag is not None and content_tag.text else ""
|
|
|
|
# Check if it has siteorigin
|
|
if 'wp:siteorigin-panels/layout-block' in content_text:
|
|
print(f"Found SiteOrigin content in: {slug}")
|
|
extracted_html = extract_siteorigin_html(content_text)
|
|
|
|
# Wrap in wrapper
|
|
extracted_html = f'<div class="wp-content-container prose max-w-none">\n{extracted_html}\n</div>'
|
|
|
|
final_content = wrap_in_editorjs(extracted_html)
|
|
|
|
cursor.execute("UPDATE sis_page SET content = :1 WHERE slug = :2", [final_content, slug])
|
|
count += 1
|
|
|
|
conn.commit()
|
|
print(f"\n--- TỔNG KẾT ---")
|
|
print(f"Successfully processed {count} SiteOrigin pages.")
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
if len(sys.argv) > 1:
|
|
main(sys.argv[1])
|
|
else:
|
|
print("Usage: python3 fix_siteorigin.py <xml_file>")
|