summaryrefslogtreecommitdiffstats
path: root/po/cy.po
Commit message (Expand)AuthorAgeFilesLines
* Updated cy translationFunda Wang2007-09-201-122/+95
* import "Enabled" translation from rpmdrakeThierry Vignaud2007-08-091-1/+1
* further improve this string by spliting out the rpmdrake substring in order t...Thierry Vignaud2007-08-091-118/+117
* typo fixThierry Vignaud2007-08-091-3/+3
* sync with codeThierry Vignaud2007-08-071-2/+8
* really display which database is lockedThierry Vignaud2007-03-211-2/+2
* update log message regarding s/mdkupdate/MandrivaUpdate/Thierry Vignaud2007-03-201-2/+2
* add new strings for warning about missing media or disabled mediaThierry Vignaud2007-03-201-2/+25
* Updated POT file of mdkonline.Funda Wang2007-03-191-126/+146
* adapted to plural-formsPablo Saratxaga2007-01-231-0/+1
* better looking message (add EOLs around list items)Thierry Vignaud2006-12-051-0/+4
* updated pot file (with an explanation for "Greeting"),Pablo Saratxaga2006-09-011-155/+156
* updated po filePablo Saratxaga2006-06-031-12/+15
* updated po files; reused general copyright text from previous translationsPablo Saratxaga2006-05-121-2/+2
* add 2 new stringsThierry Vignaud2006-04-061-3/+13
* factorize yearsThierry Vignaud2006-04-061-2/+2
* import a translation from DrakXThierry Vignaud2006-04-061-1/+1
* sync with codeThierry Vignaud2006-04-061-12/+72
* translate a string from rpmdrakeThierry Vignaud2006-03-301-1/+1
* sync messages with codeThierry Vignaud2006-03-301-43/+58
* updated po filePablo Saratxaga2006-03-221-3/+3
* updated pot filePablo Saratxaga2006-03-161-40/+50
* updated po filePablo Saratxaga2006-03-101-5/+10
* updateThierry Vignaud2006-03-071-4/+4
* sync with code (adding comment for translator)Thierry Vignaud2006-03-071-16/+17
* sync with codeThierry Vignaud2006-03-071-9/+16
* merge in translations from urpmiThierry Vignaud2006-03-071-1/+1
* sync with codeThierry Vignaud2006-03-071-35/+40
* updated Welsh po file; saved some old translationsPablo Saratxaga2006-03-061-56/+83
* merge in translations from DrakXThierry Vignaud2006-03-031-3/+3
* sync with codeThierry Vignaud2006-03-031-5/+29
* sync with codeThierry Vignaud2006-03-021-143/+159
* updated pot filePablo Saratxaga2005-09-161-132/+271
* updated pot filePablo Saratxaga2005-08-081-35/+35
* updated po filesPablo Saratxaga2005-07-181-9/+16
* More s/Mandrake/Mandriva, fix mdk bug#16077.Funda Wang2005-07-061-36/+36
* updated po filePablo Saratxaga2005-06-031-4/+4
* made more explicit help line about text mode version;Pablo Saratxaga2005-05-111-4/+4
* updated po filePablo Saratxaga2005-04-251-3/+3
* updated pot filePablo Saratxaga2005-04-181-88/+109
* - switch to MandrivaDaouda Lo2005-04-141-39/+39
* typo fix (Arpad Biro)Thierry Vignaud2005-02-171-1/+1
* updated pot filePablo Saratxaga2005-02-151-1/+1
* typo fixes (Arpad Biro)Thierry Vignaud2005-02-141-2/+2
* updated po filePablo Saratxaga2005-02-011-1/+2
* updated pot filePablo Saratxaga2005-01-311-44/+54
* updated po filesPablo Saratxaga2005-01-051-3/+3
* updated po filePablo Saratxaga2005-01-031-90/+39
* updated pot filePablo Saratxaga2005-01-031-44/+44
* updated pot filePablo Saratxaga2004-11-221-21/+14
n593'>593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 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 797 798 799 800 801 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 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Dawn Endico <endico@mozilla.org>
#                 Gregary Hendricks <ghendricks@novell.com>
#                 Vance Baarda <vrb@novell.com>
#                 Guzman Braso <gbn@hqso.net>
#                 Erik Purins <epurins@day1studios.com>
#                 Frédéric Buclin <LpSolit@gmail.com>

# This script reads in xml bug data from standard input and inserts
# a new bug into bugzilla. Everything before the beginning <?xml line
# is removed so you can pipe in email messages.

use strict;

#####################################################################
#
# This script is used to import bugs from another installation of bugzilla.
# It can be used in two ways.
# First using the move function of bugzilla
# on another system will send mail to an alias provided by
# the administrator of the target installation (you). Set up an alias
# similar to the one given below so this mail will be automatically
# run by this script and imported into your database.  Run 'newaliases'
# after adding this alias to your aliases file. Make sure your sendmail
# installation is configured to allow mail aliases to execute code.
#
# bugzilla-import: "|/usr/bin/perl /opt/bugzilla/importxml.pl"
#
# Second it can be run from the command line with any xml file from
# STDIN that conforms to the bugzilla DTD. In this case you can pass
# an argument to set whether you want to send the
# mail that will be sent to the exporter and maintainer normally.
#
# importxml.pl bugsfile.xml
#
#####################################################################

use File::Basename qw(dirname);
# MTAs may call this script from any directory, but it should always
# run from this one so that it can find its modules.
BEGIN {
    require File::Basename;
    my $dir = $0; $dir =~ /(.*)/; $dir = $1; # trick taint
    chdir(File::Basename::dirname($dir));
}

use lib qw(. lib);
# Data dumber is used for debugging, I got tired of copying it back in 
# and then removing it. 
#use Data::Dumper;


use Bugzilla;
use Bugzilla::Bug;
use Bugzilla::Product;
use Bugzilla::Version;
use Bugzilla::Component;
use Bugzilla::Milestone;
use Bugzilla::FlagType;
use Bugzilla::BugMail;
use Bugzilla::Mailer;
use Bugzilla::User;
use Bugzilla::Util;
use Bugzilla::Constants;
use Bugzilla::Keyword;
use Bugzilla::Field;
use Bugzilla::Status;

use MIME::Base64;
use MIME::Parser;
use Date::Format;
use Getopt::Long;
use Pod::Usage;
use XML::Twig;

# We want to capture errors and handle them here rather than have the Template
# code barf all over the place.
Bugzilla->usage_mode(Bugzilla::Constants::USAGE_MODE_CMDLINE);

my $debug = 0;
my $mail  = '';
my $attach_path = '';
my $help  = 0;

