Forums / Developer / Import CSV in EZP 3.9 : Missing doc

Import CSV in EZP 3.9 : Missing doc

Author Message

H-Works Agency

Friday 22 December 2006 4:06:26 am

EZP is great...but lack documentation...in fact you should put exemples in your doc instead of theorical principles.

After some work here is the correct syntax to use : /bin/php/ezcsvimport.php

This script is great and saved my life...and a lot of hours :p

Here is the synthax for the script call : Importing product under node_id 80

php -C ./bin/php/ezcsvimport.php -v --siteaccess=mysite -l myuser -p mypasswd  -d --class=product --creator=14 --storage-dir='media/images' 80 absolute_path_to_csv

Here is the syntax of the csv file i use for importing my products (87-88 is a relationlist datatype to images object_id and last value is the price)

Blind;PE06B;POXLK;0;POWELL CREAM;'Description of my product';87-86;BLK;XL;1;7,00|1|1

Hope it will help.

EZP is Great

H-Works Agency

Monday 25 December 2006 8:13:17 am

How to import a ezxml datatype attribute ?

I tried a lot of different syntax but doesn't work.

Second thing is : What is the aim of the 'storage-dir' argument ?

Thanx

EZP is Great

Kristof Coomans

Tuesday 26 December 2006 12:29:59 am

Hi Martin

You need to use the XML format as it will be stored in the data_text database field. First try to create an object with the desired content manually, then lookup the value in the database in order to have an example.

The storage-dir option is prepended to the relative file path of ezmedia, ezimage and ezbinaryfile attributes. You can for example make absolute paths by using it.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Sébastien Antoniotti

Monday 29 January 2007 2:18:36 am

Hi,

I'm looking for a similar solution in order to import sql data in my ezpublish site.

I have the following structure :

