How to import nodes to eZ publish with a PHP script?

CMS System:

eZ publish

Issue:

In many situations it is to much work to import information to eZ publish or any other Open Source CMSes. How can this task be done automatically with a PHP script ?

Solution:

What we show you here is a basic PHP script that create a new node from the article class and populate the node's data_map with title and intro data.
This script has to be executed from a PHP CLI environment (not executed with Apace) and from a position of the eZ publish site root in order to make the include works.

<?php

/* Include required files */
include_once( "lib/ezutils/classes/ezextension.php" );
include_once( "lib/ezutils/classes/ezmodule.php" );
include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'kernel/classes/ezscript.php' );
include_once( 'lib/ezutils/classes/ezexecution.php' );
include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
include_once( 'kernel/classes/eznodeassignment.php' );
include_once( "lib/ezutils/classes/ezoperationhandler.php");


/* Start up the eZ CLI enviorment that make the script work correct */
$cli =& eZCLI::instance();
$script =& eZScript::instance( array( 'debug-message' => '',
                                     'use-session' => false,
                                     'use-modules' => true,
                                     'use-extensions' => true ) );
$script->startup();
$endl = $cli->endlineString();

$script->setUseDebugOutput( false );
$script->setAllowedDebugLevels( false );
$script->setUseDebugAccumulators( false );
$script->setUseDebugTimingPoints( false );
$script->setUseIncludeFiles( false );
$script->setUseSiteAccess( false );

$script->initialize();

/* Start creating eZ object */
$class =& eZContentClass::fetch( 16 );
$contentObject =& $class->instantiate( 14, 1);

$contentObject->setAttribute( 'name', "Article import from script" );
$contentObject->store();
    
$nodeAssignment =& eZNodeAssignment::create(
array( 
            'contentobject_id' => $contentObject->attribute( 'id' ),  
            'contentobject_version' => 1,
             'parent_node' => 2,
             'is_main' => 1
          ));
$nodeAssignment->store();

/* Add data to the eZ object */
$myContentObjectAttributes =& $contentObject->attribute('data_map');

$title = $myContentObjectAttributes['title'];
$title->setAttribute("data_text", "Article import from script"); 
$title->store();

$intro = $myContentObjectAttributes['intro'];
$intro->setAttribute("data_text", "Intro to the article import");
$intro->store();

/* Publish the new eZ object */    
$operationResult = eZOperationHandler::execute( 'content', 'publish',
array(
            'object_id' => $contentObject->attribute( 'id' ), 
            'version' => $contentObject->attribute('current_version' ) ) );

/* End the eZ CLI enviorment*/ 
$script->shutdown();

?>
BuildCMS Crowdprojecting