szurubooru/scripts/process-detached-files.php

73 lines
1.3 KiB
PHP
Raw Normal View History

2013-10-18 00:31:22 +02:00
<?php
require_once __DIR__ . '/../src/core.php';
Access::disablePrivilegeChecking();
2013-10-18 00:31:22 +02:00
function usage()
{
echo 'Usage: ' . basename(__FILE__);
echo ' -print|-purge|-move DIR' . PHP_EOL;
return true;
}
array_shift($argv);
if (empty($argv))
usage() and die;
$action = array_shift($argv);
switch ($action)
{
case '-print':
$func = function($name)
{
echo $name . PHP_EOL;
};
break;
case '-move':
if (empty($argv))
usage() and die;
$dir = array_shift($argv);
if (!file_exists($dir))
mkdir($dir, 0755, true);
if (!is_dir($dir))
die($dir . ' is not a dir' . PHP_EOL);
$func = function($name) use ($dir)
2013-10-18 00:31:22 +02:00
{
echo $name . PHP_EOL;
$srcPath = PostModel::getFullPath($name);
$dstPath = $dir . DS . $name;
rename($srcPath, $dstPath);
2013-10-18 00:31:22 +02:00
};
break;
case '-purge':
$func = function($name)
2013-10-18 00:31:22 +02:00
{
echo $name . PHP_EOL;
$srcPath = PostModel::getFullPath($name);
unlink($srcPath);
2013-10-18 00:31:22 +02:00
};
break;
default:
die('Unknown action' . PHP_EOL);
}
$names = [];
foreach (PostSearchService::getEntities(null, null, null) as $post)
2013-10-18 00:31:22 +02:00
{
$names []= $post->getName();
2013-10-18 00:31:22 +02:00
}
$names = array_flip($names);
$config = Core::getConfig();
foreach (glob(TextHelper::absolutePath($config->main->filesPath) . DS . '*') as $name)
2013-10-18 00:31:22 +02:00
{
$name = basename($name);
if (!isset($names[$name]))
{
$func($name);
}
}