Skip to content

Instantly share code, notes, and snippets.

@BugBuster1701
Created October 17, 2013 11:49
Show Gist options
  • Select an option

  • Save BugBuster1701/7023513 to your computer and use it in GitHub Desktop.

Select an option

Save BugBuster1701/7023513 to your computer and use it in GitHub Desktop.
Script von wdp zur Migration von singleSRC und multiSRC von Pfad zur ID in der Tabelle tl_content Dazu müssen die Bilder/Dateien selbst schon gesynct in tl_files vorliegen.
<?php
error_reporting(E_ALL ^ E_NOTICE);
$dbUser = '';
$dbData = '';
$dbPass = '';
$dbHost = '';
/**
* DO NOT RUN THIS SCRIPT TWICE YET!!!
* @todo implement ifs (is_numeric) to verify that we actually need to convert
* @todo (if it is numeric, it's already the new format, so no need to convert)
* @todo probably implement file_exists, is_dir
* @todo implement checks if UPDATE succeeded
*
* I tried to use hash by creating a hash of the file and checking for that
* hash in the database - this is problematic if dealing with directories,
* but it would make sure that it would find files also, if they're renamed
* or if the paths wouldn't match.
*
* Usage
* Assuming that you get the error about the old contao2 src, further assuming
* that all your old files are in files/ and not in tl_files/ anymore, first
* run the following script, which will rename tl_files/ to files/ in contao's
* tables:
* https://gist.github.com/leofeyer/3304014
* It is assumed, for the following step to succeed that you already synchronized
* all your files within contao's backend, otherwise the id's won't exist.
* Finally run this script, it will look up the ID (in tl_files) for the given paths
* in tl_content of the entries in the fields singleSRC and multiSRC.
*
* If this script was helpful to you, please drop me a note: [email protected]
*/
try
{
$db = new PDO('mysql:dbname=' . $dbData . ';host=' . $dbHost, $dbUser, $dbPass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
$stmt = $db->query("SELECT * FROM tl_content");
$i = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
// this handles the singleSRC field
if ((!empty($row['singleSRC'])))
{
// use of hash and md5_file is problematic due to directories, so either use
// path like I or check if it is a file or directory you're dealing with
$astmt = $db->query("SELECT id FROM tl_files WHERE path = '" . $row['singleSRC'] . "'");
if ($astmt)
{
$arow = $astmt->fetch();
echo 'Converting singleSRC(' . $row['id'] . '): ' . $row['singleSRC'] . ' -> Picture ID: ' . $arow['id'] . '<br />';
$update = $db->query("UPDATE tl_content SET singleSRC = '" . $arow['id'] . "' WHERE id = " . $row['id'] . "");
unset($astmt);
}
$i++;
}
// this will handle the multiSRC field, which needs to be unserialized and serialized again
if (!empty($row['multiSRC']))
{
$arr = unserialize($row['multiSRC']);
$serialized = array();
foreach ($arr as $elem)
{
// use of hash and md5_file is problematic due to directories, so either use
// path like I or check if it is a file or directory you're dealing with
$bstmt = $db->query("SELECT id, path FROM tl_files WHERE path = '" . $elem . "'");
if ($bstmt)
{
$brow = $bstmt->fetch();
echo 'Converting multiSRC(' . $row['id'] . '): ' . $elem . ' -> File ID: ' . $brow['id'] . '<br />';
$serialized[] = $brow['id'];
unset($bstmt);
}
$i++;
}
$serialized = serialize($serialized);
$update = $db->query("UPDATE tl_content SET multiSRC = '" . $serialized . "' WHERE id = " . $row['id'] . "");
unset($serialized);
}
}
} catch (PDOException $e)
{
echo $e->getMessage();
}
echo "Found $i Files to convert";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment