diff --git a/scripts/find-dead-posts.php b/scripts/find-dead-posts.php new file mode 100644 index 00000000..110b7f71 --- /dev/null +++ b/scripts/find-dead-posts.php @@ -0,0 +1,31 @@ +findAll() as $post) +{ + $paths[] = $post->getContentPath(); + $paths[] = $post->getThumbnailSourceContentPath(); +} + +$paths = array_flip($paths); +foreach ($publicFileDao->listAll() as $path) +{ + if (dirname($path) !== 'posts') + continue; + if (!isset($paths[$path])) + { + echo $path . PHP_EOL; + flush(); + } +} diff --git a/src/Dao/FileDao.php b/src/Dao/FileDao.php index e0828098..512996cb 100644 --- a/src/Dao/FileDao.php +++ b/src/Dao/FileDao.php @@ -44,10 +44,53 @@ class FileDao implements IFileDao return $this->directory . DIRECTORY_SEPARATOR . $fileName; } + public function listAll() + { + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory)); + $files = []; + foreach ($iterator as $path) + { + if (!$path->isDir()) + $files[] = $this->getRelativePath($this->directory, $path->getPathName()); + } + return $files; + } + private function createFolders($fileName) { $fullPath = dirname($this->getFullPath($fileName)); if (!file_exists($fullPath)) mkdir($fullPath, 0777, true); } + + private function getRelativePath($from, $to) + { + $from = is_dir($from) ? rtrim($from, '\/') . '/' : $from; + $to = is_dir($to) ? rtrim($to, '\/') . '/' : $to; + $from = explode('/', str_replace('\\', '/', $from)); + $to = explode('/', str_replace('\\', '/', $to)); + $relPath = $to; + foreach($from as $depth => $dir) + { + if($dir === $to[$depth]) + { + array_shift($relPath); + } + else + { + $remaining = count($from) - $depth; + if ($remaining > 1) + { + $padLength = (count($relPath) + $remaining - 1) * -1; + $relPath = array_pad($relPath, $padLength, '..'); + break; + } + else + { + $relPath[0] = $relPath[0]; + } + } + } + return implode('/', $relPath); + } }