Snippets
Created by
This Mächler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | <?php
/*
** This script copies /fileadmin/_processed_/* files in TYPO3 installations if they are not matching
** the src-attributes found in tt_content.bodytext (RTE content, magic images). Just looks for the
** first part of the file (without the 10-digit postfix before the file extension) in the filesystem
** and renames it if necessary.
**
** My usecase was a TYPO3 6.2.15 instance, and lots of lost magic images thumbnails, not wanting to fix them all by hand.
**
** You have to call the script with the GET-param dry=no to make it rename files, else it will just do a dry run and show
** you what it will do.
**
** Author:
** maechler@mm-computing.ch
** http://mm-computing.ch
** 2015-10-29
*/
/****************************************************************/
/** setup your database connection to make the script work! **/
/****************************************************************/
define( 'DB_HOST', '' );
define( 'DB_USER', '' );
define( 'DB_PW', '' );
define( 'DB_NAME', '' );
/****************************************************************/
/****************************************************************/
define( 'FILE_REGEX_PATTERN', '(csm_[^\s]+)(_[a-z0-9]+\.jpg)' );
$processed_dirname = dirname(__FILE__)."/fileadmin/_processed_";
$dry_run = ( strcmp($_GET['dry'], 'no') != 0 );
echo ($dry_run ? 'Dry run...' : 'Sharp run!!').'<br/><br/>';
/* ------------------- get all '/fileadmin/_processed_/*.jpg' files from tt_content.bodytext ------------------- */
echo "image sources pointing to 'fileadmin/_processed_/*':<br/>".
"---------------------------------------------------------<br/>";
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PW );
$mysqli->select_db(DB_NAME);
$res = $mysqli->query("SELECT bodytext FROM tt_content WHERE bodytext LIKE '%fileadmin/_processed_/csm%'");
$imgsrcs = array();
while( $row = $res->fetch_row() ){
preg_match_all(
'/fileadmin\/_processed_\/'.FILE_REGEX_PATTERN.'"/U', $row[0],
$matches, PREG_SET_ORDER
);
foreach( $matches as $m ){
$imgsrcs[] = array($m[1],$m[2]);
echo $m[1].$m[2]."<br/>";
}
}
$mysqli->close();
/* ------------------- find 'fileadmin/_processed_/*.jpg' files in file system ------------------- */
echo "<br/><br/>read directory items '/fileadmin/_processed_/*':<br/>".
"---------------------------------------------------------<br/>";
$dir = dir( $processed_dirname );
$imgfiles = array();
while( $f = $dir->read() ){
if( preg_match('/^'.FILE_REGEX_PATTERN.'$/', $f, $m) ){
$imgfiles[] = array($m[1], $m[2]);
echo $m[1].$m[2]."</br>";
}
}
$dir->close();
/* ----------------------- copy 'fileadmin/_processed_/*.jpg' files to match src in html ------------ */
echo "<br/><br/>"."copy 'fileadmin/_processed_/*.jpg' files to match src in html<br/>".
"---------------------------------------------------------<br/>";
function get_src_filenames($filenamepart1){
global $imgsrcs;
$res = array();
foreach( $imgsrcs as $imgsrc ){
if( $filenamepart1 == $imgsrc[0] ){
$res[] = $imgsrc[0].$imgsrc[1];
}
}
return $res; // no matching src found
}
foreach( $imgfiles as $imgf ){
$existing_fn = $imgf[0].$imgf[1];
$src_filenames = get_src_filenames($imgf[0]);
foreach( $src_filenames as $src_fn ){
$src_file_withpath = $processed_dirname.'/'.$src_fn;
if( (strcmp($existing_fn, $src_fn)!=0) && (!file_exists($src_file_withpath)) ){
// this src attribute has no corresponding file, copy the existing one
if( !$dry_run ){
copy($processed_dirname.'/'.$existing_fn, $src_file_withpath);
echo '(!) ';
}
echo $existing_fn." ===> ".$src_fn."<br/>";
}
}
}
?>
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.