partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit a7d7a82e765319ab66a28027e780eda4ab3dd9a4
parent 4e3be27d06da0974fee1b3c42b4b3ebdf97ed258
Author: Felicitus <felicitus@felicitus.org>
Date:   Wed, 29 Jun 2011 05:29:20 +0200

Refactored setup script, misc bugfixes

Diffstat:
Msetup/data/units.yaml | 10+++++-----
Asrc/de/RaumZeitLabor/PartKeepr/Setup/DistributorSetup.php | 40++++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/Setup/FootprintSetup.php | 149+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/Setup/ManufacturerSetup.php | 36++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/Setup/PartCategorySetup.php | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/Setup/PartSetup.php | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/Setup/PartUnitSetup.php | 36++++++++++++++++++++++++++++++++++++
Msrc/de/RaumZeitLabor/PartKeepr/Setup/Setup.php | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Asrc/de/RaumZeitLabor/PartKeepr/Setup/SiPrefixSetup.php | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/Setup/StorageLocationSetup.php | 39+++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartKeepr/Setup/UnitSetup.php | 44++++++++++++++++++++++++++++++++++++++++++++
Mtesting/SetupDatabase.php | 410++++++++++++++++---------------------------------------------------------------
12 files changed, 735 insertions(+), 335 deletions(-)

