<?php
/**
 * OpenMage
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * @category   Mage
 * @package    Mage_Backup
 * @copyright  Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
 * @copyright  Copyright (c) 2017-2022 The OpenMage Contributors (https://www.openmage.org)
 * @license    https://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Database backup model
 *
 * @category   Mage
 * @package    Mage_Backup
 * @author     Magento Core Team <core@magentocommerce.com>
 */
class Mage_Backup_Model_Db
{
    /**
     * Buffer length for multi rows
     * default 100 Kb
     *
     */
    public const BUFFER_LENGTH = 102400;

    /**
     * List of tables which data should not be backed up
     *
     * @var array
     */
    protected $_ignoreDataTablesList = [
        'importexport/importdata'
    ];

    /**
     * Retrieve resource model
     *
     * @return Mage_Backup_Model_Resource_Db
     */
    public function getResource()
    {
        return Mage::getResourceSingleton('backup/db');
    }

    public function getTables()
    {
        return $this->getResource()->getTables();
    }

    public function getTableCreateScript($tableName, $addDropIfExists = false)
    {
        return $this->getResource()->getTableCreateScript($tableName, $addDropIfExists);
    }

    public function getTableDataDump($tableName)
    {
        return $this->getResource()->getTableDataDump($tableName);
    }

    public function getHeader()
    {
        return $this->getResource()->getHeader();
    }

    public function getFooter()
    {
        return $this->getResource()->getFooter();
    }

    public function renderSql()
    {
        ini_set('max_execution_time', 0);
        $sql = $this->getHeader();

        $tables = $this->getTables();
        foreach ($tables as $tableName) {
            $sql .= $this->getTableCreateScript($tableName, true);
            $sql .= $this->getTableDataDump($tableName);
        }

        $sql .= $this->getFooter();
        return $sql;
    }

    /**
     * Create backup and stream write to adapter
     *
     * @param Mage_Backup_Model_Backup $backup
     * @return $this
     */
    public function createBackup(Mage_Backup_Model_Backup $backup)
    {
        $backup->open(true);

        $this->getResource()->beginTransaction();
        try {
            $tables = $this->getResource()->getTables();

            $backup->write($this->getResource()->getHeader());

            $ignoreDataTablesList = $this->getIgnoreDataTablesList();

            foreach ($tables as $table) {
                $backup->write($this->getResource()->getTableHeader($table)
                    . $this->getResource()->getTableDropSql($table) . "\n");
                $backup->write($this->getResource()->getTableCreateSql($table, false) . "\n");

                $tableStatus = $this->getResource()->getTableStatus($table);

                if ($tableStatus->getRows() && !in_array($table, $ignoreDataTablesList)) {
                    $backup->write($this->getResource()->getTableDataBeforeSql($table));

                    if ($tableStatus->getDataLength() > self::BUFFER_LENGTH) {
                        if ($tableStatus->getAvgRowLength() > 0 && $tableStatus->getAvgRowLength() < self::BUFFER_LENGTH) {
                            // Process rows in batches
                            $limit = floor(self::BUFFER_LENGTH / $tableStatus->getAvgRowLength());
                            $multiRowsLength = ceil($tableStatus->getRows() / $limit);
                        } else {
                            // Process rows one by one
                            $limit = 1;
                            $multiRowsLength = $tableStatus->getRows();
                        }
                    } else {
                        // Process all rows at once
                        $limit = $tableStatus->getRows();
                        $multiRowsLength = 1;
                    }

                    for ($i = 0; $i < $multiRowsLength; $i++) {
                        $backup->write($this->getResource()->getTableDataSql($table, $limit, $i * $limit));
                    }

                    $backup->write($this->getResource()->getTableDataAfterSql($table));
                }
            }
            $backup->write($this->getResource()->getTableForeignKeysSql());
            $backup->write($this->getResource()->getFooter());

            $this->getResource()->commitTransaction();
        } catch (Exception $e) {
            $this->getResource()->rollBackTransaction();
            throw $e;
        }

        $backup->close();

        return $this;
    }

    /**.
     * Returns the list of tables which data should not be backed up
     *
     * @return array
     */
    public function getIgnoreDataTablesList()
    {
        $result = [];
        $resource = Mage::getSingleton('core/resource');

        foreach ($this->_ignoreDataTablesList as $table) {
            $result[] = $resource->getTableName($table);
        }

        return $result;
    }
}
