Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
#----------------------------------- |
4 |
# Copyright 2015 Vaara-kirjastot |
5 |
# |
6 |
# This file is part of Koha. |
7 |
# |
8 |
# Koha is free software; you can redistribute it and/or modify it under the |
9 |
# terms of the GNU General Public License as published by the Free Software |
10 |
# Foundation; either version 2 of the License, or (at your option) any later |
11 |
# version. |
12 |
# |
13 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
14 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
15 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License along |
18 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
19 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
#----------------------------------- |
21 |
|
22 |
use strict; |
23 |
use warnings; |
24 |
use C4::Context; |
25 |
use DBI; |
26 |
use Email::Valid; |
27 |
use POSIX qw(strftime); |
28 |
use Getopt::Long; |
29 |
use C4::Members::Messaging; |
30 |
|
31 |
my $helptext = " |
32 |
This script deletes misconfigured messaging preferences from the Koha database. A preference is |
33 |
misconfigured if the user has no valid contact information for that type of transport method. |
34 |
E.g user wants messages via e-mail while he has not provided an e-mail address. |
35 |
|
36 |
Options: |
37 |
-h|--help Prints this help documentation. |
38 |
-b|--backup:s Filename for backup file. Required in 'restore' mode and optional in 'delete' mode. |
39 |
Default value is messaging-prefs-backup_<date and time>. In 'delete' mode, if nothing |
40 |
gets deleted, the file will not be created. |
41 |
-d|--delete Activates the deletion mode (cannot be used simultaneously with mode 'restore'). |
42 |
-r|--restore Activates the restore mode (cannot be used simultaneously with mode 'delete'). |
43 |
-m|--methods=s{1,3} Optional. Specifies the transfer methods/types. The three types are: email phone sms. |
44 |
If not provided, all three types are used. |
45 |
|
46 |
Examples: |
47 |
./deleteMisconfiguredMessagingPrefs.pl -d -b |
48 |
./deleteMisconfiguredMessagingPrefs.pl -d -t email phone -b |
49 |
./deleteMisconfiguredMessagingPrefs.pl -d -t email phone sms -b my_backup_file |
50 |
./deleteMisconfiguredMessagingPrefs.pl -r -b my_backup_file |
51 |
\n"; |
52 |
|
53 |
my ($help, $delete, $filename, $restore, @methods); |
54 |
|
55 |
GetOptions( |
56 |
'h|help' => \$help, |
57 |
'd|delete' => \$delete, |
58 |
'r|restore' => \$restore, |
59 |
'b|backup:s' => \$filename, |
60 |
'm|methods=s{1,3}' => \@methods |
61 |
); |
62 |
|
63 |
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); |
64 |
my $are_we_doing_backups = 0; |
65 |
|
66 |
|
67 |
if ($help || $restore && $delete || !$restore && !$delete) { |
68 |
die $helptext; |
69 |
} |
70 |
if ($delete and $filename eq "" or length($filename) > 0) { |
71 |
$are_we_doing_backups = 1; |
72 |
$filename = (strftime "messaging-prefs-backup_%Y-%m-%d_%H-%M-%S", localtime) if ($filename eq ""); |
73 |
} |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
if ($delete) { |
87 |
my @deleted_prefs; |
88 |
|
89 |
# check if user has specified messaging transport methods |
90 |
if (@methods > 0 && @methods < 4) { |
91 |
my $correct_modes = 0; |
92 |
|
93 |
foreach my $type (@methods){ |
94 |
|
95 |
if ($type eq "email" or $type eq "phone" or $type eq "sms") { |
96 |
print "Deleting misconfigured $type messaging preferences\n"; |
97 |
|
98 |
# method exists, don't need to print warning anymore |
99 |
$correct_modes = 1; |
100 |
|
101 |
# which contact info field (primary email / primary phone / smsalertnumber) |
102 |
# should we check? |
103 |
my $contact; |
104 |
$contact = "email" if $type eq "email"; |
105 |
$contact = "phone" if $type eq "phone"; |
106 |
$contact = "smsalertnumber" if $type eq "sms"; |
107 |
|
108 |
# which validator should we use? |
109 |
my $validator; |
110 |
$validator = "email" if $type eq "email"; |
111 |
$validator = "phone" if $type eq "phone" or $type eq "sms"; |
112 |
|
113 |
# delete the misconfigured prefs and keep a counting them |
114 |
push(@deleted_prefs, C4::Members::Messaging::DeleteMisconfiguredPreference($type, $contact, $validator)); |
115 |
} |
116 |
} |
117 |
|
118 |
die "Missing or invalid in parameter --type values. See help.\n$helptext" if $correct_modes == 0; |
119 |
|
120 |
} else { |
121 |
|
122 |
# user did not specify any methods. so let's delete them all! |
123 |
print "Deleting all misconfigured messaging preferences\n"; |
124 |
push(@deleted_prefs, C4::Members::Messaging::DeleteAllMisconfiguredPreferences()); |
125 |
|
126 |
} |
127 |
|
128 |
BackupDeletedPrefs(@deleted_prefs) if $are_we_doing_backups; |
129 |
|
130 |
print "Deleted ".scalar(@deleted_prefs)." misconfigured messaging preferences in total.\n"; |
131 |
} |
132 |
elsif ($restore){ |
133 |
if (@methods > 0) { |
134 |
my $correct_modes = 0; |
135 |
for (my $i=0; $i < @methods; $i++){ |
136 |
if ($methods[$i] ne "email" and $methods[$i] ne "phone" and $methods[$i] ne "sms") { |
137 |
print "Invalid type $methods[$i]. Valid types are: email phone sms\n"; |
138 |
splice(@methods, $i); |
139 |
} |
140 |
} |
141 |
die "Missing parameter --type values. See help.\n$helptext" if @methods == 0; |
142 |
RestoreDeletedPreferences(@methods); |
143 |
} else { |
144 |
RestoreDeletedPreferences(); |
145 |
} |
146 |
} |
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
# restoring deleted prefs |
163 |
|
164 |
sub BackupDeletedPrefs { |
165 |
my @deleted = @_; |
166 |
|
167 |
open(my $fh, ">>", $filename) or die "Could not open file for writing.\n"; |
168 |
|
169 |
for (my $i=0; $i < @deleted; $i++){ |
170 |
say $fh $deleted[$i][0].",".$deleted[$i][1]; |
171 |
} |
172 |
} |
173 |
sub RestoreDeletedPreferences { |
174 |
my $count = 0; |
175 |
my @methods = @_; |
176 |
|
177 |
my $dbh = C4::Context->dbh(); |
178 |
open(my $fh, "<", $filename) or die "Could not open file for writing.\n"; |
179 |
|
180 |
my $query = "INSERT INTO borrower_message_transport_preferences (borrower_message_preference_id, message_transport_type) VALUES (?,?)"; |
181 |
my $sth = $dbh->prepare($query); |
182 |
|
183 |
# check if user has specified methods (types) |
184 |
if (@methods > 0) { |
185 |
while (my $line = <$fh>) { |
186 |
my @vars = split(',',$line); |
187 |
my @remlinebreak = split('\\n', $vars[1]); |
188 |
my $pref_id = $vars[0]; |
189 |
my $type = $remlinebreak[0]; |
190 |
|
191 |
if (grep(/$type/,@methods)) { |
192 |
$sth->execute($pref_id, $type) or $count--; |
193 |
$count++; |
194 |
} |
195 |
} |
196 |
} else { |
197 |
# simply walk through each line and restore every single line |
198 |
while (my $line = <$fh>) { |
199 |
my @vars = split(',',$line); |
200 |
next if @vars == 0; |
201 |
my @remlinebreak = split('\\n', $vars[1]); |
202 |
next if not defined $remlinebreak[0]; |
203 |
my $pref_id = $vars[0]; |
204 |
my $type = $remlinebreak[0]; |
205 |
|
206 |
$sth->execute($pref_id, $type) or $count--; |
207 |
$count++; |
208 |
} |
209 |
} |
210 |
|
211 |
print "Restored $count preferences.\n"; |
212 |
|
213 |
} |