Recover accidentally deleted photos

This commit is contained in:
2016-02-08 20:49:49 +00:00
parent 97f3025259
commit 665ca58988
18 changed files with 217 additions and 17 deletions

View File

@@ -1,9 +1,7 @@
<?php
require __DIR__ . '/utilities.php';
use Symfony\Component\Yaml\Yaml;
foreach (glob('_data/gallery/*.yml') as $sFile) {
foreach (glob('C:\Users\Rik\Downloads\yy/*.yml') as $sFile) {
yamlParse($sFile, $aYaml, $sContents);
preg_match_all('~\s{2}(\w{7}):\n~', $sContents, $aMatches);
$aPhotos = $aMatches[1];

View File

@@ -0,0 +1,85 @@
<?php
require __DIR__ . '/utilities.php';
$sAssetDir = __DIR__ . '/../../asset/gallery';
$sDataDir = __DIR__ . '/../_data/gallery';
$sFixDir = __DIR__ . '/../../x/gallery';
$bFix = true;
// Scan for photo files
$aFiles = [];
foreach (glob(sprintf('%s%s*', $sAssetDir, DIRECTORY_SEPARATOR)) as $sGalleryDir) {
$sGallery = basename($sGalleryDir);
$aPhotos = [];
foreach (glob(sprintf('%s%s*', $sGalleryDir, DIRECTORY_SEPARATOR)) as $sFile) {
$sPhoto = basename($sFile, '.jpg');
list($sName, $sDimension) = explode('~', $sPhoto);
if (isset($aPhotos[$sName])) {
$aPhotos[$sName][] = $sDimension;
} else {
$aPhotos[$sName] = [$sDimension];
}
}
$aFiles[$sGallery] = array_keys($aPhotos);
}
// Scan for gallery photos
foreach (glob(sprintf('%s%s*.yml', $sDataDir, DIRECTORY_SEPARATOR)) as $sFile) {
yamlParse($sFile, $aYaml, $sContents);
preg_match_all('~\s{2}(\w{7}):\n~', $sContents, $aMatches);
$aPhotos = $aMatches[1];
$sGallery = basename($sFile, '.yml');
// Check match
if (!isset($aFiles[$sGallery])) {
printf("[%s] no files!\n", $sGallery);
}
// Compare
$aFileOnly = array_diff($aFiles[$sGallery], $aPhotos);
$aReferenceOnly = array_diff($aPhotos, $aFiles[$sGallery]);
$bFileOnly = count($aFileOnly) > 0;
$bReferenceOnly = count($aReferenceOnly) > 0;
// Display results
if ($bFileOnly || $bReferenceOnly) {
printf("[%s]\n", $sGallery);
}
if ($bFileOnly) {
printf(" files:\n - %s\n", implode("\n - ", $aFileOnly));
if ($bFix) {
$aFix = [];
foreach ($aFileOnly as $sPhoto) {
$sPhotoFile = sprintf('%s%s%s%s%s.md', $sFixDir, DIRECTORY_SEPARATOR, $sGallery, DIRECTORY_SEPARATOR, $sPhoto);
if (!file_exists($sPhotoFile)) {
printf(" <%s> non existent!\n", $sPhoto);
continue;
}
parseFile($sPhotoFile, $aPhotoYaml);
unset($aPhotoYaml['gallery']);
unset($aPhotoYaml['layout']);
unset($aPhotoYaml['next']);
unset($aPhotoYaml['ordering']);
unset($aPhotoYaml['previous']);
unset($aPhotoYaml['sizes']);
unset($aPhotoYaml['title']);
if (!isset($aPhotoYaml['date'])) {
printf(" <%s> no date!\n", $sPhoto);
continue;
} else {
$aPhotoYaml['date'] = date('Y-m-d H:i:s', $aPhotoYaml['date']);
}
if (isset($aPhotoYaml['location'])) {
$aPhotoYaml = array_merge($aPhotoYaml, $aPhotoYaml['location']);
unset($aPhotoYaml['location']);
}
$aFix[$sPhoto] = $aPhotoYaml;
}
$sFix = substr(yamlDump(array('' => $aFix)), 1);
file_put_contents($sFile, trim(sprintf("%s%s", file_get_contents($sFile), $sFix)));
}
}
if ($bReferenceOnly) {
printf(" references:\n - %s\n", implode("\n - ", $aReferenceOnly));
}
}