<?php
/**
* Lorem Ipsum CLI
*
*
* @author Ammar Ibrahim
*/
// ------------------------------------------------------------------------
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' );
include_once( 'extension/loremipsum/classes/ezloremipsum.php' );
set_include_path( ini_get('include_path') . PATH_SEPARATOR . 'PEAR/');
include_once('Console/ProgressBar.php');
$cli =& eZCLI::instance();
$script =& eZScript::instance();
$script->startup();
$options = $script->getOptions();
$script->initialize();
$cli->output('Enter parent node ID (69):');
$parameters['nodes'][] = cliGetLine('69');
$cli->output('Enter number of items to create (1000):');
$parameters['count'] = cliGetLine('1000');
$cli->output('Enter sleep in seconds after every node creation, "0" no sleep(0):');
$parameters['sleep'] = cliGetLine('0');
//get all content classes
$classes = eZContentClass::fetchList();
foreach ($classes as $class ){
$cli->output($class->ID . '. ' . $class->Name);
}
$cli->output('Enter type of nodes to create(16):');
$parameters['class'] = cliGetLine('16');
//get all attributes of the selected class
$attributes = eZContentClassAttribute::fetchListByClassID($parameters['class']);
foreach ($attributes as $attribute){
$attribHint = $attribute->Name;
$attribHint .= ($attribute->IsRequired ? ' (Required)' : '') . ', ';
$attribHint .= $attribute->DataTypeString;
$cli->output($attribHint);
switch($attribute->DataTypeString){
case 'ezstring';
$cli->output('Generate min,max words (4,6)');
list($parameters['attributes'][$attribute->ID]['min_words'],
$parameters['attributes'][$attribute->ID]['max_words']) = explode(',',cliGetLine('4,6'));
break;
case 'ezxmltext';
$cli->output('Generate min,max paragraphs (4,6)');
list($parameters['attributes'][$attribute->ID]['min_pars'],
$parameters['attributes'][$attribute->ID]['max_pars']) = explode(',',cliGetLine('4,6'));
$cli->output('Each paragraph min,max sentences (4,6)');
list($parameters['attributes'][$attribute->ID]['min_sentences'],
$parameters['attributes'][$attribute->ID]['max_sentences']) = explode(',',cliGetLine('4,6'));
break;
case 'eztext':
$cli->output('Generate min,max paragraphs (4,6)');
list($parameters['attributes'][$attribute->ID]['min_pars'],
$parameters['attributes'][$attribute->ID]['max_pars']) = explode(',',cliGetLine('4,6'));
$cli->output('Each paragraph min,max sentences (4,6)');
list($parameters['attributes'][$attribute->ID]['min_sentences'],
$parameters['attributes'][$attribute->ID]['max_sentences']) = explode(',',cliGetLine('4,6'));
break;
case 'ezboolean':
$cli->output('Generate "true" with probability of %(50):');
$parameters['attributes'][$attribute->ID]['prob'] = cliGetLine('50');
break;
case 'ezinteger':
$cli->output('Generate integer min,max (0,999)');
list($parameters['attributes'][$attribute->ID]['min'],
$parameters['attributes'][$attribute->ID]['max']) = explode(',',cliGetLine('0,999'));
break;
case 'ezfloat':
$cli->output('Generate float min,max (0,999)');
list($parameters['attributes'][$attribute->ID]['min'],
$parameters['attributes'][$attribute->ID]['max']) = explode(',',cliGetLine('0,999'));
break;
case 'ezprice':
$cli->output('Generate price min,max (0,999)');
list($parameters['attributes'][$attribute->ID]['min'],
$parameters['attributes'][$attribute->ID]['max']) = explode(',',cliGetLine('0,999'));
break;
default:
$cli->output( 'Not supported, skipping.');
}
//blank line
$cli->output();
}
$cli->output('Quick mode(Y,N):');
$parameters['quick'] = ( strtolower(substr(cliGetLine('n'),0,1)) == 'y' ? 'On' : '' );
if ( !isset( $parameters['structure'] ) )
{
$parameters['structure'] = array();
$totalCount = 0;
$count = $parameters['count'];
foreach ( $parameters['nodes'] as $nodeID )
{
$nodeID = ( int ) $nodeID;
if ( $nodeID )
{
$parameters['structure'][$nodeID] = $count;
$totalCount += $count;
}
}
$parameters['total_count'] = $totalCount;
$parameters['created_count'] = 0;
$parameters['start_time'] = time();
}
$classID = $parameters['class'];
if ( !$class = eZContentClass::fetch( $classID ) )
{
// TODO
return;
}
if ( !$attributes =& eZContentClassAttribute::fetchListByClassID( $classID, EZ_CLASS_VERSION_STATUS_DEFINED, false ) )
{
// TODO
return;
}
foreach ( $attributes as $attribute )
{
if ( $attribute['is_required'] && !isset( $parameters['attributes'][$attribute['id']] ) )
{
// TODO
return;
}
}
$db =& eZDB::instance();
$db->setIsSQLOutputEnabled(false);
$cli->output('Generating content:');
$cli->output();
//progress bar stuff
$bar = new Console_ProgressBar('- %fraction% [%bar%] %percent% ETA: %estimate%', '=>', '-', 78, $parameters['count']);
$bar->update(0);
foreach ( array_keys( $parameters['structure'] ) as $nodeID )
{
$node = eZContentObjectTreeNode::fetch( $nodeID );
if ( !$node )
{
// TODO
continue;
}
if ( isset( $parameters['quick'] ) && $parameters['quick'] )
{
$parentObject =& $node->attribute( 'object' );
$sectionID =& $parentObject->attribute( 'section_id' );
}
while ( $parameters['structure'][$nodeID] > 0 )
{
//check if we need to sleep this round
if ( isset( $parameters['sleep'] ) && $parameters['sleep'] )
{
sleep($parameters['sleep']);
}
// create object
$object =& $class->instantiate();
if ( $object )
{
$db->begin();
$objectID = $object->attribute( 'id' );
$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 )
{
$attribute =& $dataMap[$key];
$classAttributeID = $attribute->attribute( 'contentclassattribute_id' );
if ( isset( $parameters['attributes'][$classAttributeID] ) )
{
$attributeparameters = $parameters['attributes'][$classAttributeID];
$dataType = $attribute->attribute( 'data_type_string' );
switch ( $dataType )
{
case 'ezstring':
{
$attribute->setAttribute( 'data_text',
eZLoremIpsum::generateString( $attributeparameters['min_words'], $attributeparameters['max_words'] ) );
} break;
case 'ezxmltext':
{
$xml = '<?xml version="1.0" encoding="utf-8"?>'."\n".
'<section xmlns:image="https://ez.no/namespaces/ezpublish3/image/"'."\n".
' xmlns:xhtml="https://ez.no/namespaces/ezpublish3/xhtml/"'."\n".
' xmlns:custom="https://ez.no/namespaces/ezpublish3/custom/">'."\n".
' <section>'."\n";
$numPars = mt_rand( ( int ) $attributeparameters['min_pars'], ( int ) $attributeparameters['max_pars'] );
for ( $par = 0; $par < $numPars; $par++ )
{
$xml .= ' <paragraph>';
$numSentences = mt_rand( ( int ) $attributeparameters['min_sentences'], ( int ) $attributeparameters['max_sentences'] );
for ( $sentence = 0; $sentence < $numSentences; $sentence++ )
{
if ( $sentence != 0 )
{
$xml .= ' ';
}
$xml .= eZLoremIpsum::generateSentence();
}
$xml .= "</paragraph>\n";
}
$xml .= " </section>\n</section>\n";
$attribute->setAttribute( 'data_text', $xml );
} break;
case 'eztext':
{
$numPars = mt_rand( ( int ) $attributeparameters['min_pars'], ( int ) $attributeparameters['max_pars'] );
for ( $par = 0; $par < $numPars; $par++ )
{
if ( $par == 0 )
{
$text = '';
}
else
{
$text .= "\n";
}
$numSentences = mt_rand( ( int ) $attributeparameters['min_sentences'], ( int ) $attributeparameters['max_sentences'] );
for ( $sentence = 0; $sentence < $numSentences; $sentence++ )
{
$text .= eZLoremIpsum::generateSentence();
}
$text .= "\n";
}
$attribute->setAttribute( 'data_text', $text );
} break;
case 'ezboolean':
{
$rnd = mt_rand( 0, 99 );
$value = 0;
if ( $rnd < $attributeparameters['prob'] )
{
$value = 1;
}
$attribute->setAttribute( 'data_int', $value );
} break;
case 'ezinteger':
{
$integer = mt_rand( ( int ) $attributeparameters['min'], ( int ) $attributeparameters['max'] );
$attribute->setAttribute( 'data_int', $integer );
} break;
case 'ezfloat':
{
$power = 100;
$float = mt_rand( $power * ( int ) $attributeparameters['min'], $power * ( int ) $attributeparameters['max'] );
$float = $float / $power;
$attribute->setAttribute( 'data_float', $float );
} break;
case 'ezprice':
{
$power = 10;
$price = mt_rand( $power * ( int ) $attributeparameters['min'], $power * ( int ) $attributeparameters['max'] );
$price = $price / $power;
$attribute->setAttribute( 'data_float', $price );
} break;
}
$attribute->store();
}
}
if ( isset( $parameters['quick'] ) && $parameters['quick'] )
{
$version =& $object->version( 1 );
$version->setAttribute( 'status', 3 );
$version->store();
$object->setAttribute( 'status', 1 );
$objectName = $class->contentObjectName( $object );
$object->setName( $objectName, 1 );
$object->setAttribute( 'current_version', 1 );
$time = mktime();
$object->setAttribute( 'modified', $time );
$object->setAttribute( 'published', $time );
$object->setAttribute( 'section_id', $sectionID );
$object->store();
$newNode =& $node->addChild( $objectID, 0, true );
$newNode->setAttribute( 'contentobject_version', 1 );
$newNode->setAttribute( 'contentobject_is_published', 1 );
$newNode->setName( $objectName );
$newNode->setAttribute( 'main_node_id', $newNode->attribute( 'node_id' ) );
$newNode->setAttribute( 'sort_field', $nodeAssignment->attribute( 'sort_field' ) );
$newNode->setAttribute( 'sort_order', $nodeAssignment->attribute( 'sort_order' ) );
$newNode->updateSubTreePath();
$newNode->store();
$db->commit();
}
else
{
$db->commit();
if ( !eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $objectID, 'version' => 1 ) ) )
{
// TODO:
// add to the list of errors
}
}
}
else
{
// TODO:
// add to the list of errors
}
$parameters['structure'][$nodeID]--;
$parameters['created_count']++;
$bar->update($parameters['created_count']);
}
}
if ( isset( $parameters['quick'] ) && $parameters['quick'] )
{
include_once( 'kernel/classes/ezcontentcachemanager.php' );
eZContentCacheManager::clearAllContentCache();
}
function cliGetLine($default = ''){
$input = strtolower(trim(fgets(STDIN)));
return ( strlen($input) ? $input : $default );
}
//////////////////////////
$script->shutdown();
?>