1<?php
2
3$req = Sql_Query(sprintf('select l.name,count(l.id) from %s l group by l.name',$GLOBALS['tables']['list']));
4cl_output(s('Merging lists'));
5while ($row = Sql_Fetch_Row($req)) {
6    if ($row[1] > 1) {
7        cl_output(s('List name: "%s", duplicates: %d',$row[0],$row[1]));
8        $req2 = Sql_Query(sprintf('select l.id from %s l where name = "%s" order by l.id',$GLOBALS['tables']['list'],$row[0]));
9        $first = Sql_Fetch_Row($req2);
10        cl_output(s('First list: %d',$first[0]));
11        while ($dup = Sql_Fetch_Row($req2)) {
12            cl_output('List id: '.$dup[0]);
13            $listusers = Sql_Query(sprintf('update ignore %s lu set listid = %d where listid = %d',$GLOBALS['tables']['listuser'],$first[0],$dup[0]));
14            $num = Sql_Affected_Rows();
15            cl_output(s('%d subscribers moved',$num));
16            $listusers = Sql_Query(sprintf('update ignore %s lm set listid = %d where listid = %d',$GLOBALS['tables']['listmessage'],$first[0],$dup[0]));
17            $num = Sql_Affected_Rows();
18            cl_output(s('%d campaigns moved',$num));
19            Sql_Query(sprintf('delete from %s where listid = %d',$GLOBALS['tables']['listuser'],$dup[0]));
20            Sql_Query(sprintf('delete from %s where listid = %d',$GLOBALS['tables']['listmessage'],$dup[0]));
21            Sql_Query(sprintf('delete from %s where id = %d',$GLOBALS['tables']['list'],$dup[0]));
22        }
23
24    }
25}
26$status = s('All done');