This file is indexed.

/usr/share/otrs/scripts/DBUpdate-to-3.3.pl is in otrs2 5.0.7-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl
# --
# DBUpdate-to-3.3.pl - update script to migrate OTRS 3.2.x to 3.3.x
# Copyright (C) 2001-2014 OTRS AG, http://otrs.com/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# 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 Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
# or see http://www.gnu.org/licenses/agpl.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::Log;
use Kernel::System::Time;
use Kernel::System::Encode;
use Kernel::System::DB;
use Kernel::System::Main;
use Kernel::System::SysConfig;
use Kernel::System::Cache;
use Kernel::System::Package;
use Kernel::System::VariableCheck qw(:all);

{

    # get options
    my %Opts;
    Getopt::Std::getopt( 'h', \%Opts );

    if ( exists $Opts{h} ) {
        print <<"EOF";

DBUpdate-to-3.3.pl - Upgrade scripts for OTRS 3.2.x to 3.3.x migration.
Copyright (C) 2001-2013 OTRS AG, http://otrs.com/

Usage: $0 [-h]
    Options are as follows:
        -h      display this help

EOF
        exit 1;
    }

    # UID check if not on Windows
#    if ( $^O ne 'MSWin32' && $> == 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 autoflushing of STDOUT
    $| = 1;

    print "\nMigration started...\n\n";

    # create common objects
    my $CommonObject = _CommonObjectsBase();

    # define the number of steps
    my $Steps = 13;
    my $Step  = 1;

    print "Step $Step of $Steps: Refresh configuration cache... ";
    RebuildConfig($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    # create common objects with new default config
    $CommonObject = _CommonObjectsBase();

    # check framework version
    print "Step $Step of $Steps: Check framework version... ";
    _CheckFrameworkVersion($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Generate MessageID md5sums... ";
    _GenerateMessageIDMD5($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    # migrate old settings
    print "Step $Step of $Steps: Migrate old settings... ";
    if ( _MigrateOldSettings($CommonObject) ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    # migrate OTRSExternalTicketNumberRecognition
    print "Step $Step of $Steps: Migrate OTRSExternalTicketNumberRecognition... ";
    if ( _MigrateOTRSExternalTicketNumberRecognition($CommonObject) ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    print "Step $Step of $Steps: Checking Standard Template table columns... ";
    _AddTemplateTypeColumn($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Updating Queue Standard Template relations table... ";

    # do not die if foreign keys already exists
    _AddQueueStandardTemplateForeignKeys($CommonObject);
    print "done.\n\n";
    $Step++;

    # migrate OTRSGenericStandardTemplates
    print "Step $Step of $Steps: Migrate OTRSGenericStandardTemplates... ";
    if ( _MigrateOTRSGenericStandardTemplates($CommonObject) ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    print "Step $Step of $Steps: Checking if ACL tables already exist... ";
    _AddACLTables($CommonObject) || die;
    print "done.\n\n";
    $Step++;

    # uninstall Merged Feature Add-Ons
    print "Step $Step of $Steps: Uninstall Merged Feature Add-Ons... ";
    if ( _UninstallMergedFeatureAddOns($CommonObject) ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    # Delete Old Files
    print "Step $Step of $Steps: Delete the files that are not longer needed... ";
    if ( _DeleteOldFiles($CommonObject) ) {
        print "done.\n\n";
    }
    else {
        print "error.\n\n";
        die;
    }
    $Step++;

    # Clean up the cache completely at the end.
    print "Step $Step of $Steps: Clean up the cache... ";
    my $CacheObject = Kernel::System::Cache->new( %{$CommonObject} ) || die;
    $CacheObject->CleanUp();
    print "done.\n\n";
    $Step++;

    print "Step $Step of $Steps: Refresh configuration cache another time... ";
    RebuildConfig($CommonObject) || die;
    print "done.\n\n";

    print "Migration completed!\n";

    exit 0;
}

sub _CommonObjectsBase {
    my %CommonObject;
    $CommonObject{ConfigObject} = Kernel::Config->new();
    $CommonObject{LogObject}    = Kernel::System::Log->new(
        LogPrefix => 'OTRS-DBUpdate-to-3.3',
        %CommonObject,
    );
    $CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
    $CommonObject{MainObject}   = Kernel::System::Main->new(%CommonObject);
    $CommonObject{TimeObject}   = Kernel::System::Time->new(%CommonObject);
    $CommonObject{DBObject}     = Kernel::System::DB->new(%CommonObject);
    return \%CommonObject;
}

=item RebuildConfig($CommonObject)

refreshes the configuration to make sure that a ZZZAAuto.pm is present
after the upgrade.

    RebuildConfig($CommonObject);

=cut

sub RebuildConfig {
    my $CommonObject = shift;

    my $SysConfigObject = Kernel::System::SysConfig->new( %{$CommonObject} );

    # 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";
    $CommonObject = _CommonObjectsBase();

    return 1;
}

=item _CheckFrameworkVersion()

Check if framework it's the correct one for Dinamic Fields migration.

    _CheckFrameworkVersion();

=cut

sub _CheckFrameworkVersion {
    my $CommonObject = shift;

    my $Home = $CommonObject->{ConfigObject}->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 $CommonObject->{Home}/RELEASE: $!";
    }

    if ( $ProductName ne 'OTRS' ) {
        die "Error: No OTRS system found"
    }
    if ( $Version !~ /^3\.3(.*)$/ ) {

        die "Error: You are trying to run this script on the wrong framework version $Version!"
    }

    return 1;
}

=item _AddACLTables($CommonObject)

This function checks if the acl and acl_sync tables already exist
and creates them otherwise.

    _AddACLTables($CommonObject);

=cut

sub _AddACLTables {
    my $CommonObject = shift;

    my $ACLTablesExist;
    print "Check if ACL table exists.\n";
    {
        my $STDERR;

        # Catch STDERR log messages to not confuse the user. The Prepare() will fail
        #   if the columns are not present.
        local *STDERR;
        open STDERR, '>:encoding(UTF-8)', \$STDERR;    ## no critic

        $ACLTablesExist = $CommonObject->{DBObject}->Prepare(
            SQL   => "SELECT * FROM acl WHERE 1=0",
            Limit => 1,
        );
    }

    my $XMLString;

    if ($ACLTablesExist) {

        print "ACL tables are present, no need to create them.\n";

        # fetch data to avoid warnings
        while ( my @Row = $CommonObject->{DBObject}->FetchrowArray() ) {

            # noop
        }

        return 1;
    }
    else {

        print "ACL tables not found, create it.\n";

        $XMLString = '<?xml version="1.0" encoding="utf-8" ?>
        <database Name="otrs">
            <TableCreate Name="acl">
                <Column Name="id" Required="true" PrimaryKey="true" AutoIncrement="true" Type="INTEGER"/>
                <Column Name="name" Required="true" Type="VARCHAR" Size="200"/>
                <Column Name="comments" Required="false" Size="250" Type="VARCHAR"/>
                <Column Name="description" Required="false" Size="250" Type="VARCHAR"/>
                <Column Name="valid_id" Required="true" Type="SMALLINT"/>
                <Column Name="stop_after_match" Required="false" Type="SMALLINT"/>
                <Column Name="config_match" Required="false" Type="LONGBLOB"/>
                <Column Name="config_change" Required="false" Type="LONGBLOB"/>
                <Column Name="create_time" Required="true" Type="DATE"/>
                <Column Name="create_by" Required="true" Type="INTEGER"/>
                <Column Name="change_time" Required="true" Type="DATE"/>
                <Column Name="change_by" Required="true" Type="INTEGER"/>
                <Unique Name="acl_name">
                    <UniqueColumn Name="name"/>
                </Unique>
                <ForeignKey ForeignTable="valid">
                    <Reference Local="valid_id" Foreign="id"/>
                </ForeignKey>
                <ForeignKey ForeignTable="users">
                    <Reference Local="create_by" Foreign="id"/>
                    <Reference Local="change_by" Foreign="id"/>
                </ForeignKey>
            </TableCreate>
            <TableCreate Name="acl_sync">
                <Column Name="acl_id" Required="true" Size="200" Type="VARCHAR"/>
                <Column Name="sync_state" Required="true" Size="30" Type="VARCHAR"/>
                <Column Name="create_time" Required="true" Type="DATE"/>
                <Column Name="change_time" Required="true" Type="DATE"/>
            </TableCreate>
        </database>';
    }

    my @SQL;
    my @SQLPost;

    my $XMLObject = Kernel::System::XML->new( %{$CommonObject} );

    # create database specific SQL and PostSQL commands
    my @XMLARRAY = $XMLObject->XMLParse( String => $XMLString );

    # create database specific SQL
    push @SQL, $CommonObject->{DBObject}->SQLProcessor(
        Database => \@XMLARRAY,
    );

    # create database specific PostSQL
    push @SQLPost, $CommonObject->{DBObject}->SQLProcessorPost();

    # execute SQL
    for my $SQL ( @SQL, @SQLPost ) {
        print $SQL . "\n";
        my $Success = $CommonObject->{DBObject}->Do( SQL => $SQL );
        if ( !$Success ) {
            $CommonObject->{LogObject}->Log(
                Priority => 'error',
                Message  => "Error during execution of '$SQL'!",
            );
            return;
        }
    }
    return 1;
}

=item _AddTemplateTypeColumn()

This function checks if the template_type column from standard_template tables already exist
and sets new default value, otherwise creates it.

    _AddTemplateTypeColumn($CommonObject);

=cut

sub _AddTemplateTypeColumn {
    my $CommonObject = shift;

    my $TemplateTypeColumnExists;
    print "\nCheck if 'template_type' columns exists.\n";
    {
        my $STDERR;

        # Catch STDERR log messages to not confuse the user. The Prepare() will fail
        #   if the columns are not present.
        local *STDERR;
        open STDERR, '>:encoding(UTF-8)', \$STDERR;    ## no critic

        $TemplateTypeColumnExists = $CommonObject->{DBObject}->Prepare(
            SQL   => "SELECT template_type FROM standard_template WHERE 1=0",
            Limit => 1,
        );
    }

    my $XMLString;

    if ($TemplateTypeColumnExists) {

        print "'template_type' column exists, set new default value.\n";

        # fetch data to avoid warnings
        while ( my @Row = $CommonObject->{DBObject}->FetchrowArray() ) {

            # noop
        }

        $XMLString = '<?xml version="1.0" encoding="utf-8" ?>
        <database Name="otrs">
            <TableAlter Name="standard_template">
                <ColumnChange NameOld="template_type" NameNew="template_type" Required="true" Size="100" Type="VARCHAR" Default="Answer"/>
            </TableAlter>
        </database>';
    }
    else {

        print "'template_type' column not found, create it.\n";

        $XMLString = '<?xml version="1.0" encoding="utf-8" ?>
        <database Name="otrs">
            <TableAlter Name="standard_template">
                <ColumnAdd Name="template_type" Required="true" Size="100" Type="VARCHAR" Default="Answer"/>
            </TableAlter>
        </database>';
    }

    my @SQL;
    my @SQLPost;

    my $XMLObject = Kernel::System::XML->new( %{$CommonObject} );

    # create database specific SQL and PostSQL commands
    my @XMLARRAY = $XMLObject->XMLParse( String => $XMLString );

    # create database specific SQL
    push @SQL, $CommonObject->{DBObject}->SQLProcessor(
        Database => \@XMLARRAY,
    );

    # create database specific PostSQL
    push @SQLPost, $CommonObject->{DBObject}->SQLProcessorPost();

    # execute SQL
    for my $SQL ( @SQL, @SQLPost ) {
        print $SQL . "\n";
        my $Success = $CommonObject->{DBObject}->Do( SQL => $SQL );
        if ( !$Success ) {
            $CommonObject->{LogObject}->Log(
                Priority => 'error',
                Message  => "Error during execution of '$SQL'!",
            );
            return;
        }
    }
    return 1;
}

=item _AddQueueStandardTemplateForeignKeys()

This function cleans queue_standard_template for inconsistent regusters that points to non existing
templates before creating the foreign key to prevent errors

    _AddQueueStandardTemplateForeignKeys($CommonObject);

=cut

sub _AddQueueStandardTemplateForeignKeys {
    my $CommonObject = shift;

    print "\nCleaning queue_standard_template table\n";
    my $Success = $CommonObject->{DBObject}->Do(
        SQL => '
        DELETE FROM queue_standard_template
        WHERE (
            SELECT COUNT(*)
            FROM standard_template
            WHERE queue_standard_template.standard_template_id = standard_template.id
        ) = 0',
    );

    print "Creating new Foreign Keys for queue_standard_template table\n";
    print "\n--- Note: ---\n";
    print "If you have already run this script before then the Foreign Keys are already set and"
        . " you might see errors regarding 'duplicate key' or 'constrain already exists', that's"
        . " fine, no need to worry!\n";
    print "---\n\n";

    my $XMLString = '<?xml version="1.0" encoding="utf-8" ?>
    <database Name="otrs">
        <TableAlter Name="queue_standard_template">

            <!--  create foreign key (new name) -->
            <ForeignKeyCreate ForeignTable="standard_template">
                <Reference Local="standard_template_id" Foreign="id"/>
            </ForeignKeyCreate>
            <ForeignKeyCreate ForeignTable="queue">
                <Reference Local="queue_id" Foreign="id"/>
            </ForeignKeyCreate>
            <ForeignKeyCreate ForeignTable="users">
                <Reference Local="create_by" Foreign="id"/>
                <Reference Local="change_by" Foreign="id"/>
            </ForeignKeyCreate>
        </TableAlter>
    </database>';

    my @SQL;
    my @SQLPost;

    my $XMLObject = Kernel::System::XML->new( %{$CommonObject} );

    # create database specific SQL and PostSQL commands
    my @XMLARRAY = $XMLObject->XMLParse( String => $XMLString );

    # create database specific SQL
    push @SQL, $CommonObject->{DBObject}->SQLProcessor(
        Database => \@XMLARRAY,
    );

    # create database specific PostSQL
    push @SQLPost, $CommonObject->{DBObject}->SQLProcessorPost();

    # execute SQL
    for my $SQL ( @SQL, @SQLPost ) {
        print $SQL . "\n";
        my $Success = $CommonObject->{DBObject}->Do( SQL => $SQL );
        if ( !$Success ) {
            $CommonObject->{LogObject}->Log(
                Priority => 'error',
                Message  => "Error during execution of '$SQL'!",
            );
            return;
        }
    }
    return 1;
}

=item _GenerateMessageIDMD5()

Create md5sums of existing MessageIDs in Article table.

=cut

sub _GenerateMessageIDMD5 {
    my $CommonObject = shift;

    # will work on all database backends; warning, we might want to add
    # UPDATE statements for databases that can natively create md5sums

    # conversion to MD5 - if possible using one UPDATE statement
    if (
        $CommonObject->{DBObject}->GetDatabaseFunction('Type') eq 'mysql'
        || $CommonObject->{DBObject}->GetDatabaseFunction('Type') eq 'postgresql'
        )
    {

        $CommonObject->{DBObject}->Do(
            SQL => '
                UPDATE article
                SET a_message_id_md5 = MD5(a_message_id)
                WHERE a_message_id IS NOT NULL
                ',
        );

    }

    # otherwise convert every row using MainObject - much slower
    else {

        # fetch results and calculate MD5sums
        my %MD5sum;
        $CommonObject->{DBObject}->Prepare(
            SQL => 'SELECT id, a_message_id
                        FROM article
                        WHERE a_message_id IS NOT NULL',
        );
        MESSAGEID:
        while ( my @Row = $CommonObject->{DBObject}->FetchrowArray() ) {
            next MESSAGEID if !$Row[1];
            $MD5sum{ $Row[0] } = $CommonObject->{MainObject}->MD5sum( String => $Row[1] );
        }

        # update records
        for my $ArticleID ( sort keys %MD5sum ) {
            $CommonObject->{DBObject}->Do(
                SQL => "UPDATE article
                         SET a_message_id_md5 = ?
                         WHERE id = ?",
                Bind => [ \$MD5sum{$ArticleID}, \$ArticleID ],
            );
        }
    }

    return 1;
}

=item _MigrateOldSettings()

Migrate settings that has changed it name.

    _MigrateOldSettings($CommonObject);

=cut

sub _MigrateOldSettings {
    my $CommonObject = shift;

    my $SysConfigObject = Kernel::System::SysConfig->new( %{$CommonObject} );

    # Ticket::Frontend::AgentTicketMove
    # get original setting (old name)
    my $Setting = $CommonObject->{ConfigObject}->Get('Ticket::DefaultNextMoveStateType');

    if ( IsArrayRefWithData($Setting) ) {

        # set new setting,
        my $Success = $SysConfigObject->ConfigItemUpdate(
            Valid => 1,
            Key   => 'Ticket::Frontend::AgentTicketMove###StateType',
            Value => $Setting,
        );
    }

    # StandardResponse2QueueByCreating
    # get original setting (old name)
    $Setting = $CommonObject->{ConfigObject}->Get('StandardResponse2QueueByCreating');

    if ( IsArrayRefWithData($Setting) ) {

        # set new setting,
        my $Success = $SysConfigObject->ConfigItemUpdate(
            Valid => 1,
            Key   => 'StandardTemplate2QueueByCreating',
            Value => $Setting,
        );
    }

    # DashboardBackend should have default columns defined
    my %DefaultColumns = (
        Age                    => 2,
        Changed                => 1,
        CustomerID             => 1,
        CustomerName           => 1,
        CustomerUserID         => 1,
        EscalationResponseTime => 1,
        EscalationSolutionTime => 1,
        EscalationTime         => 1,
        EscalationUpdateTime   => 1,
        Lock                   => 1,
        Owner                  => 1,
        PendingTime            => 1,
        Priority               => 1,
        Queue                  => 1,
        Responsible            => 1,
        SLA                    => 1,
        Service                => 1,
        State                  => 1,
        TicketNumber           => 2,
        Title                  => 2,
        Type                   => 1
    );

    # dashboard backends
    # get values from config
    $Setting = $CommonObject->{ConfigObject}->Get('DashboardBackend');

    # check config
    DASHBOARDBACKEND:
    for my $DashboardBackend ( sort keys %{$Setting} ) {
        next DASHBOARDBACKEND if !IsHashRefWithData( $Setting->{$DashboardBackend} );

        my $Module = $Setting->{$DashboardBackend}->{Module} || '';
        next DASHBOARDBACKEND
            if $Module ne 'Kernel::Output::HTML::DashboardTicketGeneric';

        next DASHBOARDBACKEND
            if IsHashRefWithData( $Setting->{$DashboardBackend}->{DefaultColumns} );

        # set default column values
        $Setting->{$DashboardBackend}->{DefaultColumns} = \%DefaultColumns;

        # set new setting,
        my $Success = $SysConfigObject->ConfigItemUpdate(
            Valid => 1,
            Key   => 'DashboardBackend###' . $DashboardBackend,
            Value => $Setting->{$DashboardBackend},
        );
    }

    # customer information center backends
    # get values from config
    $Setting = $CommonObject->{ConfigObject}->Get('AgentCustomerInformationCenter::Backend');

    # remove CustomerID for CIC
    delete $DefaultColumns{CustomerID};

    # check config
    CICBACKEND:
    for my $DashboardBackend ( sort keys %{$Setting} ) {
        next CICBACKEND if !IsHashRefWithData( $Setting->{$DashboardBackend} );

        my $Module = $Setting->{$DashboardBackend}->{Module} || '';
        next CICBACKEND
            if $Module ne 'Kernel::Output::HTML::DashboardTicketGeneric';

        next CICBACKEND
            if IsHashRefWithData( $Setting->{$DashboardBackend}->{DefaultColumns} );

        # set default column values
        $Setting->{$DashboardBackend}->{DefaultColumns} = \%DefaultColumns;

        # set new setting,
        my $Success = $SysConfigObject->ConfigItemUpdate(
            Valid => 1,
            Key   => 'AgentCustomerInformationCenter::Backend###' . $DashboardBackend,
            Value => $Setting->{$DashboardBackend},
        );
    }

    # update toolbar items settings
    # otherwise the new fontawesome icons won't be displayed

    # collect icon data for toolbar items
    my %ModuleAttributes = (
        '1-Ticket::AgentTicketQueue' => {
            'Icon' => 'icon-folder-close',
        },
        '2-Ticket::AgentTicketStatus' => {
            'Icon' => 'icon-list-ol',
        },
        '3-Ticket::AgentTicketEscalation' => {
            'Icon' => 'icon-exclamation',
        },
        '4-Ticket::AgentTicketPhone' => {
            'Icon' => 'icon-phone',
        },
        '5-Ticket::AgentTicketEmail' => {
            'Icon' => 'icon-envelope',
        },
        '6-Ticket::TicketResponsible' => {
            'Icon'        => 'icon-user',
            'IconNew'     => 'icon-user',
            'IconReached' => 'icon-user',
        },
        '7-Ticket::TicketWatcher' => {
            'Icon'        => 'icon-eye-open',
            'IconNew'     => 'icon-eye-open',
            'IconReached' => 'icon-eye-open',
        },
        '8-Ticket::TicketLocked' => {
            'Icon'        => 'icon-lock',
            'IconNew'     => 'icon-lock',
            'IconReached' => 'icon-lock',
        },
    );

    $Setting = $CommonObject->{ConfigObject}->Get('Frontend::ToolBarModule');

    TOOLBARMODULE:
    for my $ToolbarModule ( sort keys %ModuleAttributes ) {

        next TOOLBARMODULE if !IsHashRefWithData( $Setting->{$ToolbarModule} );

        # set icon and class infos
        for my $Attribute ( sort keys %{ $ModuleAttributes{$ToolbarModule} } ) {
            $Setting->{$ToolbarModule}->{$Attribute} = $ModuleAttributes{$ToolbarModule}->{$Attribute};
        }

        # set new setting,
        my $Success = $SysConfigObject->ConfigItemUpdate(
            Valid => 1,
            Key   => 'Frontend::ToolBarModule###' . $ToolbarModule,
            Value => $Setting->{$ToolbarModule},
        );
    }

    return 1;
}

=item _MigrateOTRSExternalTicketNumberRecognition()

Migrate PostMaster ExternalTicketNumberRecognition settings to the new names and deletes the FAO
package from the database if installed.

    _MigrateOTRSExternalTicketNumberRecognition($CommonObject);

=cut

sub _MigrateOTRSExternalTicketNumberRecognition {
    my $CommonObject = shift;

    my $SysConfigObject = Kernel::System::SysConfig->new( %{$CommonObject} );

    # convert settings
    for my $Number ( 1 .. 4 ) {

        # get original setting (from FAO using old name)
        my $Setting = $CommonObject->{ConfigObject}->Get('PostMaster::PreFilterModule')
            ->{ '00-ExternalTicketNumberRecognition' . $Number };

        if ( IsHashRefWithData($Setting) ) {

            # set new setting, notice that it has an extra 0 in the name
            my $Success = $SysConfigObject->ConfigItemUpdate(
                Valid => 1,
                Key =>
                    'PostMaster::PreFilterModule###000-ExternalTicketNumberRecognition' . $Number,
                Value => $Setting,
            );
        }
    }

    return 1;
}

=item _MigrateOTRSGenericStandardTemplates()

Migrate Standard Templates Types to new style

    _MigrateMigrateOTRSGenericStandardTemplates($CommonObject);

=cut

sub _MigrateOTRSGenericStandardTemplates {
    my $CommonObject = shift;

    # seach all templates with template type anwer or forward
    for my $TemplateType (qw(answer forward)) {

        # set new template type to Answer or Forward (with capital leter)
        my $NewTemplateType = ucfirst $TemplateType;

        # update DB
        return if !$CommonObject->{DBObject}->Do(
            SQL => 'UPDATE standard_template
                SET template_type = ?
                WHERE template_type = ?',
            Bind => [ \$NewTemplateType, \$TemplateType, ],
        );
    }

    return 1;
}

=item _UninstallMergedFeatureAddOns()

safe uninstall packages from the database.

    UninstallMergedFeatureAddOns($CommonObject);

=cut

sub _UninstallMergedFeatureAddOns {
    my $CommonObject = shift;

    my $PackageObject = Kernel::System::Package->new( %{$CommonObject} );

    # qw( ) contains a list of the feature add-ons to uninstall
    for my $PackageName (
        qw(
        OTRSPostMasterFilterExtensions
        OTRSFreeTextFromCustomerUser
        OTRSExternalTicketNumberRecognition
        OTRSDashboardQueueOverview
        OTRSImportantArticles
        OTRSImportantArticlesITSM
        OTRSDashboardTicketCalendar
        OTRSMultiServiceSelect
        OTRSMultiQueueSelect
        OTRSDynamicFieldMultiLevelSelection
        OTRSEventBasedTicketActions
        OTRSTicketAclEditor
        OTRSCustomerProcessSelection
        OTRSACLExtensions
        OTRSGenericStandardTemplates
        OTRSExtendedDynamicDateFieldSearch
        OTRSDashboardTicketOverviewFilters
        OTRSKeepFAQAttachments
        )
        )
    {
        my $Success = $PackageObject->_PackageUninstallMerged(
            Name => $PackageName,
        );
        if ( !$Success ) {
            print STDERR "There was an error uninstalling package $PackageName\n";
            return;
        }
    }
    return 1;
}

=item _DeleteOldFiles()

delete not longer needed files.

    _DeleteOldFiles($CommonObject);

=cut

sub _DeleteOldFiles {
    my $CommonObject = shift;

    my $Home = $CommonObject->{ConfigObject}->Get('Home');

    # qw( ) contains a list of the feature files to uninstall
    for my $File (
        qw(
        bin/otrs.AddQueue2StdResponse.pl
        Kernel/Modules/AdminQueueResponses.pm
        Kernel/Modules/AdminResponse.pm
        Kernel/Modules/AdminResponseAttachment.pm
        Kernel/Output/HTML/Standard/AdminQueueResponses.dtl
        Kernel/Output/HTML/Standard/AdminResponse.dtl
        Kernel/Output/HTML/Standard/AdminResponseAttachment.dtl
        )
        )
    {

        # add home path
        my $Location = $Home . '/' . $File;

        # check if file exists
        if ( -e $Location ) {

            # delete file
            my $Success = unlink $Location;
            if ( !$Success ) {
                print STDERR "Could not delete $Location: $!\n";
                return;
            }
        }
    }
    return 1;
}

1;