Artist (artiste_id, name)
Album (album_id, name, year, #artiste_id)
Song (song_id, title, text, #album_id)

It's a very simple schema, and I would like to import this data in my ezpublish tree by automatically create :

1 Artiste class (container) instance by artist
1 Album class (container) instance by album and place them in her artist
1 Song class instance by song and place them in her album.

So I need your help because I don't know how to make link between artists, albums ans songs.

Thanks in advance !

eZ Publish Freelance
web : http://www.webaxis.fr

Xavier Dutoit

Monday 29 January 2007 4:26:25 am

Hi,

When we have multiple relations, we uses the xml import.

For the relation, the problem is to make the link between the external relation and the internal object id. I doubt there is a universal solution, and we modify the import script (not a big deal, but needs to know php...

X+

http://www.sydesy.com

Sébastien Antoniotti

Monday 29 January 2007 4:40:37 am

Hi,

So I have to install the xml import contribution and custom it.

Thanks and good dev ;)

eZ Publish Freelance
web : http://www.webaxis.fr

Felix Woldt

Monday 29 January 2007 5:05:00 am

Hi,
another extension you can use is
http://pubsvn.ez.no/community/trunk/extension/import/

this is a framework to import any kind of data to ez.
But you have to write php code to use it.

Some Features:
- create contentobjects of any classes with attributes e.g. images, objectrelation, objectrelationlist, text ...
- you can create object or update it ( create new versions ) if your import script run more than one times

Have a look to the example.

Best Regards
Felix Woldt

http://www.jac-systeme.de - Developers united in eZ Publish: http://www.cjw-network.com

CJW Newsletter 1.0.0 released: http://projects.ez.no/cjw_newsletter

Sébastien Antoniotti

Monday 29 January 2007 7:00:25 am

Hi,

Thanks for this tip, but how can I download this contrib ?

eZ Publish Freelance
web : http://www.webaxis.fr

kracker (the)

Monday 29 January 2007 7:49:53 am

<b>Use Subversion!</b>

<i>http://svnbook.red-bean.com/
http://ezpedia.org/wiki/en/ez/using_pubsvn_subversion_repositories</i>

<i>//kracker

Monty Python - Alzheimer Answering Machine
Gibbs, Richard and Jonathan Davis - Queen of the Damned - Air Lestat</i>

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

Sébastien Antoniotti

Monday 29 January 2007 8:15:50 am

Thanks, I don't know subversion at all !

eZ Publish Freelance
web : http://www.webaxis.fr

kracker (the)

Monday 29 January 2007 2:55:47 pm

You could learn the little you need to know if you really cared at all.

I have shared with you, links to the best documentation and teaching information available.

Those who fail to change will find themselves unable to communicate with those who have ...

Download Tortuous SVN a GUI to Subversion and get the code already :)
Or you can download the files one by one by hand :\

//kracker

<i>Cowboy Bebop - Blue 3

Any sympathy for the ***** in the crowd tonight?</i>

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

Monday 29 January 2007 3:24:51 pm

Here's also some subversion documentation the eZ crew have made:
http://ez.no/ezpublish/documentation/incoming/the_community_server/help/svn

Sébastien Antoniotti

Tuesday 30 January 2007 12:24:54 am

Thanks for your help, I'm downloading tortoise ;)

eZ Publish Freelance
web : http://www.webaxis.fr

H-Works Agency

Tuesday 30 January 2007 4:33:35 am

The import script in the 3.9 install uses toString() or fromString() for export & import.

Those function are only available from 3.9+ so if you are in 3.8 my syntax won't apply.

You could use the import csv function but possible datatypes are limited.

EZP is Great

Rodrigo Pinheiro

Wednesday 07 February 2007 9:31:15 am

I'm using the ezcsvimport.php script to import almost all kind of data with sucess, including product prices. I'm only having problems when I try to import data wich should be stored at a ezuser datatype.

I checked the toString and fromString documentation [1] and it seams that to import to a ezuser datatype you should have at the .csv file a field with the following configuration:

login|email|password hash|password hash type

I'm generating the password hash with md5 and I'm using the password hash type equal to 1.

When I import the data everything gets imported except the ezuser data. I'm I doing something wrong?

[1] http://pubsvn.ez.no/nextgen/trunk/doc/features/3.9/to_from_string_datatype_functionality.txt

H-Works Agency

Wednesday 07 February 2007 11:29:20 am

I didn't try to import user data but i don't think the password hash is a md5...its has to be confirmed. And maybe you should try other password types.

Sorry for the poor help.

Martin

EZP is Great

Rodrigo Pinheiro

Thursday 08 February 2007 10:41:18 am

I've managed to import the users by modifying the script.

    $attributes =& $contentObject->contentObjectAttributes();

    while ( list( $key, $attribute ) = each( $attributes ) )
    {
        $dataString = $objectData[$key];
        switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
        {
            case 'ezimage':
            case 'ezbinaryfile':
            case 'ezmedia':
                $dataString = $storageDir . $dataString;
                break;
            case 'ezprice':
                $dataString = $dataString . '|1|0';
                break;
            case 'ezuser':
                $userAccountObject = eZUser::fetch($attribute->ContentObjectID);
                $username = 'client' . $objectData[0];
                $email = $objectData[7];
                $password = eZUser::createPassword(6, 0);
                $confirmPassword = $password;
                $password_hash = md5( "$username\n$password" );

                $userAccountObject->setInformation($contentObject->attribute('id' ), $username, $email, $password, $confirmPassword);
                $userAccountObject->store();
                break;
        }
        $attribute->fromString( $dataString );
        $attribute->store();

    }

What I'm doing above is getting the internal client number and email so I can generate a username + password to store at a ezuser datatype. It works, but it imports the .csv very slowly.

Sébastien Antoniotti

Friday 09 February 2007 3:15:02 am

Hi,

Precedently I wrote this :

<i>
I have the following structure :

Artist (artiste_id, name)
Album (album_id, name, year, #artiste_id)
Song (song_id, title, text, #album_id)

It's a very simple schema, and I would like to import this data in my ezpublish tree by automatically create :

1 Artiste class (container) instance by artist
1 Album class (container) instance by album and place them in her artist
1 Song class instance by song and place them in her album.
</i>

And today I haved downloaded the importXML extention, and inspected the importXMLDatafunctioncollection.php.
I understand a little the import procedure with Ez libs, and I would like to write a script that
- make connection to a mysql database
- query all the albums in album_table
- for each album, create an instance of my ezpublish Album class in a parent_node "hard coded"
- query all the songs related to the current album
- for each song create an instance of my ezpublish Song class into the album instance created precedently
-etc.

So I have to modify the importXMLDatafunctioncollection.php, but before start I would like to know if I can place this script into the ezpublish root directory, and call it by the shell.

I ask you that because I think in my case a module is not necessary (this import will be run a single time).

Thanks in advance !

eZ Publish Freelance
web : http://www.webaxis.fr

Sébastien Antoniotti

Friday 09 February 2007 6:07:07 am

Can you tell me what you think about this ?

include_once ('lib/ezlocale/classes/ezdate.php');
include_once ('lib/ezutils/classes/ezoperationhandler.php');

eZINI::setIsDebugEnabled( true );

/* ############## Fonction qui ajoute un album dans un artiste  ################## */

function ajouterAlbum( $titre, $annee, $remove, $idArtisteParent)
{ 
	// identifiant de la classe album
	$identifiantClasse = "10";
	$class =& eZContentClass::fetchByIdentifier( $identifiantClasse );
	// ID du noeud de l'artiste parent de cet album 
	$node_id=$idArtisteParent;
	
	// fetch parent node 
		$node =& eZContentObjectTreeNode::fetch($node_id);
	// set associated object to parent node (folder object) 
	if ( !is_object( $node ) ) {
		eZDebug::writeDebug( "node_id=$node_id doesn't seem to be a valid node id", 'importSQLData' );
		return array( 'result' => array('statut' => "erreur"), 'msg' => "node_id=$node_id doesn't seem to be a valid node id" );  
	}
	$parentContentObject =& $node->attribute( 'object' );
	
	/* --------- ici mettre mon id d'user en dur ------- */
	// set user = current user 
	$user =& eZUser::currentUser();
	// set user ID 
	$userID =& $user->attribute( 'contentobject_id' );
	// set section ID =  parent object section
	
	$sectionID = $parentContentObject->attribute( 'section_id' );
	if ($remove) {
		$children =& $node->subTree( array( 'Limitation' => array() ) );
		$deleteIDArray = array();
		foreach ($children as $child) {
			array_push ($deleteIDArray, $child->attribute( 'node_id' ));
		}
		eZContentObjectTreeNode::removeSubtrees( $deleteIDArray, $movetotrash );
	}

	if ( !is_object( $class ) ) {
		eZDebug::writeDebug( "DefaultClassIdentifier=$identifiantClasse doesn't seem to be a valid class identifier", 'importSQLData' );
		return array( 'result' => array('statut' => "erreur"), 'msg' => "DefaultClassIdentifier=$identifiantClasse doesn't seem to be a valid class identifier" );  
	}
		
	// Crée une instance d'album
	$contentObject =& $class->instantiate( $userID, $sectionID );

	// La ratache à son artiste en créant le noeud
	$nodeAssignment =& eZNodeAssignment::create( array(
			 'contentobject_id' => $contentObject->attribute( 'id' ),
			 'contentobject_version' => $contentObject->attribute( 'current_version' ),
			 'parent_node' => $node->attribute( 'node_id' ),
			 'is_main' => 1 ));
	$nodeAssignment->store();

	// Spécifie la version de l'objet
	$contentObjectVersion =& $contentObject->version($contentObject->attribute( 'current_version' ) );
	$contentObjectVersion->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT);
	$contentObjectVersion->store();

	// Enumere les attributs du nouvel objet (album) pour les renseigner
	$contentObjectAttributes =& $contentObjectVersion->contentObjectAttributes();
	foreach ($contentObjectAttributes as $attribute)
	{
		$identifiantAttribut=$attribute->attribute("contentclass_attribute_identifier");			
		 
		switch( $identifiantAttribut ) {
			case 'titre':
				$attribute->setAttribute("data_text",trim($titre));
				break;				
			case 'annee':
				$attribute->setAttribute("data_int",$annee);
				break;				
			default :					
		}	
		$attribute->store();			
	}			
		
	//publish the newly created node
	$operationResult = eZOperationHandler::execute( 'content', 
													'publish', array( 'object_id' => $contentObject->attribute( 'id' ),
													'version' => $contentObject->attribute( 'current_version' ) ) );
	return $nodeAssignment->attribute('id');			
}

This fonction create an album instance under the parent node id given by parameter.

For the moment I cant test them because first I need to install a test version of ezpublish to avoid a trouble in my website.

So if this fonction is good, did you think that I can execute this script by the unix shell ?

eZ Publish Freelance
web : http://www.webaxis.fr

Sébastien Antoniotti

Friday 09 February 2007 6:27:33 am

It's still me !

I have tried to run my script by "php script.php" and I got this :

Fatal error: Undefined class name 'ezcontentclass' in /var/www/eztest/importSQLData_seb.php on line 21

I think I must include one or many library but I don't know wich...

eZ Publish Freelance
web : http://www.webaxis.fr