Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update path in database
<?php
// Database credentials
$strHost = 'localhost';
$strUser = '';
$strPassword = '';
$strDatabase = '';
// 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());
}
@Olli

This comment has been minimized.

Copy link

commented Jul 24, 2014

Gibts auch irgendwo mal ein Hinweis, was dieses Script eigentlich macht? Es wird immer nur als "Tristans Script" referenziert.

@Olli

This comment has been minimized.

Copy link

commented Jul 25, 2014

Es erfolgt eine umbenennung aller Dateiverweise in der Datenbank von tl_files auf files

@SyZn

This comment has been minimized.

Copy link

commented Apr 18, 2015

Ich würde vorschlagen das script noch die Daten aus der localconfig nehmen zu lassen: siehe https://gist.github.com/SyZn/eaaa7100f75d38273aca

@thismaechler

This comment has been minimized.

Copy link

commented Dec 22, 2018

Ich hatte das Problem, dass es serialisierte Daten gab, die wiederum String-Felder mit serialisierten Daten in sich trugen (dma_elementgenerator mit mehreren Bildern).. so wurden dann dort zwar ersetzt, aber die Anzahl Zeichen im String war latürnich falsch nachher. Meine Lösung, rekursiv un-serialisieren: (bin zu faul das jetzt zu forken und alles.. ist aber praxis-getestet):

function array_str_replace($strSearch, $strReplace, $arrData)
{
	foreach ($arrData as $k=>$v)
	{
		// check if value is serialized data!
		$vWasSerialized = false;
		$tmp = unserialize($v);
		if( $tmp !== false)
		{
			$vWasSerialized = true;
			$v = $tmp;
			if(is_object($v))
				$v = get_object_vars($v);
		}
		if (is_array($v))
		{
			$v = array_str_replace($strSearch, $strReplace, $v);
			$arrData[$k] = $vWasSerialized ? serialize($v) : $v;
		}
		elseif (is_string($v))
		{
			$arrData[$k] = str_replace($strSearch, $strReplace, $v);
		}
	}
	return $arrData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.