/
DBUpdate-to-5.pl
executable file
·3561 lines (2735 loc) · 129 KB
/
DBUpdate-to-5.pl
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/perl
# --
# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
# --
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
use strict;
use warnings;
# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . '/Kernel/cpan-lib';
use Getopt::Std qw();
use Kernel::Config;
use Kernel::System::Cache;
use Kernel::System::ObjectManager;
use Kernel::System::Package;
use Kernel::System::SysConfig;
use Kernel::System::VariableCheck qw(:all);
local $Kernel::OM = Kernel::System::ObjectManager->new(
'Kernel::System::Log' => {
LogPrefix => 'OTRS-DBUpdate-to-5.pl',
},
);
{
# get options
my %Opts;
Getopt::Std::getopt( 'h', \%Opts );
if ( exists $Opts{h} ) {
print <<"EOF";
DBUpdate-to-5.pl - Upgrade script for OTRS 4 to 5 migration.
Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
Usage: $0 [-h]
Options are as follows:
-h display this help
EOF
exit 1;
}
# UID check
if ( $> == 0 ) { # $EFFECTIVE_USER_ID
die "
Cannot run this program as root.
Please run it as the 'otrs' user or with the help of su:
su -c \"$0\" -s /bin/bash otrs
";
}
# enable auto-flushing of STDOUT
$| = 1; ## no critic
# define tasks and their messages
my @Tasks = (
{
Message => 'Refresh configuration cache',
Command => \&RebuildConfig,
},
{
Message => 'Check framework version',
Command => \&_CheckFrameworkVersion,
},
{
Message => 'Migrate Database Column Types',
Command => \&_MigrateDatabaseColumnTypes,
},
{
Message => 'Migrate charset to UTF-8 on auto_response table',
Command => \&_MigrateCharsetAndDeleteCharsetColumnsAutoResponse,
},
{
Message => 'Migrate charset to UTF-8 on notification_event table',
Command => \&_MigrateCharsetAndDeleteCharsetColumnsNotificationEvent,
},
{
Message => 'Migrate event based notifications to support multiple languages',
Command => \&_MigrateEventNotificationsToMultiLanguage,
},
{
Message => 'Migrate notifications to event based notifications',
Command => \&_MigrateNotifications,
},
{
Message => 'Migrate send notification user preferences',
Command => \&_MigrateUserNotificationPreferences,
},
{
Message => 'Add Email notification method to all event based notifications',
Command => \&_AddEmailNotificationMethod,
},
{
Message => 'Migrate Output configurations to the new module locations',
Command => \&_MigrateConfigs,
},
{
Message => 'Add TicketZoom menu cluster configurations',
Command => \&_AddZoomMenuClusters,
},
{
Message => 'Fix wrong entries in module registrations',
Command => \&_FixModuleRegistration,
},
{
Message => 'Fixup statistics time field configuration where the time interval is too small',
Command => \&_FixupStatsTimeInterval,
},
{
Message => 'Fixup wrong notification tags',
Command => \&_FixNotificationTags,
},
{
Message => 'Fixup dashboard statistics output format configuration',
Command => \&_FixupDashboardStatsFormats,
},
{
Message => 'Fix user preference keys',
Command => \&_FixUserPreferenceKeys,
},
{
Message => 'Uninstall Merged Feature Add-Ons',
Command => \&_UninstallMergedFeatureAddOns,
},
{
Message => 'Clean up the cache',
Command => sub {
$Kernel::OM->Get('Kernel::System::Cache')->CleanUp();
},
},
{
Message => 'Refresh configuration cache another time',
Command => \&RebuildConfig,
},
);
print "\nMigration started...\n\n";
# get the number of total steps
my $Steps = scalar @Tasks;
my $Step = 1;
for my $Task (@Tasks) {
# show task message
print "Step $Step of $Steps: $Task->{Message}...";
# run task command
if ( &{ $Task->{Command} } ) {
print "done.\n\n";
}
else {
print "error.\n\n";
die;
}
$Step++;
}
print "Migration completed!\n";
exit 0;
}
=item RebuildConfig()
refreshes the configuration to make sure that a ZZZAAuto.pm is present
after the upgrade.
RebuildConfig();
=cut
sub RebuildConfig {
my $SysConfigObject = Kernel::System::SysConfig->new();
# Rebuild ZZZAAuto.pm with current values
if ( !$SysConfigObject->WriteDefault() ) {
die "Error: Can't write default config files!";
}
# Force a reload of ZZZAuto.pm and ZZZAAuto.pm to get the new values
for my $Module ( sort keys %INC ) {
if ( $Module =~ m/ZZZAA?uto\.pm$/ ) {
delete $INC{$Module};
}
}
# reload config object
print "\nIf you see warnings about 'Subroutine Load redefined', that's fine, no need to worry!\n";
# create common objects with new default config
$Kernel::OM->ObjectsDiscard();
return 1;
}
=item _CheckFrameworkVersion()
Check if framework it's the correct one for Dynamic Fields migration.
_CheckFrameworkVersion();
=cut
sub _CheckFrameworkVersion {
my $Home = $Kernel::OM->Get('Kernel::Config')->Get('Home');
# load RELEASE file
if ( -e !"$Home/RELEASE" ) {
die "Error: $Home/RELEASE does not exist!";
}
my $ProductName;
my $Version;
if ( open( my $Product, '<', "$Home/RELEASE" ) ) { ## no critic
while (<$Product>) {
# filtering of comment lines
if ( $_ !~ /^#/ ) {
if ( $_ =~ /^PRODUCT\s{0,2}=\s{0,2}(.*)\s{0,2}$/i ) {
$ProductName = $1;
}
elsif ( $_ =~ /^VERSION\s{0,2}=\s{0,2}(.*)\s{0,2}$/i ) {
$Version = $1;
}
}
}
close($Product);
}
else {
die "Error: Can't read $Home/RELEASE: $!";
}
if ( $ProductName ne 'OTRS' ) {
die "Error: No OTRS system found";
}
if ( $Version !~ /^5\.0(.*)$/ ) {
die "Error: You are trying to run this script on the wrong framework version $Version!";
}
return 1;
}
=item _MigrateDatabaseColumnTypes()
Migrate the column type from INTEGER to BIGINT for all columns on all tables where it was originally
defined (applies only to PostgreSQL databases).
_MigrateDatabaseColumnTypes();
=cut
sub _MigrateDatabaseColumnTypes {
# get database object
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
# do nothing if current DB is not postgressql
return 1 if $DBObject->GetDatabaseFunction('Type') ne 'postgresql';
# read otrs schema file
my $Home = $Kernel::OM->Get('Kernel::Config')->Get('Home');
my $SchemaFile = $Kernel::OM->Get('Kernel::System::Main')->FileRead(
Location => "$Home/scripts/database/otrs-schema.xml",
Result => 'ARRAY',
);
# get needed SQL statements to update affected columns
my @SQL = _GetColumnUpdateSQL($SchemaFile);
# get package object
my $PackageObject = $Kernel::OM->Get('Kernel::System::Package');
# get a list of all installed packages (only the meta-data as content is parsed in this function)
my @PackageList = $PackageObject->RepositoryList(
Result => 'short',
);
# check installed packages
PACKAGE:
for my $Package (@PackageList) {
# skip package if it does not have the needed meta-data to get its contents
next PACKAGE if !$Package->{Name};
next PACKAGE if !$Package->{Version};
# get package XML content
my $PackageContent = $PackageObject->RepositoryGet(
%{$Package},
);
# skip package if it does not have DatabaseInstall section
next PACKAGE if $PackageContent !~ m{<DatabaseInstall [^>]+ >}msxig;
# remove any other sections in the file but DatabaseInstall
$PackageContent =~ s{(?: .*) ( <DatabaseInstall [^>]+ > (.*) </DatabaseInstall> ) (?: .*) }{$1}msxig;
# skip if resulting content is empty
next PACKAGE if !$PackageContent;
# convert columns tags into self closing from <Column ...></Column ...> to <Column .../>
$PackageContent =~ s{( <Column (?:Change|Add)? [^>]+ ) (>) </Column (?:Change|Add)? >}{$1/$2}msxig;
# convert package content into an array (be sure to keep the line feed)
my @Schema = map { $_ . "\n" } split /\n/, $PackageContent;
# get needed SQL statements to update affected columns
my @PackageSQL = _GetColumnUpdateSQL( \@Schema );
# skip if there are no SQL statements
next PACKAGE if !@PackageSQL;
# join the SQL statements with the ones from the framework and other packages
@SQL = ( @SQL, @PackageSQL );
}
# execute SQL
for my $SQL (@SQL) {
my $Success = $DBObject->Do( SQL => $SQL );
if ( !$Success ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Error during execution of '$SQL'!",
);
return;
}
}
return 1;
}
=item _GetColumnUpdateSQL
Gets the SQL statements to update a column type to BIGINT from an XML schema string.
my @SQL _GetColumnUpdateSQL(
$XMLContent, # Database table and column definitions
# (e.g the contents of otrs.schema.xml).
);
=cut
sub _GetColumnUpdateSQL {
my $XML = shift;
# define a top level tag
my $SourceXML = "<Database>\n";
my $Table;
my $SaveTable;
# check each line
LINE:
for my $Line ( @{$XML} ) {
# skip if line is not a table start or end tag or if is not a column tag
next LINE if $Line !~ m{< /? (?: Table | Column)}msx;
# skip line if is a column tag but the type is not BIGINT
next LINE if $Line =~ m{<Column}msx && $Line !~ m (BIGINT)msx;
# remember line as a part of the whole table tag (opening table tag or column tag)
$Table .= $Line;
# got to next line if current is an opening table tag
next LINE if $Line =~ m{<Table}msxi;
# check if line is a BIGINT column and mark table for save and go to next line
if ( $Line =~ m{BIGINT} ) {
$SaveTable = 1;
next LINE;
}
# otherwise (closing table tag), save table if it has at least a column
if ($SaveTable) {
$SourceXML .= $Table;
}
# reset for next table
$Table = '';
$SaveTable = 0;
}
# convert <Table ...> and <TableCreate...> tags into <TableAlter ...> tags
# preserving the rest of the attributes
$SourceXML =~ s{(<Table) (?:Create|Alter)? ([^>]+)}{$1Alter$2}msxig;
# convert </Table> and </TableCreate> tags into </TableAlter> tags
$SourceXML =~ s{(</Table) (?:Create|Alter)?}{$1Alter}msxig;
# convert <Column ... Name="###" ...> and <ColumnAdd ... Name="###" ...> tags into
# <ColumnChange ... NameOld="###" NameNew="###" ...> tags preserving the rest of the attributes
$SourceXML =~ s{(<Column) (?:Add)? (.*) Name(="[^"]+") ([^>]+) >}{$1Change$2 NameOld$3 NameNew$3$4>}mxig;
# close top level tag
$SourceXML .= '</Database>';
# parse XML update schema
my @XMLARRAY = $Kernel::OM->Get('Kernel::System::XML')->XMLParse(
String => $SourceXML,
);
# get database object
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
my @SQL;
# create database specific SQL
push @SQL, $DBObject->SQLProcessor(
Database => \@XMLARRAY,
);
my @SQLPost;
# create database specific PostSQL
push @SQLPost, $DBObject->SQLProcessorPost();
return ( @SQL, @SQLPost );
}
=item _MigrateCharsetAndDeleteCharsetColumnsAutoResponse()
Migrate the charset of the auto_response table and the notification_event table to UTF-8 and delete the charset columns.
_MigrateCharsetAndDeleteCharsetColumnsAutoResponse();
=cut
sub _MigrateCharsetAndDeleteCharsetColumnsAutoResponse {
# get database object
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
my $ErrorOutput;
my $CharsetIsPresent = 1;
# use do for restoring STDERR
do {
local *STDERR;
open STDERR, '>:utf8', \$ErrorOutput; ## no critic
# sanity check to verify the charset column is present
$CharsetIsPresent = $DBObject->Prepare(
SQL => 'SELECT charset FROM auto_response',
);
};
return 1 if !$CharsetIsPresent;
# read auto_response table
return if !$DBObject->Prepare(
SQL => 'SELECT id, text0, text1, charset FROM auto_response',
);
# get encode object
my $EncodeObject = $Kernel::OM->Get('Kernel::System::Encode');
# read the auto responses and store in array
my @AutoResponses;
ROW:
while ( my @Row = $DBObject->FetchrowArray() ) {
my %Data = (
ID => $Row[0],
Text0 => $Row[1],
Text1 => $Row[2],
Charset => $Row[3],
);
# convert body
$Data{Text0} = $EncodeObject->Convert(
Text => $Data{Text0},
From => $Data{Charset},
To => 'utf-8',
Check => 1,
);
# convert subject
$Data{Text1} = $EncodeObject->Convert(
Text => $Data{Text1},
From => $Data{Charset},
To => 'utf-8',
Check => 1,
);
# skip if nothing was changed and charset column is already in utf-8
if ( ( $Data{Text0} eq $Row[1] ) && ( $Data{Text1} eq $Row[2] ) && ( $Data{Charset} eq 'utf-8' ) ) {
next ROW;
}
# update charset column to utf-8
$Data{Charset} = 'utf-8';
push @AutoResponses, \%Data;
}
# write converted responses back to database
for my $AutoResponse (@AutoResponses) {
# update the database
return if !$DBObject->Do(
SQL => 'UPDATE auto_response SET text0 = ?, text1 = ?, charset = ? WHERE id = ?',
Bind => [
\$AutoResponse->{Text0},
\$AutoResponse->{Text1},
\$AutoResponse->{Charset},
\$AutoResponse->{ID},
],
);
}
# drop charset column from auto_response table
my $XMLString = '
<TableAlter Name="auto_response">
<ColumnDrop Name="charset"/>
</TableAlter>';
my @SQL;
my @SQLPost;
my $XMLObject = $Kernel::OM->Get('Kernel::System::XML');
# create database specific SQL and PostSQL commands
my @XMLARRAY = $XMLObject->XMLParse( String => $XMLString );
# create database specific SQL
push @SQL, $DBObject->SQLProcessor(
Database => \@XMLARRAY,
);
# create database specific PostSQL
push @SQLPost, $DBObject->SQLProcessorPost();
# execute SQL
for my $SQL ( @SQL, @SQLPost ) {
my $Success = $DBObject->Do( SQL => $SQL );
if ( !$Success ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Error during execution of '$SQL'!",
);
return;
}
}
return 1;
}
=item _MigrateCharsetAndDeleteCharsetColumnsNotificationEvent()
Migrate the charset of the auto_response table and the notification_event table to UTF-8 and delete the charset columns.
_MigrateCharsetAndDeleteCharsetColumnsNotificationEvent();
=cut
sub _MigrateCharsetAndDeleteCharsetColumnsNotificationEvent {
# get database object
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
my $ErrorOutput;
my $CharsetIsPresent = 1;
# use do for restoring STDERR
do {
local *STDERR;
open STDERR, '>:utf8', \$ErrorOutput; ## no critic
# sanity check to verify the charset column is present
$CharsetIsPresent = $DBObject->Prepare(
SQL => 'SELECT charset FROM notification_event',
);
};
return 1 if !$CharsetIsPresent;
# get encode object
my $EncodeObject = $Kernel::OM->Get('Kernel::System::Encode');
# read notification_event
return if !$DBObject->Prepare(
SQL => 'SELECT id, subject, text, charset FROM notification_event',
);
# read the notification_event entries and store in array
my @NotificationsEvent;
ROW:
while ( my @Row = $DBObject->FetchrowArray() ) {
my %Data = (
ID => $Row[0],
Subject => $Row[1],
Body => $Row[2],
Charset => $Row[3],
);
# convert subject
$Data{Subject} = $EncodeObject->Convert(
Text => $Data{Subject},
From => $Data{Charset},
To => 'utf-8',
Check => 1,
);
# convert body
$Data{Body} = $EncodeObject->Convert(
Text => $Data{Body},
From => $Data{Charset},
To => 'utf-8',
Check => 1,
);
# skip if nothing was changed and charset column is already in utf-8
if ( ( $Data{Subject} eq $Row[1] ) && ( $Data{Body} eq $Row[2] ) && ( $Data{Charset} eq 'utf-8' ) ) {
next ROW;
}
# update Charset column to utf-8
$Data{Charset} = 'utf-8';
push @NotificationsEvent, \%Data;
}
# write converted event notifications back to database
for my $Notification (@NotificationsEvent) {
# update the database
return if !$DBObject->Do(
SQL => 'UPDATE notification_event SET subject = ?, text = ?, charset = ? WHERE id = ?',
Bind => [
\$Notification->{Subject},
\$Notification->{Body},
\$Notification->{Charset},
\$Notification->{ID},
],
);
}
# drop charset column from notification_event table
my $XMLString = '
<TableAlter Name="notification_event">
<ColumnDrop Name="charset"/>
</TableAlter>';
my @SQL;
my @SQLPost;
my $XMLObject = $Kernel::OM->Get('Kernel::System::XML');
# create database specific SQL and PostSQL commands
my @XMLARRAY = $XMLObject->XMLParse( String => $XMLString );
# create database specific SQL
push @SQL, $DBObject->SQLProcessor(
Database => \@XMLARRAY,
);
# create database specific PostSQL
push @SQLPost, $DBObject->SQLProcessorPost();
# execute SQL
for my $SQL ( @SQL, @SQLPost ) {
my $Success = $DBObject->Do( SQL => $SQL );
if ( !$Success ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Error during execution of '$SQL'!",
);
return;
}
}
return 1;
}
=item _MigrateEventNotificationsToMultiLanguage()
Migrate event based notifications to support multiple languages.
_MigrateEventNotificationsToMultiLanguage();
=cut
sub _MigrateEventNotificationsToMultiLanguage {
# get needed objects
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
my $NotificationEventObject = $Kernel::OM->Get('Kernel::System::NotificationEvent');
my $ErrorOutput;
my $ColumnsArePresent = 1;
# use do for restoring STDERR
do {
local *STDERR;
open STDERR, '>:utf8', \$ErrorOutput; ## no critic
# sanity check to verify the subject, text and content_type columns are present
$ColumnsArePresent = $DBObject->Prepare(
SQL => 'SELECT subject, text, content_type FROM notification_event',
);
};
return 1 if !$ColumnsArePresent;
# get the systems default language
my $Language = $Kernel::OM->Get('Kernel::Config')->Get('DefaultLanguage') || 'en';
return if !$DBObject->Prepare(
SQL => 'SELECT id, name, subject, text, content_type, valid_id, comments
FROM notification_event',
);
# read the notification_event entries and store in array
my @NotificationsEvent;
ROW:
while ( my @Row = $DBObject->FetchrowArray() ) {
if ( !$Row[2] || !$Row[3] || !$Row[4] ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Notification: $Row[1] is invalid and will not be migrated, or was migrated previously.",
);
next ROW;
}
my %Data = (
ID => $Row[0],
Name => $Row[1],
Message => {
$Language => {
Subject => $Row[2],
Body => $Row[3],
ContentType => $Row[4],
},
},
ValidID => $Row[5],
Comment => $Row[6],
);
push @NotificationsEvent, \%Data;
}
# read notification event item data
for my $Notification (@NotificationsEvent) {
$DBObject->Prepare(
SQL => 'SELECT event_key, event_value
FROM notification_event_item
WHERE notification_id = ?',
Bind => [ \$Notification->{ID} ],
);
while ( my @Row = $DBObject->FetchrowArray() ) {
push @{ $Notification->{Data}->{ $Row[0] } }, $Row[1];
}
}
# update notifications
for my $Notification (@NotificationsEvent) {
# update
my $Success = $NotificationEventObject->NotificationUpdate(
%{$Notification},
UserID => 1,
);
# error handling
return if !$Success;
}
# drop migrated columns from notification_event table
my $XMLString = '
<TableAlter Name="notification_event">
<ColumnDrop Name="subject"/>
<ColumnDrop Name="text"/>
<ColumnDrop Name="content_type"/>
</TableAlter>';
my @SQL;
my @SQLPost;
my $XMLObject = $Kernel::OM->Get('Kernel::System::XML');
# create database specific SQL and PostSQL commands
my @XMLARRAY = $XMLObject->XMLParse( String => $XMLString );
# create database specific SQL
push @SQL, $DBObject->SQLProcessor(
Database => \@XMLARRAY,
);
# create database specific PostSQL
push @SQLPost, $DBObject->SQLProcessorPost();
# execute SQL
for my $SQL ( @SQL, @SQLPost ) {
my $Success = $DBObject->Do( SQL => $SQL );
if ( !$Success ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Error during execution of '$SQL'!",
);
return;
}
}
return 1;
}
=item _MigrateNotifications()
Migrate notifications to event based notifications.
_MigrateNotifications();
=cut
sub _MigrateNotifications {
# get needed objects
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
my $EncodeObject = $Kernel::OM->Get('Kernel::System::Encode');
my $NotificationEventObject = $Kernel::OM->Get('Kernel::System::NotificationEvent');
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $ErrorOutput;
my $NotificationsTableIsPresent = 1;
# use do for restoring STDERR
do {
local *STDERR;
open STDERR, '>:utf8', \$ErrorOutput; ## no critic
# sanity check to verify the notifications table is present
$NotificationsTableIsPresent = $DBObject->Prepare(
SQL => 'SELECT notification_type FROM notifications',
);
};
return 1 if !$NotificationsTableIsPresent;
# get valid list
my %ValidList = $Kernel::OM->Get('Kernel::System::Valid')->ValidList();
my %ValidListReverse = reverse %ValidList;
my %NotificationList = $NotificationEventObject->NotificationList();
my %NotificationListReverse = reverse %NotificationList;
# mapping array based on notification type
my %NotificationTypeMapping = (
'Agent::NewTicket' => [
{
Name => 'Ticket create notification',
VisibleForAgent => [1],
VisibleForAgentTooltip => [
'You will receive a notification each time a new ticket is created in one of your "My Queues" or "My Services".'
],
Events => ['NotificationNewTicket'],
Recipients => [ 'AgentMyQueues', 'AgentMyServices' ],
SendOnOutOfOffice => [1],
Transports => ['Email'],
AgentEnabledByDefault => ['Email'],
},
],
'Agent::FollowUp' => [
{
Name => 'Ticket follow-up notification (unlocked)',
VisibleForAgent => [1],
VisibleForAgentTooltip => [
'You will receive a notification if a customer sends a follow-up to an unlocked ticket which is in your "My Queues" or "My Services".'
],
Events => ['NotificationFollowUp'],
Recipients => [ 'AgentOwner', 'AgentWatcher', 'AgentMyQueues', 'AgentMyServices' ],
SendOnOutOfOffice => [1],
LockID => [1], # unlocked
Transports => ['Email'],
AgentEnabledByDefault => ['Email'],
},
{
Name => 'Ticket follow-up notification (locked)',
VisibleForAgent => [1],
VisibleForAgentTooltip => [
'You will receive a notification if a customer sends a follow-up to a locked ticket of which you are the ticket owner or responsible.'
],
Events => ['NotificationFollowUp'],
Recipients => [ 'AgentOwner', 'AgentResponsible', 'AgentWatcher' ],
SendOnOutOfOffice => [1],
LockID => [ 2, 3 ], # locked
Transports => ['Email'],
AgentEnabledByDefault => ['Email'],
},
],
'Agent::LockTimeout' => [
{
Name => 'Ticket lock timeout notification',
VisibleForAgent => [1],
VisibleForAgentTooltip =>
['You will receive a notification as soon as a ticket owned by you is automatically unlocked.'],
Events => ['NotificationLockTimeout'],
Recipients => ['AgentOwner'],
SendOnOutOfOffice => [1],
Transports => ['Email'],
AgentEnabledByDefault => ['Email'],
},
],
'Agent::OwnerUpdate' => [
{
Name => 'Ticket owner update notification',
Events => ['NotificationOwnerUpdate'],
Recipients => ['AgentOwner'],
SendOnOutOfOffice => [1],
Transports => ['Email'],
},
],
'Agent::ResponsibleUpdate' => [
{
Name => 'Ticket responsible update notification',
Events => ['NotificationResponsibleUpdate'],
Recipients => ['AgentResponsible'],
SendOnOutOfOffice => [1],
Transports => ['Email'],
},
],
'Agent::AddNote' => [
{
Name => 'Ticket new note notification',
Events => ['NotificationAddNote'],
Recipients => [ 'AgentOwner', 'AgentResponsible', 'AgentWatcher' ],
SendOnOutOfOffice => [1],
Transports => ['Email'],
},
],
'Agent::Move' => [
{
Name => 'Ticket queue update notification',
VisibleForAgent => [1],
VisibleForAgentTooltip =>
['You will receive a notification if a ticket is moved into one of your "My Queues".'],
Events => ['NotificationMove'],
Recipients => ['AgentMyQueues'],
SendOnOutOfOffice => [1],
Transports => ['Email'],
AgentEnabledByDefault => ['Email'],
},
],
'Agent::PendingReminder' => [
{
Name => 'Ticket pending reminder notification (locked)',
Events => ['NotificationPendingReminder'],
Recipients => [ 'AgentOwner', 'AgentResponsible' ],
SendOnOutOfOffice => [1],
OncePerDay => [1],
LockID => [ 2, 3 ], # locked
Transports => ['Email'],
},
{
Name => 'Ticket pending reminder notification (unlocked)',
Events => ['NotificationPendingReminder'],
Recipients => [ 'AgentOwner', 'AgentResponsible', 'AgentMyQueues' ],
SendOnOutOfOffice => [1],
OncePerDay => [1],
LockID => [1], # unlocked
Transports => ['Email'],
},
],
'Agent::Escalation' => [
{
Name => 'Ticket escalation notification',
Events => ['NotificationEscalation'],
Recipients => [ 'AgentMyQueues', 'AgentWritePermissions' ],
SendOnOutOfOffice => [1],
OncePerDay => [1],
Transports => ['Email'],
},
],
'Agent::EscalationNotifyBefore' => [
{
Name => 'Ticket escalation warning notification',
Events => ['NotificationEscalationNotifyBefore'],
Recipients => [ 'AgentMyQueues', 'AgentWritePermissions' ],
SendOnOutOfOffice => [1],