Upgrade to 4.2.8
[usit-rt.git] / etc / upgrade / upgrade-articles
CommitLineData
01e3b242
MKG
1#!/usr/bin/perl
2# BEGIN BPS TAGGED BLOCK {{{
3#
4# COPYRIGHT:
5#
3ffc5f4f 6# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
01e3b242
MKG
7# <sales@bestpractical.com>
8#
9# (Except where explicitly superseded by other copyright notices)
10#
11#
12# LICENSE:
13#
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
17# from www.gnu.org.
18#
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.
23#
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.
29#
30#
31# CONTRIBUTION SUBMISSION POLICY:
32#
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.)
38#
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.
47#
48# END BPS TAGGED BLOCK }}}
49use strict;
50use warnings;
51
52use lib "local/lib";
53use lib "lib";
54
55use RT;
56RT::LoadConfig();
3ffc5f4f 57RT->Config->Set('LogToSTDERR' => 'debug');
01e3b242
MKG
58RT::Init();
59
60$| = 1;
61
62my $db_name = RT->Config->Get('DatabaseName');
63my $db_type = RT->Config->Get('DatabaseType');
64
65my $dbh = $RT::Handle->dbh;
66
67my $found_fm_tables;
68foreach my $name ( $RT::Handle->_TableNames ) {
69 next unless $name =~ /^fm_/i;
70 $found_fm_tables->{lc $name}++;
71}
72
73unless ( $found_fm_tables->{fm_topics} && $found_fm_tables->{fm_objecttopics} ) {
74 warn "Couldn't find topics tables, it appears you have RTFM 2.0 or earlier.";
75 warn "This script cannot yet upgrade RTFM versions which are that old";
76 exit;
77}
78
79{ # port over Articles
80 my @columns = qw(id Name Summary SortOrder Class Parent URI Creator Created LastUpdatedBy LastUpdated);
81 copy_tables('FM_Articles','Articles',\@columns);
82
83}
84
85
86{ # port over Classes
87 my @columns = qw(id Name Description SortOrder Disabled Creator Created LastUpdatedBy LastUpdated);
88 if ( grep lc($_) eq 'hotlist', $RT::Handle->Fields('FM_Classes') ) {
89 push @columns, 'HotList';
90 }
91 copy_tables('FM_Classes','Classes',\@columns);
92}
93
94{ # port over Topics
95 my @columns = qw(id Parent Name Description ObjectType ObjectId);
96 copy_tables('FM_Topics','Topics',\@columns);
97}
98
99{ # port over ObjectTopics
100 my @columns = qw(id Topic ObjectType ObjectId);
101 copy_tables('FM_ObjectTopics','ObjectTopics',\@columns);
102}
103
104sub copy_tables {
105 my ($source, $dest, $columns) = @_;
106 my $column_list = join(', ',@$columns);
107 my $sql;
108 # SQLite: http://www.sqlite.org/lang_insert.html
109 if ( $db_type eq 'mysql' || $db_type eq 'SQLite' ) {
110 $sql = "insert into $dest ($column_list) select $column_list from $source";
111 }
112 # Oracle: http://www.adp-gmbh.ch/ora/sql/insert/select_and_subquery.html
113 elsif ( $db_type eq 'Pg' || $db_type eq 'Oracle' ) {
114 $sql = "insert into $dest ($column_list) (select $column_list from $source)";
115 }
116 $RT::Logger->debug($sql);
117 $dbh->do($sql);
118}
119
120{ # create ObjectClasses
121 # this logic will need updating when folks have an FM_ObjectClasses table
122 use RT::Classes;
123 use RT::ObjectClass;
124
125 my $classes = RT::Classes->new(RT->SystemUser);
126 $classes->UnLimit;
127 while ( my $class = $classes->Next ) {
128 my $objectclass = RT::ObjectClass->new(RT->SystemUser);
129 my ($ret, $msg ) = $objectclass->Create( Class => $class->Id, ObjectType => 'RT::System', ObjectId => 0 );
130 if ($ret) {
131 warn("Applied Class '".$class->Name."' globally");
132 } else {
133 warn("Couldn't create linkage for Class ".$class->Name.": $msg");
134 }
135 }
136}
137
138{ # update ACLs
139 use RT::ACL;
140 my $acl = RT::ACL->new(RT->SystemUser);
141 $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
142 $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::System' );
143 while ( my $ace = $acl->Next ) {
144 if ( $ace->__Value('ObjectType') eq 'RT::FM::Class' ) {
145 my ($ret, $msg ) = $ace->__Set( Field => 'ObjectType', Value => 'RT::Class');
146 warn "Fixing ACL ".$ace->Id." to refer to RT::Class: $msg";
147 } elsif ( $ace->__Value('ObjectType') eq 'RT::FM::System' ) {
148 my ($ret, $msg) = $ace->__Set(Field => 'ObjectType', Value => 'RT::System');
149 warn "Fixing ACL ".$ace->Id." to refer to RT::System: $msg";
150 }
151 }
152
153
154}
155
156{ # update CustomFields
157 use RT::CustomFields;
158 my $cfs = RT::CustomFields->new(RT->SystemUser);
159 $cfs->Limit( FIELD => 'LookupType', VALUE => 'RT::FM::Class-RT::FM::Article' );
3ffc5f4f 160 $cfs->{'find_disabled_rows'} = 1;
01e3b242
MKG
161 while ( my $cf = $cfs->Next ) {
162 my ($ret, $msg) = $cf->__Set( Field => 'LookupType', Value => 'RT::Class-RT::Article' );
163 warn "Update Custom Field LookupType for CF.".$cf->Id." $msg";
164 }
165}
166
167{ # update ObjectCustomFieldValues
168 use RT::ObjectCustomFieldValues;
169 my $ocfvs = RT::ObjectCustomFieldValues->new(RT->System);
170 $ocfvs->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article' );
3ffc5f4f 171 $ocfvs->{'find_expired_rows'} = 1;
01e3b242
MKG
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');
175 }
176
177}
178
179{ # update Topics
180 use RT::Topics;
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";
191 }
192 }
193}
194
195{ # update ObjectTopics
196 use RT::ObjectTopics;
197 my $otopics = RT::ObjectTopics->new(RT->SystemUser);
198 $otopics->UnLimit;
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";
203 }
204 }
205}
206
207{ # update Links
208 use RT::Links;
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;
218 }
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;
222 }
223
224 }
225}
226
227{ # update Transactions
228 # we only keep article transactions at this point
229 no warnings 'once';
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";
241 }
242
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";
252 }
253}
254
255{ # update Attributes
256 # these are all things we should make real columns someday
257 use RT::Attributes;
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";
263 }
264}