my $result = GetOptions(
    "verbose|debug+" => \$debug,
    "mail|sendmail!" => \$mail,
    "attach_path=s"  => \$attach_path,
    "help|?"         => \$help
);

pod2usage(0) if $help;

use constant OK_LEVEL    => 3;
use constant DEBUG_LEVEL => 2;
use constant ERR_LEVEL   => 1;

our @logs;
our @attachments;
our $bugtotal;
my $xml;
my $dbh = Bugzilla->dbh;
my $params = Bugzilla->params;
my ($timestamp) = $dbh->selectrow_array("SELECT NOW()");

###############################################################################
# Helper sub routines                                                         #
###############################################################################

sub MailMessage {
    return unless ($mail);
    my $subject    = shift;
    my $message    = shift;
    my @recipients = @_;
    my $from   = $params->{"moved-from-address"};
    $from =~ s/@/\@/g;

    foreach my $to (@recipients){
        my $header = "To: $to\n";
        $header .= "From: Bugzilla <$from>\n";
        $header .= "Subject: $subject\n\n";
        my $sendmessage = $header . $message . "\n";
        MessageToMTA($sendmessage);
    }

}

sub Debug {
    return unless ($debug);
    my ( $message, $level ) = (@_);
    print STDERR "OK: $message \n" if ( $level == OK_LEVEL );
    print STDERR "ERR: $message \n" if ( $level == ERR_LEVEL );
    print STDERR "$message\n"
      if ( ( $debug == $level ) && ( $level == DEBUG_LEVEL ) );
}

sub Error {
    my ( $reason, $errtype, $exporter ) = @_;
    my $subject = "Bug import error: $reason";
    my $message = "Cannot import these bugs because $reason ";
    $message .= "\n\nPlease re-open the original bug.\n" if ($errtype);
    $message .= "For more info, contact " . $params->{"maintainer"} . ".\n";
    my @to = ( $params->{"maintainer"}, $exporter);
    Debug( $message, ERR_LEVEL );
    MailMessage( $subject, $message, @to );
    exit;
}

# This subroutine handles flags for process_bug. It is generic in that
# it can handle both attachment flags and bug flags.
sub flag_handler {
    my (
        $name,            $status,      $setter_login,
        $requestee_login, $exporterid,  $bugid,
        $productid,       $componentid, $attachid
      )
      = @_;

    my $type         = ($attachid) ? "attachment" : "bug";
    my $err          = '';
    my $setter       = new Bugzilla::User({ name => $setter_login });
    my $requestee;
    my $requestee_id;

    unless ($setter) {
        $err = "Invalid setter $setter_login on $type flag $name\n";
        $err .= "   Dropping flag $name\n";
        return $err;
    }
    if ( !$setter->can_see_bug($bugid) ) {
        $err .= "Setter is not a member of bug group\n";
        $err .= "   Dropping flag $name\n";
        return $err;
    }
    my $setter_id = $setter->id;
    if ( defined($requestee_login) ) {
        $requestee = new Bugzilla::User({ name => $requestee_login });
        if ( $requestee ) {
            if ( !$requestee->can_see_bug($bugid) ) {
                $err .= "Requestee is not a member of bug group\n";
                $err .= "   Requesting from the wind\n";
            }    
            else{
                $requestee_id = $requestee->id;
            }
        }
        else {
            $err = "Invalid requestee $requestee_login on $type flag $name\n";
            $err .= "   Requesting from the wind.\n";
        }
        
    }
    my $flag_types;

    # If this is an attachment flag we need to do some dirty work to look
    # up the flagtype ID
    if ($attachid) {
        $flag_types = Bugzilla::FlagType::match(
            {
                'target_type'  => 'attachment',
                'product_id'   => $productid,
                'component_id' => $componentid
            } );
    }
    else {
        my $bug = new Bugzilla::Bug($bugid);
        $flag_types = $bug->flag_types;
    }
    unless ($flag_types){
        $err  = "No flag types defined for this bug\n";
        $err .= "   Dropping flag $name\n";
        return $err;
    }

    # We need to see if the imported flag is in the list of known flags
    # It is possible for two flags on the same bug have the same name
    # If this is the case, we will only match the first one.
    my $ftype;
    foreach my $f ( @{$flag_types} ) {
        if ( $f->name eq $name) {
            $ftype = $f;
            last;
        }
    }

    if ($ftype) {    # We found the flag in the list
        my $grant_group = $ftype->grant_group;
        if (( $status eq '+' || $status eq '-' ) 
            && $grant_group && !$setter->in_group_id($grant_group->id)) {
            $err = "Setter $setter_login on $type flag $name ";
            $err .= "is not in the Grant Group\n";
            $err .= "   Dropping flag $name\n";
            return $err;
        }
        my $request_group = $ftype->request_group;
        if ($request_group
            && $status eq '?' && !$setter->in_group_id($request_group->id)) {
            $err = "Setter $setter_login on $type flag $name ";
            $err .= "is not in the Request Group\n";
            $err .= "   Dropping flag $name\n";
            return $err;
        }

        # Take the first flag_type that matches
        unless ($ftype->is_active) {
            $err = "Flag $name is not active in this database\n";
            $err .= "   Dropping flag $name\n";
            return $err;
        }

        $dbh->do("INSERT INTO flags 
                 (type_id, status, bug_id, attach_id, creation_date, 
                  setter_id, requestee_id)
                  VALUES (?, ?, ?, ?, ?, ?, ?)", undef,
            ($ftype->id, $status, $bugid, $attachid, $timestamp,
            $setter_id, $requestee_id));
    }
    else {
        $err = "Dropping unknown $type flag: $name\n";
        return $err;
    }
    return $err;
}

# Converts and returns the input data as an array.
sub _to_array {
    my $value = shift;

    $value = [$value] if !ref($value);
    return @$value;
}

###############################################################################
# XML Handlers                                                                #
###############################################################################

