mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-10 10:24:31 +02:00
Started to work on an import possibility for Partkeepr databases
This commit is contained in:
parent
0550c045c7
commit
fce32e70b9
7 changed files with 9365 additions and 1 deletions
154
src/Services/ImportExportSystem/PartkeeprImporter.php
Normal file
154
src/Services/ImportExportSystem/PartkeeprImporter.php
Normal file
|
@ -0,0 +1,154 @@
|
|||
<?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\Services\ImportExportSystem;
|
||||
|
||||
use App\Doctrine\Purger\ResetAutoIncrementORMPurger;
|
||||
use App\Doctrine\Purger\ResetAutoIncrementPurgerFactory;
|
||||
use App\Entity\Base\AbstractDBElement;
|
||||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\MeasurementUnit;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Entity\Parts\Supplier;
|
||||
use Doctrine\Bundle\FixturesBundle\Purger\ORMPurgerFactory;
|
||||
use Doctrine\Bundle\FixturesBundle\Purger\PurgerFactory;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
||||
|
||||
class PartkeeprImporter
|
||||
{
|
||||
|
||||
protected EntityManagerInterface $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function purgeDatabaseForImport(): void
|
||||
{
|
||||
//Versions with "" are needed !!
|
||||
$purger = new ResetAutoIncrementORMPurger($this->em, ['users', '"users"', 'groups', '"groups"', 'u2f_keys', 'internal', 'migration_versions']);
|
||||
$purger->purge();
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports the distributors from the given data.
|
||||
* @param array $data The data to import (associated array, containing a 'distributor' key
|
||||
* @return int The number of imported distributors
|
||||
*/
|
||||
public function importDistributors(array $data): int
|
||||
{
|
||||
if (!isset($data['distributor'])) {
|
||||
throw new \RuntimeException('$data must contain a "distributor" key!');
|
||||
}
|
||||
|
||||
$distributor_data = $data['distributor'];
|
||||
|
||||
foreach ($distributor_data as $distributor) {
|
||||
$supplier = new Supplier();
|
||||
$supplier->setName($distributor['name']);
|
||||
$supplier->setWebsite($distributor['url'] ?? '');
|
||||
$supplier->setAddress($distributor['address'] ?? '');
|
||||
$supplier->setPhoneNumber($distributor['phone'] ?? '');
|
||||
$supplier->setFaxNumber($distributor['fax'] ?? '');
|
||||
$supplier->setEmailAddress($distributor['email'] ?? '');
|
||||
$supplier->setComment($distributor['comment']);
|
||||
$supplier->setAutoProductUrl($distributor['skuurl'] ?? '');
|
||||
|
||||
$this->setIDOfEntity($supplier, $distributor['id']);
|
||||
$this->em->persist($supplier);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return count($distributor_data);
|
||||
}
|
||||
|
||||
public function importManufacturers(array $data): int
|
||||
{
|
||||
if (!isset($data['manufacturer'])) {
|
||||
throw new \RuntimeException('$data must contain a "manufacturer" key!');
|
||||
}
|
||||
|
||||
$manufacturer_data = $data['manufacturer'];
|
||||
|
||||
$max_id = 0;
|
||||
|
||||
//Assign a parent manufacturer to all manufacturers, as partkeepr has a lot of manufacturers by default
|
||||
$parent_manufacturer = new Manufacturer();
|
||||
$parent_manufacturer->setName('PartKeepr');
|
||||
$parent_manufacturer->setNotSelectable(true);
|
||||
|
||||
foreach ($manufacturer_data as $manufacturer) {
|
||||
$entity = new Manufacturer();
|
||||
$entity->setName($manufacturer['name']);
|
||||
$entity->setWebsite($manufacturer['url'] ?? '');
|
||||
$entity->setAddress($manufacturer['address'] ?? '');
|
||||
$entity->setPhoneNumber($manufacturer['phone'] ?? '');
|
||||
$entity->setFaxNumber($manufacturer['fax'] ?? '');
|
||||
$entity->setEmailAddress($manufacturer['email'] ?? '');
|
||||
$entity->setComment($manufacturer['comment']);
|
||||
$entity->setParent($parent_manufacturer);
|
||||
|
||||
$this->setIDOfEntity($entity, $manufacturer['id']);
|
||||
$this->em->persist($entity);
|
||||
|
||||
$max_id = max($max_id, $manufacturer['id']);
|
||||
}
|
||||
|
||||
//Set the ID of the parent manufacturer to the max ID + 1, to avoid trouble with the auto increment
|
||||
$this->setIDOfEntity($parent_manufacturer, $max_id + 1);
|
||||
$this->em->persist($parent_manufacturer);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return count($manufacturer_data);
|
||||
}
|
||||
|
||||
public function importPartUnits(array $data): int
|
||||
{
|
||||
if (!isset($data['partunit'])) {
|
||||
throw new \RuntimeException('$data must contain a "partunit" key!');
|
||||
}
|
||||
|
||||
$partunit_data = $data['partunit'];
|
||||
foreach ($partunit_data as $partunit) {
|
||||
$unit = new MeasurementUnit();
|
||||
$unit->setName($partunit['name']);
|
||||
$unit->setUnit($partunit['shortName'] ?? null);
|
||||
|
||||
$this->setIDOfEntity($unit, $partunit['id']);
|
||||
$this->em->persist($unit);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return count($partunit_data);
|
||||
}
|
||||
|
||||
public function setIDOfEntity(AbstractDBElement $element, int $id): void
|
||||
{
|
||||
$metadata = $this->em->getClassMetadata(get_class($element));
|
||||
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
|
||||
$metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
|
||||
$metadata->setIdentifierValues($element, ['id' => $id]);
|
||||
}
|
||||
}
|
107
src/Services/Misc/MySQLDumpXMLConverter.php
Normal file
107
src/Services/Misc/MySQLDumpXMLConverter.php
Normal file
|
@ -0,0 +1,107 @@
|
|||
<?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\Services\Misc;
|
||||
|
||||
class MySQLDumpXMLConverter
|
||||
{
|
||||
|
||||
/**
|
||||
* Converts a MySQL dump XML file to an associative array structure in the following form
|
||||
* [
|
||||
* 'table_name' => [
|
||||
* [
|
||||
* 'column_name' => 'value',
|
||||
* 'column_name' => 'value',
|
||||
* ...
|
||||
* ],
|
||||
* [
|
||||
* 'column_name' => 'value',
|
||||
* 'column_name' => 'value',
|
||||
* ...
|
||||
* ],
|
||||
* ...
|
||||
* ],
|
||||
*
|
||||
* @param string $xml_string The XML string to convert
|
||||
* @return array The associative array structure
|
||||
*/
|
||||
public function convertMySQLDumpXMLDataToArrayStructure(string $xml_string): array
|
||||
{
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadXML($xml_string);
|
||||
|
||||
//Check that the root node is a <mysqldump> node
|
||||
$root = $dom->documentElement;
|
||||
if ($root->nodeName !== 'mysqldump') {
|
||||
throw new \InvalidArgumentException('The given XML string is not a valid MySQL dump XML file!');
|
||||
}
|
||||
|
||||
//Get all <database> nodes (there must be exactly one)
|
||||
$databases = $root->getElementsByTagName('database');
|
||||
if ($databases->length !== 1) {
|
||||
throw new \InvalidArgumentException('The given XML string is not a valid MySQL dump XML file!');
|
||||
}
|
||||
|
||||
//Get the <database> node
|
||||
$database = $databases->item(0);
|
||||
|
||||
//Get all <table_data> nodes
|
||||
$tables = $database->getElementsByTagName('table_data');
|
||||
$table_data = [];
|
||||
|
||||
//Iterate over all <table> nodes and convert them to arrays
|
||||
foreach ($tables as $table) {
|
||||
$table_data[$table->getAttribute('name')] = $this->convertTableToArray($table);
|
||||
}
|
||||
|
||||
return $table_data;
|
||||
}
|
||||
|
||||
private function convertTableToArray(\DOMElement $table): array
|
||||
{
|
||||
$table_data = [];
|
||||
|
||||
//Get all <row> nodes
|
||||
$rows = $table->getElementsByTagName('row');
|
||||
|
||||
//Iterate over all <row> nodes and convert them to arrays
|
||||
foreach ($rows as $row) {
|
||||
$table_data[] = $this->convertTableRowToArray($row);
|
||||
}
|
||||
|
||||
return $table_data;
|
||||
}
|
||||
|
||||
private function convertTableRowToArray(\DOMElement $table_row): array
|
||||
{
|
||||
$row_data = [];
|
||||
|
||||
//Get all <field> nodes
|
||||
$fields = $table_row->getElementsByTagName('field');
|
||||
|
||||
//Iterate over all <field> nodes
|
||||
foreach ($fields as $field) {
|
||||
$row_data[$field->getAttribute('name')] = $field->nodeValue;
|
||||
}
|
||||
|
||||
return $row_data;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue