aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio <s1lv10@uol.com.br>2010-07-30 10:51:29 -0300
committerSilvio <s1lv10@uol.com.br>2010-07-30 10:51:29 -0300
commite2e83df93a77ff7d7597384a26d17a0aef2eb232 (patch)
treecbb95816a65cdf0dce9b7f76654004212a2da8fb
parent94d00beeab1f520159ddee0c81f3c57a8accb394 (diff)
downloadsf_isis_importer_plugin-e2e83df93a77ff7d7597384a26d17a0aef2eb232.tar.gz
sf_isis_importer_plugin-e2e83df93a77ff7d7597384a26d17a0aef2eb232.tar.bz2
Adding missing class files
-rw-r--r--lib/sfIsisImporter.class.php400
-rw-r--r--lib/sfIsisImporterLog.class.php109
-rw-r--r--lib/sfIsisImporterManager.class.php4
-rw-r--r--lib/sfIsisImporterStats.class.php78
4 files changed, 588 insertions, 3 deletions
diff --git a/lib/sfIsisImporter.class.php b/lib/sfIsisImporter.class.php
new file mode 100644
index 0000000..6755477
--- /dev/null
+++ b/lib/sfIsisImporter.class.php
@@ -0,0 +1,400 @@
+<?php
+
+/**
+ * IsisImporter: provides ISIS import methods for importing data into
+ * a Symfony project.
+ */
+class sfIsisImporter extends IsisConnector
+{
+ /**
+ * Log dispatcher.
+ *
+ * @param string $message Log message
+ * @param string $level Log level
+ */
+ public function log($message, $level = 'info')
+ {
+ $this->logger->log($message, $level);
+ }
+
+ /**
+ * Constructor.
+ */
+ public function __construct($config = null) {
+ parent::__construct($config);
+ $this->stats = sfIsisImporterStats::getInstance();
+
+ // Get a logger instance.
+ $this->logger = sfIsisImporterLog::getInstance();
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * Add a single entry from the database.
+ *
+ * @param string $base_model Model to use
+ * @param int $entry Entry number
+ */
+ 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.");
+ }
+ }
+
+ /**
+ * 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
+ */
+ public function newModel($base_model, $entry)
+ {
+ $model = new $base_model();
+ $id = $this->getBaseModelId($model);
+
+ if ($id && !call_user_func(array($base_model, 'getById'), $id))
+ {
+ $this->setBaseModelId($model);
+ return $model;
+ }
+
+ return false;
+ }
+
+ /**
+ * Set the primary key for the model by getting it or just saving it.
+ *
+ * @param object $model Base model
+ */
+ public function setBaseModelId(&$model)
+ {
+ $model->id = $this->getBaseModelId($model);
+ $model->save();
+ }
+
+ /**
+ * Get the primary key for the base model.
+ *
+ * @param object $model Base model
+ * @return int Base model id
+ */
+ public function getBaseModelId(&$model)
+ {
+ $method = get . $this->getModelName($model) .'PrimaryKey';
+ if (method_exists($this, $method))
+ {
+ return $this->{$method}($model);
+ }
+ }
+
+ /**
+ * Import a single field into a model.
+ *
+ * @param object $model Model
+ * @param array $field Field data from ISIS database schema
+ * @param int $row Row number
+ */
+ 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));
+
+ if ($value != null)
+ {
+ $map = $this->getMap($field, $subfield);
+ $model->{$map}($value);
+ }
+ }
+
+ /**
+ * 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);
+ }
+
+ foreach ($this->getSubfieldList($field) as $subfield)
+ {
+ if ($this->subfieldHasMap($field, $subfield))
+ {
+ $this->addSubfield($model, $field, $subfield);
+ }
+ }
+ }
+
+ /**
+ * Add a new entity into the database if needed, returning
+ * the corresponding object.
+ *
+ * @param string $name Genre name
+ * @return Genre Genre data
+ */
+ public function addEntity($entity, $name)
+ {
+ $name = $this->entityName($name);
+ $data = Doctrine_Core::getTable($entity)->findOneByName($name);
+
+ if (!$data)
+ {
+ $this->log("Adding new $entity $name.");
+ $data = new $entity();
+ $data->name = $name;
+ $data->save();
+ }
+
+ return $data;
+ }
+
+ /**
+ * Import one to one data.
+ *
+ * @param object $model Model
+ * @param array $field Field data from ISIS database schema
+ * @param string $relation Relation name
+ */
+ public function addOneToOne(&$model, array $field, $relation)
+ {
+ foreach (new IsisRowIterator($this, $field) as $row)
+ {
+ $data = new $relation();
+
+ foreach ($this->getSubfieldList($field) as $subfield)
+ {
+ $this->addSubfield($data, $field, $subfield, $row);
+ }
+
+ $data->save();
+ $key = sfInflector::underscore($relation) .'_id';
+ $model->$key = $relation->id;
+ }
+ }
+
+ /**
+ * Import many to many data.
+ *
+ * @param object $model Model
+ * @param array $values Values to be added
+ * @param string $relation Relation name
+ */
+ public function addManyToMany(&$model, array $values, $relation) {
+ $method = 'add'. $relation;
+
+ foreach ($values as $value)
+ {
+ // Populate related data.
+ if (is_callable(array($this, $method)))
+ {
+ $data = $this->{$method}($value);
+ }
+ else
+ {
+ $data = $this->addEntity($relation, $value);
+ }
+
+ // Get model and relation names and id fields.
+ $model_id = $this->getModelId($model);
+ $relation_id = $this->getRelationId($relation);
+ $model_relation = $this->getModelRelation($model, $relation);
+
+ // Make the relation.
+ $model_data = new $model_relation();
+ $model_data->{$model_id} = $model->id;
+ $model_data->{$relation_id} = $data->id;
+ $model_data->save();
+ }
+ }
+
+ /**
+ * 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.
+ *
+ * @param object $model Model
+ * @param array $field Field data from ISIS database schema
+ * @param string $relation Relation name
+ */
+ public function addManyToManyField(&$model, array $field, $relation)
+ {
+ foreach (new IsisMainItemIterator($this, $field) as $value)
+ {
+ $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;
+ }
+
+ // 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;
+ }
+}
diff --git a/lib/sfIsisImporterLog.class.php b/lib/sfIsisImporterLog.class.php
new file mode 100644
index 0000000..34594a6
--- /dev/null
+++ b/lib/sfIsisImporterLog.class.php
@@ -0,0 +1,109 @@
+<?php
+
+/**
+ * Logger.
+ *
+ * @todo $this->caller, $this->section
+ */
+class sfIsisImporterLog
+{
+ /**
+ * @var object $instance The singleton instance.
+ */
+ private static $instance = null;
+
+ /**
+ * @var object $instance Default loglevel.
+ */
+ public static $loglevel;
+
+ /**
+ * @var int $processed Number of processed entries.
+ */
+ var $processed = 0;
+
+ /**
+ * Get the singleton instance.
+ *
+ * @param string $loglevel Log level to use
+ */
+ public static function getInstance($loglevel = null)
+ {
+ if(self::$instance == null) {
+ self::$instance = new self;
+ }
+
+ if ($loglevel != null)
+ {
+ self::$loglevel = $loglevel;
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Constructor.
+ */
+ private function __construct($loglevel = 'info')
+ {
+ $this->loglevel = $loglevel;
+ }
+
+ /**
+ * 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);
+ }
+ }
+
+ return $this->processed;
+ }
+}
diff --git a/lib/sfIsisImporterManager.class.php b/lib/sfIsisImporterManager.class.php
index 15bf5de..7d5d9b3 100644
--- a/lib/sfIsisImporterManager.class.php
+++ b/lib/sfIsisImporterManager.class.php
@@ -1,9 +1,7 @@
<?php
/**
- * IsisImporter: provides ISIS import methods for importing data into
- * a Symfony project.
- *
+ * Manager class for importing ISIS data into a Symfony project.
* Importing can be done either on actions or tasks.
*/
class sfIsisImporterManager extends IsisConnector
diff --git a/lib/sfIsisImporterStats.class.php b/lib/sfIsisImporterStats.class.php
new file mode 100644
index 0000000..d7dae34
--- /dev/null
+++ b/lib/sfIsisImporterStats.class.php
@@ -0,0 +1,78 @@
+<?php
+
+/**
+ * Statistics gathering for importing procedures.
+ */
+class sfIsisImporterStats
+{
+ /**
+ * @var $instance Singleton instance
+ */
+ private static $instance = null;
+
+ /**
+ * Get the singleton instance.
+ */
+ public static function getInstance()
+ {
+ if(self::$instance == null) {
+ self::$instance = new self;
+ }
+
+ return self::$instance;
+ }
+
+ /**
+ * Constructor.
+ */
+ private function __construct()
+ {
+ $this->stats = array();
+ }
+
+ /**
+ * Simple accumulator.
+ *
+ * @param string $section Section name
+ */
+ public function increase($section)
+ {
+ $this->stats[$section]++;
+ }
+
+ /**
+ * Compute soundex.
+ *
+ * @param string $name Name
+ */
+ public function soundex($name) {
+ $this->stats['sounded'][soundex($name)] = $name;
+ }
+
+ /**
+ * Compute metaphone.
+ *
+ * @param string $name Name
+ */
+ public function metaphone($name) {
+ $this->stats['sounded'][metaphone($name)] = $name;
+ }
+
+ /**
+ * Compute similar words indexes.
+ *
+ * @param string $name Name
+ */
+ public function similar($name) {
+ $this->soundex($name);
+ $this->metaphone($name);
+ }
+
+ /**
+ * Display statistics.
+ *
+ * @todo Write and test
+ */
+ public function display() {
+ }
+}