-
Notifications
You must be signed in to change notification settings - Fork 241
/
ezconvertdbcharset.php
executable file
·1754 lines (1473 loc) · 60 KB
/
ezconvertdbcharset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env php
<?php
/**
* File containing the ezconvertdbcharset.php script.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
* @package kernel
*/
require_once 'autoload.php';
define( 'EZ_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_NAME', 'ezcontentclass_attribute_tmp' );
define( 'EZ_CREATE_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_SQL_MYSQL',
"
CREATE TEMPORARY TABLE " . EZ_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_NAME . " (
id int(11) NOT NULL default '0',
version int(11) NOT NULL default '0',
is_always_available int(11) NOT NULL default '0',
language_locale varchar(20) NOT NULL default '',
name varchar(255) NOT NULL default '',
PRIMARY KEY (id,version,language_locale)
)" );
// create persistent tmp table since this table is needed between connect-sessions.
define( 'EZ_CREATE_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_SQL_POSTGRESQL',
"
CREATE TABLE " . EZ_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_NAME . " (
id integer DEFAULT 0 NOT NULL,
version integer DEFAULT 0 NOT NULL,
is_always_available integer DEFAULT 0 NOT NULL,
language_locale character varying(20) DEFAULT ''::character varying NOT NULL,
name character varying(255) DEFAULT ''::character varying NOT NULL
)" );
define( 'EZ_CREATE_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_SQL_ORACLE',
"
CREATE GLOBAL TEMPORARY TABLE " . EZ_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_NAME . " (
id number(11) DEFAULT 0 NOT NULL,
version number(11) DEFAULT 0 NOT NULL,
is_always_available number(11) DEFAULT 0 NOT NULL,
language_locale varchar2(20) NOT NULL,
name varchar2(255) NOT NULL
) ON COMMIT PRESERVE ROWS;" );
define( 'EZ_DROP_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_SQL',
"DROP TABLE " . EZ_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_NAME );
/**
* Class used to store some of the command line arguments
*/
class CommandLineArguments
{
protected static $iconvCharacterSet = false;
protected static $logFilename = false;
function iconvCharacterSet()
{
return CommandLineArguments::$iconvCharacterSet;
}
function setIconvCharacterSet( $iconvCharacterSet )
{
CommandLineArguments::$iconvCharacterSet = $iconvCharacterSet;
}
function logFilename()
{
return CommandLineArguments::$logFilename;
}
function setLogFilename( $logFilename )
{
CommandLineArguments::$logFilename = $logFilename;
}
}
/**************************************************************
* 'cli->output' wrappers *
***************************************************************/
function showError( $message, $addEOL = true, $bailOut = true )
{
global $cli, $script, $eZDir;
$cli->output( $cli->stylize( 'error', "Error: " . $message ), $addEOL );
if( $bailOut )
{
chdir( $eZDir ); // since it might have changed while running...
$script->shutdown( 1 );
}
}
function showWarning( $message, $addEOL = true )
{
global $cli;
$cli->output( $cli->stylize( 'warning', "Warning: " . $message ), $addEOL );
}
function showNotice( $message, $addEOL = true )
{
global $cli;
$cli->output( $cli->stylize( 'notice', "Notice: " ) . $message, $addEOL );
}
function showMessage( $message, $addEOL = true )
{
global $cli;
$cli->output( $cli->stylize( 'blue', $message ), $addEOL );
}
function showMessage2( $message, $addEOL = true )
{
global $cli;
$cli->output( $cli->stylize( 'red', $message ), $addEOL );
}
function showMessage3( $message, $addEOL = true )
{
global $cli;
$cli->output( $message, $addEOL );
}
/*!
prompt user to choose what to do next
*/
function eZGetUserInput( $prompt )
{
echo $prompt;
$userInput = fgets( STDIN );
$userInput = trim( $userInput, "\n\r" );
return $userInput;
}
function eZExecuteShellCommand( $command, $errMessage = '', $retry = true, $ignore = false )
{
$err = 0;
do
{
$out = system( $command, $err );
if ( $err )
{
if ( $errMessage )
{
showMessage2( $errMessage );
}
if ( $retry )
{
do
{
$action = $ignore ? eZGetUserInput( "Retry? [y/n/Ignore]: " ) : eZGetUserInput( "Retry? [y/n]: " );
if ( strpos( $action, 'y' ) === 0 )
{
$continue = false;
}
elseif ( $ignore && strpos( $action, 'I' ) === 0 )
{
$continue = true;
$retry = false;
}
else
{
// default action is not to retry but to abort
showError( "Aborting..." );
}
}
while ( !$continue );
}
else
{
showError( "Aborting..." );
}
}
else
{
$retry = false;
}
}
while ( $retry );
return $err;
}
/**************************************************************
* helper functions *
***************************************************************/
/*!
process xml attributes info
\return \c false or an array of table infos.
*/
function parseXMLAttributesOption( $xmlAttributesOption )
{
if ( !$xmlAttributesOption )
{
return false;
}
$xmlAttributesInfo = array();
$xmlAttributesOption = explode( ',', $xmlAttributesOption );
foreach ( $xmlAttributesOption as $attributeTableInfoOption )
{
$attributeTableInfo = explode( '.', $attributeTableInfoOption );
switch ( count( $attributeTableInfo ) )
{
case 1:
{
$attributeTableInfo = array( 'datatype' => trim( $attributeTableInfo[0] ),
'table' => 'ezcontentobject_attribute',
'data_field' => 'data_text' );
} break;
case 3:
{
$attributeTableInfo = array( 'datatype' => trim( $attributeTableInfo[0] ),
'table' => trim( $attributeTableInfo[1] ),
'data_field' => trim( $attributeTableInfo[2] ) );
} break;
default:
{
showError( "invalid 'extra-xml-attributes' '$attributeTableInfoOption' option" );
} break;
}
$xmlAttributesInfo[] = $attributeTableInfo;
}
return $xmlAttributesInfo;
}
/*!
process custom xml data info
\retruns \c false of an array of table infos.
*/
function parseCustomXMLDataOption( $xmlCustomDataOption )
{
if ( !$xmlCustomDataOption )
{
return false;
}
$xmlCustomDataInfo = array();
$xmlCustomDataOption = explode( ',', $xmlCustomDataOption );
foreach ( $xmlCustomDataOption as $tableInfoOption )
{
$tableInfo = explode( '.', $tableInfoOption );
switch ( count( $tableInfo ) )
{
case 2:
{
$tableInfo = array( 'table' => trim( $tableInfo[0] ),
'data_field' => trim( $tableInfo[1] ) );
} break;
default:
{
showError( "invalid 'extra-xml-data' '$tableInfoOption' option" );
} break;
}
$xmlCustomDataInfo[] = $tableInfo;
}
return $xmlCustomDataInfo;
}
/*!
process custom xml data info
\returns \c false or an array of table infos.
*/
function parseCustomSerializedDataOption( $serializedCustomDataOption )
{
if ( !$serializedCustomDataOption )
{
return false;
}
$db = eZDB::instance();
$serializedDataInfo = array();
$serializedCustomDataOption = explode( ',', $serializedCustomDataOption );
foreach ( $serializedCustomDataOption as $tableInfoOption )
{
$tableInfo = explode( ';', $tableInfoOption );
if ( count( $tableInfo ) != 2 )
{
showError( "invalid 'extra-serialized-data' '$tableInfoOption' option" );
}
$dataInfo = explode( '.', $tableInfo[0] );
$keyInfo = explode( '.', $tableInfo[1] );
switch ( count( $dataInfo ) )
{
case 2:
{
$dataInfo = array( 'table' => trim( $dataInfo[0] ),
'data_field' => trim( $dataInfo[1] ) );
} break;
default:
{
showError( "invalid 'extra-serialized-data' '$tableInfoOption' option" );
} break;
}
foreach ( array_keys( $keyInfo ) as $key => $value )
{
trim( $keyInfo[$key] );
// check column exists
$result = $db->query( "SELECT " . $keyInfo[$key] . " from " . $dataInfo['table'] . " limit 1" );
if ( $result === false )
{
showError( "invalid 'extra-serialized-data' '$tableInfoOption' option" );
}
}
$serializedDataInfo[] = array( 'table' => $dataInfo['table'],
'data_field' => $dataInfo['data_field'],
'blob_field' => $dataInfo['data_field'] . "_blob",
'keys' => $keyInfo );
}
return $serializedDataInfo;
}
/*!
Check db driver
*/
function checkDBDriver()
{
$db = eZDB::instance();
$dbType = $db->databaseName();
switch( strtolower( $dbType ) )
{
case 'mysql':
case 'postgresql':
case 'oracle':
break;
default:
{
return false;
} break;
}
return true;
}
/*!
Check db charset
*/
function checkDBCharset( $iconvCharacterSet )
{
$db = eZDB::instance();
if ( $iconvCharacterSet !== false )
{
$dbCharset = $iconvCharacterSet;
showMessage3( 'Overriding iconv character set. Configured to be "' . $db->charset() . "\" in settings but will be using \"$dbCharset\" for iconv() conversion instead." );
} else
{
$dbCharset = $db->charset();
}
switch( strtolower( $dbCharset ) )
{
case 'utf8':
case 'utf-8':
{
return false;
} break;
default:
break;
}
return true;
}
/*!
DB specific checks. A string is returned for error conditions (script halts after printing it)
*/
function checkDBExtraConditions()
{
$db = eZDB::instance();
$function = "checkDBExtraConditions" . strtoupper( $db->databaseName() );
if ( function_exists( $function ) )
{
return $function();
}
return true;
}
/*!
@todo We should really check for exp_full, imp_full, (sysdba) privileges rather than DBA role...
@todo add check for RAC - do not try anything in such a case
*/
function checkDBExtraConditionsORACLE()
{
global $oracleDbaAccount, $oracleHome;
//$db = eZDB::instance();
global $db;
showMessage3( '' );
showWarning( "The procedure of upgrading the character set of an Oracle database needs manual intervention.\n".
"This upgrade script will only create a database export, and later reimport it, leaving to\n".
"the end user (you) the responsibility of changing database character set.\n".
"Please refer to Oracle documentation for instructions on altering a database character set\n".
"(it usually involves creating a new database from scratch, and it always has effect on all\n".
"schemas contained in the database)." );
showMessage3( '' );
$continue = eZGetUserInput( "Do you want to continue? (y to accept) " );
if ( $continue != 'y' && $continue != 'Y' )
{
return "Aborting";
}
$oracleDbaAccount = false;
while( $oracleDbaAccount === false )
{
// NB: we need export_full privileges for full export.
// We could need SYSDBA for connecting AS SYSDBA and altering db - IF IT WORKED...
// In that case we should try to support OSOPER connections, too...
if ( $db->isConnected() )
{
$dba = $db->arrayQuery("select default_role from user_role_privs where granted_role in ('DBA')");
//$dba = $db->arrayQuery("select sysdba from V\$PWFILE_USERS where username='".strtoupper($db->User)."'");
}
else
{
showWarning( "Supplied username/password are incorrect" );
$dba = false;
}
if (count($dba) && $dba[0]['default_role'] == 'YES')
//if (count($dba) && $dba[0]['sysdba'] == 'TRUE')
{
$oracleDbaAccount = array( 'user' => $db->User, 'password' => $db->Password );
// reconnect using standard credentials, in case we have logged in as admin
$db->close();
$db = eZDB::instance( false, false, true );
}
else
{
//$account = eZGetUserInput( "Please enter username/password of a valid SYSDBA account (return to abort): ");
$account = eZGetUserInput( "Please enter username/password of a valid DBA account (return to abort): ");
if ( $account == "" )
{
//return "The charset conversion script needs to run with an oracle user account that has SYSDBA privileges. Aborting.";
return "The charset conversion script needs to run with an oracle user account that has DBA privileges. Aborting.";
}
else
{
$account = explode( '/', $account );
if ( count( $account ) > 1 )
{
$db->close();
$db = eZDB::instance( false, array( 'user' => $account[0], 'password' => $account[1] ), true );
}
}
}
} // while
$oracleHome = getenv( 'ORACLE_HOME' );
if ( $oracleHome != "" )
{
$ok = eZGetUserInput( "Found ORACLE_HOME $oracleHome, is this the correct directory? (y to accept) ");
if ( $ok != "y" && $ok != "Y" )
{
$oracleHome = '';
}
}
$exe = eZSys::osType() == 'win32' ? '.exe' : '';
//while( !is_dir( $oracleHome ) || !file_exists( $oracleHome . '/bin/sqlplus' . $exe ) || !file_exists( $oracleHome . '/bin/imp' . $exe ) || !file_exists( $oracleHome . '/bin/exp' . $exe ) )
while( !is_dir( $oracleHome ) || !file_exists( $oracleHome . '/bin/imp' . $exe ) || !file_exists( $oracleHome . '/bin/exp' . $exe ) )
{
if ( $oracleHome != "" )
{
showWarning( "imp or exp tools not found in ORACLE_HOME $oracleHome" );
}
$oracleHome = eZGetUserInput( "Please enter the path to ORACLE_HOME, where the imp and exp tools are (return to abort): ");
if ( $oracleHome == "" )
{
return "The charset conversion script needs to run with an oracle home where the imp and exp tools are available. Aborting.";
}
}
return true;
}
/**************************************************************
* handle content class / content class attributes *
***************************************************************/
function createContentClassAttributeTempTable()
{
$db = eZDB::instance();
$sql = 'EZ_CREATE_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_SQL_' . strtoupper( $db->databaseName() );
$sql = constant( $sql );
$db->query( $sql );
}
function dropContentClassAttributeTempTable()
{
$db = eZDB::instance();
$sql = EZ_DROP_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_SQL;
$db->query( $sql );
}
function unserializeContentClassAttributeNames()
{
$db = eZDB::instance();
$attributeName = new eZSerializedObjectNameList();
$limit = 100;
$offset = 0;
$selectSQL = "SELECT id, version, serialized_name_list FROM ezcontentclass_attribute ORDER BY id, version";
while ( $result = $db->arrayQuery( $selectSQL , array( 'limit' => $limit, 'offset' => $offset ) ) )
{
foreach ( $result as $row )
{
//showMessage3( "id: '" . $row['id'] . "' version: '" . $row['version'] . "'" );
$attributeName->initFromSerializedList( $row['serialized_name_list'] );
$nameList = $attributeName->cleanNameList();
$alwaysAvailableLocale = $attributeName->alwaysAvailableLanguageLocale();
$insertSQL = 'INSERT INTO ' . EZ_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_NAME . '(id, version, is_always_available, language_locale, name) VALUES (' .
"{$row['id']}, {$row['version']}";
foreach ( $nameList as $locale => $name )
{
$isAlwaysAvailable = ( $locale == $alwaysAvailableLocale ) ? 1 : 0;
$sql = $insertSQL . ", $isAlwaysAvailable, '" . $db->escapeString( $locale) . "', '" . $db->escapeString( $name ) . "')";
$db->query( $sql );
}
}
$offset += $limit;
}
}
function serializeContentClassNames()
{
$selectSQL = "SELECT contentclass_id as id," .
" contentclass_version as version," .
" language_id as is_always_available," .
" language_locale, name " .
"FROM ezcontentclass_name " .
"ORDER BY id, version";
$table = 'ezcontentclass';
serializeNames( $selectSQL, $table );
}
function serializeContentClassAttributeNames()
{
$selectSQL = "SELECT * FROM " . EZ_CONTENTCLASS_ATTRIBUTE_TMP_TABLE_NAME . " ORDER BY id, version";
$table = 'ezcontentclass_attribute';
serializeNames( $selectSQL, $table );
}
function serializeNames( $selectSQL, $storeToTable )
{
$db = eZDB::instance();
$limit = 100;
$offset = 0;
//$selectSQL .= "\nLIMIT $limit";
while ( $result = $db->arrayQuery( $selectSQL , array( 'limit' => $limit, 'offset' => $offset ) ) )
{
// since name data is splitted between rows,
// need to adjust selected data:
// exclude the last id/version and process it during next 'select' iteration
// 1. get last id/version pair
$lastIdx = count( $result ) - 1;
if ( $lastIdx > 0 )
{
$lastID = $result[$lastIdx]['id'];
$lastVersion = $result[$lastIdx]['version'];
// 2. check remained data
for ( $lastIdx = $lastIdx - 1; $lastIdx >= 0; $lastIdx-- )
{
$row = $result[$lastIdx];
if ( $lastID != $row['id'] || $lastVersion != $row['version'] )
{
break;
}
}
// 3. check whether $lastIdx is valid
if ( $lastIdx < 0 )
{
// all selected data belongs to the same id/version
$lastIdx = count( $result ) - 1;
}
}
// 4. adjust offset to include excluded data to the next 'select'
$offset += $lastIdx + 1;
// process selected data
$serializedName = false;
$prevId = false;
$prevVersion = false;
for ( $idx = 0; $idx <= $lastIdx; $idx++ )
{
if( !isset( $result[$idx] ) )
continue;
$row = $result[$idx];
// check whether serialized name is completely assembled
if ( $prevId != $row['id'] || $prevVersion != $row['version'] )
{
if ( $serializedName !== false )
{
// store serialized name
storeSerializedName( $serializedName, $prevId, $prevVersion, $storeToTable );
}
// create new $serializedName to collect data
$serializedName = new eZSerializedObjectNameList();
$serializedName->resetNameList();
}
$prevId = $row['id'];
$prevVersion = $row['version'];
$serializedName->setNameByLanguageLocale( $row['name'], $row['language_locale'] );
if ( $row['is_always_available'] & 1 )
{
$serializedName->setAlwaysAvailableLanguage( $row['language_locale'] );
}
if ( $idx == $lastIdx )
{
// no more date => store serialized name
storeSerializedName( $serializedName, $prevId, $prevVersion, $storeToTable );
}
}
}
}
function storeSerializedName( $serializedName, $id, $version, $table )
{
if ( $serializedName instanceof eZSerializedObjectNameList )
{
$serializedNameString = $serializedName->serializeNames();
$db = eZDB::instance();
$updateSQL = "UPDATE $table " .
"SET serialized_name_list = '" . $db->escapeString( $serializedNameString ) . "' " .
"WHERE id = $id AND version = $version";
$db->query( $updateSQL );
}
}
/**************************************************************
* handle custom serialized data *
***************************************************************/
/*!
Logic:
- create binary column as temp storage to not loose data when table will be converted
- get original data
- unseiazlize
- convert data to utf-8
- serialize
- store data in binary column
*/
function convertSerializedData( $serializedDataInfo )
{
if ( !is_array( $serializedDataInfo ) )
{
return;
}
$db = eZDB::instance();
// create blob column
$function = "createBLOBColumn" . strtoupper( $db->databaseName() );
if ( function_exists( $function ) )
{
foreach ( $serializedDataInfo as $tableInfo )
{
$function( $tableInfo );
}
}
else
{
showError( "no function to create BLOB column" );
}
// convert data
$dbEncoding = strtolower( CommandLineArguments::iconvCharacterSet() );
foreach ( $serializedDataInfo as $tableInfo )
{
showMessage3( $tableInfo['table'] . '.' . $tableInfo['data_field'] );
$limit = 100;
$offset = 0;
$keysString = implode( ', ', $tableInfo['keys'] );
$dataFieldName = $tableInfo['data_field'];
$selectSQL = "SELECT " . $keysString . ', ' . $dataFieldName .
" FROM " . $tableInfo['table'];
//" LIMIT $limit";
while ( $result = $db->arrayQuery( $selectSQL, array( 'limit' => $limit, 'offset' => $offset ) ) )
{
foreach ( $result as $row )
{
$data = unserialize( $row[$dataFieldName] );
if ( !$data )
{
// nothing to do
continue;
}
$data = convertArray( $data, $dbEncoding, 'utf8' );
$data = serialize( $data );
$whereSql = '';
foreach ( $tableInfo['keys'] as $key )
{
if ( $whereSql != '' )
{
$whereSql .= " AND ";
}
$whereSql .= "$key = " . $row[$key];
}
$updateSql = "UPDATE " . $tableInfo['table'] .
" SET " . $tableInfo['blob_field'] . " = '" . $data . "'" .
" WHERE $whereSql";
$db->query( $updateSql );
}
$offset += $limit;
}
}
}
/*!
Restore data from binary column
*/
function restoreSerializedData( $serializedDataInfo )
{
if ( !is_array( $serializedDataInfo ) )
{
return;
}
$db = eZDB::instance();
foreach ( $serializedDataInfo as $tableInfo )
{
$sql = "UPDATE " . $tableInfo['table'] .
" SET " . $tableInfo['data_field'] . ' = ' . $tableInfo['blob_field'];
$db->query( $sql );
}
}
function dropBLOBColumns( $serializedDataInfo )
{
if ( !is_array( $serializedDataInfo ) )
{
return;
}
foreach ( $serializedDataInfo as $tableInfo )
{
dropBLOBColumn( $tableInfo );
}
}
function convertArray( $array, $inCharset, $outCharset )
{
if ( !is_array( $array ) )
{
showError( "convertArray: not an array was passed" );
var_dump( $array );
return;
}
foreach ( array_keys( $array ) as $key )
{
$value = $array[$key];
if ( is_string( $value ) )
{
// First check if $inCharset is correct
$valueTest = iconv( $inCharset, $inCharset, $value );
if( strlen( $valueTest ) <> strlen( $value ) )
{
if( CommandLineArguments::logFilename() !== false )
{
$logString = "ERROR: Unable to predict correct character set while converting array. Value is $value, inCharset : $inCharset, outCharset : $outCharset\n";
file_put_contents( CommandLineArguments::logFilename(), $logString, FILE_APPEND );
}
}
$array[$key] = iconv( $inCharset, $outCharset . '//TRANSLIT', $value );
}
else if ( is_array( $value ) )
{
$array[$key] = convertArray( $value, $inCharset, $outCharset );
}
else
{
//nothing to do
}
}
return $array;
}
function createBLOBColumnMYSQL( $tableInfo )
{
$db = eZDB::instance();
$query = "ALTER TABLE " . $tableInfo['table'] . " ADD COLUMN " . $tableInfo['blob_field'] . " BLOB";
$db->query( $query );
}
function createBLOBColumnPOSTGRESQL( $tableInfo )
{
}
function createBLOBColumnORACLE( $tableInfo )
{
$db = eZDB::instance();
$query = "ALTER TABLE " . $tableInfo['table'] . " ADD " . $tableInfo['blob_field'] . " BLOB";
$db->query( $query );
}
function dropBLOBColumn( $tableInfo )
{
$db = eZDB::instance();
$query = "ALTER TABLE " . $tableInfo['table'] . " DROP COLUMN " . $tableInfo['blob_field'];
$db->query( $query );
}
/**************************************************************
* handle xml data *
***************************************************************/
function convertXMLDatatypes( $tableInfoList )
{
foreach ( $tableInfoList as $tableInfo )
{
showMessage3( " converting '" . $tableInfo['datatype'] . "': " . $tableInfo['table'] . "." . $tableInfo['data_field'] );
convertXMLData( $tableInfo, "xmlDatatypeSelectSQL", "xmlDatatypeUpdateSQL", "convertXMLDatatypeProgress" );
}
}
function convertCustomXMLData( $tableInfoList )
{
foreach ( $tableInfoList as $tableInfo )
{
showMessage3( " converting: '" . $tableInfo['table'] . "." . $tableInfo['data_field'] . "' table" );
convertXMLData( $tableInfo, "xmlCustomDataSelectSQL", "xmlCustomDataUpdateSQL", "convertXMLCustomDataProgress" );
}
}
function xmlDatatypeSelectSQL( $dataTableInfo )
{
$table = $dataTableInfo['table'];
$data_field = $dataTableInfo['data_field'];
$datatype = $dataTableInfo['datatype'];
$selectSQL = "SELECT id, version, $data_field as xml_data " .
"FROM $table " .
"WHERE $data_field LIKE '<?xml%' " .
"AND data_type_string = '$datatype' " .
"ORDER BY id, version";
return $selectSQL;
}
function xmlDatatypeUpdateSQL( $dataTableInfo, $row )
{
$db = eZDB::instance();
$table = $dataTableInfo['table'];
$data_field = $dataTableInfo['data_field'];
$updateSQL = "UPDATE $table " .
"SET $data_field = '" . $db->escapeString( $row['xml_data'] ) . "' " .
"WHERE id = " . $row['id'] . " " .
"AND version = " . $row['version'];
return $updateSQL;
}
function convertXMLDatatypeProgress( $row )
{
//showMessage3( "id: '" . $row['id'] . "' version: '" . $row['version'] . "'" );
}
function xmlCustomDataSelectSQL( $dataTableInfo )
{
$table = $dataTableInfo['table'];
$data_field = $dataTableInfo['data_field'];
$selectSQL = "SELECT id, $data_field as xml_data " .
"FROM $table " .
"WHERE $data_field LIKE '<?xml%' " .
"ORDER BY id";
return $selectSQL;
}
function xmlCustomDataUpdateSQL( $dataTableInfo, $row )
{
$db = eZDB::instance();
$table = $dataTableInfo['table'];
$data_field = $dataTableInfo['data_field'];
$updateSQL = "UPDATE $table " .
"SET $data_field = '" . $db->escapeString( $row['xml_data'] ) . "' " .
"WHERE id = " . $row['id'];
return $updateSQL;
}
function convertXMLCustomDataProgress( $row )
{
showMessage3( "id: '" . $row['id'] );
}
/**
* For some reason, some utf8 encoded text stored in the db might contain
* illegal utf8 characters.
* This function will strip/replace such known characters
*/
function removeIllegalUTF8Characters( $text )
{
// 0xE2 0x80 0x3F seems to be some kind of quote, replacing it with '
// 0x3F is acutally a "?" and needs to be escaped
return preg_replace( "#\xE2\x80\\\x3F#", "'", $text );
}
/*!
Convert xml text to db's charset. However for optimization the xml processing instruction 'encoding' will be set
to utf-8.
*/
function convertXMLData( $tableInfo, $xmlDataSelectSQLFunction, $xmlDataUpdateSQLFunction, $convertXMLProgressFunction )
{
$db = eZDB::instance();
$dbEncoding = strtolower( CommandLineArguments::iconvCharacterSet() );
$limit = 500;
$offset = 0;
$selectSQL = $xmlDataSelectSQLFunction( $tableInfo );
//$selectSQL .= "\nLIMIT $limit";
while ( $result = $db->arrayQuery( $selectSQL, array( 'limit' => $limit, 'offset' => $offset ) ) )
{
foreach ( $result as $row )
{
$convertXMLProgressFunction( $row );
$xmlString = $row['xml_data'];