2023-03-23 01:16:12 +01:00
< ? php
/*
* This file is part of Part - DB ( https :// github . com / Part - DB / Part - DB - symfony ) .
*
* Copyright ( C ) 2019 - 2023 Jan Böhmer ( https :// github . com / jbtronics )
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
namespace App\Command\Migrations ;
2023-03-25 00:25:18 +01:00
use App\Services\ImportExportSystem\PartKeeprImporter\PKDatastructureImporter ;
use App\Services\ImportExportSystem\PartKeeprImporter\MySQLDumpXMLConverter ;
use App\Services\ImportExportSystem\PartKeeprImporter\PKImportHelper ;
use App\Services\ImportExportSystem\PartKeeprImporter\PKPartImporter ;
2023-03-25 21:09:02 +01:00
use App\Services\ImportExportSystem\PartKeeprImporter\PKOptionalImporter ;
2023-03-23 01:16:12 +01:00
use Doctrine\ORM\EntityManagerInterface ;
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputInterface ;
2023-03-25 21:09:02 +01:00
use Symfony\Component\Console\Input\InputOption ;
2023-03-23 01:16:12 +01:00
use Symfony\Component\Console\Output\OutputInterface ;
use Symfony\Component\Console\Style\SymfonyStyle ;
2023-05-28 01:21:05 +02:00
#[\Symfony\Component\Console\Attribute\AsCommand('partdb:migrations:import-partkeepr', 'Import a PartKeepr database XML dump into Part-DB')]
2023-03-23 01:16:12 +01:00
class ImportPartKeeprCommand extends Command
{
2023-06-11 14:15:46 +02:00
public function __construct ( protected EntityManagerInterface $em , protected MySQLDumpXMLConverter $xml_converter ,
protected PKDatastructureImporter $datastructureImporter , protected PKPartImporter $partImporter , protected PKImportHelper $importHelper ,
protected PKOptionalImporter $optionalImporter )
2023-03-23 01:16:12 +01:00
{
parent :: __construct ( self :: $defaultName );
}
protected function configure ()
{
2023-03-25 23:09:12 +01:00
$this -> setHelp ( 'This command allows you to import a PartKeepr database exported by mysqldump as XML file into Part-DB' );
2023-03-23 01:16:12 +01:00
$this -> addArgument ( 'file' , InputArgument :: REQUIRED , 'The file to which should be imported.' );
2023-03-25 21:09:02 +01:00
$this -> addOption ( '--no-projects' , null , InputOption :: VALUE_NONE , 'Do not import projects.' );
$this -> addOption ( '--import-users' , null , InputOption :: VALUE_NONE , 'Import users (passwords will not be imported).' );
2023-03-23 01:16:12 +01:00
}
2023-04-15 19:33:39 +02:00
public function execute ( InputInterface $input , OutputInterface $output ) : int
2023-03-23 01:16:12 +01:00
{
$io = new SymfonyStyle ( $input , $output );
$input_path = $input -> getArgument ( 'file' );
2023-03-25 21:09:02 +01:00
$no_projects_import = $input -> getOption ( 'no-projects' );
$import_users = $input -> getOption ( 'import-users' );
2023-03-23 01:16:12 +01:00
2023-03-25 23:09:12 +01:00
$io -> note ( 'This command is still in development. If you encounter any problems, please report them to the issue tracker on GitHub.' );
$io -> warning ( 'This command will delete all existing data in the database (except users). Make sure that you have no important data in the database before you continue!' );
$io -> ask ( 'Please type "DELETE ALL DATA" to continue.' , '' , function ( $answer ) {
if ( strtoupper ( $answer ) !== 'DELETE ALL DATA' ) {
throw new \RuntimeException ( 'You did not type "DELETE ALL DATA"!' );
}
return $answer ;
});
2023-03-23 01:16:12 +01:00
//Make more checks here
//$io->confirm('This will delete all data in the database. Do you want to continue?', false);
//Purge the databse, so we will not have any conflicts
2023-03-25 00:25:18 +01:00
$this -> importHelper -> purgeDatabaseForImport ();
2023-03-23 01:16:12 +01:00
//Convert the XML file to an array
$xml = file_get_contents ( $input_path );
$data = $this -> xml_converter -> convertMySQLDumpXMLDataToArrayStructure ( $xml );
2023-03-25 21:24:58 +01:00
if ( ! $this -> importHelper -> checkVersion ( $data )) {
$db_version = $this -> importHelper -> getDatabaseSchemaVersion ( $data );
$io -> error ( 'The version of the imported database is not supported! (Version: ' . $db_version . ')' );
2023-05-28 01:21:05 +02:00
return \Symfony\Component\Console\Command\Command :: FAILURE ;
2023-03-25 21:24:58 +01:00
}
2023-03-25 21:09:02 +01:00
//Import the mandatory data
2023-03-23 01:16:12 +01:00
$this -> doImport ( $io , $data );
2023-03-25 21:09:02 +01:00
if ( ! $no_projects_import ) {
$io -> info ( 'Importing projects...' );
$count = $this -> optionalImporter -> importProjects ( $data );
$io -> success ( 'Imported ' . $count . ' projects.' );
}
if ( $import_users ) {
$io -> info ( 'Importing users...' );
$count = $this -> optionalImporter -> importUsers ( $data );
$io -> success ( 'Imported ' . $count . ' users.' );
}
2023-05-28 01:21:05 +02:00
return \Symfony\Component\Console\Command\Command :: SUCCESS ;
2023-03-23 01:16:12 +01:00
}
2023-03-25 00:25:18 +01:00
private function doImport ( SymfonyStyle $io , array $data ) : void
2023-03-23 01:16:12 +01:00
{
//First import the distributors
$io -> info ( 'Importing distributors...' );
2023-03-25 00:25:18 +01:00
$count = $this -> datastructureImporter -> importDistributors ( $data );
2023-03-23 01:16:12 +01:00
$io -> success ( 'Imported ' . $count . ' distributors.' );
//Import the measurement units
$io -> info ( 'Importing part measurement units...' );
2023-03-25 00:25:18 +01:00
$count = $this -> datastructureImporter -> importPartUnits ( $data );
2023-03-23 01:16:12 +01:00
$io -> success ( 'Imported ' . $count . ' measurement units.' );
//Import manufacturers
$io -> info ( 'Importing manufacturers...' );
2023-03-25 00:25:18 +01:00
$count = $this -> datastructureImporter -> importManufacturers ( $data );
2023-03-23 01:16:12 +01:00
$io -> success ( 'Imported ' . $count . ' manufacturers.' );
2023-03-24 22:41:33 +01:00
$io -> info ( 'Importing categories...' );
2023-03-25 00:25:18 +01:00
$count = $this -> datastructureImporter -> importCategories ( $data );
2023-03-24 22:41:33 +01:00
$io -> success ( 'Imported ' . $count . ' categories.' );
$io -> info ( 'Importing Footprints...' );
2023-03-25 00:25:18 +01:00
$count = $this -> datastructureImporter -> importFootprints ( $data );
2023-03-24 22:41:33 +01:00
$io -> success ( 'Imported ' . $count . ' footprints.' );
2023-03-24 22:51:41 +01:00
$io -> info ( 'Importing storage locations...' );
2023-03-25 00:25:18 +01:00
$count = $this -> datastructureImporter -> importStorelocations ( $data );
2023-03-24 22:51:41 +01:00
$io -> success ( 'Imported ' . $count . ' storage locations.' );
2023-03-24 23:43:05 +01:00
$io -> info ( 'Importing parts...' );
2023-03-25 00:25:18 +01:00
$count = $this -> partImporter -> importParts ( $data );
2023-03-24 23:43:05 +01:00
$io -> success ( 'Imported ' . $count . ' parts.' );
2023-03-23 01:16:12 +01:00
}
}