Last active
December 26, 2015 12:39
-
-
Save chani/7152187 to your computer and use it in GitHub Desktop.
Script to convert old contao 2 src format to the new one by simply replacing the path with the id's. check comment in file for details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright (c) 2013 Jean Bruenn | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
/** | |
* Requirenments: | |
* - PDO | |
* - Your database needs to support show tables and show columns from syntax. I've | |
* tested this script only with MySQL. | |
* | |
* What does it do: | |
* This script might be helpful for you, if you tried to convert your contao | |
* installation (contao2) to contao3 and kept getting errors about your images | |
* still beeing in contao2src format. This script iterates through every | |
* table, looks for columns called singleSRC and multiSRC, checks for non-numeric | |
* values within these fields and assume the fieldcontent needs to be | |
* converted if found - hence it searches for the id of the file in tl_files and | |
* updates the path accordingly. | |
* | |
* Due to this behaviour it is important that you synchronize your files before | |
* running this script so it can find all files within tl_files. | |
* | |
* You might also want to run the following script first, which replaces occurences | |
* of tl_files/ with files/ | |
* https://gist.github.com/leofeyer/3304014 | |
* | |
* Example Output: | |
* Retrieving a list of tables to convert... | |
* Found 6 tables with columns which might need to be converted... | |
* | |
* Converted 76 pictures (0 failed) in table tl_calendar_events | |
* Converted 0 pictures (0 failed) in table tl_content | |
* Converted 0 pictures (0 failed) in table tl_faq | |
* Converted 0 pictures (0 failed) in table tl_form_field | |
* Converted 73 pictures (0 failed) in table tl_module | |
* Converted 0 pictures (0 failed) in table tl_news | |
* | |
* Converted 149 pictures overall, | |
* couldn't convert 0 (did you synchronize files?) and | |
* failed to convert 0 files | |
*/ | |
error_reporting(E_ALL ^ E_NOTICE); | |
$dbUser = ''; | |
$dbData = ''; | |
$dbPass = ''; | |
$dbHost = ''; | |
/** | |
* @param $table | |
* @param $db | |
* @return bool | |
*/ | |
function imageColumnsExist($table, $db) | |
{ | |
/** | |
* I assume that a table which holds a column called | |
* "singleSRC" and/or "multiSRC" needs to be converted. | |
*/ | |
$colres = $db->query("show columns from $table"); | |
while ($row = $colres->fetch(PDO::FETCH_NUM)) | |
{ | |
$columnName = $row[0]; | |
if (($columnName == 'singleSRC') || ($columnName == 'multiSRC')) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* @param $path | |
* @param $db | |
* @return bool|int | |
*/ | |
function findFileByPath($path, $db) | |
{ | |
/** | |
* i had trouble using hash to find the file, hence using path. | |
*/ | |
$stmt = $db->query("SELECT id FROM tl_files WHERE path = '" . $path . "'"); | |
if ($stmt) | |
{ | |
$row = $stmt->fetch(); | |
if ((isset($row['id'])) && (intval($row['id']) > 0)) | |
{ | |
return intval($row['id']); | |
} | |
} | |
return false; | |
} | |
try | |
{ | |
$db = new PDO('mysql:dbname=' . $dbData . ';host=' . $dbHost, $dbUser, $dbPass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")); | |
$res = $db->query("show tables"); | |
echo 'Retrieving a list of tables to convert...<br />'; | |
while ($row = $res->fetch(PDO::FETCH_NUM)) | |
{ | |
$tableName = $row[0]; | |
if (substr($tableName, 0, 3) == 'tl_') | |
{ | |
if (imageColumnsExist($tableName, $db)) | |
{ | |
$tables[] = $tableName; | |
} | |
} | |
} | |
echo 'Found ' . count($tables) . ' tables with columns which might need to be converted...<br /><br />'; | |
$converted = 0; | |
$missingFile = 0; | |
$failed = 0; | |
foreach ($tables as $tableName) | |
{ | |
$tableConverted = 0; | |
$tableFailed = 0; | |
$stmt = $db->query("SELECT * FROM $tableName"); | |
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) | |
{ | |
if ((isset($row['singleSRC'])) && (!empty($row['singleSRC'])) && (!is_numeric($row['singleSRC']))) | |
{ | |
if ($newId = findFileByPath($row['singleSRC'], $db)) | |
{ | |
$update = $db->exec("UPDATE $tableName SET singleSRC = $newId WHERE id = '" . $row['id'] . "'"); | |
if ($update > 0) | |
{ | |
$tableConverted++; | |
} else | |
{ | |
$tableFailed++; | |
} | |
} else | |
{ | |
$missingFile++; | |
} | |
} | |
if ((isset($row['multiSRC'])) && (!empty($row['multiSRC']))) | |
{ | |
$pictureList = unserialize($row['multiSRC']); | |
if (is_array($pictureList)) | |
{ | |
foreach ($pictureList as $picture) | |
{ | |
if (!empty($picture) && (!is_numeric($picture))) | |
{ | |
if ($newId = findFileByPath($picture, $db)) | |
{ | |
$multiSRC[] = $newId; | |
$tableConverted++; | |
} else | |
{ | |
$missingFile++; | |
} | |
} | |
} | |
} | |
if ((isset($multiSRC)) && (count($multiSRC) > 0)) | |
{ | |
$multiSRCstr = serialize($multiSRC); | |
$update = $db->exec("UPDATE $tableName SET multiSRC = '" . $multiSRCstr . "' WHERE id = '" . $row['id'] . "'"); | |
if ($update <= 0) | |
{ | |
$tableFailed++; | |
} | |
unset($multiSRC); | |
} | |
} | |
} | |
$converted += $tableConverted; | |
$failed += $tableFailed; | |
echo "Converted $tableConverted pictures ($tableFailed failed) in table $tableName<br />"; | |
} | |
echo "<br />Converted $converted pictures overall,<br />couldn't convert $missingFile (did you synchronize files?) and<br />failed to convert $failed files<br />"; | |
} catch (PDOException $e) | |
{ | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment