summaryrefslogtreecommitdiff
path: root/rt/etc/upgrade/upgrade-articles
blob: ed442d1c9a3af7b99babf0c8dd6a63fe5012dfb5 (plain)
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
#!/usr/bin/perl
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
#                                          <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
use 5.10.1;
use strict;
use warnings;

use lib "/opt/rt3/local/lib";
use lib "/opt/rt3/lib";

use RT::Interface::CLI qw(Init);
Init();

my $db_name = RT->Config->Get('DatabaseName');
my $db_type = RT->Config->Get('DatabaseType');

my $dbh = $RT::Handle->dbh;

my $found_fm_tables;
foreach my $name ( $RT::Handle->_TableNames ) {
    next unless $name =~ /^fm_/i;
    $found_fm_tables->{lc $name}++;
}

unless ( $found_fm_tables->{fm_topics} && $found_fm_tables->{fm_objecttopics} ) {
    warn "Couldn't find topics tables, it appears you have RTFM 2.0 or earlier.";
    warn "This script cannot yet upgrade RTFM versions which are that old";
    exit;
}

{ # port over Articles
    my @columns = qw(id Name Summary SortOrder Class Parent URI Creator Created LastUpdatedBy LastUpdated);
    copy_tables('FM_Articles','Articles',\@columns);

}


{ # port over Classes
    my @columns = qw(id Name Description SortOrder Disabled Creator Created LastUpdatedBy LastUpdated);
    if ( grep lc($_) eq 'hotlist', $RT::Handle->Fields('FM_Classes') ) {
        push @columns, 'HotList';
    }
    copy_tables('FM_Classes','Classes',\@columns);
}

{ # port over Topics
    my @columns = qw(id Parent Name Description ObjectType ObjectId);
    copy_tables('FM_Topics','Topics',\@columns);
}

{ # port over ObjectTopics
    my @columns = qw(id Topic ObjectType ObjectId);
    copy_tables('FM_ObjectTopics','ObjectTopics',\@columns);
}

sub copy_tables {
    my ($source, $dest, $columns) = @_;
    my $column_list = join(', ',@$columns);
    my $sql;
    # SQLite: http://www.sqlite.org/lang_insert.html
    if ( $db_type eq 'mysql' || $db_type eq 'SQLite' ) {
        $sql = "insert into $dest ($column_list) select $column_list from $source";
    }
    # Oracle: http://www.adp-gmbh.ch/ora/sql/insert/select_and_subquery.html
    elsif ( $db_type eq 'Pg' || $db_type eq 'Oracle' ) {
        $sql = "insert into $dest ($column_list) (select $column_list from $source)";
    }
    $RT::Logger->debug($sql);
    $dbh->do($sql);
}

{ # create ObjectClasses
  # this logic will need updating when folks have an FM_ObjectClasses table
    use RT::Classes;
    use RT::ObjectClass;

    my $classes = RT::Classes->new(RT->SystemUser);
    $classes->UnLimit;
    while ( my $class = $classes->Next ) {
        my $objectclass = RT::ObjectClass->new(RT->SystemUser);
        my ($ret, $msg ) = $objectclass->Create( Class => $class->Id, ObjectType => 'RT::System', ObjectId => 0 );
        if ($ret) {
            warn("Applied Class '".$class->Name."' globally");
        } else {
            warn("Couldn't create linkage for Class ".$class->Name.": $msg");
        }
    }
}

{ # update ACLs
    use RT::ACL;
    my $acl = RT::ACL->new(RT->SystemUser);
    $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
    $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::System' );
    while ( my $ace = $acl->Next ) {
        if ( $ace->__Value('ObjectType') eq 'RT::FM::Class' ) {
            my ($ret, $msg ) = $ace->__Set( Field => 'ObjectType', Value => 'RT::Class');
            warn "Fixing ACL ".$ace->Id." to refer to RT::Class: $msg";
        } elsif ( $ace->__Value('ObjectType') eq 'RT::FM::System' ) {
            my ($ret, $msg) = $ace->__Set(Field => 'ObjectType', Value => 'RT::System');
            warn "Fixing ACL ".$ace->Id." to refer to RT::System: $msg";
        }
    }


}

{ # update CustomFields
    use RT::CustomFields;
    my $cfs = RT::CustomFields->new(RT->SystemUser);
    $cfs->Limit( FIELD => 'LookupType', VALUE => 'RT::FM::Class-RT::FM::Article' );
    $cfs->{'find_disabled_rows'} = 1;
    while ( my $cf = $cfs->Next ) {
        my ($ret, $msg) = $cf->__Set( Field => 'LookupType', Value => 'RT::Class-RT::Article' );
        warn "Update Custom Field LookupType for CF.".$cf->Id." $msg";
    }
}

{ # update ObjectCustomFieldValues
    use RT::ObjectCustomFieldValues;
    my $ocfvs = RT::ObjectCustomFieldValues->new(RT->System);
    $ocfvs->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article' );
    $ocfvs->{'find_expired_rows'} = 1;
    while ( my $ocfv = $ocfvs->Next ) {
        my ($ret, $msg) = $ocfv->__Set( Field => 'ObjectType', Value => 'RT::Article' );
        warn "Updated CF ".$ocfv->__Value('CustomField')." Value for Article ".$ocfv->__Value('ObjectId');
    }

}

{ # update Topics
    use RT::Topics;
    my $topics = RT::Topics->new(RT->SystemUser);
    $topics->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
    $topics->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::System' );
    while ( my $topic = $topics->Next ) {
        if ( $topic->__Value('ObjectType') eq 'RT::FM::Class' ) {
            my ($ret, $msg ) = $topic->__Set( Field => 'ObjectType', Value => 'RT::Class');
            warn "Fixing Topic ".$topic->Id." to refer to RT::Class: $msg";
        } elsif ( $topic->__Value('ObjectType') eq 'RT::FM::System' ) {
            my ($ret, $msg) = $topic->__Set(Field => 'ObjectType', Value => 'RT::System');
            warn "Fixing Topic ".$topic->Id." to refer to RT::System: $msg";
        }
    }
}

{ # update ObjectTopics
    use RT::ObjectTopics;
    my $otopics = RT::ObjectTopics->new(RT->SystemUser);
    $otopics->UnLimit;
    while ( my $otopic = $otopics->Next ) {
        if ( $otopic->ObjectType eq 'RT::FM::Article' ) {
            my ($ret, $msg) = $otopic->SetObjectType('RT::Article');
            warn "Fixing Topic ".$otopic->Topic." to apply to article: $msg";
        }
    }
}

{ # update Links
    use RT::Links;
    my $links = RT::Links->new(RT->SystemUser);
    $links->Limit(FIELD => 'Base', VALUE => 'rtfm', OPERATOR => 'LIKE', SUBCLAUSE => 'stopanding', ENTRYAGGREGATOR => 'OR');
    $links->Limit(FIELD => 'Target', VALUE => 'rtfm', OPERATOR => 'LIKE', SUBCLAUSE => 'stopanding', ENTRYAGGREGATOR => 'OR' );
    while ( my $link = $links->Next ) {
        my $base   = $link->__Value('Base');
        my $target = $link->__Value('Target');
        if ( $base =~ s/rtfm/article/i ) {
            my ($ret, $msg) = $link->__Set( Field => 'Base', Value => $base );
            warn "Updating base to $base: $msg for link ".$link->id;
        }
        if ( $target =~ s/rtfm/article/i ) {
            my ($ret, $msg) = $link->__Set( Field => 'Target', Value => $target );
            warn "Updating target to $target: $msg for link ".$link->id;
        }

    }
}

{ # update Transactions
  # we only keep article transactions at this point
    no warnings 'once';
    use RT::Transactions;
    # Next calls Type to check readability and Type calls _Accessible
    # which called CurrentUserCanSee which calls Object which tries to instantiate
    # an RT::FM::Article.  Rather than a shim RT::FM::Article class, I'm just avoiding
    # the ACL check since we're running around as the superuser.
    local *RT::Transaction::Type = sub { shift->__Value('Type') };
    my $transactions = RT::Transactions->new(RT->SystemUser);
    $transactions->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article' );
    while ( my $t = $transactions->Next ) {
        my ($ret, $msg) = $t->__Set( Field => 'ObjectType', Value => 'RT::Article' );
        warn "Updated Transaction ".$t->Id." to point to RT::Article";
    }

    # we also need to change links that point to articles
    $transactions = RT::Transactions->new(RT->SystemUser);
    $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
    $transactions->Limit( FIELD => 'NewValue', VALUE => 'rtfm', OPERATOR => 'LIKE' );
    while ( my $t = $transactions->Next ) {
        my $value = $t->__Value('NewValue');
        $value =~ s/rtfm/article/;
        my ($ret, $msg) = $t->__Set( Field => 'NewValue', Value => $value );
        warn "Updated Transaction ".$t->Id." to link to $value";
    }
}

{ # update Attributes
  # these are all things we should make real columns someday
    use RT::Attributes;
    my $attributes = RT::Attributes->new(RT->SystemUser);
    $attributes->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
    while ( my $a = $attributes->Next ) {
        my ($ret,$msg) = $a->__Set( Field => 'ObjectType', Value => 'RT::Class' );
        warn "Updating Attribute ".$a->Name." to point to RT::Class";
    }
}