hedera-web/rest/image/sync.php

121 lines
2.5 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
2018-05-23 10:14:20 +00:00
require_once(__DIR__.'/util.php');
2016-07-22 20:00:27 +00:00
/**
* Syncronizes the data directory with the database, this may take
* some time.
2017-05-08 15:54:35 +00:00
*/
2018-05-23 10:14:20 +00:00
class Sync extends Vn\Lib\Method {
2016-07-22 20:00:27 +00:00
private $trashSubdir;
2016-08-25 10:47:09 +00:00
private $util;
2018-05-23 10:14:20 +00:00
function __construct($app) {
parent::__construct($app);
$this->util = new Util($app);
2016-08-25 10:47:09 +00:00
$this->dataDir = $this->util->dataDir;
}
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
function run($db) {
$db = $this->getSysConn();
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
set_time_limit(0);
$this->trashSubdir = date('YmdHis');
2016-07-22 20:00:27 +00:00
$checkCount = 0;
$query = 'SELECT DISTINCT `%3$s` FROM `%1$s`.`%2$s`
WHERE `%3$s` IS NOT NULL AND `%3$s` != \'\'';
2018-05-23 10:14:20 +00:00
$dir = opendir($this->dataDir);
2016-07-22 20:00:27 +00:00
if ($dir)
2018-05-23 10:14:20 +00:00
while ($schema = readdir($dir))
if (!in_array($schema, ['.', '..'])) {
$info = $this->util->loadInfo($schema);
2016-07-22 20:00:27 +00:00
$schemaPath = "{$this->dataDir}/$schema";
// Deletes unreferenced schemas.
2018-05-23 10:14:20 +00:00
if (!isset($info)) {
$this->moveTrash($schema);
2016-07-22 20:00:27 +00:00
continue;
}
// Deletes unreferenced sizes.
2018-05-23 10:14:20 +00:00
$schemaDir = opendir($schemaPath);
2016-07-22 20:00:27 +00:00
if ($schemaDir)
2018-05-23 10:14:20 +00:00
while ($size = readdir($schemaDir))
if (!in_array($size, ['.', '..', 'full'])
&& !isset($info['sizes'][$size]))
$this->moveTrash("$schema/$size");
2016-07-22 20:00:27 +00:00
// Gets a list of referenced images from the database.
2018-05-23 10:14:20 +00:00
$result = $db->query(sprintf($query
2016-07-22 20:00:27 +00:00
,$info['schema']
,$info['table']
,$info['column']
));
if (!$result)
continue;
$map = [];
2018-05-23 10:14:20 +00:00
while ($row = $result->fetch_row()) {
2016-07-22 20:00:27 +00:00
$map[$row[0]] = TRUE;
$checkCount++;
}
2018-05-23 10:14:20 +00:00
$result->free();
2016-07-22 20:00:27 +00:00
// Deletes unreferenced images.
2018-05-23 10:14:20 +00:00
$this->cleanImages($schema, 'full', $map);
2016-07-22 20:00:27 +00:00
foreach ($info['sizes'] as $size => $i)
2018-05-23 10:14:20 +00:00
$this->cleanImages($schema, $size, $map);
2016-07-22 20:00:27 +00:00
}
echo "Syncronization finished.\n";
2016-07-22 20:00:27 +00:00
}
2018-05-23 10:14:20 +00:00
function cleanImages($schema, $size, &$map) {
2016-07-22 20:00:27 +00:00
$sizePath = "{$this->dataDir}/$schema/$size";
2018-05-23 10:14:20 +00:00
if (!is_dir($sizePath))
2016-07-22 20:00:27 +00:00
return;
2018-05-23 10:14:20 +00:00
$iter = new DirectoryIterator($sizePath);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
for (; $iter->valid(); $iter->next())
if (!$iter->isDir() && strripos($iter->getFilename(), '.png', -4) !== FALSE) {
$name = substr($iter->getFilename(), 0, -4);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
if (!isset($map[$name]))
$this->moveTrash("$schema/$size/". $iter->getFilename());
2016-07-22 20:00:27 +00:00
}
}
/**
* Moves a data directory to the trash.
*
* @param string $file The file to move to the trash
2017-05-08 15:54:35 +00:00
*/
2018-05-23 10:14:20 +00:00
function moveTrash($file) {
$trashBasedir = "{$this->dataDir}/.trash/". $this->trashSubdir;
2018-05-23 10:14:20 +00:00
$trashdir = "$trashBasedir/". dirname($file);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
if (!is_dir($trashdir))
mkdir($trashdir, 0775, TRUE);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
rename(
2016-07-22 20:00:27 +00:00
"{$this->dataDir}/$file",
"$trashBasedir/$file"
);
}
}