szurubooru/scripts/process-detached-files.php

78 lines
1.5 KiB
PHP
Raw Normal View History

2013-10-18 00:31:22 +02:00
<?php
require_once __DIR__ . '/../src/core.php';
Access::disablePrivilegeChecking();
2014-06-01 13:41:08 +02:00
$usage = function()
2013-10-18 00:31:22 +02:00
{
2014-06-01 13:41:08 +02:00
echo 'Usage: ' . basename(__FILE__) . PHP_EOL;
echo ' -p|--print OR' . PHP_EOL;
echo ' -d|--delete OR' . PHP_EOL;
echo ' -m|--move [TARGET]' . PHP_EOL;
2013-10-18 00:31:22 +02:00
return true;
2014-06-01 13:41:08 +02:00
};
2013-10-18 00:31:22 +02:00
array_shift($argv);
if (empty($argv))
2014-06-01 13:41:08 +02:00
$usage() and die;
2013-10-18 00:31:22 +02:00
$action = array_shift($argv);
switch ($action)
{
2014-06-01 13:41:08 +02:00
case '-p':
case '--print':
2013-10-18 00:31:22 +02:00
$func = function($name)
{
echo $name . PHP_EOL;
};
break;
2014-06-01 13:41:08 +02:00
case '-m':
case '--move':
2013-10-18 00:31:22 +02:00
if (empty($argv))
2014-06-01 13:41:08 +02:00
$usage() and die;
2013-10-18 00:31:22 +02:00
$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 = Core::getConfig()->main->filesPath . DS . $name;
$dstPath = $dir . DS . $name;
rename($srcPath, $dstPath);
2013-10-18 00:31:22 +02:00
};
break;
2014-06-01 13:41:08 +02:00
case '-d':
case '--delete':
$func = function($name)
2013-10-18 00:31:22 +02:00
{
echo $name . PHP_EOL;
$srcPath = Core::getConfig()->main->filesPath . DS . $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);
}
}