Added script for finding dead posts
This commit is contained in:
parent
5f0706c0b4
commit
65bc6705d3
2 changed files with 74 additions and 0 deletions
31
scripts/find-dead-posts.php
Normal file
31
scripts/find-dead-posts.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
require_once(__DIR__
|
||||
. DIRECTORY_SEPARATOR . '..'
|
||||
. DIRECTORY_SEPARATOR . 'src'
|
||||
. DIRECTORY_SEPARATOR . 'Bootstrap.php');
|
||||
|
||||
use Szurubooru\Injector;
|
||||
use Szurubooru\Dao\PublicFileDao;
|
||||
use Szurubooru\Dao\PostDao;
|
||||
|
||||
$publicFileDao = Injector::get(PublicFileDao::class);
|
||||
$postDao = Injector::get(PostDao::class);
|
||||
|
||||
$paths = [];
|
||||
foreach ($postDao->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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue