Ejemplo n.º 1
0
def generate(wp_env,
             wp_url,
             wp_title=None,
             wp_tagline=None,
             admin_password=None,
             theme=None,
             theme_faculty=None,
             installs_locked=None,
             updates_automatic=None,
             extra_config=None,
             **kwargs):
    """
    This command may need more params if reference to them are done in YAML file. In this case, you'll see an
    error explaining which params are needed and how they can be added to command line
    """

    # if nothing is specified we want a locked install
    if installs_locked is None:
        installs_locked = DEFAULT_CONFIG_INSTALLS_LOCKED
    else:
        installs_locked = cast_boolean(installs_locked)

    # if nothing is specified we want automatic updates
    if updates_automatic is None:
        updates_automatic = DEFAULT_CONFIG_UPDATES_AUTOMATIC
    else:
        updates_automatic = cast_boolean(updates_automatic)

    # FIXME: When we will use 'unit_id' from CSV file, add parameter here OR dynamically get it from AD
    all_params = {
        'openshift_env': wp_env,
        'wp_site_url': wp_url,
        'theme': theme or DEFAULT_THEME_NAME,
        'installs_locked': installs_locked,
        'updates_automatic': updates_automatic
    }

    # Adding parameters if given
    if theme_faculty is not None:
        all_params['theme_faculty'] = theme_faculty

    if wp_title is not None:
        all_params['wp_site_title'] = wp_title

    if wp_tagline is not None:
        all_params['wp_tagline'] = wp_tagline

    # if we have extra configuration to load,
    if extra_config is not None:
        all_params = _add_extra_config(extra_config, all_params)

    wp_generator = WPGenerator(all_params, admin_password=admin_password)

    if not wp_generator.generate():
        raise Exception("Generation failed. More info above")

    print("Successfully created new WordPress site at {}".format(
        wp_generator.wp_site.url))
Ejemplo n.º 2
0
def generate(wp_env,
             wp_url,
             wp_title=None,
             admin_password=None,
             unit_name=None,
             theme=None,
             theme_faculty=None,
             installs_locked=None,
             updates_automatic=None,
             **kwargs):

    # if nothing is specified we want a locked install
    if installs_locked is None:
        installs_locked = DEFAULT_CONFIG_INSTALLS_LOCKED
    else:
        installs_locked = cast_boolean(installs_locked)

    # if nothing is specified we want automatic updates
    if updates_automatic is None:
        updates_automatic = DEFAULT_CONFIG_UPDATES_AUTOMATIC
    else:
        updates_automatic = cast_boolean(updates_automatic)

    wp_generator = WPGenerator(wp_env,
                               wp_url,
                               wp_default_site_title=wp_title,
                               admin_password=admin_password,
                               unit_name=unit_name,
                               theme=theme or DEFAULT_THEME_NAME,
                               theme_faculty=theme_faculty,
                               installs_locked=installs_locked,
                               updates_automatic=updates_automatic)
    if not wp_generator.generate():
        raise SystemExit("Generation failed. More info above")

    print("Successfully created new WordPress site at {}".format(
        wp_generator.wp_site.url))
