From 94d00beeab1f520159ddee0c81f3c57a8accb394 Mon Sep 17 00:00:00 2001 From: Silvio Date: Thu, 29 Jul 2010 18:22:27 -0300 Subject: Major organization rewrite --- lib/sfIsisImporterBase.class.php | 210 --------------------- lib/sfIsisImporterManager.class.php | 365 +++++++++--------------------------- 2 files changed, 92 insertions(+), 483 deletions(-) delete mode 100644 lib/sfIsisImporterBase.class.php diff --git a/lib/sfIsisImporterBase.class.php b/lib/sfIsisImporterBase.class.php deleted file mode 100644 index 62e7ead..0000000 --- a/lib/sfIsisImporterBase.class.php +++ /dev/null @@ -1,210 +0,0 @@ -loglevel = $loglevel; - } - - /** - * Create an ISIS configuration for a given database. - * - * @param string $database Database name - * @return array Basic configuration - */ - public function config($database) - { - // Default cinisis config file. - $config = $this->isis->file(); - - if (file_exists($config)) - { - $this->log("Using Cinisis config file $config."); - $config = sfYaml::load($config); - } - else { - // Default configuration. - $this->log("Cinisis config file not found, building configuration."); - $config = array( - 'implementation' => 'BiblioIsis', - ); - } - - $config['database'] = $database; - return $config; - } - - /** - * List available ISIS databases. - * - * @return array Available databases - */ - public function databases() - { - foreach (glob(sfConfig::get('sf_lib_dir') .'/cinisis/schemas/*.yaml') as $file) - { - $databases[] = basename($file, '.yaml'); - } - - return $databases; - } - - /** - * Log dispatcher. - * - * @param string $message Log message - * @param string $level Log level - */ - public function log($message, $level = 'info') - { - // Available log levels ordered by verbosity. - $levels = array_flip(array('fatal', 'info', 'warn', 'error', 'debug')); - - // Log level checking. - if (array_search($level, $levels) === FALSE) - { - $this->log("Invalid log level $level.", 'error'); - return; - } - elseif ($levels[$level] > $levels[$this->loglevel]) - { - return; - } - - // Dispatch. - if ($this->section == 'action') - { - $this->caller->logMessage($message, $level); - } - else - { - $this->caller->logSection('isisImporter', "[$level] $message"); - } - } - - /** - * Progress notifier. - * - * @param int $n Row number - */ - public function progress($n) - { - $this->processed = $n; - - if ($this->section == 'action') - { - $this->caller->output .= "Saved item $n\n"; - } - else - { - // Progress bar is just shown if loglevel is 'fatal'. - if ($this->loglevel == 'fatal') - { - $this->caller->progressBar($n, $this->entries); - } - } - } - - /** - * Guess a method name from a type. - * - * @param string $type Mapping type - * @return string Method name - */ - static function methodName($type) - { - return 'import'. ucfirst($type); - } - - /** - * Get the model foreign table id. - * - * @param object $model Model - * @return string Model table id - */ - static function getModelId($model) { - return sfInflector::underscore(get_class($model)) .'_id'; - } - - /** - * Get the relation foreign table id. - * - * @param string $model Relation name - * @return string Relation table id - */ - static function getRelationId($relation) { - return sfInflector::underscore($relation) .'_id'; - } - - /** - * Get the model and relation tablename. - * - * @param object $model Model - * @param string $model Relation name - * @return string Relation table name - */ - static function getModelRelation($model, $relation) - { - return sfInflector::camelize(self::getModelName($model)) . sfInflector::camelize($relation); - } - - /** - * Get the entity name from a subfield. - * - * @param string $subfield Subfield name - * @return string Genre name - */ - static function entityName($subfield) - { - return ucfirst($subfield); - } - - /** - * Get the model name. - * - * @param object $model Model - * @return string Model name - */ - static function getModelName($model) - { - return get_class($model); - } - - /** - * After import procedure. - */ - public function afterImport() { - // Output ISIS log messages. - if (is_array($this->isis->db->log)) - { - foreach ($this->isis->db->log as $log) - { - $this->log("[isis] $log"); - } - } - - $this->log("Finished mass import procedure."); - $this->log("Total entries processed: $this->processed."); - } -} diff --git a/lib/sfIsisImporterManager.class.php b/lib/sfIsisImporterManager.class.php index 50f0574..15bf5de 100644 --- a/lib/sfIsisImporterManager.class.php +++ b/lib/sfIsisImporterManager.class.php @@ -6,347 +6,166 @@ * * Importing can be done either on actions or tasks. */ -class sfIsisImporterManager extends sfIsisImporterBase +class sfIsisImporterManager extends IsisConnector { /** - * Execute a mass import of ISIS database entries. This function reads - * each database entry and dispatch each field read to a custom - * dispatch function. - * - * @param object $caller Caller object - * @param string $section Caller section (whether an action or task) - * @param int $entries Number of entries to import (defaults to all) + * @var string $loglevel Log level. */ - public function massImport($caller, $section, $entries = NULL) - { - // We hold arguments for logging purposes. - $this->caller = $caller; - $this->section = $section; - - foreach ($this->databases() as $database) - { - // Open database. - $this->log('Starting mass import procedure for config '. $database .'.'); - $this->open($this->config($database)); - - // Determine base model and max entries. - $base_model = $this->format['db']['base_model']; - $this->entries = ($entries != NULL && $entries <= $this->entries) ? $entries : $this->entries; - - if ($base_model) { - for ($entry = 1; $entry <= $this->entries; $entry++) - { - $this->addEntry($base_model, $entry); - $this->progress($entry); - } - } - - $this->afterImport(); - } - } + var $loglevel = 'info'; /** - * Add a single entry from the database. - * - * @param string $base_model Model to use - * @param int $entry Entry number + * @var int $processed Number of processed entries. */ - public function addEntry($base_model, $entry) - { - // Get data and setup the model. - $this->read($entry); - $model = $this->newModel($base_model, $entry); - - if ($model) - { - $this->log("Importing $base_model $entry..."); - - // Dispatch to custom import procedures. - foreach (new IsisMethodIterator($this) as $method => $field) - { - $this->{$method}($model, $field); - } - - $model->save(); - } - else { - $this->log("Skipping existing entry $entry for $base_model."); - } - } + var $processed = 0; /** - * Create a new model just if doesn't exist for a given entry. - * - * @param string $base_model Model to use - * @param int $entry Entry number - * @return mixed New model with assigned id or false + * Constructor. */ - public function newModel($base_model, $entry) + public function __construct($loglevel = 'info') { - $model = new $base_model(); - $id = $this->getBaseModelId($model); - - if ($id && !call_user_func(array($base_model, 'getById'), $id)) - { - $this->setBaseModelId($model); - return $model; - } + parent::__construct(); - return false; + // Get a logger instance. + $this->logger = sfIsisImporterLog::getInstance($loglevel); } /** - * Set the primary key for the model by getting it or just saving it. + * Log dispatcher. * - * @param object $model Base model + * @param string $message Log message + * @param string $level Log level */ - public function setBaseModelId(&$model) + public function log($message, $level = 'info') { - $model->id = $this->getBaseModelId($model); - $model->save(); + $this->logger->log($message, $level); } /** - * Get the primary key for the base model. + * Progress notifier dispatcher. * - * @param object $model Base model - * @return int Base model id + * @param int $n Row number */ - public function getBaseModelId(&$model) + public function progress($n) { - $method = get . $this->getModelName($model) .'PrimaryKey'; - if (method_exists($this, $method)) - { - return $this->{$method}($model); - } + $this->processed = $this->logger->progress($n); } /** - * Import a single field into a model. + * Create an ISIS configuration for a given database. * - * @param object $model Model - * @param array $field Field data from ISIS database schema - * @param int $row Row number + * @param string $database Database name + * @return array Basic configuration */ - public function addField(&$model, $field, $row = 0) { - $value = $this->filterBrackets($this->getMainItem($field, $row)); - - if ($value != null) - { - $map = $this->getMap($field); - $model->{$map}($value); - } - } - - /** - * Import a single subfield into a model. - * - * @param object $model Model - * @param array $field Field data from ISIS database schema - * @param string $subfield Subfield name - * @param int $row Row number - */ - public function addSubfield(&$model, $field, $subfield, $row = 0) { - $value = $this->filterBrackets($this->getSubfield($field, $subfield, $row)); + public function config($database) + { + // Default cinisis config file. + $config = $this->isis->file(); - if ($value != null) + if (file_exists($config)) { - $map = $this->getMap($field, $subfield); - $model->{$map}($value); + $this->log("Using Cinisis config file $config."); + $config = sfYaml::load($config); } - } - - /** - * Import single values into the model. - * - * Currently undefined mappings for a field/subfield - * are not saved as we would need to make sure a corresponding - * field exists in the model. This prevents the map - * configurations like $map = array('type' => 'value'); to work. - * - * As we are importing single values, here we don't care with - * row numbers as we assume that just the first row should be - * imported. - * - * @param object $model Model object - * @param array $field Field data - */ - public function importValues(&$model, array $field) - { - if ($this->fieldHasMap($field)) - { - $this->addField($model, $field); + else { + // Default configuration. + $this->log("Cinisis config file not found, building configuration."); + $config = array( + 'implementation' => 'BiblioIsis', + ); } - foreach ($this->getSubfieldList($field) as $subfield) - { - if ($this->subfieldHasMap($field, $subfield)) - { - $this->addSubfield($model, $field, $subfield); - } - } + $config['database'] = $database; + return $config; } /** - * Add a new entity into the database if needed, returning - * the corresponding object. + * List available ISIS databases. * - * @param string $name Genre name - * @return Genre Genre data + * @return array Available databases */ - public function addEntity($entity, $name) + public function databases() { - $name = $this->entityName($name); - $data = Doctrine_Core::getTable($entity)->findOneByName($name); - - if (!$data) + foreach (glob(sfConfig::get('sf_lib_dir') ."/cinisis/schemas/*.yaml") as $file) { - $this->log("Adding new $entity $name."); - $data = new $entity(); - $data->name = $name; - $data->save(); + $databases[] = basename($file, '.yaml'); } - return $data; + return $databases; } /** - * Import one to one data. - * - * @param object $model Model - * @param array $field Field data from ISIS database schema - * @param string $relation Relation name + * After import procedure. */ - public function addOneToOne(&$model, array $field, $relation) - { - foreach (new IsisRowIterator($this, $field) as $row) + public function afterImport() { + // Output ISIS log messages. + if (is_array($this->isis->db->log)) { - $data = new $relation(); - - foreach ($this->getSubfieldList($field) as $subfield) + foreach ($this->isis->db->log as $log) { - $this->addSubfield($data, $field, $subfield, $row); + $this->log("[isis] $log"); } - - $data->save(); - $key = sfInflector::underscore($relation) .'_id'; - $model->$key = $relation->id; } + + $this->log("Finished mass import procedure."); + $this->log("Total entries processed: $this->processed."); } /** - * Import many to many data. + * Execute a mass import of ISIS database entries. This function reads + * each database entry and dispatch each field read to a custom + * dispatch function. * - * @param object $model Model - * @param array $values Values to be added - * @param string $relation Relation name + * @param object $caller Caller object + * @param string $section Caller section (whether an action or task) + * @param int $entries Number of entries to import (defaults to all) */ - public function addManyToMany(&$model, array $values, $relation) { - $method = 'add'. $relation; + public function massImport($caller, $section, $entries = NULL) + { + // We hold arguments for logging purposes. + $this->caller = $caller; + $this->section = $section; + $this->logger->caller = $caller; + $this->logger->section = $section; - foreach ($values as $value) + foreach ($this->databases() as $database) { - // Populate related data. - if (is_callable(array($this, $method))) - { - $data = $this->{$method}($value); - } - else - { - $data = $this->addEntity($relation, $value); - } + // Open database. + $importer = $this->newImporter($database); - // Get model and relation names and id fields. - $model_id = $this->getModelId($model); - $relation_id = $this->getRelationId($relation); - $model_relation = $this->getModelRelation($model, $relation); + if ($importer) + { + // Determine base model and max entries. + $this->log('Starting mass import procedure for config '. $database .'.'); + $base_model = $importer->format['db']['base_model']; + $this->entries = ($entries != NULL && $entries <= $importer->entries) ? $entries : $importer->entries; + + if ($base_model) { + for ($entry = 1; $entry <= $this->entries; $entry++) + { + $importer->addEntry($base_model, $entry); + $this->progress($entry); + } + } - // Make the relation. - $model_data = new $model_relation(); - $model_data->{$model_id} = $model->id; - $model_data->{$relation_id} = $data->id; - $model_data->save(); + $this->afterImport(); + } } } /** - * Import one to one data. - * - * @param object $model Model - * @param string $relation Relation name - * @return object Relation model object - */ - public function addOneToMany(&$model, $relation) - { - $model_id = $this->getModelId($model); - $data = new $relation(); - $data->{$model_id} = $model->id; - $data->save(); - return $data; - } - - /** - * Add simple entities data into the model. - * - * @param object $model Model - * @param array $field Field data from ISIS database schema - */ - public function addOneToManyEntities(&$model, array $field, $entity, $key = 'name') - { - foreach (new IsisMainItemIterator($this, $field) as $row => $value) - { - $this->log("Entity: $entity; Value: $value", 'debug'); - $data = $this->addOneToMany($model, $entity); - $data->{$key} = $value; - $data->save(); - } - } - - /** - * Add field values in a many-to-many relation. + * Setup a new importer. * - * @param object $model Model - * @param array $field Field data from ISIS database schema - * @param string $relation Relation name + * @param string $database Database name + * @return mixed Importer object or false */ - public function addManyToManyField(&$model, array $field, $relation) + public function newImporter($database) { - foreach (new IsisMainItemIterator($this, $field) as $value) + $class = 'IsisImporter'. ucfirst($database); + if (class_exists($class)) { - $this->addManyToMany($model, $this->explodeBrackets($value), $relation); - } - } - - /** - * Add an element into the database if needed, returning - * the resulting object. - * - * @param string $entity Entity name - * @param string $value Entity value - * @return object Entity data - */ - public function newOrExisting($entity, $value) - { - // Check for a null value. - if ($value == null) - { - $this->log("Null element value for $entity.", 'debug'); - return; + return new $class($this->config($database)); } - // Get name. - $name = $this->parseName($value); - - // Get existing element. - $element = call_user_func(array($entity, 'getByName'), $name); - - // Create new element if needed. - if (!$element) - { - $this->log("Adding new $entity $value."); - $element = call_user_func(array($entity, 'addByName'), $name); - } - - return $element; + return false; } } -- cgit v1.2.3