diff --git a/setup/data/units.yaml b/setup/data/units.yaml @@ -2,7 +2,7 @@ Meter: symbol: m prefixes: - n - - µ + - μ - m - c - d @@ -37,7 +37,7 @@ Ampere: - k - m - "" - - µ + - μ - n - p Ohm: @@ -89,7 +89,7 @@ Watt: - "" - G - m - - µ + - μ Coulomb: symbol: C prefixes: @@ -120,7 +120,7 @@ Henry: symbol: H prefixes: - m - - µ + - μ - "" Celsius: symbol: °C @@ -147,7 +147,7 @@ Sievert: prefixes: - m - "" - - µ + - μ Katal: symbol: kat prefixes: diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/DistributorSetup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/DistributorSetup.php @@ -0,0 +1,40 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Setup; + +use de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\Distributor\Distributor, + de\RaumZeitLabor\PartKeepr\Setup\SiPrefixSetup; + +class DistributorSetup { + /** + * Holds the migrated distributors for easy access by id + * @var array + */ + private static $migratedDistributors = array(); + + /** + * Migrates the existing distributors + */ + public static function migrateDistributors () { + Setup::progress("Migrating distributors..."); + + $r = mysql_query("SELECT * FROM suppliers"); + while ($supplier = mysql_fetch_assoc($r)) { + Setup::progress(" - Migrating distributor ".$supplier["name"], true); + $distributor = new Distributor(); + $distributor->setName($supplier["name"]); + + DistributorSetup::$migratedDistributors[$supplier["id"]] = $distributor; + PartKeepr::getEM()->persist($distributor); + } + + } + + /** + * Returns the migrated distributor by it's ID from the old partdb + * @param int $id The distributor's ID from the old partdb + */ + public static function getMigratedDistributor ($id) { + return DistributorSetup::$migratedDistributors[$id]; + } +} diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/FootprintSetup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/FootprintSetup.php @@ -0,0 +1,148 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Setup; + +use de\RaumZeitLabor\PartKeepr\Footprint\FootprintManager, + de\RaumZeitLabor\PartKeepr\FootprintCategory\FootprintCategoryManager, + de\RaumZeitLabor\PartKeepr\FootprintCategory\FootprintCategory, + de\RaumZeitLabor\PartKeepr\Footprint\Footprint, + de\RaumZeitLabor\PartKeepr\Footprint\FootprintImage, + de\RaumZeitLabor\PartKeepr\Footprint\FootprintAttachment, + de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\Setup\Setup; + +class FootprintSetup { + /** + * Holds the migrated footprints + * @var array + */ + private static $migratedFootprints = array(); + + /** + * Creates the root node for the footprints + */ + public static function setupRootNode () { + FootprintCategoryManager::getInstance()->ensureRootExists(); + } + + /** + * Returns a footprint by it's partdb id + * @param int $id The footprint id from the old partdb + */ + public static function getFootprintForOldId ($id) { + return FootprintSetup::$migratedFootprints[$id]; + } + + /** + * Creates a node structure for the given path + * + * @param $path array The components of the path + * @param $node Node The parent node + */ + public static function addFootprintPath (Array $path, $node) { + if (count($path) == 0) { + return $node; + } + $name = array_shift($path); + + $childNode = null; + + foreach ($node->getChildren() as $child) { + if ($child->getNode()->getName() == $name) { + $childNode = $child; + } + } + + if ($childNode === null) { + $category = new FootprintCategory(); + $category->setParent($node->getNode()->getId()); + $category->setName($name); + $childNode = FootprintCategoryManager::getInstance()->addCategory($category); + } + + return FootprintSetup::addFootprintPath($path, $childNode); + } + + /** + * Migrates the existing footprints from the PartDB. + */ + public static function migrateFootprints () { + Setup::progress("Migrating footprints..."); + $r = mysql_query("SELECT * FROM footprints"); + + while ($sFootprint = mysql_fetch_assoc($r)) { + Setup::progress(" - Migrating footprint ".$sFootprint["name"], true); + + $footprint = new Footprint(); + $footprint->setName(Setup::convertText($sFootprint["name"])); + + + $footprintCategory = FootprintSetup::addFootprintPath(explode("/", "Imported Footprints"), FootprintCategoryManager::getInstance()->getRootNode()); + $footprint->setCategory($footprintCategory->getNode()); + + PartKeepr::getEM()->persist($footprint); + + FootprintSetup::$migratedFootprints[$sFootprint["id"]] = $footprint; + } + } + /** + * Imports the footprints + * @throws \Exception + */ + public static function importFootprintData ($file) { + Setup::progress("Adding predefined footprints..."); + + /* Import pre-defined footprints */ + $data = \Symfony\Component\Yaml\Yaml::load("../setup/data/footprints/footprints.yaml"); + + foreach ($data as $footprintName => $footprintData) { + Setup::progress(" - Adding footprint ".$footprintName, true); + $footprint = new Footprint(); + $footprint->setName($footprintName); + + if (array_key_exists("description", $footprintData)) { + $footprint->setDescription($footprintData["description"]); + } + + if (array_key_exists("category", $footprintData)) { + $footprintCategory = FootprintSetup::addFootprintPath(explode("/", $footprintData["category"]), FootprintCategoryManager::getInstance()->getRootNode()); + $footprint->setCategory($footprintCategory->getNode()); + } + + if (array_key_exists("image", $footprintData)) { + $footprintImage = new FootprintImage(); + $footprintImage->setFootprint($footprint); + $footprintImage->replace("../setup/data/footprints/" . $footprintData["image"]); + + $footprint->setImage($footprintImage); + } + + if (array_key_exists("attachments", $footprintData) && is_array($footprintData["attachments"])) { + foreach ($footprintData["attachments"] as $attachment) { + if (!is_array($attachment)) { + throw new \Exception("Error: The property 'attachments' of $footprintName is not an array!"); + } + if (array_key_exists("url", $attachment)) { + try { + $footprintAttachment = new FootprintAttachment(); + $footprintAttachment->setFootprint($footprint); + $footprintAttachment->replaceFromURL($attachment["url"]); + if (array_key_exists("description", $attachment)) { + $footprintAttachment->setDescription($attachment["description"]); + } + + $footprint->getAttachments()->add($footprintAttachment); + } catch (\Exception $e) { + echo "error with url ".$attachment["url"]."\n"; + } + + + + } + + } + } + + PartKeepr::getEM()->persist($footprint); + } + } +}+ \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/ManufacturerSetup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/ManufacturerSetup.php @@ -0,0 +1,36 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Setup; + +use de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\Manufacturer\Manufacturer, + de\RaumZeitLabor\PartKeepr\Manufacturer\ManufacturerICLogo, + de\RaumZeitLabor\PartKeepr\Setup\SiPrefixSetup; + +class ManufacturerSetup { + /** + * Sets up the manufacturers using the YAML file. + * @param $yaml string The path to the manufacturers YAML file + */ + public static function setupManufacturers ($yaml) { + Setup::progress("Setting up Manufacturers..."); + + $data = Setup::loadYAML($yaml); + + foreach ($data as $mfgname => $logos) { + Setup::progress(" - Adding manufacturer ".$mfgname, true); + $manufacturer = new Manufacturer(); + $manufacturer->setName($mfgname); + + PartKeepr::getEM()->persist($manufacturer); + + foreach ($logos as $logo) { + $mfglogo = new ManufacturerICLogo(); + $mfglogo->setManufacturer($manufacturer); + $mfglogo->replace("../setup/data/manufacturers/images/".$logo); + $mfglogo->setOriginalFilename($logo); + + PartKeepr::getEM()->persist($mfglogo); + } + } + } +} diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/PartCategorySetup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/PartCategorySetup.php @@ -0,0 +1,77 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Setup; + +use de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\PartCategory\PartCategoryManager, + de\RaumZeitLabor\PartKeepr\PartCategory\PartCategory; + +class PartCategorySetup { + /** + * Holds the migrated categories + * @var array + */ + private static $migratedCategories = array(); + + /** + * Sets up the root category node + */ + public static function setupRootCategory () { + Setup::progress("Creating category root node..."); + PartCategoryManager::getInstance()->ensureRootExists(); + } + + /** + * Migrates the old categories + */ + public static function migrateCategories () { + Setup::progress("Migrating part categories..."); + $r = mysql_query("SELECT * FROM categories"); + + $categories = array(); + + while ($cat = mysql_fetch_assoc($r)) { + $categories[] = $cat; + } + + PartCategorySetup::addCategoryRecursive($categories, 0, PartCategoryManager::getInstance()->getRootNode()); + } + + /** + * Creates the category tree, recursive + * @param array $aCategories the categories + * @param id $currentId The current ID to migrate + * @param Node $parent The parent node + */ + private static function addCategoryRecursive ($aCategories, $currentId, $parent) { + global $newCategories; + + foreach ($aCategories as $aCategory) { + if ($aCategory["parentnode"] == $currentId) { + Setup::progress(" - Adding ".$aCategory["name"], true); + $oCategory = new PartCategory(); + $oCategory->setName(Setup::convertText($aCategory["name"])); + $oCategory->setDescription(""); + $oCategory->setParent($parent->getId()); + + $category = PartCategoryManager::getInstance()->addCategory($oCategory); + + PartCategorySetup::addCategoryRecursive($aCategories, $aCategory["id"], $category); + + PartCategorySetup::$migratedCategories[$aCategory["id"]] = $oCategory; + } + } + + } + + /** + * Returns a migrated category by it's partdb id + * @param int $id the category partdb id + */ + public static function getMigratedCategory ($id) { + if (!array_key_exists($id, PartCategorySetup::$migratedCategories)) { + return null; + } + + return PartCategorySetup::$migratedCategories[$id]; + } +} diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/PartSetup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/PartSetup.php @@ -0,0 +1,106 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Setup; + +use de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\Part\Part, + de\RaumZeitLabor\PartKeepr\Part\PartAttachment, + de\RaumZeitLabor\PartKeepr\PartCategory\PartCategoryManager, + de\RaumZeitLabor\PartKeepr\Setup\SiPrefixSetup, + de\RaumZeitLabor\PartKeepr\Part\PartDistributor, + de\RaumZeitLabor\PartKeepr\Stock\StockEntry, + de\RaumZeitLabor\PartKeepr\Part\PartManufacturer; + +class PartSetup { + /** + * Defines the size of the records which are held in memory unless we flush to the DB. + * @var int + */ + const STEP_SIZE = 100; + + /** + * Migrates the parts from partdb + */ + public static function migrateParts () { + $r = mysql_query("SELECT COUNT(*) AS count FROM parts"); + $cntArray = mysql_fetch_assoc($r); + + $count = $cntArray["count"]; + + $r = mysql_query("SELECT * FROM parts"); + + Setup::progress("Migrating parts..."); + $fc=0; + $cnt=0; + + while ($part = mysql_fetch_assoc($r)) { + $cnt++; + Setup::progress(" - Adding part ".$cnt."/".$count." ".Setup::convertText($part["name"]), true); + $oPart = new Part(); + $oPart->setName(Setup::convertText($part["name"])); + $oPart->setComment(Setup::convertText($part["comment"])); + $oPart->setFootprint(FootprintSetup::getFootprintForOldId($part["id_footprint"])); + $oPart->setReviewFlag(true); + $category = PartCategorySetup::getMigratedCategory($part["id_category"]); + + if ($category === null) { + PartCategoryManager::getInstance()->getRootNode()->getNode(); + } else { + $oPart->setCategory($category); + } + + $oPart->setStorageLocation(StorageLocationSetup::getMigratedStorageLocation($part["id_storeloc"])); + $oPart->setMinStockLevel($part["mininstock"]); + $oPart->setPartUnit(PartUnitSetup::getDefaultPartUnit()); + + $partDistributor = new PartDistributor(); + $partDistributor->setPart($oPart); + $partDistributor->setDistributor(DistributorSetup::getMigratedDistributor($part["id_supplier"])); + $partDistributor->setOrderNumber($part["supplierpartnr"]); + $oPart->getDistributors()->add($partDistributor); + + + /* Add existing datasheets */ + $datasheetQuery = "SELECT datasheeturl FROM datasheets WHERE part_id = ".$part["id"]; + $r3 = mysql_query($datasheetQuery); + while ($res = mysql_fetch_assoc($r3)) { + try { + $attachment = new PartAttachment(); + $attachment->setPart($oPart); + $attachment->replaceFromURL($res["datasheeturl"]); + $attachment->setDescription(PartKeepr::i18n("Datasheet")); + $oPart->getAttachments()->add($attachment); + } catch (\Exception $e) { + Setup::progress(" - error with url ".$res["datasheeturl"].". Maybe the datasheet was not found."); + Setup::progress(" - The exception error was: ".$e->getMessage()); + } + } + + PartKeepr::getEM()->persist($oPart); + + $oStock = new StockEntry($oPart, $part["instock"]); + + $priceQuery = "SELECT AVG(preis) AS preis FROM preise WHERE part_id = ".$part["id"]; + + $r2 = mysql_query($priceQuery); + $res = mysql_fetch_assoc($r2); + + if ($res) { + if ($res["preis"] !== null) { + $oStock->setPrice(floatval($res["preis"])); + } + } + + PartKeepr::getEM()->persist($oStock); + + $fc++; + + // Flush every STEP_SIZE parts + if ($fc>PartSetup::STEP_SIZE) { + PartKeepr::getEM()->flush(); + $fc=0; + } + + + } + } +} diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/PartUnitSetup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/PartUnitSetup.php @@ -0,0 +1,36 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Setup; + +use de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\Part\PartUnit; + +class PartUnitSetup { + /** + * Holds the default unit + * @var object + */ + private static $defaultUnit; + + /** + * Sets up the default part unit + */ + public static function setupPartUnits () { + Setup::progress("Setting up default part unit `Pieces`"); + $partUnit = new PartUnit(); + $partUnit->setName(PartKeepr::i18n("Pieces")); + $partUnit->setShortName(PartKeepr::i18n("pcs")); + $partUnit->setDefault(true); + + PartUnitSetup::$defaultUnit = $partUnit; + + PartKeepr::getEM()->persist($partUnit); + } + + /** + * Returns the default part unit + * @return PartUnit The part unit + */ + public static function getDefaultPartUnit () { + return PartUnitSetup::$defaultUnit; + } +} diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/Setup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/Setup.php @@ -4,17 +4,89 @@ namespace de\RaumZeitLabor\PartKeepr\Setup; declare(encoding = 'UTF-8'); class Setup { - + /** + * Defines if the setup runs in verbose mode. + * @var boolean + */ + private static $verbose = false; + + /** + * Runs the setup. Right now, it only tests the prequisites. + */ public function run () { $this->testPrequisites(); } - + + /** + * Tests all requires prequisites for the system. + */ public function testPrequisites () { $this->testAPC(); + $this->testMemoryLimit(); } + + /** + * Tests for APC. Throws an exception if APC is missing or not active. + * @throws \Exception + */ public function testAPC () { if (!extension_loaded("apc")) { throw new \Exception(PartKeepr::i18n("The extension 'apc' is not loaded. Make sure that it is installed (see http://php.net/manual/en/apc.installation.php) and that it is enabled (set apc.enabled=1 in your php.ini).")); } } + + /** + * Tests for suitable memory_limit settings + * @todo stub + */ + public function testMemoryLimit () { + //echo ini_get("memory_limit"); + } + + /** + * Converts strange escpaes in the database to "regular" text. + * @param string $string The string to convert + * @return string The converted string + */ + public static function convertText ($string) { + $string = stripslashes($string); + $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); + $string = str_replace("&#937;", "Ω", $string); + return $string; + } + + /** + * Sets the verbose flag + * @param boolean $verbose True if verbose output is wanted, false otherwise + */ + public static function setVerbose ($verbose) { + Setup::$verbose = $verbose; + } + + /** + * Outputs a progress message. + * + * @param string $string The string to output + * @param boolean $verbose True if the string should only be printed if verbosity is turned on + */ + public static function progress ($string, $verbose = false) { + if (!$verbose || ($verbose && Setup::$verbose)) { + echo $string."\n"; + } + + } + + /** + * Loads the given YAML file. Due to an API brach between Doctrine 2.0.5 and Doctrine 2.0.6, + * we need to work it around. + * @param string $file The path of the file to load + * @return array The parsed YAML file + */ + public static function loadYAML ($file) { + if (method_exists("\Symfony\Component\Yaml\Yaml", "load")) { + return \Symfony\Component\Yaml\Yaml::load($file); + } else { + return \Symfony\Component\Yaml\Yaml::parse($file); + } + } } \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/SiPrefixSetup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/SiPrefixSetup.php @@ -0,0 +1,47 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Setup; + +use de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\SiPrefix\SiPrefix; + +class SiPrefixSetup { + /** + * Stores the migrated si prefixes + * @var array + */ + private static $siPrefixes = array(); + + /** + * Sets up the SI prefixes + */ + public static function setupSiPrefixes () { + Setup::progress("Setting up SI-Prefixes..."); + $data = Setup::loadYAML("../setup/data/siprefixes.yaml"); + + foreach ($data as $prefixName => $data) { + Setup::progress(" - Adding prefix ".$prefixName, true); + $prefix = new SiPrefix(); + $prefix->setPrefix($prefixName); + $prefix->setPower($data["power"]); + $prefix->setSymbol($data["symbol"]); + + self::$siPrefixes[] = $prefix; + + PartKeepr::getEM()->persist($prefix); + } + } + + /** + * Returns the SI prefix by symbol + * @param string $symbol The symbol to find + */ + public static function getSiPrefixBySymbol ($symbol) { + foreach (self::$siPrefixes as $prefix) { + if ($prefix->getSymbol() == $symbol) { + return $prefix; + } + } + + return false; + } +} diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/StorageLocationSetup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/StorageLocationSetup.php @@ -0,0 +1,39 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Setup; + +use de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\StorageLocation\StorageLocation; + +class StorageLocationSetup { + /** + * Holds the migrated storage locations + * @var array + */ + private static $migratedStorageLocations = array(); + + /** + * Migrates the storage locations + */ + public static function migrateStorageLocations () { + Setup::progress("Migrating storage locations"); + + $r = mysql_query("SELECT * FROM storeloc"); + + while ($store = mysql_fetch_assoc($r)) { + Setup::progress(" - Migrating storage location ".$store["name"], true); + + $oStorageLocation = new StorageLocation(); + $oStorageLocation->setName(Setup::convertText($store["name"])); + PartKeepr::getEM()->persist($oStorageLocation); + StorageLocationSetup::$migratedStorageLocations[$store["id"]] = $oStorageLocation; + } + } + + /** + * Returns the storage location by id + * @param int $id + */ + public static function getMigratedStorageLocation ($id) { + return StorageLocationSetup::$migratedStorageLocations[$id]; + } +} diff --git a/src/de/RaumZeitLabor/PartKeepr/Setup/UnitSetup.php b/src/de/RaumZeitLabor/PartKeepr/Setup/UnitSetup.php @@ -0,0 +1,44 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Setup; + +use de\RaumZeitLabor\PartKeepr\PartKeepr, + de\RaumZeitLabor\PartKeepr\Unit\Unit, + de\RaumZeitLabor\PartKeepr\Setup\SiPrefixSetup; + +class UnitSetup { + /** + * Sets up the default units + * @throws \Exception + */ + public static function setupUnits () { + Setup::progress("Setting up Units..."); + $data = Setup::loadYAML("../setup/data/units.yaml"); + + $data = \Symfony\Component\Yaml\Yaml::load("../setup/data/units.yaml"); + + $aUnits = array(); + + foreach ($data as $unitName => $data) { + Setup::progress(" - Adding unit ".$unitName, true); + $unit = new Unit(); + $unit->setName($unitName); + $unit->setSymbol($data["symbol"]); + + if (array_key_exists("prefixes", $data)) { + if (!is_array($data["prefixes"])) { + throw new \Exception($unitName." doesn't contain a prefix list, or the prefix list is not an array."); + } + + foreach ($data["prefixes"] as $prefix) { + $siPrefix = SiPrefixSetup::getSiPrefixBySymbol($prefix); + if ($siPrefix === false) { + throw new \Exception("Unable to find prefix ".$prefix); + } + $unit->getPrefixes()->add($siPrefix); + } + } + + PartKeepr::getEM()->persist($unit); + } + } +} diff --git a/testing/SetupDatabase.php b/testing/SetupDatabase.php @@ -1,36 +1,34 @@ <?php namespace de\RaumZeitLabor\PartKeepr\Tests; -use de\RaumZeitLabor\PartKeepr\FootprintCategory\FootprintCategory; - -use de\RaumZeitLabor\PartKeepr\FootprintCategory\FootprintCategoryManager; - -use de\RaumZeitLabor\PartKeepr\Footprint\FootprintAttachment; - -use de\RaumZeitLabor\PartKeepr\Footprint\FootprintImage; - -use de\RaumZeitLabor\PartKeepr\PartParameter\PartParameter; -use de\RaumZeitLabor\PartKeepr\Part\PartAttachment; declare(encoding = 'UTF-8'); -use de\RaumZeitLabor\PartKeepr\Unit\Unit; +use de\RaumZeitLabor\PartKeepr\PartKeepr; +use de\RaumZeitLabor\PartKeepr\Util\Configuration; + -use de\RaumZeitLabor\PartKeepr\SiPrefix\SiPrefix; use de\RaumZeitLabor\PartKeepr\Setup\Setup; +use de\RaumZeitLabor\PartKeepr\Setup\FootprintSetup; +use de\RaumZeitLabor\PartKeepr\Setup\PartCategorySetup; +use de\RaumZeitLabor\PartKeepr\Setup\StorageLocationSetup; +use de\RaumZeitLabor\PartKeepr\Setup\SiPrefixSetup; +use de\RaumZeitLabor\PartKeepr\Setup\UnitSetup; +use de\RaumZeitLabor\PartKeepr\Setup\ManufacturerSetup; +use de\RaumZeitLabor\PartKeepr\Setup\DistributorSetup; +use de\RaumZeitLabor\PartKeepr\Setup\PartSetup; + declare(encoding = 'UTF-8'); include("../src/de/RaumZeitLabor/PartKeepr/PartKeepr.php"); +/* Old things */ use de\RaumZeitLabor\PartKeepr\User\User; -use de\RaumZeitLabor\PartKeepr\Footprint\Footprint; -use de\RaumZeitLabor\PartKeepr\Footprint\FootprintManager; -use de\RaumZeitLabor\PartKeepr\PartKeepr; use de\RaumZeitLabor\PartKeepr\Part\PartUnit; use de\RaumZeitLabor\PartKeepr\PartCategory\PartCategory; use de\RaumZeitLabor\PartKeepr\Part\Part; +use de\RaumZeitLabor\PartKeepr\Setup\PartUnitSetup; use de\RaumZeitLabor\PartKeepr\StorageLocation\StorageLocation; -use de\RaumZeitLabor\PartKeepr\Stock\StockEntry; use de\RaumZeitLabor\PartKeepr\PartCategory\PartCategoryManager; use de\RaumZeitLabor\PartKeepr\PartCategory\PartCategoryManagerService; @@ -38,23 +36,47 @@ use de\RaumZeitLabor\PartKeepr\Manufacturer\ManufacturerICLogo; use de\RaumZeitLabor\PartKeepr\Manufacturer\Manufacturer; use de\RaumZeitLabor\PartKeepr\Distributor\Distributor; -use de\RaumZeitLabor\PartKeepr\Part\PartDistributor; -use de\RaumZeitLabor\PartKeepr\Part\PartManufacturer; + PartKeepr::initialize(); $setup = new Setup(); $setup->run(); -echo "=)))==========================================\n"; -echo "Actions performed by this script:\n"; -echo "* Drop the old database INCLUDING ALL DATA\n"; -echo "* Creates the schema\n"; -echo "* Creates a test user (username/password test)\n"; -echo "* Imports data from database partdb\n"; -echo "=)))==========================================\n"; +$ask = true; +$migration = false; + +foreach ($_SERVER["argv"] as $arg) { + switch ($arg) { + case "--yes": + $ask = false; + break; + case "--migrate": + case "--migration": + $migration = true; + break; + case "--verbose": + Setup::setVerbose(true); + break; + case "--help": + echo "Usage: SetupDatabase.php [OPTION]\n\n"; + echo "Actions performed by this script:\n"; + echo "* Drop the tables contained in the database for PartKeepr INCLUDING ALL DATA\n"; + echo "* Creates the schema\n"; + echo "* Creates an admin user\n"; + echo "* Imports data from database partdb\n\n"; + echo "Arguments:\n"; + echo " --yes Assumes 'YES' for the security prompt. USE WITH CAUTION!\n"; + echo " --migrate Also migrates the data from the old PartDB database\n"; + echo " --verbose Outputs verbose information about the setup process\n"; + echo " --help Displays this help\n\n"; + + die; + break; + } +} -if (!($_SERVER["argc"] == 2 && $_SERVER["argv"][1] == "--yes")) { +if ($ask) { echo "\n\n"; echo "If you are sure you want to do this, type YES and hit return.\n"; @@ -68,13 +90,12 @@ if (!($_SERVER["argc"] == 2 && $_SERVER["argv"][1] == "--yes")) { } - - echo "Performing actions...\n"; @mkdir("../src/Proxies"); @mkdir("../data"); @mkdir("../data/images"); +@mkdir("../data/files"); chmod("../data/images", 0777); chmod("../data/files", 0777); @@ -82,350 +103,83 @@ $tool = new \Doctrine\ORM\Tools\SchemaTool(PartKeepr::getEM()); $classes = PartKeepr::getClassMetaData(); +Setup::progress("Dropping schema `".Configuration::getOption("partkeepr.database.dbname")."`"); $tool->dropDatabase($classes); +Setup::progress("Creating schema `".Configuration::getOption("partkeepr.database.dbname")."`"); $tool->createSchema($classes); +Setup::progress("Setting up initial admin user"); /* Create initial test user */ $user = new User(); $user->setUsername("admin"); $user->setPassword("admin"); $user->setAdmin(true); PartKeepr::getEM()->persist($user); +PartKeepr::getEM()->flush(); + +PartUnitSetup::setupPartUnits(); +PartKeepr::getEM()->flush(); /* Create footprints */ $newFootprints = array(); $newCategories = array(); -mysql_connect("localhost", "partdb", "partdb"); -mysql_select_db("partdb"); - -$partUnit = new PartUnit(); -$partUnit->setName(PartKeepr::i18n("Pieces")); -$partUnit->setShortName(PartKeepr::i18n("pcs")); -$partUnit->setDefault(true); - -PartKeepr::getEM()->persist($partUnit); -PartKeepr::getEM()->flush(); - -echo "Creating footprints from SetupData/footprints.php\n"; - -/* Import pre-defined footprints */ -$data = \Symfony\Component\Yaml\Yaml::load("../setup/data/footprints/footprints.yaml"); - -$footprintCategoryManager = FootprintCategoryManager::getInstance(); -$footprintCategoryManager->ensureRootExists(); - -function addFootprintPath (Array $path, $node) { - if (count($path) == 0) { return $node; } - $name = array_shift($path); - - $childNode = null; - - foreach ($node->getChildren() as $child) { - if ($child->getNode()->getName() == $name) { - $childNode = $child; - } - } - - if ($childNode === null) { - $category = new FootprintCategory(); - $category->setParent($node->getNode()->getId()); - $category->setName($name); - $childNode = FootprintCategoryManager::getInstance()->addCategory($category); - } - - return addFootprintPath($path, $childNode); - -} - -foreach ($data as $footprintName => $footprintData) { - $footprint = new Footprint(); - $footprint->setName($footprintName); - - if (array_key_exists("description", $footprintData)) { - $footprint->setDescription($footprintData["description"]); - } - - if (array_key_exists("category", $footprintData)) { - $footprintCategory = addFootprintPath(explode("/", $footprintData["category"]), $footprintCategoryManager->getRootNode()); - $footprint->setCategory($footprintCategory->getNode()); - } - - if (array_key_exists("image", $footprintData)) { - $footprintImage = new FootprintImage(); - $footprintImage->setFootprint($footprint); - $footprintImage->replace("../setup/data/footprints/" . $footprintData["image"]); - - $footprint->setImage($footprintImage); - } - - if (array_key_exists("attachments", $footprintData) && is_array($footprintData["attachments"])) { - foreach ($footprintData["attachments"] as $attachment) { - if (!is_array($attachment)) { - echo $footprintName ." has a string attachment\n"; - } - if (array_key_exists("url", $attachment)) { - try { - $footprintAttachment = new FootprintAttachment(); - $footprintAttachment->setFootprint($footprint); - $footprintAttachment->replaceFromURL($attachment["url"]); - if (array_key_exists("description", $attachment)) { - $footprintAttachment->setDescription($attachment["description"]); - } - - $footprint->getAttachments()->add($footprintAttachment); - } catch (\Exception $e) { - echo "error with url ".$attachment["url"]."\n"; - } - - - - } - - } - } - - PartKeepr::getEM()->persist($footprint); +if ($migration) { + mysql_connect("localhost", "partdb", "partdb"); + mysql_select_db("partdb"); } +FootprintSetup::setupRootNode(); +FootprintSetup::importFootprintData("../setup/data/footprints/footprints.yaml"); PartKeepr::getEM()->flush(); -$r = mysql_query("SELECT * FROM footprints"); - -while ($sFootprint = mysql_fetch_assoc($r)) { - $footprint = new Footprint(); - $footprint->setName(convertText($sFootprint["name"])); - - echo " Adding footprint ".$sFootprint["name"]."\r"; - - $footprintCategory = addFootprintPath(explode("/", "Imported Footprints"), $footprintCategoryManager->getRootNode()); - $footprint->setCategory($footprintCategory->getNode()); - - PartKeepr::getEM()->persist($footprint); - - $newFootprints[$sFootprint["id"]] = $footprint; +if ($migration) { + FootprintSetup::migrateFootprints(); + PartKeepr::getEM()->flush(); } -echo "\n\Creating categories from existing data\n"; - -$categoryManager = PartCategoryManager::getInstance(); -$categoryManager->ensureRootExists(); - -/* Pass 1: Create numeric nodes */ - -$r = mysql_query("SELECT * FROM categories"); - -$categories = array(); +PartCategorySetup::setupRootCategory(); -while ($cat = mysql_fetch_assoc($r)) { - $categories[] = $cat; -} - -addCategoryRecursive($categories, 0, $categoryManager->getRootNode()); - -function addCategoryRecursive ($aCategories, $currentId, $parent) { - global $newCategories; - - foreach ($aCategories as $aCategory) { - if ($aCategory["parentnode"] == $currentId) { - echo "Adding ".sprintf("%40s", $aCategory["name"])."\r"; - $oCategory = new PartCategory(); - $oCategory->setName(convertText($aCategory["name"])); - $oCategory->setDescription(""); - $oCategory->setParent($parent->getId()); - - $category = PartCategoryManager::getInstance()->addCategory($oCategory); - - addCategoryRecursive($aCategories, $aCategory["id"], $category); - - $newCategories[$aCategory["id"]] = $oCategory; - } - } - +if ($migration) { + PartCategorySetup::migrateCategories(); } -echo "\n"; -$r = mysql_query("SELECT * FROM storeloc"); - -while ($store = mysql_fetch_assoc($r)) { - $oStorageLocation = new StorageLocation(); - $oStorageLocation->setName(convertText($store["name"])); - PartKeepr::getEM()->persist($oStorageLocation); - echo "Migrating storage location ".sprintf("%-40s", $store["name"])."\r"; - $newStorageLocations[$store["id"]] = $oStorageLocation; +if ($migration) { + StorageLocationSetup::migrateStorageLocations(); + PartKeepr::getEM()->flush(); } -echo "\n"; /* Add Si-Prefixes */ -$data = \Symfony\Component\Yaml\Yaml::load("../setup/data/siprefixes.yaml"); - -$aSiPrefixes = array(); - -foreach ($data as $prefixName => $data) { - $prefix = new SiPrefix(); - $prefix->setPrefix($prefixName); - $prefix->setPower($data["power"]); - $prefix->setSymbol($data["symbol"]); - - $aSiPrefixes[] = $prefix; - PartKeepr::getEM()->persist($prefix); - -} - +SiPrefixSetup::setupSiPrefixes(); PartKeepr::getEM()->flush(); -/* Add units */ -$data = \Symfony\Component\Yaml\Yaml::load("../setup/data/units.yaml"); - -$aUnits = array(); -foreach ($data as $unitName => $data) { - $unit = new Unit(); - $unit->setName($unitName); - $unit->setSymbol($data["symbol"]); - - if (array_key_exists("prefixes", $data)) { - if (!is_array($data["prefixes"])) { - echo "Obacht ".$unitName." ist falsch\n"; - } - foreach ($data["prefixes"] as $prefix) { - foreach ($aSiPrefixes as $siPrefix) { - if ($siPrefix->getSymbol() == $prefix) { - $unit->getPrefixes()->add($siPrefix); - } - } - } - } - - PartKeepr::getEM()->persist($unit); - $aUnits[] = $unit; - -} +/* Add units */ +UnitSetup::setupUnits(); PartKeepr::getEM()->flush(); - -/* Add manufacturers and IC logos */ -$data = \Symfony\Component\Yaml\Yaml::load("../setup/data/manufacturers/manufacturers.yaml"); - -$aManufacturers = array(); -$aDistributors = array(); - -foreach ($data as $mfgname => $logos) { - $manufacturer = new Manufacturer(); - $manufacturer->setName($mfgname); - - PartKeepr::getEM()->persist($manufacturer); - $aManufacturers[] = $manufacturer; - - foreach ($logos as $logo) { - $mfglogo = new ManufacturerICLogo(); - $mfglogo->setManufacturer($manufacturer); - $mfglogo->replace("../setup/data/manufacturers/images/".$logo); - $mfglogo->setOriginalFilename($logo); - - PartKeepr::getEM()->persist($mfglogo); - } -} - +/* Add Manufacturers */ +ManufacturerSetup::setupManufacturers("../setup/data/manufacturers/manufacturers.yaml"); PartKeepr::getEM()->flush(); -$r = mysql_query("SELECT * FROM suppliers"); -while ($supplier = mysql_fetch_assoc($r)) { - $distributor = new Distributor(); - $distributor->setName($supplier["name"]); - - PartKeepr::getEM()->persist($distributor); - $aDistributors[$supplier["id"]] = $distributor; +if ($migration) { + DistributorSetup::migrateDistributors(); + PartKeepr::getEM()->flush(); } -PartKeepr::getEM()->flush(); - -$r = mysql_query("SELECT * FROM parts"); - -$aRandomUnitNames = array("Spannung", "Strom", "Leitfähigkeit", "Viskosität", "Nessis"); - -$fc=0; - -while ($part = mysql_fetch_assoc($r)) { - $oPart = new Part(); - $oPart->setName(convertText($part["name"])); - $oPart->setComment(convertText($part["comment"])); - $oPart->setFootprint($newFootprints[$part["id_footprint"]]); - $oPart->setCategory($newCategories[$part["id_category"]]); - $oPart->setStorageLocation($newStorageLocations[$part["id_storeloc"]]); - $oPart->setMinStockLevel($part["mininstock"]); - $oPart->setPartUnit($partUnit); - - $partDistributor = new PartDistributor(); - $partDistributor->setPart($oPart); - $partDistributor->setDistributor($aDistributors[$part["id_supplier"]]); - $partDistributor->setOrderNumber($part["supplierpartnr"]); - $oPart->getDistributors()->add($partDistributor); - - //echo "Migrating part ".sprintf("%-40s", $part["name"])."\r"; - - /* Add existing datasheets */ - $datasheetQuery = "SELECT datasheeturl FROM datasheets WHERE part_id = ".$part["id"]; - $r3 = mysql_query($datasheetQuery); - while ($res = mysql_fetch_assoc($r3)) { - try { - $attachment = new PartAttachment(); - $attachment->setPart($oPart); - $attachment->replaceFromURL($res["datasheeturl"]); - $attachment->setDescription(PartKeepr::i18n("Datasheet")); - $oPart->getAttachments()->add($attachment); - } catch (\Exception $e) { - echo "error with url ".$res["datasheeturl"]."\n"; - } - } - - PartKeepr::getEM()->persist($oPart); - - $oStock = new StockEntry($oPart, $part["instock"]); - - $priceQuery = "SELECT AVG(preis) AS preis FROM preise WHERE part_id = ".$part["id"]; - - $r2 = mysql_query($priceQuery); - $res = mysql_fetch_assoc($r2); - - if ($res) { - if ($res["preis"] !== null) { - $oStock->setPrice(floatval($res["preis"])); - } - } - - PartKeepr::getEM()->persist($oStock); - - $fc++; - - if ($fc>100) { - PartKeepr::getEM()->flush(); - $fc=0; - } - - +if ($migration) { + PartSetup::migrateParts(); + PartKeepr::getEM()->flush(); } -PartKeepr::getEM()->flush(); - - - - - - echo "All done. Use the user 'admin' with password 'admin' to login\n"; apc_clear_cache(); apc_clear_cache("user"); -function convertText ($string) { - $string = stripslashes($string); - $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); - - return $string; -} + ?>