Ejemplo n.º 3
0
def export(site,
           wp_site_url,
           unit_name_or_id,
           to_wordpress=False,
           clean_wordpress=False,
           to_dictionary=False,
           admin_password=None,
           output_dir=None,
           theme=None,
           installs_locked=False,
           updates_automatic=False,
           openshift_env=None,
           use_cache=None,
           keep_extracted_files=False,
           features_flags=False,
           category=None,
           **kwargs):
    """
    Export the jahia content into a WordPress site.

    :param site: the name of the WordPress site
    :param wp_site_url: URL of WordPress site
    :param unit_name_or_id: unit name or unit ID of the WordPress site
    :param to_wordpress: to migrate data
    :param clean_wordpress: to clean data
    :param admin_password: an admin password
    :param output_dir: directory where the jahia zip file will be unzipped
    :param theme: WordPress theme used for the WordPress site
    :param installs_locked: boolean
    :param updates_automatic: boolean
    :param openshift_env: openshift_env environment (prod, int, gcharmier ...)
    :param keep_extracted_files: command to keep files extracted from jahia zip
    :param features_flags: Tell to clean page content or not
    :param category: Site category which defines plugin list to install and configure
    """

    # Download, Unzip the jahia zip and parse the xml data
    site = parse(site=site, use_cache=use_cache, output_dir=output_dir)

    # Define the default language
    default_language = _get_default_language(site.languages)

    # For polylang plugin, we need position default lang in first position
    languages = _set_default_language_in_first_position(
        default_language, site.languages)

    if not site.acronym[default_language]:
        logging.warning("No wp site title in %s", default_language)
        wp_site_title = None
    else:
        wp_site_title = site.acronym[default_language]

    # theme
    if not site.theme[default_language] or site.theme[
            default_language] == "epfl":
        theme_faculty = ""
    else:
        theme_faculty = site.theme[default_language]

    if not theme:
        # Setting correct theme depending on parsing result
        theme = BANNER_THEME_NAME if default_language in site.banner else DEFAULT_THEME_NAME

    # If nothing specified, we use default
    if category is None:
        category = DEFAULT_WP_SITE_CATEGORY

    # tagline
    if not site.title[default_language]:
        logging.warning("No wp tagline in %s", default_language)
        wp_tagline = None
    else:
        wp_tagline = site.title

    # If we get unit ID
    if unit_name_or_id.isdigit():
        unit_id = unit_name_or_id

        # fetch unit name from ldap
        try:
            logging.info("Fetching LDAP for unit '%s' name...", unit_id)
            unit_name = get_unit_name(unit_id)
            logging.info("LDAP name found = %s...", unit_name)
        except LDAPSocketOpenError:
            logging.error("LDAP is not responding, aborting here...")
            raise

    else:  # We get unit name

        unit_name = unit_name_or_id
        # fetch unit id from ldap
        try:
            logging.info("Fetching LDAP for unit '%s' ID...", unit_name)
            unit_id = get_unit_id(unit_name)
            logging.info("LDAP ID found = %s...", unit_id)
        except LDAPSocketOpenError:
            logging.error("LDAP is not responding, aborting here...")
            raise

    info = {
        # information from parser
        'langs': ",".join(languages),
        'wp_site_title': wp_site_title,
        'wp_tagline': wp_tagline,
        'theme_faculty': theme_faculty,
        'unit_name': unit_name,

        # information from source of truth
        'openshift_env': openshift_env,
        'wp_site_url': wp_site_url,
        'theme': theme,
        'updates_automatic': updates_automatic,
        'installs_locked': installs_locked,
        'category': category,

        # determined information
        'unit_id': unit_id,
        'from_export': True
    }

    # skip options, used only during development
    #
    # skip_base: if True don't install WordPress, use the existing site
    # skip_media: if True don't import the media
    # skip_pages: if True don't import the pages
    skip_base = False
    skip_media = False
    skip_pages = False

    # List of plugins to let in 'deactivated' state during import. To earn more time, they are not activated during
    # WordPress empty site generation. Because activating them takes time and we have to take the same amount of time
    # to deactivate them before running Jahia site import.
    # Deactivating plugins can improve import time by ~80%
    # WARNING: be careful with the list order. Plugins will be reactivated after import by using list order. So if
    # there are dependencies between plugins, arrange them in the right way.
    deactivated_plugins = [
        'mainwp-child',
        'epfl-faq',
        'epfl-grid',
        'epfl-infoscience',
        'epfl-infoscience-search',
        'epfl-map',
        'epfl-memento',
        'epfl-news',
        'epfl-people',
        'epfl-scheduler',
        'EPFL-Content-Filter',
        'EPFL-Share',
        'epfl-snippet',
        'epfl-toggle',
        'epfl-tableau',
        'epfl-twitter',
        'epfl-xml',
        'epfl-video',
        'epfl-404',
        'epfl-stats',
        'epfl-google-forms',
        'feedzy-rss-feeds',
        'cache-control',
        'remote-content-shortcode',
        'shortcode-ui',
        'shortcode-ui-richtext',  # This one needs to come after the previous one
        'shortcodes-ultimate',
        'simple-sitemap',
        'svg-support',
        'enlighter',
        'pdfjs-viewer-shortcode',
        'tinymce-advanced',
        'varnish-http-purge'
    ]

    # Generate a WordPress site
    wp_generator = WPGenerator(info, admin_password)

    # base installation
    if skip_base:

        logging.info("Deactivating %s plugins...", len(deactivated_plugins))
        for plugin_name in deactivated_plugins:
            # We do a 'try' to handle missing plugins (if exists)
            try:
                wp_generator.run_wp_cli(
                    "plugin deactivate {}".format(plugin_name))
            except:
                logging.info("Plugin %s doesn't seem's to be installed",
                             plugin_name)

        try:

            # even if we skip the base installation we need to reactivate
            # the basic auth plugin for the rest API
            wp_generator.run_wp_cli("plugin activate Basic-Auth")
        except:
            # if activation fails it means the plugin is not installed
            wp_generator.install_basic_auth_plugin()
    else:
        # If returns false, it means there was an error
        if not wp_generator.generate(deactivated_plugins):
            # We just display line to add to CSV
            _generate_csv_line(wp_generator)
            return

        wp_generator.install_basic_auth_plugin()

    # dual auth
    if settings.ACTIVE_DUAL_AUTH:
        wp_generator.active_dual_auth()

    # exporter
    wp_exporter = WPExporter(site,
                             wp_generator,
                             default_language,
                             output_dir=output_dir)

    # clean
    if clean_wordpress:
        logging.info("Cleaning WordPress for %s...", site.name)
        wp_exporter.delete_all_content()
        logging.info("Data of WordPress site %s successfully deleted",
                     site.name)

    # to WordPress
    if to_wordpress:
        logging.info("Exporting %s to WordPress...", site.name)
        try:

            if wp_generator.get_number_of_pages() == 0:

                wp_exporter.import_data_to_wordpress(
                    skip_pages=skip_pages,
                    skip_media=skip_media,
                    features_flags=features_flags)
                wp_exporter.write_redirections()
                _fix_menu_location(wp_generator, languages, default_language)

                logging.info("Reactivating %s plugins...",
                             len(deactivated_plugins))

                # Reactivating plugins
                for plugin_name in deactivated_plugins:
                    # We do a 'try' to handle missing plugins (if exists)
                    try:
                        wp_generator.run_wp_cli(
                            "plugin activate {}".format(plugin_name))
                    except:
                        logging.info(
                            "Plugin %s doesn't seem's to be installed",
                            plugin_name)

                logging.info("Site %s successfully exported to WordPress",
                             site.name)
            else:
                logging.info("Site %s already exported to WordPress",
                             site.name)
        except (Exception, subprocess.CalledProcessError) as e:
            logging.error(str(e))
            Tracer.write_row(site=site.name, step=e, status="KO")
            if not settings.DEBUG:
                wp_generator.clean()
            raise e

        Tracer.write_row(site=site.name, step="export", status="OK")

    wp_generator.uninstall_basic_auth_plugin()
    wp_generator.enable_updates_automatic_if_allowed()

    # to dictionary
    if to_dictionary:
        data = DictExporter.generate_data(site)
        pprint(data, width=settings.LINE_LENGTH_ON_PPRINT)

    _generate_csv_line(wp_generator)

    if not keep_extracted_files:
        # Delete extracted zip files
        # We take dirname because site.base_path is the path to the subfolder in the zip.
        # Example : path_to_extract/dcsl/dcsl
        # And we want to delete path_to_extract/dcsl
        base_zip_path = os.path.dirname(os.path.abspath(site.base_path))
        logging.debug("Removing zip extracted folder '%s'", base_zip_path)
        if os.path.exists(base_zip_path):
            shutil.rmtree(base_zip_path)

    return wp_exporter
