Skip to content

Instantly share code, notes, and snippets.

@stefansl
Last active October 17, 2019 21:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stefansl/eaf2f7d8ef8aec544b4e to your computer and use it in GitHub Desktop.
Save stefansl/eaf2f7d8ef8aec544b4e to your computer and use it in GitHub Desktop.
Modified path upgrade script for contao. No config editing necessary. Copy in your Contao root and run.
<?php
/*
When updating from Contao 2 to Contao 3, you must not rename the tl_files folder to files!
If you want to re­name the folder, you have to take the following steps:
· Complete the version 3 update in the install tool
· Then rename the upload folder in the back end settings to files
· Last run the modified version of Tristan's script
If you do not want to rename the directory, the name of the upload folder remains tl_files,
also in Contao 3. This does not apply to fresh installations of course.
*/
define('TL_ROOT', '/');
require '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());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment