|
eZ Publish
[4.0]
|
00001 #!/usr/bin/env php 00002 <?php 00003 // 00004 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00005 // SOFTWARE NAME: eZ Publish 00006 // SOFTWARE RELEASE: 4.0.x 00007 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00008 // SOFTWARE LICENSE: GNU General Public License v2.0 00009 // NOTICE: > 00010 // This program is free software; you can redistribute it and/or 00011 // modify it under the terms of version 2.0 of the GNU General 00012 // Public License as published by the Free Software Foundation. 00013 // 00014 // This program is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of version 2.0 of the GNU General 00020 // Public License along with this program; if not, write to the Free 00021 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00022 // MA 02110-1301, USA. 00023 // 00024 // 00025 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00026 // 00027 00028 require 'autoload.php'; 00029 00030 include_once( 'kernel/classes/ezscript.php' ); 00031 include_once( 'lib/ezutils/classes/ezcli.php' ); 00032 00033 $cli = eZCLI::instance(); 00034 00035 $scriptSettings = array(); 00036 $scriptSettings['description'] = 'Convert attributes of the type ezenum to ezselection'; 00037 $scriptSettings['use-session'] = true; 00038 $scriptSettings['use-modules'] = true; 00039 $scriptSettings['use-extensions'] = true; 00040 00041 $script = eZScript::instance( $scriptSettings ); 00042 $script->startup(); 00043 00044 $config = '[preview]'; 00045 $argumentConfig = '[ATTRIBUTE_ID]'; 00046 $optionHelp = array( 'preview' => 'show a preview, do not really make changes' ); 00047 $arguments = false; 00048 $useStandardOptions = true; 00049 00050 $options = $script->getOptions( $config, $argumentConfig, $optionHelp, $arguments, $useStandardOptions ); 00051 $script->initialize(); 00052 00053 if ( count( $options['arguments'] ) != 1 ) 00054 { 00055 $script->shutdown( 1, 'wrong argument count' ); 00056 } 00057 00058 $preview = !is_null( $options['preview'] ); 00059 00060 $attributeID = $options['arguments'][0]; 00061 00062 include_once( 'kernel/classes/ezcontentobjecttreenode.php' ); 00063 include_once( 'kernel/classes/ezcontentclassattribute.php' ); 00064 00065 if ( !is_numeric( $attributeID ) ) 00066 { 00067 $attributeID = eZContentObjectTreeNode::classAttributeIDByIdentifier( $attributeID ); 00068 00069 if ( $attributeID === false ) 00070 { 00071 $script->shutdown( 2, 'unknown attribute identifier' ); 00072 } 00073 } 00074 00075 $classAttribute = eZContentClassAttribute::fetch( $attributeID ); 00076 00077 if ( !is_object( $classAttribute ) ) 00078 { 00079 $script->shutdown( 3, 'could not find a class attribute with the specified ID' ); 00080 } 00081 00082 $enum = $classAttribute->attribute( 'content' ); 00083 00084 // both datatypes use data_int1 for the multiple flag, so the following code is not necessary 00085 /* 00086 $isMultiple = $enum->attribute( 'enum_ismultiple' ); 00087 $classAttribute->setAttribute( 'data_int1', $isMultiple ); 00088 */ 00089 00090 //var_dump( $enum ); 00091 00092 $enumValues = $enum->attribute( 'enum_list' ); 00093 00094 $oldOptions = array(); 00095 $oldToNewIDMap = array(); 00096 00097 foreach ( $enumValues as $enumValue ) 00098 { 00099 $element = $enumValue->attribute( 'enumelement' ); 00100 $id = $enumValue->attribute( 'id' ); 00101 $oldOptions[$id] = $element; 00102 } 00103 00104 $doc = new DOMDocument( '1.0', 'utf-8' ); 00105 $root = $doc->createElement( "ezselection" ); 00106 $doc->appendChild( $root ); 00107 00108 $options = $doc->createElement( "options" ); 00109 00110 $root->appendChild( $options ); 00111 $i = 0; 00112 foreach ( $oldOptions as $enumValueID => $name ) 00113 { 00114 $optionNode = $doc->createElement( "option" ); 00115 $optionNode->setAttribute( 'id', $i ); 00116 $optionNode->setAttribute( 'name', $name ); 00117 $oldToNewIDMap[$enumValueID] = $i; 00118 $i ++; 00119 00120 $options->appendChild( $optionNode ); 00121 } 00122 00123 $xml = $doc->saveXML(); 00124 00125 include_once( 'lib/ezdb/classes/ezdb.php' ); 00126 $db = eZDB::instance(); 00127 $db->begin(); 00128 00129 $classAttribute->setAttribute( 'data_text5', $xml ); 00130 $classAttribute->setAttribute( 'data_type_string', 'ezselection' ); 00131 $classAttribute->store(); 00132 00133 include_once( 'kernel/classes/ezcontentobjectattribute.php' ); 00134 $attributes = eZContentObjectAttribute::fetchSameClassAttributeIDList( $classAttribute->attribute( 'id' ) ); 00135 00136 $count = count( $attributes ); 00137 $cli->output( 'number of object attributes to convert: ' . $count ); 00138 00139 $script->setIterationData( '.', '~' ); 00140 $script->resetIteration( $count ); 00141 for ( $i = 0; $i < $count; $i++ ) 00142 { 00143 $objectAttributeID = $attributes[$i]->attribute( 'id' ); 00144 $objectAttributeVersion = $attributes[$i]->attribute( 'version' ); 00145 00146 $selectOptions = array(); 00147 $enumContent = $attributes[$i]->attribute( 'content' ); 00148 $enumObjectValues = $enumContent->attribute( 'enumobject_list' ); 00149 00150 foreach ( $enumObjectValues as $enumObjectValue ) 00151 { 00152 $value = $enumObjectValue->attribute( 'enumvalue' ); 00153 $element = $enumObjectValue->attribute( 'enumelement' ); 00154 $enumValueID = $enumObjectValue->attribute( 'enumid' ); 00155 if ( !array_key_exists( $enumValueID, $oldToNewIDMap ) ) 00156 { 00157 $cli->error( 'old enum element ' . $element . ' with value ' . $value . ' does not exist in current options' ); 00158 $db->rollback(); 00159 $script->shutdown( 4 ); 00160 } 00161 00162 $selectOptions[] = $oldToNewIDMap[$enumValueID]; 00163 } 00164 00165 $idString = ( is_array( $selectOptions ) ? implode( '-', $selectOptions ) : "" ); 00166 //eZDebug::writeDebug( $idString, 'id string' ); 00167 00168 // reset object attribute's virtual content attribute 00169 $attributes[$i]->Content = null; 00170 00171 if ( !$preview ) 00172 { 00173 $attributes[$i]->setAttribute( 'data_type_string', 'ezselection' ); 00174 $attributes[$i]->setAttribute( 'data_text', $idString ); 00175 $attributes[$i]->store(); 00176 } 00177 00178 $status = true; 00179 $text = 'converted attribute in object ' . $attributes[$i]->attribute( 'contentobject_id' ) . ', version ' . $attributes[$i]->attribute( 'version' ); 00180 00181 if ( !$preview ) 00182 { 00183 $db->query( "DELETE FROM ezenumobjectvalue WHERE contentobject_attribute_id=$objectAttributeID AND contentobject_attribute_version=$objectAttributeVersion" ); 00184 } 00185 00186 $script->iterate( $cli, $status, $text ); 00187 } 00188 00189 if ( !$preview ) 00190 { 00191 $db->query( "DELETE FROM ezenumvalue WHERE contentclass_attribute_id=$attributeID" ); 00192 $db->commit(); 00193 } 00194 else 00195 { 00196 $db->rollback(); 00197 } 00198 00199 $script->shutdown( 0 ); 00200 00201 ?>