[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZCsvexport class 4 // 5 // Created on: <27-Sep-2006 17:23:23 sp> 6 // 7 8 // Copyright (C) 1999-2004 eZ systems as. All rights reserved. 9 // 10 // This source file is part of the eZ publish (tm) Open Source Content 11 // Management System. 12 // 13 // This file may be distributed and/or modified under the terms of the 14 // "GNU General Public License" version 2 as published by the Free 15 // Software Foundation and appearing in the file LICENSE included in 16 // the packaging of this file. 17 // 18 // Licencees holding a valid "eZ publish professional licence" version 2 19 // may use this file in accordance with the "eZ publish professional licence" 20 // version 2 Agreement provided with the Software. 21 // 22 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING 23 // THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 // PURPOSE. 25 // 26 // The "eZ publish professional licence" version 2 is available at 27 // http://ez.no/ez_publish/licences/professional/ and in the file 28 // PROFESSIONAL_LICENCE included in the packaging of this file. 29 // For pricing of this licence please contact us via e-mail to licence@ez.no. 30 // Further contact information is available at http://ez.no/company/contact/. 31 // 32 // The "GNU General Public License" (GPL) is available at 33 // http://www.gnu.org/copyleft/gpl.html. 34 // 35 // Contact licence@ez.no if any conditions of this licencing isn't clear to 36 // you. 37 // 38 39 /*! \file ezcsvexport.php 40 */ 41 42 /*! 43 \class eZCsvexport ezcsvexport.php 44 \brief The class eZCsvexport does 45 46 */ 47 48 function fputcsv4( $fh, $arr ) 49 { 50 $csv = ""; 51 while ( list( $key, $val ) = each( $arr ) ) 52 { 53 $val = str_replace( '"', '""', $val ); 54 $csv .= '"'.$val.'";'; 55 } 56 $csv = substr( $csv, 0, -1 ); 57 $csv .= "\n"; 58 if ( ! $num = @fwrite( $fh, $csv ) ) 59 return FALSE; 60 else 61 return $num; 62 } 63 64 include_once ( 'lib/ezutils/classes/ezcli.php' ); 65 include_once ( 'kernel/classes/ezscript.php' ); 66 include_once ( 'kernel/classes/ezcontentobjecttreenode.php' ); 67 68 $cli =& eZCLI::instance(); 69 $script =& eZScript::instance( array( 'description' => ( "eZ publish CSV export script\n\n" . 70 "\n" . 71 "\n" . 72 "\n" . 73 "\n" . 74 "" ), 75 'use-session' => false, 76 'use-modules' => true, 77 'use-extensions' => true, 78 'user' => true ) ); 79 80 $script->startup(); 81 82 $options = $script->getOptions( "[storage-dir:]", 83 "[node]", 84 array( 'node' => 'node_id or url_alias of the node to export', 85 'storage-dir' => 'directory to place exported files if any'), 86 false, 87 array( 'user' => true )); 88 $script->initialize(); 89 90 if ( !$options['node'] and count( $options['arguments'] ) < 1 ) 91 { 92 $cli->error( "Need a node to export and file for output" ); 93 $script->shutdown( 1 ); 94 } 95 96 97 $nodeID = $options['arguments'][0]; 98 99 if ( $options['storage-dir'] ) 100 { 101 $storageDir = $options['storage-dir']; 102 } 103 else 104 { 105 $storageDir = ''; 106 } 107 108 109 $cli->output( "Going to export subtree from node " . $nodeID . " to directory " . $storageDir . "\n" ); 110 111 112 $node = eZContentObjectTreeNode::fetch( $nodeID ); 113 if ( !$node ) 114 { 115 $cli->error( "No such node" ); 116 $script->shutdown( 1 ); 117 } 118 119 $subTree = $node->subTree(); 120 $openedFPs = array(); 121 122 while ( list( $key, $childNode ) = each( $subTree ) ) 123 { 124 $object = $childNode->attribute( 'object' ); 125 126 $classIdentifier = $object->attribute( 'class_identifier' ); 127 128 if ( !isset( $openedFPs[$classIdentifier] ) ) 129 { 130 $tempFP = @fopen( $storageDir . $classIdentifier . '.csv', "w" ); 131 if ( !$tempFP ) 132 { 133 $cli->error( "Can not open output file for $classIdentifier class" ); 134 $script->shutdown( 1 ); 135 } 136 else 137 { 138 $cli->output( "Created file $classIdentifier.csv " ); 139 $openedFPs[$classIdentifier] = $tempFP; 140 } 141 } 142 else 143 { 144 if ( ! $openedFPs[$classIdentifier] ) 145 { 146 $cli->error( "Can not open output file for $classIdentifier class" ); 147 $script->shutdown( 1 ); 148 } 149 } 150 151 $fp = $openedFPs[$classIdentifier]; 152 153 154 155 156 $objectData = array(); 157 foreach ( $object->attribute( 'contentobject_attributes' ) as $attribute ) 158 { 159 $attributeStringContent = $attribute->toString(); 160 161 switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) ) 162 { 163 case 'ezimage': 164 { 165 $imageFile = array_pop( explode( '/', $attributeStringContent ) ); 166 // here it would be nice to add a check if such file allready exists 167 eZFileHandler::copy( $attributeStringContent, $storageDir . $imageFile ); 168 $attributeStringContent = $imageFile; 169 break; 170 } 171 case 'ezbinaryfile': 172 case 'ezmedia': 173 { 174 $binaryData = explode( '|', $attributeStringContent ); 175 eZFileHandler::copy( $binaryData[0], $storageDir . $binaryData[1] ); 176 $attributeStringContent = $binaryData[1]; 177 break; 178 } 179 default: 180 } 181 182 $objectData[] = $attributeStringContent; 183 } 184 185 // $fp = fopen( $outputFileName, "w" ); 186 if ( !$fp ) 187 { 188 $cli->error( "Can not open output file" ); 189 $script->shutdown( 1 ); 190 } 191 if ( !fputcsv4( $fp, $objectData ) ) 192 { 193 $cli->error( "Can not write to file" ); 194 $script->shutdown( 1 ); 195 } 196 197 198 } 199 200 while ( $fp = each( $openedFPs ) ) 201 { 202 fclose( $fp ); 203 } 204 205 $script->shutdown(); 206 207 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |