Author: david
Date: Sat Feb 26 15:10:02 2011
New Revision: 9005
Log:
Add task to load digital objects from csv file. Not working! :(
Added:
trunk/lib/task/digitalObjectLoadTask.class.php
- copied, changed from r9000,
trunk/lib/task/propelBuildNestedSetTask.class.php
Copied and modified: trunk/lib/task/digitalObjectLoadTask.class.php (from
r9000, trunk/lib/task/propelBuildNestedSetTask.class.php)
==============================================================================
--- trunk/lib/task/propelBuildNestedSetTask.class.php Fri Feb 25 16:44:03
2011 (r9000, copy source)
+++ trunk/lib/task/digitalObjectLoadTask.class.php Sat Feb 26 15:10:02
2011 (r9005)
@@ -18,14 +18,14 @@
*/
/**
- * Regenerate nested set column values
+ * Load a csv list of digital objects
*
* @package symfony
* @subpackage task
* @author David Juhasz <[email protected]>
* @version SVN: $Id$
*/
-class propelBuildNestedSetTask extends sfBaseTask
+class digitalObjectLoadTask extends sfBaseTask
{
/**
* @see sfTask
@@ -33,6 +33,7 @@
protected function configure()
{
$this->addArguments(array(
+ new sfCommandArgument('filename', sfCommandArgument::REQUIRED, 'The
input file (csv format).')
));
$this->addOptions(array(
@@ -41,12 +42,12 @@
new sfCommandOption('connection', null,
sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
));
- $this->namespace = 'propel';
- $this->name = 'build-nested-set';
- $this->briefDescription = 'Build all nested set values.';
+ $this->namespace = 'digitalobject';
+ $this->name = 'load';
+ $this->briefDescription = 'Load a csv list of digital objects';
$this->detailedDescription = <<<EOF
-Build all nested set values.
+Load a csv list of digital objects
EOF;
}
@@ -57,119 +58,44 @@
{
$databaseManager = new sfDatabaseManager($this->configuration);
$conn = $databaseManager->getDatabase('propel')->getConnection();
-
- $tables = array(
- 'information_object' => 'QubitInformationObject',
- 'actor' => 'QubitActor'
- );
-
- foreach ($tables as $table => $classname)
+
+ if (false === $fh = fopen($arguments['filename'], 'rb'))
{
- $this->logSection('propel', 'Build nested set for '.$table.'...');
-
- $sql = 'SELECT id, parent_id';
- $sql .= ' FROM '.constant($classname.'::TABLE_NAME');
- $sql .= ' ORDER BY parent_id ASC, lft ASC';
-
- $tree = $children = array();
-
- foreach ($conn->query($sql, PDO::FETCH_ASSOC) as $item)
- {
- // Add root node to tree
- if (constant($classname.'::ROOT_ID') == $item['id'])
- {
- array_push($tree, array(
- 'id' => $item['id'],
- 'lft' => 1,
- 'rgt' => null,
- 'children' => array())
- );
- }
- else
- {
- // build hash of child rows keyed on parent_id
- if (isset($children[$item['parent_id']]))
- {
- array_push($children[$item['parent_id']], $item['id']);
- }
- else
- {
- $children[$item['parent_id']] = array($item['id']);
- }
- }
- }
+ throw new sfException('You must specify a valid filename');
+ }
+
+ // Get header (first) row
+ $header = fgetcsv($fh, 1000);
- // Recursively add child nodes
- self::addChildren($tree[0], $children, 1);
+ if (!in_array('information_object_id', $header) || !in_array('filename',
$header))
+ {
+ throw new sfException('Import file must contain an
\'information_object_id\' and \'filename\' column');
+ }
- // Crawl tree and build sql statement to update nested set columns
- $rows = self::getNsUpdateRows($tree[0], $classname);
+ $idKey = array_search('information_object_id', $header);
+ $fileKey = array_search('filename', $header);
- // Update database
- $conn->beginTransaction();
- try
- {
- // There seems to be some limit on how many rows we can update with one
- // exec() statement, so chunk the update rows
- $incr = 4000;
- for ($i=0; $i <= count($rows); $i+=$incr)
- {
- $sql = implode("\n", array_slice($rows, $i, $incr));
- $conn->exec($sql);
- }
- }
- catch (PDOException $e)
+ while ($item = fgetcsv($fh, 1000))
+ {
+ if (null === $informationObject =
QubitInformationObject::getById($item[$idKey]))
{
- $conn->rollback();
- throw sfException($e);
+ continue;
}
- $conn->commit();
- } // endforeach
-
- $this->logSection('propel', 'Done!');
- }
-
- protected function addChildren(&$node, $children, $lft)
- {
- $width = 2;
-
- if (isset($children[$node['id']]))
- {
- $lft++;
- foreach ($children[$node['id']] as $id)
+ // read file contents
+ if (false === $content = file_get_contents($item[$fileKey]))
{
- $child = array('id' => $id, 'lft' => $lft, 'rgt' => null, 'children'
=> array());
-
- $w0 = self::addChildren($child, $children, $lft);
- $lft += $w0;
- $width += $w0;
-
- array_push($node['children'], $child);
+ continue;
}
- }
- $node['rgt'] = $node['lft'] + $width - 1;
+ // Create digital object
+ $do = new QubitDigitalObject;
+ $do->informationObject = $informationObject;
+ $do->usageId = QubitTerm::MASTER_ID;
+ $do->assets[] = new QubitAsset(basename($item[$fileKey]), $content);
- return $width;
- }
-
- protected function getNsUpdateRows($node, $classname)
- {
- $str = 'UPDATE '.constant($classname.'::TABLE_NAME');
- $str .= ' SET lft = '.$node['lft'];
- $str .= ', rgt = '.$node['rgt'];
- $str .= ' WHERE id = '.$node['id'].";";
- $rows = array($str);
-
- if (0 < count($node['children']))
- {
- foreach ($node['children'] as $child)
- {
- $rows = array_merge($rows, self::getNsUpdateRows($child, $classname));
- }
+ // Save information object with attached digital object
+ $do->save();
}
-
- return $rows;
}
}
--
You received this message because you are subscribed to the Google Groups
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/qubit-commits?hl=en.