#!/usr/bin/php
<?php
/**
 * Copyright © OXID eSales AG. All rights reserved.
 * See LICENSE file for license details.
 */

/**
 * Configure the database connection to your OXID eShop 4.10 / 5.3 database
 */
$dbHost = "localhost";
$dbUser = "";
$dbPassword = "";
$dbName = "";

/**
 * These are the tables where files from the WysiwygPro editor get stored in OXID eShop 5.3.
 * If you either
 *  - use more than 4 languages in your OXID eShop
 *  - or use modules which have own database tables with content managed by WysiwygPro
 * please use the array $custom_tables.
 * Do not modify the array $core_tables.
 *
 */
$core_tables = [
    'oxactions' => [
        'OXLONGDESC',
        'OXLONGDESC_1',
        'OXLONGDESC_2',
        'OXLONGDESC_3'
    ],
    'oxlinks' => [
        'OXURLDESC',
        'OXURLDESC_1',
        'OXURLDESC_2',
        'OXURLDESC_3'
    ],
    'oxcategories' => [
        'OXLONGDESC',
        'OXLONGDESC_1',
        'OXLONGDESC_2',
        'OXLONGDESC_3'
    ],
    'oxcontents' => [
        'OXCONTENT',
        'OXCONTENT_1',
        'OXCONTENT_2',
        'OXCONTENT_3'
    ],
    'oxnews' => [
        'OXLONGDESC',
        'OXLONGDESC_1',
        'OXLONGDESC_2',
        'OXLONGDESC_3'
    ],
    'oxpayments' => [
        'OXLONGDESC',
        'OXLONGDESC_1',
        'OXLONGDESC_2',
        'OXLONGDESC_3'
    ],
    'oxartextends' => [
        'OXLONGDESC',
        'OXLONGDESC_1',
        'OXLONGDESC_2',
        'OXLONGDESC_3'
    ]
];

/**
 * If you either
 *  - use more than 4 languages in your OXID eShop
 *  - or use modules which have own database tables with content managed by WysiwygPro
 * uncomment and configure the array $custom_tables below.
 *
 * Please have a look at https://docs.oxid-esales.com/developer/en/6.0/system_architecture/language.html
 * on how to configure additional language specific OXID eShop core tables.
 *
 * Below there is an example for a fifth language configured in OXID eShop and one custom / module database table.
 */

/*
$custom_tables = [
    'my_custom_table' => [
        'LONGDESC'
    ],

    'oxactions' => [
        'OXLONGDESC_4'
    ],
    'oxlinks' => [
        'OXURLDESC_4'
    ],
    'oxcategories' => [
        'OXLONGDESC_4'
    ],
    'oxcontents' => [
        'OXCONTENT_4'
    ],
    'oxnews' => [
        'OXLONGDESC_4'
    ],
    'oxpayments' => [
        'OXLONGDESC_4'
    ],
    'oxartextends' => [
        'OXLONGDESC_4'
    ]
];
*/
if (isset($custom_tables) && is_array($custom_tables)) {
    $tables = array_merge_recursive($core_tables, $custom_tables);
} else {
    $tables = $core_tables;
}

// == LOGIC SECTION ===================================================================

if (empty($dbUser) || empty($dbPassword) || empty($dbName)) {
    echo "Please configure your database connection." . "\n";
    exit;
}

try {
    $pdo = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8",
                        $dbUser,
                        $dbPassword,
                        [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET @@SESSION.sql_mode=\'\'']
    );
} catch (PDOException $e) {
    echo "Connection to database not successful." . "\n";
    $pdo = null;
    exit;
}


$numberOfAffectedRowsTotal = 0;

foreach ($tables as $tableName => $columnNames) {
    $numberOfAffectedRowsByTable = 0;
    foreach ($columnNames as $columnName) {
        $sql = "UPDATE $tableName\n" .
                "SET $columnName = REPLACE($columnName, 'wysiwigpro/', 'ddmedia/')\n" .
                "WHERE $tableName.$columnName LIKE '%wysiwigpro/%';\n\n";
        $result = $pdo->exec($sql);
        if ($result !== false) {
            $numberOfAffectedRowsByTable += $result;
            $numberOfAffectedRowsTotal += $result;
        }
    }
    echo 'Affected rows in table "' . $tableName . '": ' . $numberOfAffectedRowsByTable . "\n";
}

echo 'Total number of affected rows: ' . $numberOfAffectedRowsTotal . "\n";
