Created
October 10, 2012 02:06
-
-
Save aghouseh/3862735 to your computer and use it in GitHub Desktop.
Concrete5 Relink Files - If you accidentally and block-headedly deleted your /files/ folder, but have source images available elsewhere, use this to restore them. Crawls a given directory and matches against Concrete5 filenames to relink missing images.
This file contains hidden or 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 defined('C5_EXECUTE') or die("Access Denied."); ?> | |
<?php | |
/** | |
* Modify these config settings to taste. | |
* I would recommend copying your source files into the /files directory | |
* to ensure that you are going to have proper permission to traverse them. | |
*/ | |
set_time_limit(600); | |
$document_root = '/var/www/mysite/public_html'; | |
$files_root = '/var/www/mysite/public_html/files/oops'; | |
/** | |
* function lifted from http://www.php.net/manual/en/function.scandir.php#109140 | |
* allows us to create a nicely formatted array of 'sanitized filename' => 'system path' | |
*/ | |
function directoryToArray($directory, $recursive = true, $listDirs = false, $listFiles = true, $exclude = '') { | |
$fh = Loader::helper('file'); | |
$arrayItems = array(); | |
$skipByExclude = false; | |
$handle = opendir($directory); | |
if ($handle) { | |
while (false !== ($file = readdir($handle))) { | |
preg_match("/(^(([\.]){1,2})$|(\.(svn|git|md))|(Thumbs\.db|\.DS_STORE))$/iu", $file, $skip); | |
if($exclude){ preg_match($exclude, $file, $skipByExclude); } | |
if (!$skip && !$skipByExclude) { | |
if (is_dir($directory. DIRECTORY_SEPARATOR . $file)) { | |
if($recursive) { | |
$arrayItems = array_merge($arrayItems, directoryToArray($directory. DIRECTORY_SEPARATOR . $file, $recursive, $listDirs, $listFiles, $exclude)); | |
} | |
if($listDirs){ | |
$file = $directory . DIRECTORY_SEPARATOR . $file; | |
$arrayItems[] = $file; | |
} | |
} else { | |
if($listFiles){ | |
$file = $directory . DIRECTORY_SEPARATOR . $file; | |
$sanitized = $fh->sanitize(basename($file)); | |
$arrayItems[$sanitized] = $file; | |
} | |
} | |
} | |
} | |
closedir($handle); | |
} | |
return $arrayItems; | |
} | |
// helpers/libs/models | |
$fh = Loader::helper('file'); | |
Loader::model('file_list'); | |
// create a nicely formatted array of 'sanitized filename' => 'system path' | |
$crawled_files = directoryToArray($files_root); | |
// we search c5 for images that are missing their file links | |
$fl = new FileList(); | |
$fl->filterByAttribute('width', 22); // these are the sizes that c5 sets to an image with an unset path | |
$fl->filterByAttribute('height', 7); | |
foreach ($fl->get() as $f) { | |
$relative_path = $f->getRelativePath(); // get c5's path so we know where to put this thing | |
$sanitized_filename = $fh->sanitize(basename($relative_path)); // get the sanitized filename from c5 | |
// check our array for the sanitized file key | |
if (isset($crawled_files[$sanitized_filename])) { | |
// notify & perform copy | |
echo 'Copying ' . $sanitized_filename . ' to ' . $document_root . $relative_path; | |
copy($crawled_files[$sanitized_filename], $document_root . $relative_path); | |
// c5 stores file attribs, so these should be rescanned | |
$fv = $f->getApprovedVersion(); | |
switch($fv->refreshAttributes()) { | |
case File::F_ERROR_FILE_NOT_FOUND: | |
echo t('ERROR: File <strong>%s</strong> could not be found.', $fv->getFilename()) | |
break; | |
default: | |
echo t('File <strong>%s</strong> has been rescanned', $fv->getFileName()); | |
} | |
} else { | |
echo 'No replacement file found for ' . $relative_path; | |
} | |
echo '<br/>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment