2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
7 # <sales@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
31 # CONTRIBUTION SUBMISSION POLICY:
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
48 # END BPS TAGGED BLOCK }}}
53 use lib "@LOCAL_LIB_PATH@";
54 use lib "@RT_LIB_PATH@";
56 use RT::Interface::CLI qw(Init);
59 my $db_name = RT->Config->Get('DatabaseName');
60 my $db_type = RT->Config->Get('DatabaseType');
62 my $dbh = $RT::Handle->dbh;
65 foreach my $name ( $RT::Handle->_TableNames ) {
66 next unless $name =~ /^fm_/i;
67 $found_fm_tables->{lc $name}++;
70 unless ( $found_fm_tables->{fm_topics} && $found_fm_tables->{fm_objecttopics} ) {
71 warn "Couldn't find topics tables, it appears you have RTFM 2.0 or earlier.";
72 warn "This script cannot yet upgrade RTFM versions which are that old";
76 { # port over Articles
77 my @columns = qw(id Name Summary SortOrder Class Parent URI Creator Created LastUpdatedBy LastUpdated);
78 copy_tables('FM_Articles','Articles',\@columns);
84 my @columns = qw(id Name Description SortOrder Disabled Creator Created LastUpdatedBy LastUpdated);
85 if ( grep lc($_) eq 'hotlist', $RT::Handle->Fields('FM_Classes') ) {
86 push @columns, 'HotList';
88 copy_tables('FM_Classes','Classes',\@columns);
92 my @columns = qw(id Parent Name Description ObjectType ObjectId);
93 copy_tables('FM_Topics','Topics',\@columns);
96 { # port over ObjectTopics
97 my @columns = qw(id Topic ObjectType ObjectId);
98 copy_tables('FM_ObjectTopics','ObjectTopics',\@columns);
102 my ($source, $dest, $columns) = @_;
103 my $column_list = join(', ',@$columns);
105 # SQLite: http://www.sqlite.org/lang_insert.html
106 if ( $db_type eq 'mysql' || $db_type eq 'SQLite' ) {
107 $sql = "insert into $dest ($column_list) select $column_list from $source";
109 # Oracle: http://www.adp-gmbh.ch/ora/sql/insert/select_and_subquery.html
110 elsif ( $db_type eq 'Pg' || $db_type eq 'Oracle' ) {
111 $sql = "insert into $dest ($column_list) (select $column_list from $source)";
113 $RT::Logger->debug($sql);
117 { # create ObjectClasses
118 # this logic will need updating when folks have an FM_ObjectClasses table
122 my $classes = RT::Classes->new(RT->SystemUser);
124 while ( my $class = $classes->Next ) {
125 my $objectclass = RT::ObjectClass->new(RT->SystemUser);
126 my ($ret, $msg ) = $objectclass->Create( Class => $class->Id, ObjectType => 'RT::System', ObjectId => 0 );
128 warn("Applied Class '".$class->Name."' globally");
130 warn("Couldn't create linkage for Class ".$class->Name.": $msg");
137 my $acl = RT::ACL->new(RT->SystemUser);
138 $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
139 $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::System' );
140 while ( my $ace = $acl->Next ) {
141 if ( $ace->__Value('ObjectType') eq 'RT::FM::Class' ) {
142 my ($ret, $msg ) = $ace->__Set( Field => 'ObjectType', Value => 'RT::Class');
143 warn "Fixing ACL ".$ace->Id." to refer to RT::Class: $msg";
144 } elsif ( $ace->__Value('ObjectType') eq 'RT::FM::System' ) {
145 my ($ret, $msg) = $ace->__Set(Field => 'ObjectType', Value => 'RT::System');
146 warn "Fixing ACL ".$ace->Id." to refer to RT::System: $msg";
153 { # update CustomFields
154 use RT::CustomFields;
155 my $cfs = RT::CustomFields->new(RT->SystemUser);
156 $cfs->Limit( FIELD => 'LookupType', VALUE => 'RT::FM::Class-RT::FM::Article' );
157 $cfs->{'find_disabled_rows'} = 1;
158 while ( my $cf = $cfs->Next ) {
159 my ($ret, $msg) = $cf->__Set( Field => 'LookupType', Value => 'RT::Class-RT::Article' );
160 warn "Update Custom Field LookupType for CF.".$cf->Id." $msg";
164 { # update ObjectCustomFieldValues
165 use RT::ObjectCustomFieldValues;
166 my $ocfvs = RT::ObjectCustomFieldValues->new(RT->System);
167 $ocfvs->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article' );
168 $ocfvs->{'find_expired_rows'} = 1;
169 while ( my $ocfv = $ocfvs->Next ) {
170 my ($ret, $msg) = $ocfv->__Set( Field => 'ObjectType', Value => 'RT::Article' );
171 warn "Updated CF ".$ocfv->__Value('CustomField')." Value for Article ".$ocfv->__Value('ObjectId');
178 my $topics = RT::Topics->new(RT->SystemUser);
179 $topics->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
180 $topics->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::System' );
181 while ( my $topic = $topics->Next ) {
182 if ( $topic->__Value('ObjectType') eq 'RT::FM::Class' ) {
183 my ($ret, $msg ) = $topic->__Set( Field => 'ObjectType', Value => 'RT::Class');
184 warn "Fixing Topic ".$topic->Id." to refer to RT::Class: $msg";
185 } elsif ( $topic->__Value('ObjectType') eq 'RT::FM::System' ) {
186 my ($ret, $msg) = $topic->__Set(Field => 'ObjectType', Value => 'RT::System');
187 warn "Fixing Topic ".$topic->Id." to refer to RT::System: $msg";
192 { # update ObjectTopics
193 use RT::ObjectTopics;
194 my $otopics = RT::ObjectTopics->new(RT->SystemUser);
196 while ( my $otopic = $otopics->Next ) {
197 if ( $otopic->ObjectType eq 'RT::FM::Article' ) {
198 my ($ret, $msg) = $otopic->SetObjectType('RT::Article');
199 warn "Fixing Topic ".$otopic->Topic." to apply to article: $msg";
206 my $links = RT::Links->new(RT->SystemUser);
207 $links->Limit(FIELD => 'Base', VALUE => 'rtfm', OPERATOR => 'LIKE', SUBCLAUSE => 'stopanding', ENTRYAGGREGATOR => 'OR');
208 $links->Limit(FIELD => 'Target', VALUE => 'rtfm', OPERATOR => 'LIKE', SUBCLAUSE => 'stopanding', ENTRYAGGREGATOR => 'OR' );
209 while ( my $link = $links->Next ) {
210 my $base = $link->__Value('Base');
211 my $target = $link->__Value('Target');
212 if ( $base =~ s/rtfm/article/i ) {
213 my ($ret, $msg) = $link->__Set( Field => 'Base', Value => $base );
214 warn "Updating base to $base: $msg for link ".$link->id;
216 if ( $target =~ s/rtfm/article/i ) {
217 my ($ret, $msg) = $link->__Set( Field => 'Target', Value => $target );
218 warn "Updating target to $target: $msg for link ".$link->id;
224 { # update Transactions
225 # we only keep article transactions at this point
227 use RT::Transactions;
228 # Next calls Type to check readability and Type calls _Accessible
229 # which called CurrentUserCanSee which calls Object which tries to instantiate
230 # an RT::FM::Article. Rather than a shim RT::FM::Article class, I'm just avoiding
231 # the ACL check since we're running around as the superuser.
232 local *RT::Transaction::Type = sub { shift->__Value('Type') };
233 my $transactions = RT::Transactions->new(RT->SystemUser);
234 $transactions->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article' );
235 while ( my $t = $transactions->Next ) {
236 my ($ret, $msg) = $t->__Set( Field => 'ObjectType', Value => 'RT::Article' );
237 warn "Updated Transaction ".$t->Id." to point to RT::Article";
240 # we also need to change links that point to articles
241 $transactions = RT::Transactions->new(RT->SystemUser);
242 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
243 $transactions->Limit( FIELD => 'NewValue', VALUE => 'rtfm', OPERATOR => 'LIKE' );
244 while ( my $t = $transactions->Next ) {
245 my $value = $t->__Value('NewValue');
246 $value =~ s/rtfm/article/;
247 my ($ret, $msg) = $t->__Set( Field => 'NewValue', Value => $value );
248 warn "Updated Transaction ".$t->Id." to link to $value";
252 { # update Attributes
253 # these are all things we should make real columns someday
255 my $attributes = RT::Attributes->new(RT->SystemUser);
256 $attributes->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
257 while ( my $a = $attributes->Next ) {
258 my ($ret,$msg) = $a->__Set( Field => 'ObjectType', Value => 'RT::Class' );
259 warn "Updating Attribute ".$a->Name." to point to RT::Class";