|
eZ Publish
[4.0]
|
00001 #!/usr/bin/env php 00002 <?php 00003 // 00004 // Definition of eZCsvexport class 00005 // 00006 // Created on: <27-Sep-2006 17:23:23 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 ezcsvexport.php 00033 */ 00034 00035 /*! 00036 \class eZCsvexport ezcsvexport.php 00037 \brief The class eZCsvexport does 00038 00039 */ 00040 00041 //include_once( 'lib/ezutils/classes/ezcli.php' ); 00042 //include_once( 'kernel/classes/ezscript.php' ); 00043 //include_once( 'kernel/classes/ezcontentobjecttreenode.php' ); 00044 00045 require 'autoload.php'; 00046 00047 $cli = eZCLI::instance(); 00048 $script = eZScript::instance( array( 'description' => ( "eZ Publish CSV export script\n" . 00049 "\n" . 00050 "ezcsvexport.php --storage-dir=export 2" ), 00051 'use-session' => false, 00052 'use-modules' => true, 00053 'use-extensions' => true, 00054 'user' => true ) ); 00055 00056 $script->startup(); 00057 00058 $options = $script->getOptions( "[storage-dir:]", 00059 "[node]", 00060 array( 'storage-dir' => 'directory to place exported files in' ), 00061 false, 00062 array( 'user' => true ) ); 00063 $script->initialize(); 00064 00065 if ( count( $options['arguments'] ) < 1 ) 00066 { 00067 $cli->error( 'Specify a node to export' ); 00068 $script->shutdown( 1 ); 00069 } 00070 00071 $nodeID = $options['arguments'][0]; 00072 00073 if ( !is_numeric( $nodeID ) ) 00074 { 00075 $cli->error( 'Specify a numeric node ID' ); 00076 $script->shutdown( 2 ); 00077 } 00078 00079 if ( $options['storage-dir'] ) 00080 { 00081 $storageDir = $options['storage-dir']; 00082 } 00083 else 00084 { 00085 $storageDir = ''; 00086 } 00087 00088 $node = eZContentObjectTreeNode::fetch( $nodeID ); 00089 if ( !$node ) 00090 { 00091 $cli->error( "No node with ID: $nodeID" ); 00092 $script->shutdown( 3 ); 00093 } 00094 00095 $cli->output( "Going to export subtree from node $nodeID to directory $storageDir \n" ); 00096 00097 $subTreeCount = $node->subTreeCount(); 00098 00099 $script->setIterationData( '.', '~' ); 00100 00101 $script->resetIteration( $subTreeCount ); 00102 00103 $subTree = $node->subTree(); 00104 $openedFPs = array(); 00105 00106 while ( list( $key, $childNode ) = each( $subTree ) ) 00107 { 00108 $status = true; 00109 00110 $object = $childNode->attribute( 'object' ); 00111 00112 $classIdentifier = $object->attribute( 'class_identifier' ); 00113 00114 if ( !isset( $openedFPs[$classIdentifier] ) ) 00115 { 00116 $tempFP = @fopen( $storageDir . '/' . $classIdentifier . '.csv', "w" ); 00117 if ( $tempFP ) 00118 { 00119 $openedFPs[$classIdentifier] = $tempFP; 00120 } 00121 else 00122 { 00123 $cli->error( "Can not open output file for $classIdentifier class" ); 00124 $script->shutdown( 4 ); 00125 } 00126 } 00127 else 00128 { 00129 if ( !$openedFPs[$classIdentifier] ) 00130 { 00131 $cli->error( "Can not open output file for $classIdentifier class" ); 00132 $script->shutdown( 4 ); 00133 } 00134 } 00135 00136 $fp = $openedFPs[$classIdentifier]; 00137 00138 $objectData = array(); 00139 foreach ( $object->attribute( 'contentobject_attributes' ) as $attribute ) 00140 { 00141 $attributeStringContent = $attribute->toString(); 00142 00143 if ( $attributeStringContent != '' ) 00144 { 00145 switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) ) 00146 { 00147 case 'ezimage': 00148 { 00149 $imagePathParts = explode( '/', $attributeStringContent ); 00150 $imageFile = array_pop( $imagePathParts ); 00151 // here it would be nice to add a check if such file allready exists 00152 $success = eZFileHandler::copy( $attributeStringContent, $storageDir . '/' . $imageFile ); 00153 if ( !$success ) 00154 { 00155 $status = false; 00156 } 00157 $attributeStringContent = $imageFile; 00158 } break; 00159 00160 case 'ezbinaryfile': 00161 case 'ezmedia': 00162 { 00163 $binaryData = explode( '|', $attributeStringContent ); 00164 $success = eZFileHandler::copy( $binaryData[0], $storageDir . '/' . $binaryData[1] ); 00165 if ( !$success ) 00166 { 00167 $status = false; 00168 } 00169 $attributeStringContent = $binaryData[1]; 00170 } break; 00171 00172 default: 00173 } 00174 } 00175 00176 $objectData[] = $attributeStringContent; 00177 } 00178 00179 if ( !$fp ) 00180 { 00181 $cli->error( "Can not open output file" ); 00182 $script->shutdown( 5 ); 00183 } 00184 00185 if ( !fputcsv( $fp, $objectData, ';' ) ) 00186 { 00187 $cli->error( "Can not write to file" ); 00188 $script->shutdown( 6 ); 00189 } 00190 00191 $script->iterate( $cli, $status ); 00192 } 00193 00194 while ( $fp = each( $openedFPs ) ) 00195 { 00196 fclose( $fp['value'] ); 00197 } 00198 00199 $script->shutdown(); 00200 00201 ?>