2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2013 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 }}}
52 use lib "/opt/rt3/local/lib";
53 use lib "/opt/rt3/lib";
57 RT->Config->Set('LogToScreen' => 'debug');
62 my $db_name = RT->Config->Get('DatabaseName');
63 my $db_type = RT->Config->Get('DatabaseType');
65 my $dbh = $RT::Handle->dbh;
67 my $sth = $dbh->table_info( '', undef, undef, "'TABLE'");
69 while ( my $table = $sth->fetchrow_hashref ) {
70 my $name = $table->{TABLE_NAME} || $table->{'table_name'}; # Oracle's table_info affected by NAME_lc
71 next unless $name =~ /^fm_/i;
72 $found_fm_tables->{lc $name}++;
75 unless ( $found_fm_tables->{fm_topics} && $found_fm_tables->{fm_objecttopics} ) {
76 warn "Couldn't find topics tables, it appears you have RTFM 2.0 or earlier.";
77 warn "This script cannot yet upgrade RTFM versions which are that old";
81 { # port over Articles
82 my @columns = qw(id Name Summary SortOrder Class Parent URI Creator Created LastUpdatedBy LastUpdated);
83 copy_tables('FM_Articles','Articles',\@columns);
89 my @columns = qw(id Name Description SortOrder Disabled Creator Created LastUpdatedBy LastUpdated);
90 if ( grep lc($_) eq 'hotlist', $RT::Handle->Fields('FM_Classes') ) {
91 push @columns, 'HotList';
93 copy_tables('FM_Classes','Classes',\@columns);
97 my @columns = qw(id Parent Name Description ObjectType ObjectId);
98 copy_tables('FM_Topics','Topics',\@columns);
101 { # port over ObjectTopics
102 my @columns = qw(id Topic ObjectType ObjectId);
103 copy_tables('FM_ObjectTopics','ObjectTopics',\@columns);
107 my ($source, $dest, $columns) = @_;
108 my $column_list = join(', ',@$columns);
110 # SQLite: http://www.sqlite.org/lang_insert.html
111 if ( $db_type eq 'mysql' || $db_type eq 'SQLite' ) {
112 $sql = "insert into $dest ($column_list) select $column_list from $source";
114 # Oracle: http://www.adp-gmbh.ch/ora/sql/insert/select_and_subquery.html
115 elsif ( $db_type eq 'Pg' || $db_type eq 'Oracle' ) {
116 $sql = "insert into $dest ($column_list) (select $column_list from $source)";
118 $RT::Logger->debug($sql);
122 { # create ObjectClasses
123 # this logic will need updating when folks have an FM_ObjectClasses table
127 my $classes = RT::Classes->new(RT->SystemUser);
129 while ( my $class = $classes->Next ) {
130 my $objectclass = RT::ObjectClass->new(RT->SystemUser);
131 my ($ret, $msg ) = $objectclass->Create( Class => $class->Id, ObjectType => 'RT::System', ObjectId => 0 );
133 warn("Applied Class '".$class->Name."' globally");
135 warn("Couldn't create linkage for Class ".$class->Name.": $msg");
142 my $acl = RT::ACL->new(RT->SystemUser);
143 $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
144 $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::System' );
145 while ( my $ace = $acl->Next ) {
146 if ( $ace->__Value('ObjectType') eq 'RT::FM::Class' ) {
147 my ($ret, $msg ) = $ace->__Set( Field => 'ObjectType', Value => 'RT::Class');
148 warn "Fixing ACL ".$ace->Id." to refer to RT::Class: $msg";
149 } elsif ( $ace->__Value('ObjectType') eq 'RT::FM::System' ) {
150 my ($ret, $msg) = $ace->__Set(Field => 'ObjectType', Value => 'RT::System');
151 warn "Fixing ACL ".$ace->Id." to refer to RT::System: $msg";
158 { # update CustomFields
159 use RT::CustomFields;
160 my $cfs = RT::CustomFields->new(RT->SystemUser);
161 $cfs->Limit( FIELD => 'LookupType', VALUE => 'RT::FM::Class-RT::FM::Article' );
162 while ( my $cf = $cfs->Next ) {
163 my ($ret, $msg) = $cf->__Set( Field => 'LookupType', Value => 'RT::Class-RT::Article' );
164 warn "Update Custom Field LookupType for CF.".$cf->Id." $msg";
168 { # update ObjectCustomFieldValues
169 use RT::ObjectCustomFieldValues;
170 my $ocfvs = RT::ObjectCustomFieldValues->new(RT->System);
171 $ocfvs->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article' );
172 while ( my $ocfv = $ocfvs->Next ) {
173 my ($ret, $msg) = $ocfv->__Set( Field => 'ObjectType', Value => 'RT::Article' );
174 warn "Updated CF ".$ocfv->__Value('CustomField')." Value for Article ".$ocfv->__Value('ObjectId');
181 my $topics = RT::Topics->new(RT->SystemUser);
182 $topics->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
183 $topics->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::System' );
184 while ( my $topic = $topics->Next ) {
185 if ( $topic->__Value('ObjectType') eq 'RT::FM::Class' ) {
186 my ($ret, $msg ) = $topic->__Set( Field => 'ObjectType', Value => 'RT::Class');
187 warn "Fixing Topic ".$topic->Id." to refer to RT::Class: $msg";
188 } elsif ( $topic->__Value('ObjectType') eq 'RT::FM::System' ) {
189 my ($ret, $msg) = $topic->__Set(Field => 'ObjectType', Value => 'RT::System');
190 warn "Fixing Topic ".$topic->Id." to refer to RT::System: $msg";
195 { # update ObjectTopics
196 use RT::ObjectTopics;
197 my $otopics = RT::ObjectTopics->new(RT->SystemUser);
199 while ( my $otopic = $otopics->Next ) {
200 if ( $otopic->ObjectType eq 'RT::FM::Article' ) {
201 my ($ret, $msg) = $otopic->SetObjectType('RT::Article');
202 warn "Fixing Topic ".$otopic->Topic." to apply to article: $msg";
209 my $links = RT::Links->new(RT->SystemUser);
210 $links->Limit(FIELD => 'Base', VALUE => 'rtfm', OPERATOR => 'LIKE', SUBCLAUSE => 'stopanding', ENTRYAGGREGATOR => 'OR');
211 $links->Limit(FIELD => 'Target', VALUE => 'rtfm', OPERATOR => 'LIKE', SUBCLAUSE => 'stopanding', ENTRYAGGREGATOR => 'OR' );
212 while ( my $link = $links->Next ) {
213 my $base = $link->__Value('Base');
214 my $target = $link->__Value('Target');
215 if ( $base =~ s/rtfm/article/i ) {
216 my ($ret, $msg) = $link->__Set( Field => 'Base', Value => $base );
217 warn "Updating base to $base: $msg for link ".$link->id;
219 if ( $target =~ s/rtfm/article/i ) {
220 my ($ret, $msg) = $link->__Set( Field => 'Target', Value => $target );
221 warn "Updating target to $target: $msg for link ".$link->id;
227 { # update Transactions
228 # we only keep article transactions at this point
230 use RT::Transactions;
231 # Next calls Type to check readability and Type calls _Accessible
232 # which called CurrentUserCanSee which calls Object which tries to instantiate
233 # an RT::FM::Article. Rather than a shim RT::FM::Article class, I'm just avoiding
234 # the ACL check since we're running around as the superuser.
235 local *RT::Transaction::Type = sub { shift->__Value('Type') };
236 my $transactions = RT::Transactions->new(RT->SystemUser);
237 $transactions->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article' );
238 while ( my $t = $transactions->Next ) {
239 my ($ret, $msg) = $t->__Set( Field => 'ObjectType', Value => 'RT::Article' );
240 warn "Updated Transaction ".$t->Id." to point to RT::Article";
243 # we also need to change links that point to articles
244 $transactions = RT::Transactions->new(RT->SystemUser);
245 $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
246 $transactions->Limit( FIELD => 'NewValue', VALUE => 'rtfm', OPERATOR => 'LIKE' );
247 while ( my $t = $transactions->Next ) {
248 my $value = $t->__Value('NewValue');
249 $value =~ s/rtfm/article/;
250 my ($ret, $msg) = $t->__Set( Field => 'NewValue', Value => $value );
251 warn "Updated Transaction ".$t->Id." to link to $value";
255 { # update Attributes
256 # these are all things we should make real columns someday
258 my $attributes = RT::Attributes->new(RT->SystemUser);
259 $attributes->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
260 while ( my $a = $attributes->Next ) {
261 my ($ret,$msg) = $a->__Set( Field => 'ObjectType', Value => 'RT::Class' );
262 warn "Updating Attribute ".$a->Name." to point to RT::Class";