|
eZ Publish
[4.0]
|
00001 #!/usr/bin/env php 00002 <?php 00003 // 00004 // Definition of eZCsvimport class 00005 // 00006 // Created on: <27-Sep-2006 22:23:27 sp> 00007 // 00008 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00009 // SOFTWARE NAME: eZ Publish 00010 // SOFTWARE RELEASE: 4.0.x 00011 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00012 // SOFTWARE LICENSE: GNU General Public License v2.0 00013 // NOTICE: > 00014 // This program is free software; you can redistribute it and/or 00015 // modify it under the terms of version 2.0 of the GNU General 00016 // Public License as published by the Free Software Foundation. 00017 // 00018 // This program is distributed in the hope that it will be useful, 00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 // GNU General Public License for more details. 00022 // 00023 // You should have received a copy of version 2.0 of the GNU General 00024 // Public License along with this program; if not, write to the Free 00025 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00026 // MA 02110-1301, USA. 00027 // 00028 // 00029 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00030 // 00031 00032 /*! \file ezcsvimport.php 00033 */ 00034 00035 /*! 00036 \class eZCsvimport ezcsvimport.php 00037 \brief The class eZCsvimport does 00038 00039 */ 00040 00041 00042 //include_once( 'lib/ezutils/classes/ezcli.php' ); 00043 //include_once( 'kernel/classes/ezscript.php' ); 00044 //include_once( 'kernel/classes/ezcontentobjecttreenode.php' ); 00045 //include_once( "lib/ezlocale/classes/ezdatetime.php" ); 00046 00047 require 'autoload.php'; 00048 00049 $cli = eZCLI::instance(); 00050 $script = eZScript::instance( array( 'description' => ( "eZ Publish CSV import script\n\n" . 00051 "\n" . 00052 "\n" . 00053 "\n" . 00054 "\n" . 00055 "" ), 00056 'use-session' => false, 00057 'use-modules' => true, 00058 'use-extensions' => true ) ); 00059 00060 $script->startup(); 00061 00062 $options = $script->getOptions( "[class:][creator:][storage-dir:]", 00063 "[node][file]", 00064 array( 'node' => 'parent node_id to upload object under', 00065 'file' => 'file to read CSV data from', 00066 'class' => 'class identifier to create objects', 00067 'creator' => 'user id of imported objects creator', 00068 'storage-dir' => 'path to directory which will be added to the path of CSV elements' ), 00069 false, 00070 array( 'user' => true )); 00071 $script->initialize(); 00072 00073 if ( count( $options['arguments'] ) < 2 ) 00074 { 00075 $cli->error( "Need a parent node to place object under and file to read data from" ); 00076 $script->shutdown( 1 ); 00077 } 00078 00079 $nodeID = $options['arguments'][0]; 00080 $inputFileName = $options['arguments'][1]; 00081 $createClass = $options['class']; 00082 $creator = $options['creator']; 00083 if ( $options['storage-dir'] ) 00084 { 00085 $storageDir = $options['storage-dir']; 00086 } 00087 else 00088 { 00089 $storageDir = ''; 00090 } 00091 00092 $csvLineLength = 100000; 00093 00094 $cli->output( "Going to import objects of class $createClass under node $nodeID from file $inputFileName\n" ); 00095 00096 $node = eZContentObjectTreeNode::fetch( $nodeID ); 00097 if ( !$node ) 00098 { 00099 $cli->error( "No such node to import objects" ); 00100 $script->shutdown( 1 ); 00101 } 00102 $parentObject = $node->attribute( 'object' ); 00103 00104 $class = eZContentClass::fetchByIdentifier( $createClass ); 00105 00106 if ( !$class ) 00107 { 00108 $cli->error( "No class with identifier $createClass" ); 00109 $script->shutdown( 1 ); 00110 } 00111 00112 $fp = @fopen( $inputFileName, "r" ); 00113 if ( !$fp ) 00114 { 00115 $cli->error( "Can not open file $inputFileName for reading" ); 00116 $script->shutdown( 1 ); 00117 } 00118 00119 while ( $objectData = fgetcsv( $fp, $csvLineLength , ';', '"' ) ) 00120 { 00121 00122 $contentObject = $class->instantiate( $creator ); 00123 $contentObject->store(); 00124 00125 $nodeAssignment = eZNodeAssignment::create( array( 00126 'contentobject_id' => $contentObject->attribute( 'id' ), 00127 'contentobject_version' => $contentObject->attribute( 'current_version' ), 00128 'parent_node' => $nodeID, 00129 'is_main' => 1 00130 ) 00131 ); 00132 $nodeAssignment->store(); 00133 00134 $version = $contentObject->version( 1 ); 00135 $version->setAttribute( 'modified', eZDateTime::currentTimeStamp() ); 00136 $version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT ); 00137 $version->store(); 00138 00139 $contentObjectID = $contentObject->attribute( 'id' ); 00140 00141 $attributes = $contentObject->attribute( 'contentobject_attributes' ); 00142 00143 while ( list( $key, $attribute ) = each( $attributes ) ) 00144 { 00145 $dataString = $objectData[$key]; 00146 switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) ) 00147 { 00148 case 'ezimage': 00149 case 'ezbinaryfile': 00150 case 'ezmedia': 00151 { 00152 $dataString = eZDir::path( array( $storageDir, $dataString ) ); 00153 break; 00154 } 00155 default: 00156 } 00157 $attribute->fromString( $dataString ); 00158 $attribute->store(); 00159 } 00160 00161 //include_once( 'lib/ezutils/classes/ezoperationhandler.php' ); 00162 $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID, 00163 'version' => 1 ) ); 00164 } 00165 00166 fclose( $fp ); 00167 00168 $script->shutdown(); 00169 00170 ?>