4 php import_mailman.php OLD_PROJECT_NAME NEW_PROJECT_NAME OLD_DOMAIN_NAME NEW_DOMAIN_NAME
5 OLD_PROJECT_NAME : Name of the project when it was exported (should be the name of the archive)
6 NEW_PROJECT_NAME : Name of the project where the lists should be imported
7 OLD_DOMAIN_NAME : Old mailman server address in the original forge, it can be found by reading an archive file. Cannot be automatically retrieved for now, if nothing provided, no change will be done
8 NEW_DOMAIN_NAME : New mailman server address on the target forge, this param should never be used if the preceding one was not used
13 function __construct($mailingDir, $oldlistname, $oldprojectname, $gid, $olddomain="", $newdomain=""){
14 $this->dir = $mailingDir; //Directory where the mailing list was extracted, probably /tmp/[old_project_name]/mailings/[old_list_name_full]
15 $this->oldlistname = $oldlistname; //full name of the list (ie, name of the last directory of this->dir), for instance [old_project_name]-[list_name]
16 $this->listname = substr($oldlistname, strlen($oldprojectname)+1); //name of the list without "[old_project_name]-"
17 $this->newpjct = $gid;//group_get_object($gid)->getUnixName(); // UnixName of the project where the data will be imported
18 $this->olddomain = $olddomain; // Old domain (lists.yyy.yyy.fr ...)
19 $this->newdomain = $newdomain; // New domain (lists.xxx.xxx.fr ...)
21 //USAGE - rename, copy, update
23 //$this->newpjct = $newpjct;
24 shell_exec( " mv ".$this->dir."/archives/private/".$this->oldlistname." ".$this->dir."/archives/private/".$this->newpjct."-".$this->listname);
25 shell_exec( " mv ".$this->dir."/archives/private/".$this->oldlistname.".mbox ".$this->dir."/archives/private/".$this->newpjct."-".$this->listname.".mbox");
26 shell_exec( " mv ".$this->dir."/lists/".$this->oldlistname." ".$this->dir."/lists/".$this->newpjct."-".$this->listname);
27 $this->newlistname = $this->newpjct."-".$this->listname;
31 echo "Copying archives : ".$this->dir."/archives/* ---> /var/lib/mailman/archives/\n\n";
32 shell_exec( " cp -r -a ".$this->dir."/archives/* /var/lib/mailman/archives/ 2>&1 " );
33 echo "Copying lists : ".$this->dir."/lists/* ---> /var/lib/mailman/lists\n\n";
34 shell_exec( " cp -r -a ".$this->dir."/lists/* /var/lib/mailman/lists 2>&1 " );#possible to replace with PHP code like copy(), useful?
37 function search_replace($startdir, $original, $new){
38 if (count($original) != count($new)){
41 $fh = opendir($startdir);
42 while (($file = readdir($fh)) !== false){
43 $filepath = $startdir.'/'.$file;
44 $fileinfo = pathinfo($file);
45 $fileext = $fileinfo['extension'];
46 if($fileext == 'html'){
47 for($i =0; $i < count($original); $i++){
48 $content = str_replace( $original[$i], $new[$i] , file_get_contents($filepath) );
49 file_put_contents($filepath, $content);
57 isset($this->newlistname)? $name=$this->newlistname : $name=$this->oldlistname;
58 shell_exec( " /var/lib/mailman/bin/check_perms -f 1>/dev/null 2>/dev/null" );
59 shell_exec( " /var/lib/mailman/bin/check_perms -f 1>/dev/null 2>/dev/null" );
60 echo "permission update done\n\n";
61 //Updates list domain in config
62 shell_exec( " /var/lib/mailman/bin/withlist -l -r fix_url ".$name." -v -u ".$this->newdomain."/".$name." 1>/dev/null 2>/dev/null");
63 echo "url fixed to ".$this->newdomain."\n\n";
64 if(isset($this->newlistname)){
65 $filepath = "/tmp/".$name;
66 shell_exec( " /var/lib/mailman/bin/config_list -o ".$filepath." ".$name." 1>/dev/null 2>/dev/null");
67 echo "config output to ".$filepath."\n\n";
69 //updates list name in config
70 $pattern = "/real_name = '[^']*'/";
71 $replacement = "real_name = '".$name."'";
72 $content = preg_replace($pattern, $replacement, file_get_contents($filepath));
73 file_put_contents($filepath, $content);
75 echo "updated real name\n\n";
76 shell_exec( " /var/lib/mailman/bin/config_list -i ".$filepath." ".$name);
77 echo "new configuration loaded\n\n";
80 echo "Replacing old name strings and list domain in archives : \n";
83 if(isset($this->newlistname)){
84 echo $this->oldlistname." --> ".$name."\n";
85 $original[] = $this->oldlistname;
87 echo ucfirst($this->oldlistname)." --> ".ucfirst($name)."\n";
88 $original[] = ucfirst($this->oldlistname);
89 $new[] = ucfirst($name);
91 if($this->olddomain != ""){
92 echo $this->olddomain." --> ".$this->newdomain."\n\n";
93 $original[] = $this->olddomain;
94 $new[] = $this->newdomain;
96 $this->search_replace("/var/lib/mailman/archives/private/".$name, $original, $new);
101 $oldpjctname = $argv[1];
102 $newpjctname = $argv[2];
103 $olddomainname = $argv[3];
104 $newdomainname = $argv[4];
107 Optional necessitates PHP>5.3.0 not supported by lenny, use $argv in the meantime
108 $options = "o:n:d::e::";
109 $chosen = getopt($options);
110 $oldpjctname = $chosen["o"];
111 $newpjctname = $chosen["n"];
112 $olddomainname = $chosen["d"];
113 $newdomainname = $chosen["e"];
116 if($dirs = scandir($mailingspath = '/tmp/'.$oldpjctname.'/mailings')){
117 echo "Mailing lists found\n\n";
118 foreach ($dirs as $dir){
119 if($dir != '.' && $dir != '..'){
120 if (isset($olddomainname) && isset($newdomainname)){
121 $mailing = new Mailman($mailingspath.'/'.$dir, $dir, $oldpjctname, $newpjctname, $olddomainname, $newdomainname);
123 $mailing = new Mailman($mailingspath.'/'.$dir, $dir, $oldpjctname, $newpjctname);