Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

[WIP] Consolidate sites and pages hierarchy #218

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/jahia2wp.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
jahia2wp.py rotate-backup <csv_file> [--dry-run] [--debug | --quiet]
jahia2wp.py veritas <csv_file> [--debug | --quiet]
jahia2wp.py inventory <path> [--debug | --quiet]
jahia2wp.py consolidate-hierarchy <path> [--debug | --quiet]
jahia2wp.py extract-plugin-config <wp_env> <wp_url> <output_file> [--debug | --quiet]
jahia2wp.py list-plugins <wp_env> <wp_url> [--debug | --quiet]
[--config [--plugin=<PLUGIN_NAME>]] [--extra-config=<YAML_FILE>]
Expand All @@ -55,6 +56,8 @@
import getpass
import logging
import pickle
import json
from urllib.parse import urlparse
import subprocess
import shutil

Expand Down Expand Up @@ -716,6 +719,34 @@ def inventory(path, **kwargs):
logging.info("Inventory made for %s", path)


@dispatch.on('consolidate-hierarchy')
def consolidate_hierarchy(path, **kwargs):
"""
Consolidates the hierarchy of wordpress sites and sub-sites found in `path`. Also includes the pages of the
sites in the consolidated hierarchy.
"""
logging.info("Consolidating sites and pages hierarchy...")
sites_hierarchy = {}
for site_details in WPConfig.inventory(path):
parsed_url = urlparse(site_details.url)
root = parsed_url.netloc
pages_hierarchy = WPConfig(site_details).get_page_hierarchy()
if parsed_url.path in ['/', '']:
sites_hierarchy[root] = {'path': site_details.path, 'url': site_details.url, **pages_hierarchy}
else:
splitted_path = parsed_url.path.split('/')
# Remove empty elements
splitted_path = list(filter(None, splitted_path))
current_depth = sites_hierarchy[root]
# Go to the right level in the hierarchy
for sub_site in splitted_path[:-1]:
current_depth = current_depth[sub_site]
current_depth[splitted_path[-1]] = {'path': site_details.path, 'url': site_details.url, **pages_hierarchy}
with open('consolidation.json', 'w+') as f:
json.dump(sites_hierarchy, f, indent=4)
logging.info("Consolidation made for %s", path)


@dispatch.on('veritas')
def veritas(csv_file, **kwargs):
validator = VeritasValidor(csv_file)
Expand Down
27 changes: 27 additions & 0 deletions src/wordpress/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
import collections
import sys
import json
from urllib.parse import urlparse

import settings

Expand Down Expand Up @@ -325,3 +327,28 @@ def _add_user(self, user):
if self.run_wp_cli(cmd) is False:
logging.error("%s - wp user create failed. More details in the logs above", repr(self.wp_site))
return user

def get_page_hierarchy(self):
"""
Returns a Dict object representing the hierarchy of all the published pages of the site linked
to this WPConfig object.

Return:
dictionnary of pages hierarchy for the current site.
"""
pages = self.run_wp_cli('post list --post_type=page --fields=ID,post_title,url --format=json --order=asc')
pages = json.loads(pages)
pages_hierarchy = {}
for page in pages:
# Next line keeps the part of the URL corresponding to the page localtion:
# https://localhost/site1/page -> /page
parsed_url = urlparse(page['url'].replace(self.wp_site.url, ''))
splitted_path = parsed_url.path.split('/')
# Remove empty elements
splitted_path = list(filter(None, splitted_path))
current_depth = pages_hierarchy
# Go to the right level in the hierarchy
for ancestor_page in splitted_path[:-1]:
current_depth = current_depth[ancestor_page]
current_depth[splitted_path[-1]] = {'url': page['url']}
return pages_hierarchy