Upgrade.pm 36.4 KB
Newer Older
1
2
3
4
5
# Upgrade.pm - This module gathers all subroutines used to upgrade Sympa data structures
#<!-- RCS Identication ; $Revision$ --> 

#
# Sympa - SYsteme de Multi-Postage Automatique
6
7
8
9
10
#
# Copyright (c) 1997, 1998, 1999 Institut Pasteur & Christophe Wolfhugel
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
# Copyright (c) 2011, 2012, 2013, 2014 GIP RENATER
11
12
13
14
15
16
17
18
19
20
21
22
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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
23
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
25
26
27
28
29

package Upgrade;

use strict;

use Carp;
30
use POSIX qw(strftime);
31
32
33

use Conf;
use Log;
34
use Sympa::Constants;
35
use SDM;
36
use Data::Dumper;
37

38
39
## Return the previous Sympa version, ie the one listed in data_structure.version
sub get_previous_version {
40
    my $version_file = "$Conf::Conf{'etc'}/data_structure.version";
41
42
43
44
    my $previous_version;
    
    if (-f $version_file) {
	unless (open VFILE, $version_file) {
45
	    &Log::do_log('err', "Unable to open %s : %s", $version_file, $!);
46
47
48
49
50
51
52
53
54
55
	    return undef;
	}
	while (<VFILE>) {
	    next if /^\s*$/;
	    next if /^\s*\#/;
	    chomp;
	    $previous_version = $_;
	    last;
	}
	close VFILE;
56
	
57
58
59
60
61
62
63
	return $previous_version;
    }
    
    return undef;
}

sub update_version {
64
    my $version_file = "$Conf::Conf{'etc'}/data_structure.version";
65
66
67

    ## Saving current version if required
    unless (open VFILE, ">$version_file") {
68
	&Log::do_log('err', "Unable to write %s ; sympa.pl needs write access on %s directory : %s", $version_file, $Conf::Conf{'etc'}, $!);
69
70
71
	return undef;
    }
    printf VFILE "# This file is automatically created by sympa.pl after installation\n# Unless you know what you are doing, you should not modify it\n";
72
    printf VFILE "%s\n", Sympa::Constants::VERSION;
73
74
75
76
77
78
79
80
81
82
    close VFILE;
    
    return 1;
}


## Upgrade data structure from one version to another
sub upgrade {
    my ($previous_version, $new_version) = @_;

83
    &Log::do_log('notice', 'Upgrade::upgrade(%s, %s)', $previous_version, $new_version);
84
    
85
    if (&tools::lower_version($new_version, $previous_version)) {
86
	&Log::do_log('notice', 'Installing  older version of Sympa ; no upgrade operation is required');
87
88
89
	return 1;
    }

90
91
92
    ## Always update config.bin files while upgrading
    &Conf::delete_binaries();

93
94
    ## Always update config.bin files while upgrading
    ## This is especially useful for character encoding reasons
95
    &Log::do_log('notice','Rebuilding config.bin files for ALL lists...it may take a while...');
96
97
98
    my $all_lists = &List::get_lists('*',{'reload_config' => 1});

    ## Empty the admin_table entries and recreate them
99
    &Log::do_log('notice','Rebuilding the admin_table...');
100
    &List::delete_all_list_admin();
101
102
103
    foreach my $list (@$all_lists) {
	$list->sync_include_admin();
    }
104

105
106
107
    ## Migration to tt2
    if (&tools::lower_version($previous_version, '4.2b')) {

108
	&Log::do_log('notice','Migrating templates to TT2 format...');	
109
	
110
	my $tpl_script = Sympa::Constants::SCRIPTDIR . '/tpl2tt2.pl';
111
	unless (open EXEC, "$tpl_script|") {
112
	    &Log::do_log('err', "Unable to run $tpl_script");
113
114
115
116
	    return undef;
	}
	close EXEC;
	
117
	&Log::do_log('notice','Rebuilding web archives...');
118
119
120
121
	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {

	    next unless (defined $list->{'admin'}{'web_archive'});
122
	    my $file = $Conf::Conf{'queueoutgoing'}.'/.rebuild.'.$list->get_list_id();
123
124
	    
	    unless (open REBUILD, ">$file") {
125
		&Log::do_log('err','Cannot create %s', $file);
126
127
128
129
130
131
132
133
134
		next;
	    }
	    print REBUILD ' ';
	    close REBUILD;
	}	
    }
    
    ## Initializing the new admin_table
    if (&tools::lower_version($previous_version, '4.2b.4')) {
135
	&Log::do_log('notice','Initializing the new admin_table...');
136
137
138
139
140
141
142
143
	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {
	    $list->sync_include_admin();
	}
    }

    ## Move old-style web templates out of the include_path
    if (&tools::lower_version($previous_version, '5.0.1')) {
144
145
	&Log::do_log('notice','Old web templates HTML structure is not compliant with latest ones.');
	&Log::do_log('notice','Moving old-style web templates out of the include_path...');
146
147
148

	my @directories;

149
150
	if (-d "$Conf::Conf{'etc'}/web_tt2") {
	    push @directories, "$Conf::Conf{'etc'}/web_tt2";
151
152
153
	}

	## Go through Virtual Robots
154
	foreach my $vr (keys %{$Conf::Conf{'robots'}}) {
155

156
157
	    if (-d "$Conf::Conf{'etc'}/$vr/web_tt2") {
		push @directories, "$Conf::Conf{'etc'}/$vr/web_tt2";
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
	    }
	}

	## Search in V. Robot Lists
	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {
	    if (-d "$list->{'dir'}/web_tt2") {
		push @directories, "$list->{'dir'}/web_tt2";
	    }	    
	}

	my @templates;

	foreach my $d (@directories) {
	    unless (opendir DIR, $d) {
		printf STDERR "Error: Cannot read %s directory : %s", $d, $!;
		next;
	    }
	    
	    foreach my $tt2 (sort grep(/\.tt2$/,readdir DIR)) {
		push @templates, "$d/$tt2";
	    }
	    
	    closedir DIR;
	}

	foreach my $tpl (@templates) {
	    unless (rename $tpl, "$tpl.oldtemplate") {
		printf STDERR "Error : failed to rename $tpl to $tpl.oldtemplate : $!\n";
		next;
	    }

190
	    &Log::do_log('notice','File %s renamed %s', $tpl, "$tpl.oldtemplate");
191
192
193
194
195
196
	}
    }


    ## Clean buggy list config files
    if (&tools::lower_version($previous_version, '5.1b')) {
197
	&Log::do_log('notice','Cleaning buggy list config files...');
198
199
200
201
202
203
204
205
	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {
	    $list->save_config('listmaster@'.$list->{'domain'});
	}
    }

    ## Fix a bug in Sympa 5.1
    if (&tools::lower_version($previous_version, '5.1.2')) {
206
	&Log::do_log('notice','Rename archives/log. files...');
207
208
209
210
211
212
213
214
215
216
217
218
	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {
	    my $l = $list->{'name'}; 
	    if (-f $list->{'dir'}.'/archives/log.') {
		rename $list->{'dir'}.'/archives/log.', $list->{'dir'}.'/archives/log.00';
	    }
	}
    }

    if (&tools::lower_version($previous_version, '5.2a.1')) {

	## Fill the robot_subscriber and robot_admin fields in DB
219
	&Log::do_log('notice','Updating the new robot_subscriber and robot_admin  Db fields...');
220

221
	foreach my $r (keys %{$Conf::Conf{'robots'}}) {
222
223
224
225
	    my $all_lists = &List::get_lists($r, {'skip_sync_admin' => 1});
	    foreach my $list ( @$all_lists ) {
		
		foreach my $table ('subscriber','admin') {
226
		    unless (&SDM::do_query("UPDATE %s_table SET robot_%s=%s WHERE (list_%s=%s)",
227
228
		    $table,
		    $table,
229
		    &SDM::quote($r),
230
		    $table,
231
232
233
		    &SDM::quote($list->{'name'}))) {
			&Log::do_log('err','Unable to fille the robot_admin and robot_subscriber fields in database for robot %s.',$r);
			&List::send_notify_to_listmaster('upgrade_failed', $Conf::Conf{'domain'},{'error' => $SDM::db_source->{'db_handler'}->errstr});
234
235
236
237
238
239
240
241
242
243
			return undef;
		    }
		}
		
		## Force Sync_admin
		$list = new List ($list->{'name'}, $list->{'domain'}, {'force_sync_admin' => 1});
	    }
	}

	## Rename web archive directories using 'domain' instead of 'host'
244
	&Log::do_log('notice','Renaming web archive directories with the list domain...');
245
	
246
	my $root_dir = &Conf::get_robot_conf($Conf::Conf{'domain'},'arc_path');
247
	unless (opendir ARCDIR, $root_dir) {
248
	    &Log::do_log('err',"Unable to open $root_dir : $!");
249
250
251
252
253
254
255
256
	    return undef;
	}
	
	foreach my $dir (sort readdir(ARCDIR)) {
	    next if (($dir =~ /^\./o) || (! -d $root_dir.'/'.$dir)); ## Skip files and entries starting with '.'
		     
	    my ($listname, $listdomain) = split /\@/, $dir;

sikeda's avatar
sikeda committed
257
	    next unless ($listname && $listdomain);
258
259
260

	    my $list = new List $listname;
	    unless (defined $list) {
261
		&Log::do_log('notice',"Skipping unknown list $listname");
262
263
264
265
266
267
268
269
		next;
	    }
	    
	    if ($listdomain ne $list->{'domain'}) {
		my $old_path = $root_dir.'/'.$listname.'@'.$listdomain;		
		my $new_path = $root_dir.'/'.$listname.'@'.$list->{'domain'};

		if (-d $new_path) {
270
		    &Log::do_log('err',"Could not rename %s to %s ; directory already exists", $old_path, $new_path);
271
272
273
		    next;
		}else {
		    unless (rename $old_path, $new_path) {
274
			&Log::do_log('err',"Failed to rename %s to %s : %s", $old_path, $new_path, $!);
275
276
			next;
		    }
277
		    &Log::do_log('notice', "Renamed %s to %s", $old_path, $new_path);
278
279
280
281
282
283
284
285
286
287
		}
	    }		     
	}
	close ARCDIR;
	
    }

    ## DB fields of enum type have been changed to int
    if (&tools::lower_version($previous_version, '5.2a.1')) {
	
sikeda's avatar
sikeda committed
288
	if (&SDM::use_db && $Conf::Conf{'db_type'} eq 'mysql') {
289
290
291
292
293
	    my %check = ('subscribed_subscriber' => 'subscriber_table',
			 'included_subscriber' => 'subscriber_table',
			 'subscribed_admin' => 'admin_table',
			 'included_admin' => 'admin_table');
	    
294
295
    my $dbh = &SDM::db_get_handler();

296
297
298
299
300
301
302
303
304
305
	    foreach my $field (keys %check) {

		my $statement;
				
		## Query the Database
		$statement = sprintf "SELECT max(%s) FROM %s", $field, $check{$field};
		
		my $sth;
		
		unless ($sth = $dbh->prepare($statement)) {
306
		    &Log::do_log('err','Unable to prepare SQL statement : %s', $dbh->errstr);
307
308
309
310
		    return undef;
		}
		
		unless ($sth->execute) {
311
		    &Log::do_log('err','Unable to execute SQL statement "%s" : %s', $statement, $dbh->errstr);
312
313
314
315
316
317
318
319
320
321
		    return undef;
		}
		
		my $max = $sth->fetchrow();
		$sth->finish();		

		## '0' has been mapped to 1 and '1' to 2
		## Restore correct field value
		if ($max > 1) {
		    ## 1 to 0
322
		    &Log::do_log('notice', 'Fixing DB field %s ; turning 1 to 0...', $field);
323
324
325
326
		    
		    my $statement = sprintf "UPDATE %s SET %s=%d WHERE (%s=%d)", $check{$field}, $field, 0, $field, 1;
		    my $rows;
		    unless ($rows = $dbh->do($statement)) {
327
			&Log::do_log('err','Unable to execute SQL statement "%s" : %s', $statement, $dbh->errstr);
328
329
330
			return undef;
		    }
		    
331
		    &Log::do_log('notice', 'Updated %d rows', $rows);
332
333

		    ## 2 to 1
334
		    &Log::do_log('notice', 'Fixing DB field %s ; turning 2 to 1...', $field);
335
		    
336
		    $statement = sprintf "UPDATE %s SET %s=%d WHERE (%s=%d)", $check{$field}, $field, 1, $field, 2;
337

338
		    unless ($rows = $dbh->do($statement)) {
339
			&Log::do_log('err','Unable to execute SQL statement "%s" : %s', $statement, $dbh->errstr);
340
341
342
			return undef;
		    }
		    
343
		    &Log::do_log('notice', 'Updated %d rows', $rows);		    
344
345

		}
346
347

		## Set 'subscribed' data field to '1' is none of 'subscribed' and 'included' is set		
348
		$statement = "UPDATE subscriber_table SET subscribed_subscriber=1 WHERE ((included_subscriber IS NULL OR included_subscriber!=1) AND (subscribed_subscriber IS NULL OR subscribed_subscriber!=1))";
349
		
350
		&Log::do_log('notice','Updating subscribed field of the subscriber table...');
351
352
		my $rows = $dbh->do($statement);
		unless (defined $rows) {
353
		    &Log::fatal_err("Unable to execute SQL statement %s : %s", $statement, $dbh->errstr);	    
354
		}
355
		&Log::do_log('notice','%d rows have been updated', $rows);
356
				
357
358
359
360
361
362
363
	    }
	}
    }

    ## Rename bounce sub-directories
    if (&tools::lower_version($previous_version, '5.2a.1')) {

364
	&Log::do_log('notice','Renaming bounce sub-directories adding list domain...');
365
	
366
	my $root_dir = &Conf::get_robot_conf($Conf::Conf{'domain'},'bounce_path');
367
	unless (opendir BOUNCEDIR, $root_dir) {
368
	    &Log::do_log('err',"Unable to open $root_dir : $!");
369
370
371
372
373
374
375
376
377
378
379
	    return undef;
	}
	
	foreach my $dir (sort readdir(BOUNCEDIR)) {
	    next if (($dir =~ /^\./o) || (! -d $root_dir.'/'.$dir)); ## Skip files and entries starting with '.'
		     
	    next if ($dir =~ /\@/); ## Directory already include the list domain

	    my $listname = $dir;
	    my $list = new List $listname;
	    unless (defined $list) {
380
		&Log::do_log('notice',"Skipping unknown list $listname");
381
382
383
384
385
386
387
		next;
	    }
	    
	    my $old_path = $root_dir.'/'.$listname;		
	    my $new_path = $root_dir.'/'.$listname.'@'.$list->{'domain'};
	    
	    if (-d $new_path) {
388
		&Log::do_log('err',"Could not rename %s to %s ; directory already exists", $old_path, $new_path);
389
390
391
		next;
	    }else {
		unless (rename $old_path, $new_path) {
392
		    &Log::do_log('err',"Failed to rename %s to %s : %s", $old_path, $new_path, $!);
393
394
		    next;
		}
395
		&Log::do_log('notice', "Renamed %s to %s", $old_path, $new_path);
396
397
398
399
400
401
402
403
	    }
	}
	close BOUNCEDIR;
    }

    ## Update lists config using 'include_list'
    if (&tools::lower_version($previous_version, '5.2a.1')) {
	
404
	&Log::do_log('notice','Update lists config using include_list parameter...');
405
406
407
408
409
410
411
412
413
414

	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {

	    if (defined $list->{'admin'}{'include_list'}) {
	    
		foreach my $index (0..$#{$list->{'admin'}{'include_list'}}) {
		    my $incl = $list->{'admin'}{'include_list'}[$index];
		    my $incl_list = new List ($incl);
		    
415
		    if (defined $incl_list &
416
			$incl_list->{'domain'} ne $list->{'domain'}) {
417
			&Log::do_log('notice','Update config file of list %s, including list %s', $list->get_list_id(), $incl_list->get_list_id());
418
419
420
421
422
423
424
425
426
427
428
429
430
			
			$list->{'admin'}{'include_list'}[$index] = $incl_list->get_list_id();

			$list->save_config('listmaster@'.$list->{'domain'});
		    }
		}
	    }
	}	
    }

    ## New mhonarc ressource file with utf-8 recoding
    if (&tools::lower_version($previous_version, '5.3a.6')) {
	
431
	&Log::do_log('notice','Looking for customized mhonarc-ressources.tt2 files...');
432
433
	foreach my $vr (keys %{$Conf::Conf{'robots'}}) {
	    my $etc_dir = $Conf::Conf{'etc'};
434

435
	    if ($vr ne $Conf::Conf{'domain'}) {
436
437
438
439
440
441
		$etc_dir .= '/'.$vr;
	    }

	    if (-f $etc_dir.'/mhonarc-ressources.tt2') {
		my $new_filename = $etc_dir.'/mhonarc-ressources.tt2'.'.'.time;
		rename $etc_dir.'/mhonarc-ressources.tt2', $new_filename;
442
		&Log::do_log('notice', "Custom %s file has been backed up as %s", $etc_dir.'/mhonarc-ressources.tt2', $new_filename);
443
		&List::send_notify_to_listmaster('file_removed',$Conf::Conf{'domain'},
444
445
446
447
448
						 [$etc_dir.'/mhonarc-ressources.tt2', $new_filename]);
	    }
	}


449
	&Log::do_log('notice','Rebuilding web archives...');
450
451
452
453
	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {

	    next unless (defined $list->{'admin'}{'web_archive'});
454
	    my $file = $Conf::Conf{'queueoutgoing'}.'/.rebuild.'.$list->get_list_id();
455
456
	    
	    unless (open REBUILD, ">$file") {
457
		&Log::do_log('err','Cannot create %s', $file);
458
459
460
461
462
463
464
465
466
467
468
		next;
	    }
	    print REBUILD ' ';
	    close REBUILD;
	}	

    }

    ## Changed shared documents name encoding
    ## They are Q-encoded therefore easier to store on any filesystem with any encoding
    if (&tools::lower_version($previous_version, '5.3a.8')) {
469
	&Log::do_log('notice','Q-Encoding web documents filenames...');
470

471
	Language::PushLang($Conf::Conf{'lang'});
472
473
474
	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {
	    if (-d $list->{'dir'}.'/shared') {
475
		&Log::do_log('notice','  Processing list %s...', $list->get_list_address());
476
477
478
479
480
481
482

		## Determine default lang for this list
		## It should tell us what character encoding was used for filenames
		&Language::SetLang($list->{'admin'}{'lang'});
		my $list_encoding = &Language::GetCharset();

		my $count = &tools::qencode_hierarchy($list->{'dir'}.'/shared', $list_encoding);
483
484

		if ($count) {
485
		    &Log::do_log('notice', 'List %s : %d filenames has been changed', $list->{'name'}, $count);
486
487
488
		}
	    }
	}
489
490
	Language::PopLang();
    }
491

492
    ## We now support UTF-8 only for custom templates, config files, headers and footers, info files
493
    ## + web_tt2, scenari, create_list_templatee, families
olivier.salaun's avatar
olivier.salaun committed
494
    if (&tools::lower_version($previous_version, '5.3b.3')) {
495
	&Log::do_log('notice','Encoding all custom files to UTF-8...');
496
497
498
499

	my (@directories, @files);

	## Site level
500
	foreach my $type ('mail_tt2','web_tt2','scenari','create_list_templates','families') {
501
502
	    if (-d $Conf::Conf{'etc'}.'/'.$type) {
		push @directories, [$Conf::Conf{'etc'}.'/'.$type, $Conf::Conf{'lang'}];
503
	    }
504
	}
505

506
	foreach my $f (
507
508
509
510
511
	    Conf::get_sympa_conf(),
	    Conf::get_wwsympa_conf(),
	    $Conf::Conf{'etc'}.'/'.'topics.conf',
	    $Conf::Conf{'etc'}.'/'.'auth.conf'
	) {
512
	    if (-f $f) {
513
		push @files, [$f, $Conf::Conf{'lang'}];
514
515
516
517
	    }
	}

	## Go through Virtual Robots
518
	foreach my $vr (keys %{$Conf::Conf{'robots'}}) {
519
	    foreach my $type ('mail_tt2','web_tt2','scenari','create_list_templates','families') {
520
521
		if (-d $Conf::Conf{'etc'}.'/'.$vr.'/'.$type) {
		    push @directories, [$Conf::Conf{'etc'}.'/'.$vr.'/'.$type, &Conf::get_robot_conf($vr, 'lang')];
522
		}
523
524
	    }

525
	    foreach my $f ('robot.conf','topics.conf','auth.conf') {
526
527
		if (-f $Conf::Conf{'etc'}.'/'.$vr.'/'.$f) {
		    push @files, [$Conf::Conf{'etc'}.'/'.$vr.'/'.$f, $Conf::Conf{'lang'}];
528
529
530
531
532
533
534
		}
	    }
	}

	## Search in Lists
	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {
535
	    foreach my $f ('config','info','homepage','message.header','message.footer') {
536
537
538
539
540
		if (-f $list->{'dir'}.'/'.$f){
		    push @files, [$list->{'dir'}.'/'.$f, $list->{'admin'}{'lang'}];
		}
	    }

541
542
543
544
545
546
	    foreach my $type ('mail_tt2','web_tt2','scenari') {
		my $directory = $list->{'dir'}.'/'.$type;
		if (-d $directory) {
		    push @directories, [$directory, $list->{'admin'}{'lang'}];
		}	    
	    }
547
548
549
550
551
552
553
554
	}

	## Search language directories
	foreach my $pair (@directories) {
	    my ($d, $lang) = @$pair;
	    unless (opendir DIR, $d) {
		next;
	    }
555
556
557
558
559
560

	    if ($d =~ /(mail_tt2|web_tt2)$/) {
		foreach my $subdir (grep(/^[a-z]{2}(_[A-Z]{2})?$/, readdir DIR)) {
		    if (-d "$d/$subdir") {
			push @directories, ["$d/$subdir", $subdir];
		    }
561
		}
562
563
564
565
566
		closedir DIR;

	    }elsif ($d =~ /(create_list_templates|families)$/) {
		foreach my $subdir (grep(/^\w+$/, readdir DIR)) {
		    if (-d "$d/$subdir") {
567
			push @directories, ["$d/$subdir", $Conf::Conf{'lang'}];
568
569
570
		    }
		}
		closedir DIR;
571
572
573
574
575
576
577
578
	    }
	}

	foreach my $pair (@directories) {
	    my ($d, $lang) = @$pair;
	    unless (opendir DIR, $d) {
		next;
	    }
579
	    foreach my $file (readdir DIR) {
sikeda's avatar
sikeda committed
580
581
		next unless (($d =~ /mail_tt2|web_tt2|create_list_templates|families/ && $file =~ /\.tt2$/) ||
			     ($d =~ /scenari$/ && $file =~ /\w+\.\w+$/));
582
		push @files, [$d.'/'.$file, $lang];
583
584
585
586
587
588
589
	    }
	    closedir DIR;
	}

	## Do the encoding modifications
	## Previous versions of files are backed up with the date extension
	my $total = &to_utf8(\@files);
590
	&Log::do_log('notice','%d files have been modified', $total);
591
592
    }

593
594
595
596
    ## giving up subscribers flat files ; moving subscribers to the DB
    ## Also giving up old 'database' mode
    if (&tools::lower_version($previous_version, '5.4a.1')) {
	
597
	&Log::do_log('notice','Looking for lists with user_data_source parameter set to file or database...');
598
599
600
601
602
603

	my $all_lists = &List::get_lists('*');
	foreach my $list ( @$all_lists ) {

	    if ($list->{'admin'}{'user_data_source'} eq 'file') {

604
		&Log::do_log('notice','List %s ; changing user_data_source from file to include2...', $list->{'name'});
605
		
606
		my @users = &List::_load_list_members_file("$list->{'dir'}/subscribers");
607
608
609
610
611
		
		$list->{'admin'}{'user_data_source'} = 'include2';
		$list->{'total'} = 0;
		
		## Add users to the DB
612
613
614
615
		$list->add_list_member(@users);
		my $total = $list->{'add_outcome'}{'added_members'};
		if (defined $list->{'add_outcome'}{'errors'}) {
		    &Log::do_log('err', 'Failed to add users: %s',$list->{'add_outcome'}{'errors'}{'error_message'});
616
617
		}
		
618
		&Log::do_log('notice','%d subscribers have been loaded into the database', $total);
619
		
620
		unless ($list->save_config('automatic')) {
621
		    &Log::do_log('err', 'Failed to save config file for list %s', $list->{'name'});
622
623
624
		}
	    }elsif ($list->{'admin'}{'user_data_source'} eq 'database') {

625
		&Log::do_log('notice','List %s ; changing user_data_source from database to include2...', $list->{'name'});
626

627
		unless ($list->update_list_member('*', {'subscribed' => 1})) {
628
		    &Log::do_log('err', 'Failed to update subscribed DB field');
629
630
631
632
		}

		$list->{'admin'}{'user_data_source'} = 'include2';

633
		unless ($list->save_config('automatic')) {
634
		    &Log::do_log('err', 'Failed to save config file for list %s', $list->{'name'});
635
636
637
638
		}
	    }
	}
    }
639
    
640
641
642
    if (&tools::lower_version($previous_version, '5.5a.1')) {

      ## Remove OTHER/ subdirectories in bounces
643
      &Log::do_log('notice', "Removing obsolete OTHER/ bounce directories");
644
      if (opendir BOUNCEDIR, &Conf::get_robot_conf($Conf::Conf{'domain'}, 'bounce_path')) {
645
646
	
	foreach my $subdir (sort grep (!/^\.+$/,readdir(BOUNCEDIR))) {
647
	  my $other_dir = &Conf::get_robot_conf($Conf::Conf{'domain'}, 'bounce_path').'/'.$subdir.'/OTHER';
648
	  if (-d $other_dir) {
649
650
	    &tools::remove_dir($other_dir);
	    &Log::do_log('notice', "Directory $other_dir removed");
651
652
653
654
655
656
	  }
	}
	
	close BOUNCEDIR;
 
      }else {
657
	&Log::do_log('err', "Failed to open directory $Conf::Conf{'queuebounce'} : $!");	
658
659
660
      }

   }
661

662
663
664
665
666
   if (&tools::lower_version($previous_version, '6.1b.5')) {
		## Encoding of shared documents was not consistent with recent versions of MIME::Encode
		## MIME::EncWords::encode_mimewords() used to encode characters -!*+/ 
		## Now these characters are preserved, according to RFC 2047 section 5 
		## We change encoding of shared documents according to new algorithm
667
		&Log::do_log('notice','Fixing Q-encoding of web document filenames...');
668
669
670
		my $all_lists = &List::get_lists('*');
		foreach my $list ( @$all_lists ) {
			if (-d $list->{'dir'}.'/shared') {
671
				&Log::do_log('notice','  Processing list %s...', $list->get_list_address());
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686

				my @all_files;
				&tools::list_dir($list->{'dir'}, \@all_files, 'utf-8');
				
				my $count;
				foreach my $f_struct (reverse @all_files) {
					my $new_filename = $f_struct->{'filename'};
					
					## Decode and re-encode filename
					$new_filename = &tools::qencode_filename(&tools::qdecode_filename($new_filename));
					
					if ($new_filename ne $f_struct->{'filename'}) {
						## Rename file
						my $orig_f = $f_struct->{'directory'}.'/'.$f_struct->{'filename'};
						my $new_f = $f_struct->{'directory'}.'/'.$new_filename;
687
						&Log::do_log('notice', "Renaming %s to %s", $orig_f, $new_f);
688
						unless (rename $orig_f, $new_f) {
689
							&Log::do_log('err', "Failed to rename %s to %s : %s", $orig_f, $new_f, $!);
690
691
692
693
694
695
							next;
						}
						$count++;
					}
				}
				if ($count) {
696
				&Log::do_log('notice', 'List %s : %d filenames has been changed', $list->{'name'}, $count);
697
698
699
700
701
				}
			}
		}
		
   }	
702
703
704
   if (&tools::lower_version($previous_version, '6.1.11')) {
       ## Exclusion table was not robot-enabled.
       &Log::do_log('notice','fixing robot column of exclusion table.');
705
706
707
       my $sth;
	unless ($sth = &SDM::do_query("SELECT * FROM exclusion_table")) {
	    &Log::do_log('err','Unable to gather informations from the exclusions table.');
708
709
710
711
712
713
714
715
716
	}
	my @robots = &List::get_robots();
	while (my $data = $sth->fetchrow_hashref){
	    next if (defined $data->{'robot_exclusion'} && $data->{'robot_exclusion'} ne '');
	    ## Guessing right robot for each exclusion.
	    my $valid_robot = '';
	    my @valid_robot_candidates;
	    foreach my $robot (@robots) {
		if (my $list = new List($data->{'list_exclusion'},$robot)) {
717
		    if ($list->is_list_member($data->{'user_exclusion'})) {
718
719
720
721
722
723
724
			push @valid_robot_candidates,$robot;
		    }
		}
	    }
	    if ($#valid_robot_candidates == 0) {
		$valid_robot = $valid_robot_candidates[0];
		my $sth;
725
726
		unless ($sth = &SDM::do_query("UPDATE exclusion_table SET robot_exclusion = %s WHERE list_exclusion=%s AND user_exclusion=%s", &SDM::quote($valid_robot),&SDM::quote($data->{'list_exclusion'}),&SDM::quote($data->{'user_exclusion'}))) {
		    &Log::do_log('err','Unable to update entry (%s,%s) in exclusions table (trying to add robot %s)',$data->{'list_exclusion'},$data->{'user_exclusion'},$valid_robot);
727
728
		}
	    }else {
729
		&Log::do_log('err',"Exclusion robot could not be guessed for user '%s' in list '%s'. Either this user is no longer subscribed to the list or the list appears in more than one robot (or the query to the database failed). Here is the list of robots in which this list name appears: '%s'",$data->{'user_exclusion'},$data->{'list_exclusion'},@valid_robot_candidates);
730
731
732
733
734
735
736
737
738
739
	    }
	}
	## Caching all lists config subset to database
	&Log::do_log('notice','Caching all lists config subset to database');
	&List::_flush_list_db();
	my $all_lists = &List::get_lists('*', { 'use_files' => 1 });
	foreach my $list (@$all_lists) {
	    $list->_update_list_db;
	}
   }
740

741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
    ## We have obsoleted wwsympa.conf.  It would be migrated to sympa.conf.
    if (&tools::lower_version($previous_version, '6.2b.1')) {
	my $sympa_conf = Conf::get_sympa_conf();
	my $wwsympa_conf = Conf::get_wwsympa_conf();
	my $fh;
	my %migrated = ();
	my @newconf = ();
	my $date;

	## Some sympa.conf parameters were overridden by wwsympa.conf.
	## Others prefer sympa.conf.
	my %wwsconf_override = (
	    'arc_path'                   => 'yes',
	    'archive_default_index'      => 'yes',
	    'bounce_path'                => 'yes',
	    'cookie_domain'              => 'NO',
	    'cookie_expire'              => 'yes',
	    'cookie_refresh'             => 'NO',
	    'custom_archiver'            => 'yes',
	    'default_home'               => 'NO',
	    'export_topics'              => 'yes',
	    'htmlarea_url'               => 'yes',
	    'html_editor_file'           => 'NO',
	    'html_editor_init'           => 'NO',
	    'ldap_force_canonical_email' => 'NO',
	    'log_facility'               => 'yes',
	    'mhonarc'                    => 'yes',
	    'password_case'              => 'NO',
	    'review_page_size'           => 'yes',
	    'title'                      => 'NO',
	    'use_fast_cgi'               => 'yes',
	    'use_html_editor'            => 'NO',
	    'viewlogs_page_size'         => 'yes',
	    'wws_path'                   => undef,
	);
	## Old params
	my %old_param = (
	    'alias_manager' =>
		'No more used, using ' . $Conf::Conf{'alias_manager'},
	    'wws_path'      => 'No more used',
	    'icons_url' =>
		'No more used. Using static_content/icons instead.',
	    'robots' =>
		'Not used anymore. Robots are fully described in their respective robot.conf file.',
	    'task_manager_pidfile' => 'No more used',
	    'archived_pidfile'     => 'No more used',
	    'bounced_pidfile'      => 'No more used',
	);

	## Set language of new file content
	Language::PushLang($Conf::Conf{'lang'});
	$date = Language::gettext_strftime("%d.%b.%Y-%H.%M.%S",
	    localtime time);

	if (-r $wwsympa_conf) {
	    ## load only sympa.conf
sikeda's avatar
sikeda committed
797
	    my $conf = (
798
799
800
		Conf::_load_config_file_to_hash(
		    {'path_to_config_file' => $sympa_conf}
		) || {}
sikeda's avatar
sikeda committed
801
	    )->{'config'};
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
	    # not yet implemented.
	    #my $conf = Conf::load_robot_conf(
	    #    {'robot' => '*', 'no_db' => 1, 'return_result' => 1}
	    #);

	    my %infile = ();
	    ## load defaults
	    foreach my $p (@confdef::params) {
		next if $p->{'title'};
		next unless $p->{'file'};
		next unless $p->{'file'} eq 'wwsympa.conf';
		$infile{$p->{'name'}} = $p->{'default'};
	    }
	    ## get content of wwsympa.conf
	    open my $fh, '<', $wwsympa_conf;
	    while (<$fh>) {
		next if /^\s*#/;
		chomp $_;
		next unless /^\s*(\S+)\s+(.+)$/i;
		my ($k, $v) = ($1, $2);
		$infile{$k} = $v;
	    }
	    close $fh;

	    my $name;
	    foreach my $p (@confdef::params) {
		next if $p->{'title'};
		$name = $p->{'name'};
		next unless exists $infile{$name};

		unless ($p->{'file'} and $p->{'file'} eq 'wwsympa.conf') {
		    ## may it exist in wwsympa.conf?
		    $migrated{'unknown'} ||= {};
		    $migrated{'unknown'}->{$name} = [$p, $infile{$name}];
		} elsif (exists $conf->{$name}) {
		    if ($wwsconf_override{$name} eq 'yes') {
			## does it override sympa.conf?
			$migrated{'override'} ||= {};
			$migrated{'override'}->{$name} = [$p, $infile{$name}];
		    } elsif (defined $conf->{$name}) {
			## or, is it there in sympa.conf?
			$migrated{'duplicate'} ||= {};
			$migrated{'duplicate'}->{$name} = [$p, $infile{$name}];
		    } else {
			## otherwise, use values in wwsympa.conf
			$migrated{'add'} ||= {};
			$migrated{'add'}->{$name} = [$p, $infile{$name}];
		    }
		} else {
		    ## otherwise, use values in wwsympa.conf
		    $migrated{'add'} ||= {};
		    $migrated{'add'}->{$name} = [$p, $infile{$name}];
		}
		delete $infile{$name};
	    }
	    ## obsoleted or unknown parameters
	    foreach my $name (keys %infile) {
		if ($old_param{$name}) {
		    $migrated{'obsolete'} ||= {};
		    $migrated{'obsolete'}->{$name} =
			[{'name' => $name, 'query' => $old_param{$name}},
			 $infile{$name}];
		} else {
		    $migrated{'unknown'} ||= {};
		    $migrated{'unknown'}->{$name} =
			[{'name' => $name,
			  'query' => Language::gettext("Unknown parameter")},
			 $infile{$name}];
		}
	    }
	}

	## Add contents to sympa.conf
	if (%migrated) {
	    open $fh, '<', $sympa_conf or die $!;
	    @newconf = <$fh>;
	    close $fh;
	    $newconf[$#newconf] .= "\n" unless $newconf[$#newconf] =~ /\n\z/;

	    push @newconf, "\n" . ('#' x 76) . "\n" . '#### ' .
		Language::gettext("Migration from wwsympa.conf") .  "\n" .
		'#### ' . $date . "\n" .  ('#' x 76) . "\n\n";

	    foreach my $type (qw(duplicate add obsolete unknown)) {
		my %newconf = %{$migrated{$type} || {}};
		next unless scalar keys %newconf;

		push @newconf, tools::wrap_text(
		    Language::gettext("Migrated Parameters\nFollowing parameters were migrated from wwsympa.conf."), '#### ', '#### ') . "\n"
		    if $type eq 'add';
		push @newconf, tools::wrap_text(
		    Language::gettext("Overrididing Parameters\nFollowing parameters existed both in sympa.conf and  wwsympa.conf.  Previous release of Sympa used those in wwsympa.conf.  Comment-out ones you wish to be disabled."), '#### ', '#### ') . "\n"
		    if $type eq 'override';
		push @newconf, tools::wrap_text(
		    Language::gettext("Duplicate of sympa.conf\nThese parameters were found in both sympa.conf and wwsympa.conf.  Previous release of Sympa used those in sympa.conf.  Uncomment ones you wish to be enabled."), '#### ', '#### ') . "\n"
		    if $type eq 'duplicate';
		push @newconf, tools::wrap_text(
		    Language::gettext("Old Parameters\nThese parameters are no longer used."),
		    '#### ', '#### ') . "\n"
		    if $type eq 'obsolete';
		push @newconf, tools::wrap_text(
		    Language::gettext("Unknown Parameters\nThough these parameters were found in wwsympa.conf, they were ignored.  You may simply remove them."),
		    '#### ', '#### ') . "\n"
		    if $type eq 'unknown';

		foreach my $k (sort keys %newconf) {
		    my ($param, $v) = @{$newconf{$k}};

		    push @newconf, tools::wrap_text(
			Language::gettext($param->{'query'}), '## ', '## ')
			if defined $param->{'query'};
		    push @newconf, tools::wrap_text(
			Language::gettext($param->{'advice'}), '## ', '## ')
			if defined $param->{'advice'};
		    if (defined $v and
			($type eq 'add' or $type eq 'override')) {
			push @newconf,
			    sprintf("%s\t%s\n\n", $param->{'name'}, $v);
		    } else {
			push @newconf,
			    sprintf("#%s\t%s\n\n", $param->{'name'}, $v);
		    }
		}
	    }
	}

	## Restore language
	Language::PopLang();

	if (%migrated) {
	    warn sprintf("Unable to rename %s : %s", $sympa_conf, $!)
		unless rename $sympa_conf, "$sympa_conf.$date";
	    ## Write new config files
	    my $umask = umask 037;
	    unless (open $fh, '>', $sympa_conf) {
		umask $umask;
		die sprintf("Unable to open %s : %s", $sympa_conf, $!);
	    }
	    umask $umask;
	    chown [getpwnam(Sympa::Constants::USER)]->[2],
		[getgrnam(Sympa::Constants::GROUP)]->[2], $sympa_conf;
	    print $fh @newconf;
	    close $fh;

	    ## Keep old config file
	    printf "%s has been updated.\nPrevious version has been saved as %s.\n",
		$sympa_conf, "$sympa_conf.$date";
	}

	if (-r $wwsympa_conf) {
	    ## Keep old config file
	    warn sprintf("Unable to rename %s : %s", $wwsympa_conf, $!)
		unless rename $wwsympa_conf, "$wwsympa_conf.$date";
	    printf "%s will NO LONGER be used.\nPrevious version has been saved as %s.\n",
		$wwsympa_conf, "$wwsympa_conf.$date";
	}
    }

960
961
962
963
    return 1;
}

sub probe_db {
964
    &SDM::probe_db();
965
966
967
}

sub data_structure_uptodate {
968
    &SDM::data_structure_uptodate();
969
970
}

971
972
973
974
975
976
977
978
979
980
981
982
983
## used to encode files to UTF-8
## also add X-Attach header field if template requires it
## IN : - arrayref with list of filepath/lang pairs
sub to_utf8 {
    my $files = shift;

    my $with_attachments = qr{ archive.tt2 | digest.tt2 | get_archive.tt2 | listmaster_notification.tt2 | 
				   message_report.tt2 | moderate.tt2 |  modindex.tt2 | send_auth.tt2 }x;
    my $total;
    
    foreach my $pair (@{$files}) {
	my ($file, $lang) = @$pair;
	unless (open(TEMPLATE, $file)) {
984
	    &Log::do_log('err', "Cannot open template %s", $file);
985
986
987
988
989
990
991
992
	    next;
	}
	
	my $text = '';
	my $modified = 0;

	## If filesystem_encoding is set, files are supposed to be encoded according to it
	my $charset;
993
	if ((defined $Conf::Conf::Ignored_Conf{'filesystem_encoding'})&($Conf::Conf::Ignored_Conf{'filesystem_encoding'} ne 'utf-8')) {
994
	    $charset = $Conf::Conf::Ignored_Conf{'filesystem_encoding'};
995
996
997
998
999
1000
	}else {	    
	    &Language::PushLang($lang);
	    $charset = &Language::GetCharset;
	    &Language::PopLang;
	}
	
For faster browsing, not all history is shown. View entire blame