Skip to content

Instantly share code, notes, and snippets.

@SyZn
Forked from leofeyer/contao-db-update.php
Last active March 5, 2019 08:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SyZn/eaaa7100f75d38273aca to your computer and use it in GitHub Desktop.
Save SyZn/eaaa7100f75d38273aca to your computer and use it in GitHub Desktop.
<?php
// assuming we are in a subfolder to root like 'scripts'
define('TL_ROOT', dirname(__DIR__));
require TL_ROOT . '/system/config/localconfig.php';
// Database credentials
$strHost = $GLOBALS['TL_CONFIG']['dbHost'];
$strUser = $GLOBALS['TL_CONFIG']['dbUser'];
$strPassword = $GLOBALS['TL_CONFIG']['dbPass'];
$strDatabase = $GLOBALS['TL_CONFIG']['dbDatabase'];
// From and to path
$strFrom = 'tl_files/';
$strTo = 'files/';
// DO NOT EDIT ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!
error_reporting(E_ALL ^ E_NOTICE);
function array_strpos($arrHaystack, $strNeedle)
{
foreach ($arrHaystack as $v)
{
if (is_object($v))
{
$v = get_object_vars($v);
}
if (is_array($v) && array_strpos($v, $strNeedle) || !is_array($v) && strpos($v, $strNeedle) !== false)
{
return true;
}
}
return false;
}
function array_str_replace($strSearch, $strReplace, $arrData)
{
foreach ($arrData as $k=>$v)
{
if (is_array($v))
{
$arrData[$k] = array_str_replace($strSearch, $strReplace, $v);
}
elseif (is_string($v))
{
$arrData[$k] = str_replace($strSearch, $strReplace, $v);
}
}
return $arrData;
}
try
{
$db = new PDO
(
'mysql:dbname=' . $strDatabase . ';host=' . $strHost,
$strUser,
$strPassword,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")
);
$stmt = $db->query('SHOW TABLES');
$stmt->setFetchMode(PDO::FETCH_COLUMN, 0);
foreach ($stmt as $strName)
{
$arrTables[] = $strName;
}
$intUpdates = 0;
if (substr($strFrom, -1) == '/' && substr($strTo, -1) != '/')
{
$strTo .= '/';
}
else if (substr($strFrom, -1) != '/' && substr($strTo, -1) == '/')
{
$strFrom .= '/';
}
echo 'Searching tables' . "\n";
foreach ($arrTables as $strTable)
{
echo 'Processing ' . $strTable . "\n";
$stmt = $db->query(sprintf('SELECT * FROM `%s`', $strTable));
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach ($stmt as $row)
{
foreach ($row as $k=>$v)
{
$blnSerialized = false;
$tmp = unserialize($v);
if ($tmp !== false)
{
$blnSerialized = true;
$v = $tmp;
}
if ($k != 'id')
{
$w = false;
if (is_object($v))
{
$v = get_object_vars($v);
}
if (is_array($v) && array_strpos($v, $strFrom) !== false)
{
$w = array_str_replace($strFrom, $strTo, $v);
}
if (!is_array($v) && strpos($v, $strFrom) !== false)
{
$w = str_replace($strFrom, $strTo, $v);
}
if ($w)
{
if ($blnSerialized)
{
$v = $row[$k];
$w = serialize($w);
}
$stmt = $db->prepare(sprintf('UPDATE `%s` SET `%s`=:value WHERE `id`=:id', $strTable, $k));
$stmt->bindValue('value', $w);
$stmt->bindValue('id', $row['id']);
$stmt->execute();
++$intUpdates;
}
}
}
}
}
echo 'Update finished: ' . sprintf('%d rows updated', $intUpdates) . "\n";
}
catch(PDOException $e)
{
var_dump($e->getMessage());
}
@Mediazept
Copy link

Danke! Nahe liegende Erweiterung von Leos Skript, so aber viel unkomplizierter nutzbar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment