<commit_before><?php
/*
 * This Jumpstarter init file should be pre executed before the web server
 * starts. It indempotently handles automatic wordpress installation if the
 * app is starting for the first time in an instance.
 *
 * You should pre_exec this script before starting the web server by running:
 * php /app/code/src/wp-content/plugins/jumpstarter/js-init.php
 */

require_once "js-env.php";

// Log to stderr.
function js_log($msg) {
    fwrite(STDERR, "[js-init.php] $msg\n");
}

// Execute wrapper that throws exception and leaks output if command fails.
function js_eexec($cmd) {
    exec($cmd, $out, $ret);
    // The execute could have side effects on the file system that PHP
    // doesn't know about. We need to clear the stat cache.
    clearstatcache();
    if ($ret !== 0) {
        fwrite(STDERR, implode("\n", $out));
        throw new Exception("executing cmd [$cmd] failed");
    }
}

function js_db_state_dir() {
    return "/app/state/wp-db";
}

function js_init_state_dir() {
    return "/app/code/js-init-state";
}

// Returns core wp-jumpstarter plugins that can never be disabled.
function js_core_plugins() {
    return array(
        "jumpstarter/jumpstarter.php",
        "sqlite-integration/sqlite-integration.php",
    );
}

function js_include_wp() {
    // Only include once.
    static $wp_included = false;
    if ($wp_included)
        return;

    // Include wordpress from CLI.
    define("ABSPATH", realpath(__DIR__ . "/../../../") . "/");
    $_SERVER['HTTPS'] = "on";
    js_log("including wp-load.php");
    require_once(ABSPATH . "wp-load.php");
    js_log("including upgrade.php");
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");

    // Dev sanity check: Ensure that DISALLOW_FILE_MODS is set to the right value.
    if (!defined("DISALLOW_FILE_MODS") || DISALLOW_FILE_MODS !== true) {
        js_log("error: DISALLOW_FILE_MODS is not set to the correct value in wp-config.php, it should be set to [true]");
        exit;
    }

    // Dev sanity check: Ensure that DB_DIR is set to the right value.
    $db_state_dir = js_db_state_dir();
    if (!defined("DB_DIR") || DB_DIR !== $db_state_dir) {
        js_log("error: DB_DIR is not set to the correct value in wp-config.php, it should be set to [$db_state_dir]");
        exit;
    }

    // Dev sanity check: Ensure that there is no database folder or symlink.
    $bad_db_path = (ABSPATH . "wp-content/database");
    if (file_exists($bad_db_path) || is_link($bad_db_path)) {
        js_log("error: the path [$bad_db_path] exists, it's a security hazard and should be removed");
        exit;
    }

    // Wordpress is included now.
    $wp_included = true;
}

function js_activate_plugin($plugin_path) {
    $result = activate_plugin($plugin_path);
    if ($result !== null) {
        js_log("failed to activate plugin [$plugin_path]: " . json_encode(array($result->get_error_messages(), $result->get_error_data())));
        exit(1);
    }
}

function js_run_install_hooks() {
    global $js_install_hooks;
    $js_install_hooks = array();
    function js_install_hook($name, $fn) {
        global $js_install_hooks;
        $js_install_hooks[] = array("name" => $name, "fn" => $fn);
    }
    foreach(glob("/app/code/js-install-hooks/*.php") as $file_path) {
        require_once $file_path;
    }
    foreach($js_install_hooks as $ih_arr) {
        js_log("running install hook: $ih_arr[name]");
        try {
            if (!$ih_arr["fn"]()) {
                js_log("error running hook: $ih_arr[name]");
                exit(1);
            }
        } catch (Exception $e) {
            js_log($e->getMessage());
            exit(1);
        }
    }
}