# This subroutine gets called only once - as soon as the <bugzilla> opening
# tag is parsed. It simply checks to see that the all important exporter
# maintainer and URL base are set.
#
#    exporter:   email address of the person moving the bugs
#    maintainer: the maintainer of the bugzilla installation
#                as set in the parameters file
#    urlbase:    The urlbase parameter of the installation
#                bugs are being moved from
#
sub init() {
    my ( $twig, $bugzilla ) = @_;
    my $root       = $twig->root;
    my $maintainer = $root->{'att'}->{'maintainer'};
    my $exporter   = $root->{'att'}->{'exporter'};
    my $urlbase    = $root->{'att'}->{'urlbase'};
    my $xmlversion = $root->{'att'}->{'version'};

    if ($xmlversion ne BUGZILLA_VERSION) {
            my $log = "Possible version conflict!\n";
            $log .= "   XML was exported from Bugzilla version $xmlversion\n";
            $log .= "   But this installation uses ";
            $log .= BUGZILLA_VERSION . "\n";
            Debug($log, OK_LEVEL);
            push(@logs, $log);
    }
    Error( "no maintainer", "REOPEN", $exporter ) unless ($maintainer);
    Error( "no exporter",   "REOPEN", $exporter ) unless ($exporter);
    Error( "bug importing is disabled here", undef, $exporter ) unless ( $params->{"move-enabled"} );
    Error( "invalid exporter: $exporter", "REOPEN", $exporter ) if ( !login_to_id($exporter) );
    Error( "no urlbase set", "REOPEN", $exporter ) unless ($urlbase);
    my $def_product =
        new Bugzilla::Product( { name => $params->{"moved-default-product"} } )
        || Error("Cannot import these bugs because an invalid default 
                  product was defined for the target db."
                  . $params->{"maintainer"} . " needs to fix the definitions of
                  moved-default-product. \n", "REOPEN", $exporter);
    my $def_component = new Bugzilla::Component(
        {
            product => $def_product,
            name    => $params->{"moved-default-component"}
        })
    || Error("Cannot import these bugs because an invalid default 
              component was defined for the target db."
              . $params->{"maintainer"} . " needs to fix the definitions of
              moved-default-component.\n", "REOPEN", $exporter);
}
    

# Parse attachments.
#
# This subroutine is called once for each attachment in the xml file.
# It is called as soon as the closing </attachment> tag is parsed.
# Since attachments have the potential to be very large, and
# since each attachment will be inside <bug>..</bug> tags we shove
# the attachment onto an array which will be processed by process_bug
# and then disposed of. The attachment array will then contain only
# one bugs' attachments at a time.
# The cycle will then repeat for the next <bug>
#
# The attach_id is ignored since mysql generates a new one for us.
# The submitter_id gets filled in with $exporterid.

sub process_attachment() {
    my ( $twig, $attach ) = @_;
    Debug( "Parsing attachments", DEBUG_LEVEL );
    my %attachment;

    $attachment{'date'} =
        format_time( $attach->field('date'), "%Y-%m-%d %R" ) || $timestamp;
    $attachment{'desc'}       = $attach->field('desc');
    $attachment{'ctype'}      = $attach->field('type') || "unknown/unknown";
    $attachment{'attachid'}   = $attach->field('attachid');
    $attachment{'ispatch'}    = $attach->{'att'}->{'ispatch'} || 0;
    $attachment{'isobsolete'} = $attach->{'att'}->{'isobsolete'} || 0;
    $attachment{'isprivate'}  = $attach->{'att'}->{'isprivate'} || 0;
    $attachment{'filename'}   = $attach->field('filename') || "file";
    $attachment{'attacher'}   = $attach->field('attacher');
    # Attachment data is not exported in versions 2.20 and older.
    if (defined $attach->first_child('data') &&
            defined $attach->first_child('data')->{'att'}->{'encoding'}) {
        my $encoding = $attach->first_child('data')->{'att'}->{'encoding'};
        if ($encoding =~ /base64/) {
            # decode the base64
            my $data   = $attach->field('data');
            my $output = decode_base64($data);
            $attachment{'data'} = $output;
        }
        elsif ($encoding =~ /filename/) {
            # read the attachment file
            Error("attach_path is required", undef) unless ($attach_path);
            my $attach_filename = $attach_path . "/" . $attach->field('data');
            open(ATTACH_FH, $attach_filename) or
                Error("cannot open $attach_filename", undef);
            $attachment{'data'} = do { local $/; <ATTACH_FH> };
            close ATTACH_FH;
        }
    }
    else {
        $attachment{'data'} = $attach->field('data');
    }

    # attachment flags
    my @aflags;
    foreach my $aflag ( $attach->children('flag') ) {
        my %aflag;
        $aflag{'name'}      = $aflag->{'att'}->{'name'};
        $aflag{'status'}    = $aflag->{'att'}->{'status'};
        $aflag{'setter'}    = $aflag->{'att'}->{'setter'};
        $aflag{'requestee'} = $aflag->{'att'}->{'requestee'};
        push @aflags, \%aflag;
    }
    $attachment{'flags'} = \@aflags if (@aflags);

    # free up the memory for use by the rest of the script
    $attach->delete;
    if ($attachment{'attachid'}) {
        push @attachments, \%attachment;
    }
    else {
        push @attachments, "err";
    }
}

# This subroutine will be called once for each <bug> in the xml file.
# It is called as soon as the closing </bug> tag is parsed.
# If this bug had any <attachment> tags, they will have been processed
# before we get to this point and their data will be in the @attachments
# array.
# As each bug is processed, it is inserted into the database and then
# purged from memory to free it up for later bugs.

sub process_bug {
    my ( $twig, $bug ) = @_;
    my $root             = $twig->root;
    my $maintainer       = $root->{'att'}->{'maintainer'};
    my $exporter_login   = $root->{'att'}->{'exporter'};
    my $exporter         = new Bugzilla::User({ name => $exporter_login });
    my $urlbase          = $root->{'att'}->{'urlbase'};

    # We will store output information in this variable.
    my $log = "";
    if ( defined $bug->{'att'}->{'error'} ) {
        $log .= "\nError in bug " . $bug->field('bug_id') . "\@$urlbase: ";
        $log .= $bug->{'att'}->{'error'} . "\n";
        if ( $bug->{'att'}->{'error'} =~ /NotFound/ ) {
            $log .= "$exporter_login tried to move bug " . $bug->field('bug_id');
            $log .= " here, but $urlbase reports that this bug";
            $log .= " does not exist.\n";
        }
        elsif ( $bug->{'att'}->{'error'} =~ /NotPermitted/ ) {
            $log .= "$exporter_login tried to move bug " . $bug->field('bug_id');
            $log .= " here, but $urlbase reports that $exporter_login does ";
            $log .= " not have access to that bug.\n";
        }
        return;
    }
    $bugtotal++;

    # This list contains all other bug fields that we want to process.
    # If it is not in this list it will not be included.
    my %all_fields;
    foreach my $field ( 
        qw(long_desc attachment flag group), Bugzilla::Bug::fields() )
    {
        $all_fields{$field} = 1;
    }
    
    my %bug_fields;
    my $err = "";

   # Loop through all the xml tags inside a <bug> and compare them to the
   # lists of fields. If they match throw them into the hash. Otherwise
   # append it to the log, which will go into the comments when we are done.
    foreach my $bugchild ( $bug->children() ) {
        Debug( "Parsing field: " . $bugchild->name, DEBUG_LEVEL );
        if ( defined $all_fields{ $bugchild->name } ) {
            my @values = $bug->children_text($bugchild->name);
            if (scalar @values > 1) {
                $bug_fields{$bugchild->name} = \@values;
            }
            else {
                $bug_fields{$bugchild->name} = $values[0];
            }
        }
        else {
            $err .= "Unknown bug field \"" . $bugchild->name . "\"";
            $err .= " encountered while moving bug\n";
            $err .= "   <" . $bugchild->name . ">";
            if ( $bugchild->children_count > 1 ) {
                $err .= "\n";
                foreach my $subchild ( $bugchild->children() ) {
                    $err .= "     <" . $subchild->name . ">";
                    $err .= $subchild->field;
                    $err .= "</" . $subchild->name . ">\n";
                }
            }
            else {
                $err .= $bugchild->field;
            }
            $err .= "</" . $bugchild->name . ">\n";
        }
    }

    my @long_descs;
    my $private = 0;

    # Parse long descriptions
    foreach my $comment ( $bug->children('long_desc') ) {
        Debug( "Parsing Long Description", DEBUG_LEVEL );
        my %long_desc;
        $long_desc{'who'}       = $comment->field('who');
        $long_desc{'bug_when'}  = $comment->field('bug_when');
        $long_desc{'isprivate'} = $comment->{'att'}->{'isprivate'} || 0;

        # if one of the comments is private we need to set this flag
        if ( $long_desc{'isprivate'} && $exporter->in_group($params->{'insidergroup'})) {
            $private = 1;
        }
        my $data = $comment->field('thetext');
        if ( defined $comment->first_child('thetext')->{'att'}->{'encoding'}
            && $comment->first_child('thetext')->{'att'}->{'encoding'} =~
            /base64/ )
        {
            $data = decode_base64($data);
        }

        # If we leave the attachment ID in the comment it will be made a link
        # to the wrong attachment. Since the new attachment ID is unknown yet
        # let's strip it out for now. We will make a comment with the right ID
        # later
        $data =~ s/Created an attachment \(id=\d+\)/Created an attachment/g;

        # Same goes for bug #'s Since we don't know if the referenced bug
        # is also being moved, lets make sure they know it means a different
        # bugzilla.
        my $url = $urlbase . "show_bug.cgi?id=";
        $data =~ s/([Bb]ugs?\s*\#?\s*(\d+))/$url$2/g;

        $long_desc{'thetext'} = $data;
        push @long_descs, \%long_desc;
    }

    # instead of giving each comment its own item in the longdescs
    # table like it should have, lets cat them all into one big
    # comment otherwise we would have to lie often about who
    # authored the comment since commenters in one bugzilla probably
    # don't have accounts in the other one.
    # If one of the comments is private the whole comment will be
    # private since we don't want to expose these unnecessarily
    sub by_date { my @a; my @b; $a->{'bug_when'} cmp $b->{'bug_when'}; }
    my @sorted_descs     = sort by_date @long_descs;
    my $long_description = "";
    for ( my $z = 0 ; $z <= $#sorted_descs ; $z++ ) {
        if ( $z == 0 ) {
            $long_description .= "\n\n\n---- Reported by ";
        }
        else {
            $long_description .= "\n\n\n---- Additional Comments From ";
        }
        $long_description .= "$sorted_descs[$z]->{'who'} ";
        $long_description .= "$sorted_descs[$z]->{'bug_when'}";
        $long_description .= " ----";
        $long_description .= "\n\n";
        $long_description .= "THIS COMMENT IS PRIVATE \n"
          if ( $sorted_descs[$z]->{'isprivate'} );
        $long_description .= $sorted_descs[$z]->{'thetext'};
        $long_description .= "\n";
    }

    my $comments;

    $comments .= "\n\n--- Bug imported by $exporter_login ";
    $comments .= time2str( "%Y-%m-%d %H:%M", time ) . " ";
    $comments .= $params->{'timezone'};
    $comments .= " ---\n\n";
    $comments .= "This bug was previously known as _bug_ $bug_fields{'bug_id'} at ";
    $comments .= $urlbase . "show_bug.cgi?id=" . $bug_fields{'bug_id'} . "\n";
    if ( defined $bug_fields{'dependson'} ) {
        $comments .= "This bug depended on bug(s) " .
                     join(' ', _to_array($bug_fields{'dependson'})) . ".\n";
    }
    if ( defined $bug_fields{'blocked'} ) {
        $comments .= "This bug blocked bug(s) " .
                     join(' ', _to_array($bug_fields{'blocked'})) . ".\n";
    }

    # Now we process each of the fields in turn and make sure they contain
    # valid data. We will create two parallel arrays, one for the query
    # and one for the values. For every field we need to push an entry onto
    # each array.
    my @query  = ();
    my @values = ();

    # Each of these fields we will check for newlines and shove onto the array
    foreach my $field (qw(status_whiteboard bug_file_loc short_desc)) {
        if ($bug_fields{$field}) {
            $bug_fields{$field} = clean_text( $bug_fields{$field} );
            push( @query,  $field );
            push( @values, $bug_fields{$field} );
        }
    }

    # Alias
    if ( $bug_fields{'alias'} ) {
        my ($alias) = $dbh->selectrow_array("SELECT COUNT(*) FROM bugs 
                                                WHERE alias = ?", undef,
                                                $bug_fields{'alias'} );
        if ($alias) {
            $err .= "Dropping conflicting bug alias ";
            $err .= $bug_fields{'alias'} . "\n";
        }
        else {
            $alias = $bug_fields{'alias'};
            push @query,  'alias';
            push @values, $alias;
        }
    }

    # Timestamps
    push( @query, "creation_ts" );
    push( @values,
        format_time( $bug_fields{'creation_ts'}, "%Y-%m-%d %X" )
          || $timestamp );

    push( @query, "delta_ts" );
    push( @values,
        format_time( $bug_fields{'delta_ts'}, "%Y-%m-%d %X" )
          || $timestamp );

    # Bug Access
    push( @query,  "cclist_accessible" );
    push( @values, $bug_fields{'cclist_accessible'} ? 1 : 0 );

    push( @query,  "reporter_accessible" );
    push( @values, $bug_fields{'reporter_accessible'} ? 1 : 0 );

    # Product and Component if there is no valid default product and
    # component defined in the parameters, we wouldn't be here
    my $def_product =
      new Bugzilla::Product( { name => $params->{"moved-default-product"} } );
    my $def_component = new Bugzilla::Component(
        {
            product => $def_product,
            name    => $params->{"moved-default-component"}
        }
    );
    my $product;
    my $component;

    if ( defined $bug_fields{'product'} ) {
        $product = new Bugzilla::Product( { name => $bug_fields{'product'} } );
        unless ($product) {
            $product = $def_product;
            $err .= "Unknown Product " . $bug_fields{'product'} . "\n";
            $err .= "   Using default product set in Parameters \n";
        }
    }
    else {
        $product = $def_product;
    }
    if ( defined $bug_fields{'component'} ) {
        $component = new Bugzilla::Component(
            {
                product => $product,
                name    => $bug_fields{'component'}
            }
        );
        unless ($component) {
            $component = $def_component;
            $product   = $def_product;
            $err .= "Unknown Component " . $bug_fields{'component'} . "\n";
            $err .= "   Using default product and component set ";
            $err .= "in Parameters \n";
        }
    }
    else {
        $component = $def_component;
        $product   = $def_product;
    }

    my $prod_id = $product->id;
    my $comp_id = $component->id;

    push( @query,  "product_id" );
    push( @values, $prod_id );
    push( @query,  "component_id" );
    push( @values, $comp_id );

    # Since there is no default version for a product, we check that the one
    # coming over is valid. If not we will use the first one in @versions
    # and warn them.
    my $version = new Bugzilla::Version(
          { product => $product, name => $bug_fields{'version'} });

    push( @query, "version" );
    if ($version) {
        push( @values, $version->name );
    }
    else {
        my @versions = @{ $product->versions };
        my $v        = $versions[0];
        push( @values, $v->name );
        $err .= "Unknown version \"";
        $err .= ( defined $bug_fields{'version'} )
            ? $bug_fields{'version'}
            : "unknown";
        $err .= " in product " . $product->name . ". \n";
        $err .= "   Setting version to \"" . $v->name . "\".\n";
    }

    # Milestone
    if ( $params->{"usetargetmilestone"} ) {
        my $milestone;
        if (defined $bug_fields{'target_milestone'}
            && $bug_fields{'target_milestone'} ne "") {

            $milestone = new Bugzilla::Milestone(
                { product => $product, name => $bug_fields{'target_milestone'} });
        }
        if ($milestone) {
            push( @values, $milestone->name );
        }
        else {
            push( @values, $product->default_milestone );
            $err .= "Unknown milestone \"";
            $err .= ( defined $bug_fields{'target_milestone'} )
                ? $bug_fields{'target_milestone'}
                : "unknown";
            $err .= " in product " . $product->name . ". \n";
            $err .= "   Setting to default milestone for this product, ";
            $err .= "\"" . $product->default_milestone . "\".\n";
        }
        push( @query, "target_milestone" );
    }

    # For priority, severity, opsys and platform we check that the one being
    # imported is valid. If it is not we use the defaults set in the parameters.
    if (defined( $bug_fields{'bug_severity'} )
        && check_field('bug_severity', scalar $bug_fields{'bug_severity'},
                       undef, ERR_LEVEL) )
    {
        push( @values, $bug_fields{'bug_severity'} );
    }
    else {
        push( @values, $params->{'defaultseverity'} );
        $err .= "Unknown severity ";
        $err .= ( defined $bug_fields{'bug_severity'} )
          ? $bug_fields{'bug_severity'}
          : "unknown";
        $err .= ". Setting to default severity \"";
        $err .= $params->{'defaultseverity'} . "\".\n";
    }
    push( @query, "bug_severity" );

    if (defined( $bug_fields{'priority'} )
        && check_field('priority', scalar $bug_fields{'priority'},
                       undef, ERR_LEVEL ) )
    {
        push( @values, $bug_fields{'priority'} );
    }
    else {
        push( @values, $params->{'defaultpriority'} );
        $err .= "Unknown priority ";
        $err .= ( defined $bug_fields{'priority'} )
          ? $bug_fields{'priority'}
          : "unknown";
        $err .= ". Setting to default priority \"";
        $err .= $params->{'defaultpriority'} . "\".\n";
    }
    push( @query, "priority" );

    if (defined( $bug_fields{'rep_platform'} )
        && check_field('rep_platform', scalar $bug_fields{'rep_platform'},
                       undef, ERR_LEVEL ) )
    {
        push( @values, $bug_fields{'rep_platform'} );
    }
    else {
        push( @values, $params->{'defaultplatform'} );
        $err .= "Unknown platform ";
        $err .= ( defined $bug_fields{'rep_platform'} )
          ? $bug_fields{'rep_platform'}
          : "unknown";
        $err .=". Setting to default platform \"";
        $err .= $params->{'defaultplatform'} . "\".\n";
    }
    push( @query, "rep_platform" );

    if (defined( $bug_fields{'op_sys'} )
        && check_field('op_sys',  scalar $bug_fields{'op_sys'},
                       undef, ERR_LEVEL ) )
    {
        push( @values, $bug_fields{'op_sys'} );
    }
    else {
        push( @values, $params->{'defaultopsys'} );
        $err .= "Unknown operating system ";
        $err .= ( defined $bug_fields{'op_sys'} )
          ? $bug_fields{'op_sys'}
          : "unknown";
        $err .= ". Setting to default OS \"" . $params->{'defaultopsys'} . "\".\n";
    }
    push( @query, "op_sys" );

    # Process time fields
    if ( $params->{"timetrackinggroup"} ) {
        my $date = format_time( $bug_fields{'deadline'}, "%Y-%m-%d" )
          || undef;
        push( @values, $date );
        push( @query,  "deadline" );
        if ( defined $bug_fields{'estimated_time'} ) {
            eval {
                Bugzilla::Bug::ValidateTime($bug_fields{'estimated_time'}, "e");
            };
            if (!$@){
                push( @values, $bug_fields{'estimated_time'} );
                push( @query,  "estimated_time" );
            }
        }
        if ( defined $bug_fields{'remaining_time'} ) {
            eval {
                Bugzilla::Bug::ValidateTime($bug_fields{'remaining_time'}, "r");
            };
            if (!$@){
                push( @values, $bug_fields{'remaining_time'} );
                push( @query,  "remaining_time" );
            }
        }
        if ( defined $bug_fields{'actual_time'} ) {
            eval {
                Bugzilla::Bug::ValidateTime($bug_fields{'actual_time'}, "a");
            };
            if ($@){
                $bug_fields{'actual_time'} = 0.0;
                $err .= "Invalid Actual Time. Setting to 0.0\n";
            }
        }
        else {
            $bug_fields{'actual_time'} = 0.0;
            $err .= "Actual time not defined. Setting to 0.0\n";
        }
    }

    # Reporter Assignee QA Contact
    my $exporterid = $exporter->id;
    my $reporterid = login_to_id( $bug_fields{'reporter'} )
      if $bug_fields{'reporter'};
    push( @query, "reporter" );
    if ( ( $bug_fields{'reporter'} ) && ($reporterid) ) {
        push( @values, $reporterid );
    }
    else {
        push( @values, $exporterid );
        $err .= "The original reporter of this bug does not have\n";
        $err .= "   an account here. Reassigning to the person who moved\n";
        $err .= "   it here: $exporter_login.\n";
        if ( $bug_fields{'reporter'} ) {
            $err .= "   Previous reporter was $bug_fields{'reporter'}.\n";
        }
        else {
            $err .= "   Previous reporter is unknown.\n";
        }
    }

    my $changed_owner = 0;
    my $owner;
    push( @query, "assigned_to" );
    if ( ( $bug_fields{'assigned_to'} )
        && ( $owner = login_to_id( $bug_fields{'assigned_to'} )) ) {
        push( @values, $owner );
    }
    else {
        push( @values, $component->default_assignee->id );
        $changed_owner = 1;
        $err .= "The original assignee of this bug does not have\n";
        $err .= "   an account here. Reassigning to the default assignee\n";
        $err .= "   for the component, ". $component->default_assignee->login .".\n";
        if ( $bug_fields{'assigned_to'} ) {
            $err .= "   Previous assignee was $bug_fields{'assigned_to'}.\n";
        }
        else {
            $err .= "   Previous assignee is unknown.\n";
        }
    }

    if ( $params->{"useqacontact"} ) {
        my $qa_contact;
        push( @query, "qa_contact" );
        if ( ( defined $bug_fields{'qa_contact'})
            && ( $qa_contact = login_to_id( $bug_fields{'qa_contact'} ) ) ) {
            push( @values, $qa_contact );
        }
        else {
            push( @values, $component->default_qa_contact->id || undef );
            if ($component->default_qa_contact->id){
                $err .= "Setting qa contact to the default for this product.\n";
                $err .= "   This bug either had no qa contact or an invalid one.\n";
            }
        }
    }

    # Status & Resolution
    my $has_res = defined($bug_fields{'resolution'});
    my $has_status = defined($bug_fields{'bug_status'});
    my $valid_res = check_field('resolution',  
                                  scalar $bug_fields{'resolution'}, 
                                  undef, ERR_LEVEL );
    my $valid_status = check_field('bug_status',  
                                  scalar $bug_fields{'bug_status'}, 
                                  undef, ERR_LEVEL );
    my $is_open = is_open_state($bug_fields{'bug_status'}); 
    my $status = $bug_fields{'bug_status'} || undef;
    my $resolution = $bug_fields{'resolution'} || undef;
    
    # Check everconfirmed 
    my $everconfirmed;
    if ($product->votes_to_confirm) {
        $everconfirmed = $bug_fields{'everconfirmed'} || 0;
    }
    else {
        $everconfirmed = 1;
    }
    push (@query,  "everconfirmed");
    push (@values, $everconfirmed);

    # Sanity check will complain about having bugs marked duplicate but no
    # entry in the dup table. Since we can't tell the bug ID of bugs
    # that might not yet be in the database we have no way of populating
    # this table. Change the resolution instead.
    if ( $valid_res  && ( $bug_fields{'resolution'} eq "DUPLICATE" ) ) {
        $resolution = "MOVED";
        $err .= "This bug was marked DUPLICATE in the database ";
        $err .= "it was moved from.\n    Changing resolution to \"MOVED\"\n";
    } 

    # If there is at least 1 initial bug status different from UNCO, use it,
    # else use the open bug status with the lowest sortkey (different from UNCO).
    my @bug_statuses = @{Bugzilla::Status->can_change_to()};
    @bug_statuses = grep { $_->name ne 'UNCONFIRMED' } @bug_statuses;

    my $initial_status;
    if (scalar(@bug_statuses)) {
        $initial_status = $bug_statuses[0]->name;
    }
    else {
        @bug_statuses = @{Bugzilla::Status->get_all()};
        # Exclude UNCO and inactive bug statuses.
        @bug_statuses = grep { $_->is_active && $_->name ne 'UNCONFIRMED'} @bug_statuses;
        my @open_statuses = grep { $_->is_open } @bug_statuses;
        if (scalar(@open_statuses)) {
            $initial_status = $open_statuses[0]->name;
        }
        else {
            # There is NO other open bug statuses outside UNCO???
            Error("no open bug statuses available.");
        }
    }

    if($has_status){
        if($valid_status){
            if($is_open){
                if($has_res){
                    $err .= "Resolution set on an open status.\n";
                    $err .= "   Dropping resolution $resolution\n";
                    $resolution = undef;
                }
                if($changed_owner){
                    if($everconfirmed){  
                        $status = $initial_status;
                    }
                    else{
                        $status = "UNCONFIRMED";
                    }
                    if ($status ne $bug_fields{'bug_status'}){
                        $err .= "Bug reassigned, setting status to \"$status\".\n";
                        $err .= "   Previous status was \"";
                        $err .=  $bug_fields{'bug_status'} . "\".\n";
                    }
                }
                if($everconfirmed){
                    if($status eq "UNCONFIRMED"){
                        $err .= "Bug Status was UNCONFIRMED but everconfirmed was true\n";
                        $err .= "   Setting status to $initial_status\n";
                        $err .= "Resetting votes to 0\n" if ( $bug_fields{'votes'} );
                        $status = $initial_status;
                    }
                }
                else{ # $everconfirmed is false
                    if($status ne "UNCONFIRMED"){
                        $err .= "Bug Status was $status but everconfirmed was false\n";
                        $err .= "   Setting status to UNCONFIRMED\n";
                        $status = "UNCONFIRMED";
                    }
                }
            }
            else{ # $is_open is false
               if(!$has_res){
                   $err .= "Missing Resolution. Setting status to ";
                   if($everconfirmed){
                       $status = $initial_status;
                       $err .= "$initial_status\n";
                   }
                   else{
                       $status = "UNCONFIRMED";
                       $err .= "UNCONFIRMED\n";
                   }
               }
               if(!$valid_res){
                   $err .= "Unknown resolution \"$resolution\".\n";
                   $err .= "   Setting resolution to MOVED\n";
                   $resolution = "MOVED";
               }
            }   
        }
        else{ # $valid_status is false
            if($everconfirmed){  
                $status = $initial_status;
            }
            else{
                $status = "UNCONFIRMED";
            }        
            $err .= "Bug has invalid status, setting status to \"$status\".\n";
            $err .= "   Previous status was \"";
            $err .=  $bug_fields{'bug_status'} . "\".\n";
            $resolution = undef;
        }
                
    }
    else{ #has_status is false
        if($everconfirmed){  
            $status = $initial_status;
        }
        else{
            $status = "UNCONFIRMED";
        }        
        $err .= "Bug has no status, setting status to \"$status\".\n";
        $err .= "   Previous status was unknown\n";
        $resolution = undef;
    }
                                 
    if (defined $resolution){
        push( @query,  "resolution" );
        push( @values, $resolution );
    }
    
    # Bug status
    push( @query,  "bug_status" );
    push( @values, $status );

    # Custom fields - Multi-select fields have their own table.
    my %multi_select_fields;
    foreach my $field (Bugzilla->active_custom_fields) {
        my $custom_field = $field->name;
        my $value = $bug_fields{$custom_field};
        next unless defined $value;
        if ($field->type == FIELD_TYPE_FREETEXT) {
            push(@query, $custom_field);
            push(@values, clean_text($value));
        } elsif ($field->type == FIELD_TYPE_TEXTAREA) {
            push(@query, $custom_field);
            push(@values, $value);
        } elsif ($field->type == FIELD_TYPE_SINGLE_SELECT) {
            my $is_well_formed = check_field($custom_field, $value, undef, ERR_LEVEL);
            if ($is_well_formed) {
                push(@query, $custom_field);
                push(@values, $value);
            } else {
                $err .= "Skipping illegal value \"$value\" in $custom_field.\n" ;
            }
        } elsif ($field->type == FIELD_TYPE_MULTI_SELECT) {
            my @legal_values;
            foreach my $item (_to_array($value)) {
                my $is_well_formed = check_field($custom_field, $item, undef, ERR_LEVEL);
                if ($is_well_formed) {
                    push(@legal_values, $item);
                } else {
                    $err .= "Skipping illegal value \"$item\" in $custom_field.\n" ;
                }
            }
            if (scalar @legal_values) {
                $multi_select_fields{$custom_field} = \@legal_values;
            }
        } elsif ($field->type == FIELD_TYPE_DATETIME) {
            eval { $value = Bugzilla::Bug->_check_datetime_field($value); };
            if ($@) {
                $err .= "Skipping illegal value \"$value\" in $custom_field.\n" ;
            }
            else {
                push(@query, $custom_field);
                push(@values, $value);
            }
        } else {
            $err .= "Type of custom field $custom_field is an unhandled FIELD_TYPE: " .
                    $field->type . "\n";
        }
    }

    # For the sake of sanitycheck.cgi we do this.
    # Update lastdiffed if you do not want to have mail sent
    unless ($mail) {
        push @query,  "lastdiffed";
        push @values, $timestamp;
    }

    # INSERT the bug
    my $query = "INSERT INTO bugs (" . join( ", ", @query ) . ") VALUES (";
       $query .= '?,' foreach (@values);
    chop($query);    # Remove the last comma.
       $query .= ")";

    $dbh->do( $query, undef, @values );
    my $id = $dbh->bz_last_key( 'bugs', 'bug_id' );

    # We are almost certain to get some uninitialized warnings
    # Since this is just for debugging the query, let's shut them up
    eval {
        no warnings 'uninitialized';
        Debug(
            "Bug Query: INSERT INTO bugs (\n"
              . join( ",\n", @query )
              . "\n) VALUES (\n"
              . join( ",\n", @values ),
            DEBUG_LEVEL
        );
    };

    # Handle CC's
    if ( defined $bug_fields{'cc'} ) {
        my %ccseen;
        my $sth_cc = $dbh->prepare("INSERT INTO cc (bug_id, who) VALUES (?,?)");
        foreach my $person (_to_array($bug_fields{'cc'})) {
            next unless $person;
            my $uid;
            if ($uid = login_to_id($person)) {
                if ( !$ccseen{$uid} ) {
                    $sth_cc->execute( $id, $uid );
                    $ccseen{$uid} = 1;
                }
            }
            else {
                $err .= "CC member $person does not have an account here\n";
            }
        }
    }

    # Handle keywords
    if ( defined( $bug_fields{'keywords'} ) ) {
        my %keywordseen;
        my $key_sth = $dbh->prepare(
            "INSERT INTO keywords 
                      (bug_id, keywordid) VALUES (?,?)"
        );
        foreach my $keyword ( split( /[\s,]+/, $bug_fields{'keywords'} )) {
            next unless $keyword;
            my $keyword_obj = new Bugzilla::Keyword({name => $keyword});
            if (!$keyword_obj) {
                $err .= "Skipping unknown keyword: $keyword.\n";
                next;
            }
            if (!$keywordseen{$keyword_obj->id}) {
                $key_sth->execute($id, $keyword_obj->id);
                $keywordseen{$keyword_obj->id} = 1;
            }
        }
        my ($keywordarray) = $dbh->selectcol_arrayref(
            "SELECT d.name FROM keyworddefs d
                    INNER JOIN keywords k 
                    ON d.id = k.keywordid 
                    WHERE k.bug_id = ? 
                    ORDER BY d.name", undef, $id);
        my $keywordstring = join( ", ", @{$keywordarray} );
        $dbh->do( "UPDATE bugs SET keywords = ? WHERE bug_id = ?",
            undef, $keywordstring, $id )
    }

    # Insert values of custom multi-select fields. They have already
    # been validated.
    foreach my $custom_field (keys %multi_select_fields) {
        my $sth = $dbh->prepare("INSERT INTO bug_$custom_field
                                 (bug_id, value) VALUES (?, ?)");
        foreach my $value (@{$multi_select_fields{$custom_field}}) {
            $sth->execute($id, $value);
        }
    }

    # Parse bug flags
    foreach my $bflag ( $bug->children('flag')) {
        next unless ( defined($bflag) );
        $err .= flag_handler(
            $bflag->{'att'}->{'name'},   $bflag->{'att'}->{'status'},
            $bflag->{'att'}->{'setter'}, $bflag->{'att'}->{'requestee'},
            $exporterid,                 $id,
            $comp_id,                    $prod_id,
            undef
        );
    }

    # Insert Attachments for the bug
    foreach my $att (@attachments) {
        if ($att eq "err"){
            $err .= "No attachment ID specified, dropping attachment\n";
            next;
        }
        if (!$exporter->in_group($params->{'insidergroup'}) && $att->{'isprivate'}){
            $err .= "Exporter not in insidergroup and attachment marked private.\n";
            $err .= "   Marking attachment public\n";
            $att->{'isprivate'} = 0;
        }

        my $attacher_id = $att->{'attacher'} ? login_to_id($att->{'attacher'}) : undef;

        $dbh->do("INSERT INTO attachments 
                 (bug_id, creation_ts, modification_time, filename, description,
                 mimetype, ispatch, isprivate, isobsolete, submitter_id) 
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            undef, $id, $att->{'date'}, $att->{'date'}, $att->{'filename'},
            $att->{'desc'}, $att->{'ctype'}, $att->{'ispatch'},
            $att->{'isprivate'}, $att->{'isobsolete'}, $attacher_id || $exporterid);
        my $att_id   = $dbh->bz_last_key( 'attachments', 'attach_id' );
        my $att_data = $att->{'data'};
        my $sth = $dbh->prepare("INSERT INTO attach_data (id, thedata) 
                                 VALUES ($att_id, ?)" );
        trick_taint($att_data);
        $sth->bind_param( 1, $att_data, $dbh->BLOB_TYPE );
        $sth->execute();

        $comments .= "Imported an attachment (id=$att_id)\n";
        if (!$attacher_id) {
            if ($att->{'attacher'}) {
                $err .= "The original submitter of attachment $att_id was\n   ";
                $err .= $att->{'attacher'} . ", but he doesn't have an account here.\n";
            }
            else {
                $err .= "The original submitter of attachment $att_id is unknown.\n";
            }
            $err .= "   Reassigning to the person who moved it here: $exporter_login.\n";
        }

        # Process attachment flags
        foreach my $aflag (@{ $att->{'flags'} }) {
            next unless defined($aflag) ;
            $err .= flag_handler(
                $aflag->{'name'},   $aflag->{'status'},
                $aflag->{'setter'}, $aflag->{'requestee'},
                $exporterid,        $id,
                $comp_id,           $prod_id,
                $att_id
            );
        }
    }

    # Clear the attachments array for the next bug
    @attachments = ();

    # Insert longdesc and append any errors
    my $worktime = $bug_fields{'actual_time'} || 0.0;
    $worktime = 0.0 if (!$exporter->in_group($params->{'timetrackinggroup'}));
    $long_description .= "\n" . $comments;
    if ($err) {
        $long_description .= "\n$err\n";
    }
    trick_taint($long_description);
    $dbh->do("INSERT INTO longdescs 
                     (bug_id, who, bug_when, work_time, isprivate, thetext) 
                     VALUES (?,?,?,?,?,?)", undef,
        $id, $exporterid, $timestamp, $worktime, $private, $long_description
    );
    Bugzilla::Bug->new($id)->_sync_fulltext('new_bug');

    # Add this bug to each group of which its product is a member.
    my $sth_group = $dbh->prepare("INSERT INTO bug_group_map (bug_id, group_id) 
                         VALUES (?, ?)");
    foreach my $group_id ( keys %{ $product->group_controls } ) {
        if ($product->group_controls->{$group_id}->{'membercontrol'} != CONTROLMAPNA
            && $product->group_controls->{$group_id}->{'othercontrol'} != CONTROLMAPNA){
            $sth_group->execute( $id, $group_id );
        }
    }

    $log .= "Bug ${urlbase}show_bug.cgi?id=$bug_fields{'bug_id'} ";
    $log .= "imported as bug $id.\n";
    $log .= $params->{"urlbase"} . "show_bug.cgi?id=$id\n\n";
    if ($err) {
        $log .= "The following problems were encountered while creating bug $id.\n";
        $log .= $err;
        $log .= "You may have to set certain fields in the new bug by hand.\n\n";
    }
    Debug( $log, OK_LEVEL );
    push(@logs, $log);
    Bugzilla::BugMail::Send( $id, { 'changer' => $exporter_login } ) if ($mail);

    # done with the xml data. Lets clear it from memory
    $twig->purge;

}

Debug( "Reading xml", DEBUG_LEVEL );

# Read STDIN in slurp mode. VERY dangerous, but we live on the wild side ;-)
local ($/);
$xml = <>;

# If there's anything except whitespace before <?xml then we guess it's a mail
# and MIME::Parser should parse it. Else don't.
if ($xml =~ m/\S.*<\?xml/s ) {

    # If the email was encoded (Mailer::MessageToMTA() does it when using UTF-8),
    # we have to decode it first, else the XML parsing will fail.
    my $parser = MIME::Parser->new;
    $parser->output_to_core(1);
    $parser->tmp_to_core(1);
    my $entity = $parser->parse_data($xml);
    my $bodyhandle = $entity->bodyhandle;
    $xml = $bodyhandle->as_string;

}

# remove everything in file before xml header
$xml =~ s/^.+(<\?xml version.+)$/$1/s;

Debug( "Parsing tree", DEBUG_LEVEL );
my $twig = XML::Twig->new(
    twig_handlers => {
        bug        => \&process_bug,
        attachment => \&process_attachment
    },
    start_tag_handlers => { bugzilla => \&init }
);
$twig->parse($xml);
my $root       = $twig->root;
my $maintainer = $root->{'att'}->{'maintainer'};
my $exporter   = $root->{'att'}->{'exporter'};
my $urlbase    = $root->{'att'}->{'urlbase'};

# It is time to email the result of the import.
my $log = join("\n\n", @logs);
$log .=  "\n\nImported $bugtotal bug(s) from $urlbase,\n  sent by $exporter.\n";
my $subject =  "$bugtotal Bug(s) successfully moved from $urlbase to " 
   . $params->{"urlbase"};
my @to = ($exporter, $maintainer);
MailMessage( $subject, $log, @to );

__END__

=head1 NAME

importxml - Import bugzilla bug data from xml.

=head1 SYNOPSIS

    importxml.pl [options] [file ...]

 Options:
       -? --help        brief help message
       -v --verbose     print error and debug information. 
                        Mulltiple -v increases verbosity
       -m --sendmail    send mail to recipients with log of bugs imported
       --attach_path    The path to the attachment files.
                        (Required if encoding="filename" is used for attachments.)

=head1 OPTIONS

=over 8

=item B<-?>

    Print a brief help message and exits.

=item B<-v>

    Print error and debug information. Mulltiple -v increases verbosity

=item B<-m>

    Send mail to exporter with a log of bugs imported and any errors.

=back

=head1 DESCRIPTION

     This script is used to import bugs from another installation of bugzilla.
     It can be used in two ways.
     First using the move function of bugzilla
     on another system will send mail to an alias provided by
     the administrator of the target installation (you). Set up an alias
     similar to the one given below so this mail will be automatically 
     run by this script and imported into your database.  Run 'newaliases'
     after adding this alias to your aliases file. Make sure your sendmail
     installation is configured to allow mail aliases to execute code. 

     bugzilla-import: "|/usr/bin/perl /opt/bugzilla/importxml.pl --mail"

     Second it can be run from the command line with any xml file from 
     STDIN that conforms to the bugzilla DTD. In this case you can pass 
     an argument to set whether you want to send the
     mail that will be sent to the exporter and maintainer normally.

     importxml.pl [options] bugsfile.xml

=cut