diff options
author | Mario Vavti <mario@mariovavti.com> | 2019-06-04 10:19:04 +0200 |
---|---|---|
committer | Mario Vavti <mario@mariovavti.com> | 2019-06-04 10:19:04 +0200 |
commit | e4ca3609d90bb437061e4b8db95ed0424b25eb76 (patch) | |
tree | aa070e40ab01cb1354cbf879a0fb8b2f570631d5 /util/storageconv | |
parent | 071fba2f7121aeb9690a21398935a53deac09af8 (diff) | |
parent | bc092d8d7815195d62299c0ea54caa4759e6f2e7 (diff) | |
download | volse-hubzilla-e4ca3609d90bb437061e4b8db95ed0424b25eb76.tar.gz volse-hubzilla-e4ca3609d90bb437061e4b8db95ed0424b25eb76.tar.bz2 volse-hubzilla-e4ca3609d90bb437061e4b8db95ed0424b25eb76.zip |
Merge branch '4.2RC'4.2
Diffstat (limited to 'util/storageconv')
-rwxr-xr-x | util/storageconv | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/util/storageconv b/util/storageconv new file mode 100755 index 000000000..9c49787d1 --- /dev/null +++ b/util/storageconv @@ -0,0 +1,131 @@ +#!/usr/bin/env php +<?php + +// Hubzilla thumbnails storage convertor +function usage() { + echo <<< EOT +Hubzilla thumbnails storage convertor + +Usage: + util/storageconv stats # show current stats + util/storageconv fs # move thumbnails from SQL to filesystem + util/storageconv db # move thumbnails from filesystem to SQL + +EOT; +} + +require_once('include/cli_startup.php'); + +cli_startup(); + +if($argc == 1) { + usage(); + killme(); +} + +if($argc == 2) { + + $storage = (intval(get_config('system','filesystem_storage_thumbnails', 0)) > 0 ? 1 : 0); + echo 'Current storage set to: ' . ($storage ? 'filesystem' : 'SQL database') . PHP_EOL; + switch($argv[1]) { + case 'stats': + $x = q("SELECT COUNT(resource_id) AS qty FROM photo WHERE photo_usage = 0 AND os_storage = 1 AND imgscale = 0"); + echo 'Local images: ' . $x[0]['qty'] . PHP_EOL; + $x = q("SELECT COUNT(id) AS qty FROM photo WHERE resource_id IN (SELECT DISTINCT resource_id FROM photo WHERE photo_usage = 0 and os_storage = 1) AND imgscale > 0"); + echo 'Thumbnails total: ' . $x[0]['qty'] . PHP_EOL; + $x = q("SELECT COUNT(id) AS qty FROM photo WHERE resource_id IN (SELECT DISTINCT resource_id FROM photo WHERE photo_usage = 0 and os_storage = 1) AND os_storage != %d AND imgscale > 0", + $storage + ); + echo 'Thumbnails to convert: ' . $x[0]['qty'] . PHP_EOL; + break; + + case 'fs': + if($storage == 0) { + echo 'Please set system.filesystem_storage_thumbnails to 1 before move thumbnails to filesystem storage' . PHP_EOL; + break; + } + + $x = q("SELECT DISTINCT resource_id, content FROM photo WHERE photo_usage = 0 AND os_storage = 1 AND imgscale = 0"); + + if($x) { + foreach($x as $xx) { + + $n = q("SELECT id, imgscale, content FROM photo WHERE resource_id = '%s' AND os_storage != %d AND imgscale > 0", + dbesc($xx['resource_id']), + $storage + ); + + foreach($n as $nn) { + + echo '.'; + + $filename = dbunescbin($xx['content']) . '-' . $nn['imgscale']; + if(! file_put_contents($filename, dbunescbin($nn['content']))) { + echo 'Failed to save file ' . $filename . PHP_EOL; + continue; + } + + $z = q("UPDATE photo SET content = '%s', os_storage = 1 WHERE id = %d", + dbescbin($filename), + intval($nn['id']) + ); + if(! $z) { + @unlink($filename); + echo 'Failed to update metadata for saved file ' . $filename . PHP_EOL; + } + + } + } + } + break; + + case 'db': + if($storage == 1) { + echo 'Please set system.filesystem_storage_thumbnails to 0 before move thumbnails to SQL database storage' . PHP_EOL; + break; + } + + $x = q("SELECT DISTINCT resource_id FROM photo WHERE photo_usage = 0 AND os_storage = 1 AND imgscale = 0"); + + if($x) { + foreach($x as $xx) { + + $n = q("SELECT id, content FROM photo WHERE resource_id = '%s' AND os_storage != %d AND imgscale > 0", + dbesc($xx['resource_id']), + $storage + ); + + foreach($n as $nn) { + + echo '.'; + + $filename = dbunescbin($nn['content']); + $content = file_get_contents($filename); + if($content) { + + $z = q("UPDATE photo SET content = '%s', os_storage = 0 WHERE id = %d", + dbescbin($content), + intval($nn['id']) + ); + if(! $z) { + echo 'Failed to update stored file metadata ' . $filename . PHP_EOL; + continue; + } + + @unlink($filename); + } + else + echo 'Can not read file contents ' . $filename . PHP_EOL; + } + } + } + break; + + default: + usage(); + return; + + } + + echo PHP_EOL; +} |