function js_install_wp() {
    $db_state_dir = js_db_state_dir();
    // Create symlink to allow extremly fast ram based install.
    $db_tmp_dir = "/tmp/wp-db";
    if (file_exists($db_tmp_dir))
        js_eexec("rm -rf " . escapeshellarg($db_tmp_dir));
    js_eexec("mkdir " . escapeshellarg($db_tmp_dir));
    js_eexec("ln -s " . escapeshellarg($db_tmp_dir) . " " . escapeshellarg($db_state_dir));

    // When exiting before succesfull install, return bad error code.
    $install_ok = false;
    register_shutdown_function(function() use (&$install_ok) {
        if (!$install_ok)
            exit(1);
    });

    // Read and prepare configuration for automatic install.
    $is_assembly = !empty(js_env_get_value("ident.container.is_assembly"));
    $blog_title = "My blog";
    $user_name = "admin";
    $user_email = strval(js_env_get_value("ident.user.email"));
    $public = true;
    $deprecated = null;
    // Let wordpress generate a random password for instances, for assemblies, use "test".
    $user_password = $is_assembly? "test": null;
    $language = null;

    // We pretend a user visited wordpress to install it through the generated domain.
    define("WP_SITEURL", js_env_get_siteurl());
    define("WP_INSTALLING", true);
    define("WP_ADMIN", true);

    // Include wordpress definitions and config.
    js_include_wp();

    // Install wordpress now.
    js_log("running wordpress installer with name:[$user_name], email:[$user_email], password:[$user_password]");
    wp_install($blog_title, $user_name, $user_email, $public, $deprecated, $user_password, $language);

    // Silently activate all required plugins.
    // The developer should have removed plugins that should not be activated.
    js_log("activate plugins");
    foreach (js_core_plugins() as $plugin_key) {
        js_log("activating core plugin [$plugin_key]");
        js_activate_plugin($plugin_key);
    }

    // Search for install hooks and run them.
    js_run_install_hooks();

    // Copy the database over to the state and atomically move it in place to mark wordpress as installed.
    // We delete any old temporary db state to make the install indempotent.
    js_log("copying installed database from ram to state");
    $db_state_tmp_dir = "/app/state/wp-db.tmp";
    if (file_exists($db_state_tmp_dir))
        js_eexec("rm -rf " . escapeshellarg($db_state_tmp_dir));
    js_eexec("cp -rp ". escapeshellarg($db_tmp_dir) . " " . escapeshellarg($db_state_tmp_dir));
    js_eexec("rm -rf " . escapeshellarg($db_tmp_dir));
    js_eexec("rm " . escapeshellarg($db_state_dir));
    js_log("syncing data to state");
    js_eexec("sync");
    js_log("final atomic move");
    js_eexec("mv ". escapeshellarg($db_state_tmp_dir) . " " . escapeshellarg($db_state_dir));

    // Wait for sync before considering installation complete.
    js_eexec("sync");
    $install_ok = true;
    js_log("succesfully installed wordpress!");

    // Restart the CLI script to run the init phase again to test with an installed wordpress and sync env.
    global $argv;
    $execve_path = PHP_BINARY;
    $execve_args = $argv;
    js_log("****** restarting: [$execve_path], [" . implode(", ", $execve_args) . "] ******");
    pcntl_exec($execve_path, $execve_args);
    exit(1);
}

function js_update_siteurl($old_siteurl, $new_siteurl) {
    global $wpdb;
    // Update post contents.
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET post_content = replace(post_content, %s, %s)", $old_siteurl, $new_siteurl));
    // Update post meta.
    $wpdb->query($wpdb->prepare("UPDATE wp_postmeta SET meta_value = replace(meta_value, %s, %s)", $old_siteurl, $new_siteurl));
    // Update options.
    $wpdb->query($wpdb->prepare("UPDATE wp_options SET option_value= replace(option_value, %s, %s)", $old_siteurl, $new_siteurl));
    update_option("siteurl", $new_siteurl);
    update_option("home", $new_siteurl);
}

