Commit 9e791142 authored by robertokir's avatar robertokir

[bp/r63718][NEW] Console command files:copy to copy files from a file gallery...

[bp/r63718][NEW] Console command files:copy to copy files from a file gallery to a regular directory on the file system.
[bp/r63834][NEW] Console command files:move to move files from a file gallery to a regular directory on the file system.
parent bae4673e
......@@ -2606,7 +2606,9 @@ lib/core/Tiki/Command/CacheClearCommand.php -text
lib/core/Tiki/Command/ConfigureCommand.php -text
lib/core/Tiki/Command/DailyReportSendCommand.php -text
lib/core/Tiki/Command/FilesBatchuploadCommand.php -text
lib/core/Tiki/Command/FilesCopyCommand.php -text
lib/core/Tiki/Command/FilesDeleteoldCommand.php -text
lib/core/Tiki/Command/FilesMoveCommand.php -text
lib/core/Tiki/Command/GoalCheckCommand.php -text
lib/core/Tiki/Command/IndexCatchUpCommand.php -text
lib/core/Tiki/Command/IndexOptimizeCommand.php -text
......@@ -3051,6 +3053,7 @@ lib/faqs/index.php -text
lib/featured_links/flinkslib.php -text
lib/featured_links/index.php -text
lib/filegals/filegalbatchlib.php -text
lib/filegals/filegalcopylib.php -text
lib/filegals/filegallib.php -text
lib/filegals/index.php -text
lib/filegals/max_upload_size.php -text
......
......@@ -102,7 +102,9 @@ if ($isInstalled && ! $installer->requiresUpdate()) {
$console->add(new Tiki\Command\DailyReportSendCommand);
$console->add(new Tiki\Command\GoalCheckCommand);
$console->add(new Tiki\Command\FilesBatchuploadCommand);
$console->add(new Tiki\Command\FilesCopyCommand);
$console->add(new Tiki\Command\FilesDeleteoldCommand);
$console->add(new Tiki\Command\FilesMoveCommand);
$console->add(new Tiki\Command\IndexRebuildCommand);
$console->add(new Tiki\Command\IndexOptimizeCommand);
$console->add(new Tiki\Command\IndexCatchUpCommand);
......
......@@ -105,6 +105,9 @@
<service id="tiki.lib.filegalbatch" class="FileGalBatchLib">
<file>%kernel.root_dir%/lib/filegals/filegalbatchlib.php</file>
</service>
<service id="tiki.lib.filegalcopy" class="FileGalCopyLib">
<file>%kernel.root_dir%/lib/filegals/filegalcopylib.php</file>
</service>
<service id="tiki.lib.freetag" class="FreetagLib">
<file>%kernel.root_dir%/lib/freetag/freetaglib.php</file>
</service>
......
<?php
// (c) Copyright 2002-2017 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class FilesCopyCommand extends Command
{
protected function configure()
{
$this
->setName('files:copy')
->setDescription(tra('Copy files from file galleries to a regular directory on the file system'))
->addArgument(
'galleryId',
InputArgument::REQUIRED,
'Gallery to copy files from'
)
->addArgument(
'destinationPath',
InputArgument::REQUIRED,
'Path to copy files to'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
global $prefs;
if ($prefs['feature_file_galleries'] != 'y') {
throw new \Exception(tra('Feature Galleries not set up'));
}
$filegallib = \TikiLib::lib('filegal');
$filegalcopylib = \TikiLib::lib('filegalcopy');
$galleryId = (int) $input->getArgument('galleryId');
$gal_info = $filegallib->get_file_gallery($galleryId);
if (! $gal_info || empty($gal_info['name'])) {
throw new \Exception(tr('File Copy: Gallery #%0 not found', $galleryId));
}
$destinationPath = $input->getArgument('destinationPath');
if (empty($destinationPath)) {
throw new \Exception(tra('File Copy: Destination path required'));
}
$sourcePath = $filegallib->get_gallery_save_dir($galleryId);
// in the unlikely case where fgal_use_db was once !== 'y' and then became == 'y'
if (empty($sourcePath)) {
$sourcePath = $prefs['fgal_use_dir'];
}
$files = $filegallib->get_files_info_from_gallery_id($galleryId);
if (! $files) {
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<comment>' . tra('No files to copy') . '</comment>');
}
return;
}
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<comment>' . tra('File Copy starting...') . '</comment>');
}
$feedback = $filegalcopylib->processCopy($files, $destinationPath, $sourcePath);
foreach ($feedback as $message) {
$error = strpos($message, '<span class="text-danger">') !== false;
$message = strip_tags(str_replace('<br>', ' : ', $message));
if ($error) {
$message = "<error>$message</error>";
$output->writeln($message);
} elseif ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$message = "<info>$message</info>";
$output->writeln($message);
}
}
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<comment>' . tra('File Copy complete') . '</comment>');
}
}
}
<?php
// (c) Copyright 2002-2017 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
namespace Tiki\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\HelpCommand;
class FilesMoveCommand extends Command
{
protected function configure()
{
$this
->setName('files:move')
->setDescription(tra('Move files from file galleries to a regular directory on the file system'))
->addArgument(
'galleryId',
InputArgument::REQUIRED,
'Gallery to move files from'
)
->addArgument(
'destinationPath',
InputArgument::REQUIRED,
'Path to move files to'
)
->addOption(
'confirm',
null,
InputOption::VALUE_NONE,
'Confirm the move operation (required)'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
global $prefs;
if ($prefs['feature_file_galleries'] != 'y') {
throw new \Exception(tra('Feature Galleries not set up'));
}
$filegallib = \TikiLib::lib('filegal');
$filegalcopylib = \TikiLib::lib('filegalcopy');
$galleryId = (int) $input->getArgument('galleryId');
$gal_info = $filegallib->get_file_gallery($galleryId);
if (! $gal_info || empty($gal_info['name'])) {
throw new \Exception(tr('File Move: Gallery #%0 not found', $galleryId));
}
$destinationPath = $input->getArgument('destinationPath');
if (empty($destinationPath)) {
throw new \Exception(tra('File Move: Destination path required'));
}
$sourcePath = $filegallib->get_gallery_save_dir($galleryId);
// in the unlikely case where fgal_use_db was once !== 'y' and then became == 'y'
if (empty($sourcePath)) {
$sourcePath = $prefs['fgal_use_dir'];
}
$files = $filegallib->get_files_info_from_gallery_id($galleryId);
if (! $files) {
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<comment>' . tra('No files to move') . '</comment>');
}
return;
}
$confirm = $input->getOption('confirm');
if (! $confirm) {
$help = new HelpCommand();
$help->setCommand($this);
$help->run($input, $output);
throw new \Exception(tra('Use the --confirm option to proceed with the move operation'));
}
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<comment>' . tra('File Move starting...') . '</comment>');
}
$feedback = $filegalcopylib->processCopy($files, $destinationPath, $sourcePath, true);
foreach ($feedback as $message) {
$error = strpos($message, '<span class="text-danger">') !== false;
$message = strip_tags(str_replace('<br>', ' : ', $message));
if ($error) {
$message = "<error>$message</error>";
$output->writeln($message);
} elseif ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$message = "<info>$message</info>";
$output->writeln($message);
}
}
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
$output->writeln('<comment>' . tra('File Move complete') . '</comment>');
}
}
}
<?php
// (c) Copyright 2002-2017 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
//this script may only be included - so its better to die if called directly.
if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
header("location: index.php");
exit;
}
/**
* Class FileGalCopyLib
*
* Container for functions involved in files:copy and files:move console commands
*
*/
class FileGalCopyLib extends FileGalLib
{
/**
* Processes a list of files to be copied/moved to a directory in the filesystem
*
* @param array $files
* @param string $destinationPath
* @param string $sourcePath
* @param bool $move
* @return array feedback messages
*/
function processCopy($files, $destinationPath, $sourcePath = '', $move = false)
{
$feedback = [];
// cycle through all files to copy
foreach ($files as $file) {
$result = $this->copyFile($file, $destinationPath, $sourcePath, $move);
if (isset($result['error'])) {
if ($move) {
$feedback[] = '<span class="text-danger">' . tr('Move was not successful for "%0"', $file['filename']) . '<br>(' . $result['error'] . ')</span>';
} else {
$feedback[] = '<span class="text-danger">' . tr('Copy was not successful for "%0"', $file['filename']) . '<br>(' . $result['error'] . ')</span>';
}
} else {
if ($move) {
$feedback[] = tra('Move was successful') . ': ' . $file['filename'];
} else {
$feedback[] = tra('Copy was successful') . ': ' . $file['filename'];
}
}
}
return $feedback;
}
/**
* Takes a file from a file gallery and copies/moves it to a local path
*
* @param array $file
* @param string $destinationPath
* @param string $sourcePath
* @param bool $move
* @return array [fileName[,error]]
*/
function copyFile($file, $destinationPath, $sourcePath = '', $move = false)
{
$fileId = $file['fileId'];
$filePath = $file['path'];
$fileName = $file['filename'];
if (! empty($filePath)) { // i.e., fgal_use_db !== 'y'
if ($sourcePath == '') {
return ['error' => tra('Source path empty')];
}
if (! copy($sourcePath . $filePath, $destinationPath . $fileName)) {
if (! is_writable($destinationPath)) {
return ['error' => tra('Cannot write to this path: ') . $destinationPath];
} else {
return ['error' => tra('Cannot read this file: ') . $sourcePath . $filePath];
}
}
} else {
$filesTable = $this->table('tiki_files');
$fileData = $filesTable->fetchOne('data', ['fileId' => (int)$fileId]);
if (file_put_contents($destinationPath . $fileName, $fileData) === false) {
if (! is_writable($destinationPath)) {
return ['error' => tra('Cannot write to this path: ') . $destinationPath];
} else {
return ['error' => tra('Cannot get filedata from db')];
}
}
}
if ($move) {
// This is a hack to bypass inconsistency in filegallib that would cause a Notice
// message to the user.
// remove_file() needs $file['data'], despite it being an optional field.
// In the end, no Handlers in FileGallery implement any usage of $file['data']
$file['data'] = null;
if ($this->remove_file($file, '', true) === false) {
return ['error' => tra('Cannot remove file from gallery')];
}
}
return [
'fileName' => $fileName,
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment