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;
32 echo "Copying archives : ".$this->dir."/archives/* ---> /var/lib/mailman/archives/\n\n";
33 shell_exec( " cp -r -a ".$this->dir."/archives/* /var/lib/mailman/archives/ 2>&1 " );
34 echo "Copying lists : ".$this->dir."/lists/* ---> /var/lib/mailman/lists\n\n";
35 shell_exec( " cp -r -a ".$this->dir."/lists/* /var/lib/mailman/lists 2>&1 " );#possible to replace with PHP code like copy(), useful?
38 function search_replace($startdir, $original, $new){
39 if (count($original) != count($new)){
42 $fh = opendir($startdir);
43 while (($file = readdir($fh)) !== false){
44 $filepath = $startdir.'/'.$file;
45 $fileinfo = pathinfo($file);
46 $fileext = $fileinfo['extension'];
47 if($fileext == 'html'){
48 for($i =0; $i < count($original); $i++){
49 $content = str_replace( $original[$i], $new[$i] , file_get_contents($filepath) );
50 file_put_contents($filepath, $content);
58 isset($this->newlistname)? $name=$this->newlistname : $name=$this->oldlistname;
59 shell_exec( " /var/lib/mailman/bin/check_perms -f 1>/dev/null 2>/dev/null" );
60 shell_exec( " /var/lib/mailman/bin/check_perms -f 1>/dev/null 2>/dev/null" );
61 echo "permission update done\n\n";
62 //Updates list domain in config
63 shell_exec( " /var/lib/mailman/bin/withlist -l -r fix_url ".$name." -v -u ".$this->newdomain."/".$name." 1>/dev/null 2>/dev/null");
64 echo "url fixed to ".$this->newdomain."\n\n";
65 if(isset($this->newlistname)){
66 $filepath = "/tmp/".$name;
67 shell_exec( " /var/lib/mailman/bin/config_list -o ".$filepath." ".$name." 1>/dev/null 2>/dev/null");
68 echo "config output to ".$filepath."\n\n";
70 //updates list name in config
71 $pattern = "/real_name = '[^']*'/";
72 $replacement = "real_name = '".$name."'";
73 $content = preg_replace($pattern, $replacement, file_get_contents($filepath));
74 file_put_contents($filepath, $content);
76 echo "updated real name\n\n";
77 shell_exec( " /var/lib/mailman/bin/config_list -i ".$filepath." ".$name);
78 echo "new configuration loaded\n\n";
81 echo "Replacing old name strings and list domain in archives : \n";
84 if(isset($this->newlistname)){
85 echo $this->oldlistname." --> ".$name."\n";
86 $original[] = $this->oldlistname;
88 echo ucfirst($this->oldlistname)." --> ".ucfirst($name)."\n";
89 $original[] = ucfirst($this->oldlistname);
90 $new[] = ucfirst($name);
92 if($this->olddomain != ""){
93 echo $this->olddomain." --> ".$this->newdomain."\n\n";
94 $original[] = $this->olddomain;
95 $new[] = $this->newdomain;
97 $this->search_replace("/var/lib/mailman/archives/private/".$name, $original, $new);
105 $oldpjctname = $argv[1];
106 $newpjctname = $argv[2];
107 $olddomainname = $argv[3];
108 $newdomainname = $argv[4];
112 Optional necessitates PHP>5.3.0 not supported by lenny, use $argv in the meantime
113 $options = "o:n:d::e::";
114 $chosen = getopt($options);
115 $oldpjctname = $chosen["o"];
116 $newpjctname = $chosen["n"];
117 $olddomainname = $chosen["d"];
118 $newdomainname = $chosen["e"];
121 if($dirs = scandir($mailingspath = '/tmp/'.$oldpjctname.'/mailings')){
122 echo "Mailing lists found\n\n";
123 foreach ($dirs as $dir){
124 if($dir != '.' && $dir != '..'){
125 if (isset($olddomainname) && isset($newdomainname)){
126 $mailing = new Mailman($mailingspath.'/'.$dir, $dir, $oldpjctname, $newpjctname, $olddomainname, $newdomainname);
128 $mailing = new Mailman($mailingspath.'/'.$dir, $dir, $oldpjctname, $newpjctname);