function js_sync_wp_with_env() {
    js_log("starting env sync");
    // Include wordpress definitions and config.
    js_include_wp();
    // Sync wordpress domain if it changed.
    $wp_siteurl = get_option("siteurl");
    $env_siteurl = js_env_get_siteurl();
    if ($wp_siteurl !== $env_siteurl) {
        js_log("siteurl change detected, migrating from [$wp_siteurl] to [$env_siteurl]");
        js_update_siteurl($wp_siteurl, $env_siteurl);
    } else {
        js_log("no siteurl change detected, keeping [$wp_siteurl]");
    }
    // Apply (sync) wordpress theme from env.
    $stylesheet = js_env_get_theme();
    if (is_string($stylesheet)) {
        js_log("setting theme [$stylesheet]");
        $theme = wp_get_theme($stylesheet);
        if (!$theme->exists())
            throw new Exception("theme to install [$stylesheet] not found!");<commit_msg>take care of serialized values in updating of meta and options<commit_after><?php
/*
 * This Jumpstarter init file should be pre executed before the web server
 * starts. It indempotently handles automatic wordpress installation if the
 * app is starting for the first time in an instance.
 *
 * You should pre_exec this script before starting the web server by running:
 * php /app/code/src/wp-content/plugins/jumpstarter/js-init.php
 */

require_once "js-env.php";

// Log to stderr.
function js_log($msg) {
    fwrite(STDERR, "[js-init.php] $msg\n");
}

// Execute wrapper that throws exception and leaks output if command fails.
function js_eexec($cmd) {
    exec($cmd, $out, $ret);
    // The execute could have side effects on the file system that PHP
    // doesn't know about. We need to clear the stat cache.
    clearstatcache();
    if ($ret !== 0) {
        fwrite(STDERR, implode("\n", $out));
        throw new Exception("executing cmd [$cmd] failed");
    }
}

function js_db_state_dir() {
    return "/app/state/wp-db";
}

function js_init_state_dir() {
    return "/app/code/js-init-state";
}

// Returns core wp-jumpstarter plugins that can never be disabled.
function js_core_plugins() {
    return array(
        "jumpstarter/jumpstarter.php",
        "sqlite-integration/sqlite-integration.php",
    );
}

function js_include_wp() {
    // Only include once.
    static $wp_included = false;
    if ($wp_included)
        return;

    // Include wordpress from CLI.
    define("ABSPATH", realpath(__DIR__ . "/../../../") . "/");
    $_SERVER['HTTPS'] = "on";
    js_log("including wp-load.php");
    require_once(ABSPATH . "wp-load.php");
    js_log("including upgrade.php");
    require_once(ABSPATH . "wp-admin/includes/upgrade.php");

    // Dev sanity check: Ensure that DISALLOW_FILE_MODS is set to the right value.
    if (!defined("DISALLOW_FILE_MODS") || DISALLOW_FILE_MODS !== true) {
        js_log("error: DISALLOW_FILE_MODS is not set to the correct value in wp-config.php, it should be set to [true]");
        exit;
    }

    // Dev sanity check: Ensure that DB_DIR is set to the right value.
    $db_state_dir = js_db_state_dir();
    if (!defined("DB_DIR") || DB_DIR !== $db_state_dir) {
        js_log("error: DB_DIR is not set to the correct value in wp-config.php, it should be set to [$db_state_dir]");
        exit;
    }

    // Dev sanity check: Ensure that there is no database folder or symlink.
    $bad_db_path = (ABSPATH . "wp-content/database");
    if (file_exists($bad_db_path) || is_link($bad_db_path)) {
        js_log("error: the path [$bad_db_path] exists, it's a security hazard and should be removed");
        exit;
    }

    // Wordpress is included now.
    $wp_included = true;
}

function js_activate_plugin($plugin_path) {
    $result = activate_plugin($plugin_path);
    if ($result !== null) {
        js_log("failed to activate plugin [$plugin_path]: " . json_encode(array($result->get_error_messages(), $result->get_error_data())));
        exit(1);
    }
}

function js_run_install_hooks() {
    global $js_install_hooks;
    $js_install_hooks = array();
    function js_install_hook($name, $fn) {
        global $js_install_hooks;
        $js_install_hooks[] = array("name" => $name, "fn" => $fn);
    }
    foreach(glob("/app/code/js-install-hooks/*.php") as $file_path) {
        require_once $file_path;
    }
    foreach($js_install_hooks as $ih_arr) {
        js_log("running install hook: $ih_arr[name]");
        try {
            if (!$ih_arr["fn"]()) {
                js_log("error running hook: $ih_arr[name]");
                exit(1);
            }
        } catch (Exception $e) {
            js_log($e->getMessage());
            exit(1);
        }
    }
}

function js_install_wp() {
    $db_state_dir = js_db_state_dir();
    // Create symlink to allow extremly fast ram based install.
    $db_tmp_dir = "/tmp/wp-db";
    if (file_exists($db_tmp_dir))
        js_eexec("rm -rf " . escapeshellarg($db_tmp_dir));
    js_eexec("mkdir " . escapeshellarg($db_tmp_dir));
    js_eexec("ln -s " . escapeshellarg($db_tmp_dir) . " " . escapeshellarg($db_state_dir));

    // When exiting before succesfull install, return bad error code.
    $install_ok = false;
    register_shutdown_function(function() use (&$install_ok) {
        if (!$install_ok)
            exit(1);
    });

    // Read and prepare configuration for automatic install.
    $is_assembly = !empty(js_env_get_value("ident.container.is_assembly"));
    $blog_title = "My blog";
    $user_name = "admin";
    $user_email = strval(js_env_get_value("ident.user.email"));
    $public = true;
    $deprecated = null;
    // Let wordpress generate a random password for instances, for assemblies, use "test".
    $user_password = $is_assembly? "test": null;
    $language = null;

    // We pretend a user visited wordpress to install it through the generated domain.
    define("WP_SITEURL", js_env_get_siteurl());
    define("WP_INSTALLING", true);
    define("WP_ADMIN", true);

    // Include wordpress definitions and config.
    js_include_wp();

    // Install wordpress now.
    js_log("running wordpress installer with name:[$user_name], email:[$user_email], password:[$<PASSWORD>]");
    wp_install($blog_title, $user_name, $user_email, $public, $deprecated, $user_password, $language);

    // Silently activate all required plugins.
    // The developer should have removed plugins that should not be activated.
    js_log("activate plugins");
    foreach (js_core_plugins() as $plugin_key) {
        js_log("activating core plugin [$plugin_key]");
        js_activate_plugin($plugin_key);
    }

    // Search for install hooks and run them.
    js_run_install_hooks();

    // Copy the database over to the state and atomically move it in place to mark wordpress as installed.
    // We delete any old temporary db state to make the install indempotent.
    js_log("copying installed database from ram to state");
    $db_state_tmp_dir = "/app/state/wp-db.tmp";
    if (file_exists($db_state_tmp_dir))
        js_eexec("rm -rf " . escapeshellarg($db_state_tmp_dir));
    js_eexec("cp -rp ". escapeshellarg($db_tmp_dir) . " " . escapeshellarg($db_state_tmp_dir));
    js_eexec("rm -rf " . escapeshellarg($db_tmp_dir));
    js_eexec("rm " . escapeshellarg($db_state_dir));
    js_log("syncing data to state");
    js_eexec("sync");
    js_log("final atomic move");
    js_eexec("mv ". escapeshellarg($db_state_tmp_dir) . " " . escapeshellarg($db_state_dir));

    // Wait for sync before considering installation complete.
    js_eexec("sync");
    $install_ok = true;
    js_log("succesfully installed wordpress!");

    // Restart the CLI script to run the init phase again to test with an installed wordpress and sync env.
    global $argv;
    $execve_path = PHP_BINARY;
    $execve_args = $argv;
    js_log("****** restarting: [$execve_path], [" . implode(", ", $execve_args) . "] ******");
    pcntl_exec($execve_path, $execve_args);
    exit(1);
}

function rec_replace_string($search_string, $replacement, $data, $serialized = false) {
    try {
        if (is_string($data) && ($unserialized = @unserialize($data)) !== false) {
            $data = rec_replace_string($search_string, $replacement, $unserialized, true);
        } elseif (is_array($data)) {
            $tmp = array();
            foreach($data as $key => $value) {
                $tmp[$key] = rec_replace_string($search_string, $replacement, $value, false);
            }
            $data = $tmp;
            unset($tmp);
        } else {
            if (is_string($data)) {
                $data = str_replace($search_string, $replacement, $data);
            }
        }
        if ($serialized) {
            return serialize($data);
        }
    } catch (Exception $ex) {

    }
    return $data;
}

function js_update_siteurl($old_siteurl, $new_siteurl) {
    global $wpdb;
    // Update post contents.
    $posts = $wpdb->get_results("SELECT ID, post_content FROM wp_posts");
    foreach($posts as $post) {
        $post_content = $post->post_content;
        $modified = rec_replace_string($old_siteurl, $new_siteurl, $post_content);
        if ($post_content !== $modified) {
            $wpdb->query($wpdb->prepare("UPDATE wp_posts SET post_content = %s WHERE ID = %s", $modified, $post->ID));
        }
    }
    // Update post meta.
    $post_metas = $wpdb->get_results("SELECT meta_id, meta_value FROM wp_postmeta");
    foreach($post_metas as $post_meta) {
        $meta_value = $post_meta->meta_value;
        $modified = rec_replace_string($old_siteurl, $new_siteurl, $meta_value);
        if ($meta_value !== $modified) {
            $wpdb->query($wpdb->prepare("UPDATE wp_postmeta SET meta_value = %s WHERE meta_id = %s", $modified, $post_meta->meta_id));
        }
    }
    // Update options.
    $options = $wpdb->get_results("SELECT option_id, option_value FROM wp_options");
    foreach($options as $option) {
        $option_value = $option->option_value;
        $modified = rec_replace_string($old_siteurl, $new_siteurl, $option_value);
        if ($option_value !== $modified) {
            $wpdb->query($wpdb->prepare("UPDATE wp_options SET option_value = %s WHERE option_id = %s", $modified, $option->option_id));
        }
    }
    update_option("siteurl", $new_siteurl);
    update_option("home", $new_siteurl);
}

function js_sync_wp_with_env() {
    js_log("starting env sync");
    // Include wordpress definitions and config.
    js_include_wp();
    // Sync wordpress domain if it changed.
    $wp_siteurl = get_option("siteurl");
    $env_siteurl = js_env_get_siteurl();
    if ($wp_siteurl !== $env_siteurl) {
        js_log("siteurl change detected, migrating from [$wp_siteurl] to [$env_siteurl]");
        js_update_siteurl($wp_siteurl, $env_siteurl);
    } else {
        js_log("no siteurl change detected, keeping [$wp_siteurl]");
    }
    // Apply (sync) wordpress theme from env.
    $stylesheet = js_env_get_theme();
    if (is_string($stylesheet)) {
        js_log("setting theme [$stylesheet]");
        $theme = wp_get_theme($stylesheet);
        if (!$theme->exists())
            throw new Exception("theme to install [$stylesheet] not found!");
{'fraction_non_alphanumeric': 0.1363100974408395, 'fraction_numerical': 0.00064246707356248, 'mean_word_length': 3.1712818222420722, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2164116', 'n_tokens_mistral': 6066, 'n_tokens_neox': 5945, 'n_words': 1513}