Snippets for eZ publish 3: eZ cli data creation script (unstable)

Table of contents:

<?php 
 
include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'kernel/classes/ezscript.php' );
include_once( 'kernel/classes/ezcontentclassattribute.php' );
include_once( 'kernel/classes/ezcontentclass.php' );
include_once( 'kernel/classes/ezcontentobject.php' );
include_once( 'kernel/classes/ezcontentobjectversion.php' );
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
//PEAR Console Bar
include_once( 'Console/ProgressBar.php' );

//CLI stuff
$cli =& eZCLI::instance();
$script =& eZScript::instance(); 
$script->startup();
$options = $script->getOptions();
$script->initialize();


$classID = 16;
$nodeID = 59;

if ( !$class = eZContentClass::fetch( $classID ) )
{
    $cli->output('Error: Could not fetch class');
}

$node = eZContentObjectTreeNode::fetch($nodeID);
if (!$node)
{    
    $cli->output('Error: no mapped parent node');        
}

$object =& $class->instantiate();        
if (!$object)
{
    $cli->output('Error: could not create object');
}

$objectID = $object->attribute( 'id' );
$cli->output('Object ID: ' . $objectID);
$cli->output('Node ID: ' . $nodeID);
$cli->output();
$nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $objectID,
                                                   'contentobject_version' => 1,
                                                   'parent_node' => $nodeID,
                                                   'is_main' => 1 ) 
                                           );
$nodeAssignment->store();

$dataMap =& $object->dataMap();
foreach( array_keys( $dataMap ) as $key )
{
    //$cli->output($key);

    $attribute =& $dataMap[$key];
    $classAttributeID = $attribute->attribute( 'contentclassattribute_id' );
    
    $attribute->setAttribute( 'data_text','I have a dream!' );    
}

if ( !eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $objectID, 'version' => 1 ) ) )
{
    // TODO:
    // add to the list of errors
    $cli->output('Error: Could not execute');
}

function cliGetLine($default = ''){
    $input = strtolower(trim(fgets(STDIN)));
    return  (strlen($input) ? $input : $default);
}

////////////////////////// 
$script->shutdown();

?>