<?php
/**
 * All-in-One WP Migration
 * Copyright (C) 2014-2018 ServMask Inc.
 *
 * Modified Copyright (C) 2025 Lead Connector
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

if (!defined('ABSPATH')) {
	die('Well Sir You Cannot Jump Here...haha');
}

class Lcm_Export_Database
{

	public static function execute($params)
	{

		// Set progress
		Lcm_Status::info(__('Exporting database...', 'lc-migrator'), Lcm_Stages::$STAGES['EXPORTING_DATABASE']);

		// Set exclude database
		if (isset($params['options']['no_database'])) {
			return $params;
		}

		// Set query offset
		if (isset($params['query_offset'])) {
			$query_offset = (int) $params['query_offset'];
		} else {
			$query_offset = 0;
		}

		// Set table index
		if (isset($params['table_index'])) {
			$table_index = (int) $params['table_index'];
		} else {
			$table_index = 0;
		}

		// Set table offset
		if (isset($params['table_offset'])) {
			$table_offset = (int) $params['table_offset'];
		} else {
			$table_offset = 0;
		}

		// Set table rows
		if (isset($params['table_rows'])) {
			$table_rows = (int) $params['table_rows'];
		} else {
			$table_rows = 0;
		}

		// Set total tables count
		if (isset($params['total_tables_count'])) {
			$total_tables_count = (int) $params['total_tables_count'];
		} else {
			$total_tables_count = 1;
		}

		// What percent of tables have we processed?
		$progress = (int) (($table_index / $total_tables_count) * 100);

		// Set progress
		/* translators: 1: Progress, 2: Number of records. */
		Lcm_Status::info(sprintf(__('Exporting database... %1$d%% complete %2$s records saved', 'lc-migrator'), $progress, number_format_i18n($table_rows)), Lcm_Stages::$STAGES['EXPORTING_DATABASE']);

		// Get tables list file
		$tables_list = lcm_open(lcm_tables_list_path($params), 'r');

		// Loop over tables
		$tables = array();
		while (($row = lcm_getcsv($tables_list)) !== false) {
			list($table_name) = $row;
			$tables[] = $table_name; // phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning
		}

		// Close the tables list file
		lcm_close($tables_list);

		// Get database client
		$db_client = Lcm_Database_Utility::get_client();

		// Exclude spam comments
		if (isset($params['options']['no_spam_comments'])) {
			$db_client->set_table_where_query(lcm_table_prefix() . 'comments', "`comment_approved` != 'spam'")
				->set_table_where_query(lcm_table_prefix() . 'commentmeta', sprintf("`comment_ID` IN ( SELECT `comment_ID` FROM `%s` WHERE `comment_approved` != 'spam' )", lcm_table_prefix() . 'comments'));
		}

		// Exclude post revisions
		if (isset($params['options']['no_post_revisions'])) {
			$db_client->set_table_where_query(lcm_table_prefix() . 'posts', "`post_type` != 'revision'")
				->set_table_where_query(lcm_table_prefix() . 'postmeta', sprintf("`post_id` IN ( SELECT `ID` FROM `%s` WHERE `post_type` != 'revision' )", lcm_table_prefix() . 'posts'));
		}

		$old_table_prefixes = $old_column_prefixes = array();
		$new_table_prefixes = $new_column_prefixes = array();

		// Set table prefixes
		if (lcm_table_prefix()) {
			$old_table_prefixes[] = lcm_table_prefix();
			$new_table_prefixes[] = lcm_prefix();
		} else {
			foreach ($tables as $table_name) {
				$old_table_prefixes[] = $table_name;
				$new_table_prefixes[] = lcm_prefix() . $table_name;
			}
		}

		// Set column prefixes
		if (strlen(lcm_table_prefix()) > 1) {
			$old_column_prefixes[] = lcm_table_prefix();
			$new_column_prefixes[] = lcm_prefix();
		} else {
			foreach (array('user_roles', 'capabilities', 'user_level', 'dashboard_quick_press_last_post_id', 'user-settings', 'user-settings-time') as $column_prefix) {
				$old_column_prefixes[] = lcm_table_prefix() . $column_prefix;
				$new_column_prefixes[] = lcm_prefix() . $column_prefix;
			}
		}

		$db_client->set_tables($tables)
			->set_old_table_prefixes($old_table_prefixes)
			->set_new_table_prefixes($new_table_prefixes)
			->set_old_column_prefixes($old_column_prefixes)
			->set_new_column_prefixes($new_column_prefixes);

		// Exclude column prefixes
		$db_client->set_reserved_column_prefixes(array('wp_force_deactivated_plugins', 'wp_page_for_privacy_policy', 'wp_rocket_settings', 'wp_rocket_dismiss_imagify_notice', 'wp_rocket_no_licence', 'wp_rocket_rocketcdn_old_url', 'wp_rocket_hide_deactivation_form'));

		// Exclude site options
		$db_client->set_table_where_query(lcm_table_prefix() . 'options', sprintf("`option_name` NOT IN ('%s', '%s', '%s', '%s', '%s', '%s', '%s')", LC_MIGRATOR_STATUS, LC_MIGRATOR_SECRET_KEY_NAME, LC_MIGRATOR_AUTH_USER_NAME, LC_MIGRATOR_AUTH_PASSWORD_NAME, LC_MIGRATOR_AUTH_HEADER_NAME, LC_MIGRATOR_BACKUPS_LABELS_NAME, LC_MIGRATOR_SITES_LINKS_NAME));

		// Set table select columns
		if (($column_names = $db_client->get_column_names(lcm_table_prefix() . 'options'))) {
			if (isset($column_names['option_name'], $column_names['option_value'])) {
				$column_names['option_value'] = sprintf("(CASE WHEN option_name = '%s' THEN 'a:0:{}' WHEN (option_name = '%s' OR option_name = '%s') THEN '' ELSE option_value END) AS option_value", LC_MIGRATOR_ACTIVE_PLUGINS, LC_MIGRATOR_ACTIVE_TEMPLATE, LC_MIGRATOR_ACTIVE_STYLESHEET);
			}

			$db_client->set_table_select_columns(lcm_table_prefix() . 'options', $column_names);
		}

		// Set table prefix columns
		$db_client->set_table_prefix_columns(lcm_table_prefix() . 'options', array('option_name'))
			->set_table_prefix_columns(lcm_table_prefix() . 'usermeta', array('meta_key'));

		// Export database
		if ($db_client->export(lcm_database_path($params), $query_offset, $table_index, $table_offset, $table_rows)) {

			// Set progress
			Lcm_Status::info(__('Database exported.', 'lc-migrator'), Lcm_Stages::$STAGES['EXPORTING_DATABASE']);

			// Unset query offset
			unset($params['query_offset']);

			// Unset table index
			unset($params['table_index']);

			// Unset table offset
			unset($params['table_offset']);

			// Unset table rows
			unset($params['table_rows']);

			// Unset total tables count
			unset($params['total_tables_count']);

			// Unset completed flag
			unset($params['completed']);

		} else {

			// What percent of tables have we processed?
			$progress = (int) (($table_index / $total_tables_count) * 100);

			// Set progress
			/* translators: 1: Progress, 2: Number of records. */
			Lcm_Status::info(sprintf(__('Exporting database... %1$d%% complete %2$s records saved', 'lc-migrator'), $progress, number_format_i18n($table_rows)), Lcm_Stages::$STAGES['EXPORTING_DATABASE']);

			// Set query offset
			$params['query_offset'] = $query_offset;

			// Set table index
			$params['table_index'] = $table_index;

			// Set table offset
			$params['table_offset'] = $table_offset;

			// Set table rows
			$params['table_rows'] = $table_rows;

			// Set total tables count
			$params['total_tables_count'] = $total_tables_count;

			// Set completed flag
			$params['completed'] = false;
		}

		return $params;
	}
}