Ejemplo n.º 4
0
def export(site,
           wp_site_url,
           unit_name,
           to_wordpress=False,
           clean_wordpress=False,
           admin_password=None,
           output_dir=None,
           theme=None,
           installs_locked=False,
           updates_automatic=False,
           openshift_env=None,
           **kwargs):
    """
    Export the jahia content into a WordPress site.

    :param site: the name of the WordPress site
    :param wp_site_url: URL of WordPress site
    :param unit_name: unit name of the WordPress site
    :param to_wordpress: to migrate data
    :param clean_wordpress: to clean data
    :param admin_password: an admin password
    :param output_dir: directory where the jahia zip file will be unzipped
    :param theme: WordPress theme used for the WordPress site
    :param installs_locked: boolean
    :param updates_automatic: boolean
    :param openshift_env: openshift_env environment (prod, int, gcharmier ...)
    """

    # Download, Unzip the jahia zip and parse the xml data
    site = parse(site=site)

    # Define the default language
    default_language = _get_default_language(site.languages)

    # For polylang plugin, we need position default lang in first position
    languages = _set_default_language_in_first_position(
        default_language, site.languages)

    info = {
        # information from parser
        'langs': ",".join(languages),
        'wp_site_title': site.acronym[default_language],
        'wp_tagline': site.title[default_language],
        'theme_faculty': site.theme[default_language],
        'unit_name': unit_name,

        # information from source of truth
        'openshift_env': openshift_env,
        'wp_site_url': wp_site_url,
        'theme': theme,
        'updates_automatic': updates_automatic,
        'installs_locked': installs_locked,

        # determined information
        'unit_id': get_unit_id(unit_name),
    }

    # Generate a WordPress site
    wp_generator = WPGenerator(info, admin_password)
    wp_generator.generate()

    wp_generator.install_basic_auth_plugin()

    if settings.ACTIVE_DUAL_AUTH:
        wp_generator.active_dual_auth()

    wp_exporter = WPExporter(site, wp_generator, output_dir)

    if to_wordpress:
        logging.info("Exporting %s to WordPress...", site.name)
        wp_exporter.import_all_data_to_wordpress()
        _fix_menu_location(wp_generator, languages, default_language)
        logging.info("Site %s successfully exported to WordPress", site.name)

    if clean_wordpress:
        logging.info("Cleaning WordPress for %s...", site.name)
        wp_exporter.delete_all_content()
        logging.info("Data of WordPress site %s successfully deleted",
                     site.name)

    wp_generator.uninstall_basic_auth_plugin()

    return wp_exporter