View Issue Details

IDProjectCategoryView StatusLast Update
0007469mantisbtmigrationpublic2023-08-06 14:06
Reporterafontquerni Assigned Todregad  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionno change required 
Product Version1.0.5 
Summary0007469: bugzilla2mantis.php script
Description

On Set-2006 I was looking for Bugzilla to Mantis (Perl) by Cris Daniluk (http://www.cadencetools.org/projects/bugzilla2mantis/)

But Nobody didn't find any thing on the web, all web page links are broken or deleted (http://freshmeat.net/redir/bugzilla2mantis/52873/url_tgz/bugzilla2mantis-0.2.tar.gz) ((http://forums.mantisbugtracker.com/viewtopic.php?p=5745&sid=1a679a9fa211306bfd51eb33f7da2579))

At last, I decided to start my first php script to convert bugs data from bugzilla to mantis (bugzilla2mantis.php). It is alfa script but its did basic and functional great job.
I give mantis2bugzilla.php to mantis community to improve bugzilla migration feature

TagsNo tags attached.
Attached Files
bugzilla2mantis.php (39,251 bytes)   
<?php
/*
// ------------------------------------------------------------------------------------------
// LICENSE
// ------------------------------------------------------------------------------------------

        # bugzilla2mantis.php - Development Status "Alpha" 
        # Convert bugzilla 2.20 database to mantis 1.0.5 sql data inserts
        #
        # Copyright (C) 2006  Agusti Fontquerni afontquerni -at- gmail -dot- com
        # This program is distributed under the terms and conditions 
        # of the GNU GENERAL PUBLIC LICENSE Version 2 (http://www.gnu.org/copyleft/gpl.html)

// ------------------------------------------------------------------------------------------
// README
// ------------------------------------------------------------------------------------------
On Set-2006 I was looking for Bugzilla to Mantis (Perl) by Cris Daniluk (http://www.cadencetools.org/projects/bugzilla2mantis/)     

But Nobody didn't find any thing on the web, all web page links are broken or deleted (http://freshmeat.net/redir/bugzilla2mantis/52873/url_tgz/bugzilla2mantis-0.2.tar.gz) ((http://forums.mantisbugtracker.com/viewtopic.php?p=5745&sid=1a679a9fa211306bfd51eb33f7da2579))    

At last, I decided to start my first php script to convert bugs data from bugzilla to mantis (bugzilla2mantis.php).  It is alfa script but its did basic and functional great job.  
I give mantis2bugzilla.php to mantis community to improve bugzilla migration feature

// ------------------------------------------------------------------------------------------
// INSTALL
// ------------------------------------------------------------------------------------------
1) Backup all databases
2) Copy this file to bugzilla web site ( example: /var/www/bugzilla )
3) Modify database parameters "server", "user", "password", "database" ( 'localhost','root','', 'bugzilla' ) in "bz2m_connect()" function 
4) Open "bugzilla2mantis.php" with web browser ( example: localhost:/bugzilla/bugzilla2mantis.php )
5) Check output web page
6) Select all (Ctrl+A). Copy from web browser to test editor. Save to file ( example: mantis.sql )
7) Install mantis website ( http://www.mantisbugtracker.com/ )
8) Modify mantis database from bugzilla data "bugzilla2mantis.php" ( example: mysql -u root bugtracker < mantis.sql )
9) Now, report bugs with mantis ... 

// ------------------------------------------------------------------------------------------
// CHANGELOG
// ------------------------------------------------------------------------------------------
2006.09.24 - Clean code and Documentation (afontquerni)
2006.09.16 - Resolve some major bugs (afontquerni)
2006.09.15 - Add bugnote_table and bugnote_text_table  (afontquerni)
2006.09.14 - Initial release (afontquerni)

// ------------------------------------------------------------------------------------------
// DESCRIPTION STRUCTURE - DOCUMENTATION
// ------------------------------------------------------------------------------------------

bz2m_connect($link); // !!! MODIFY: "server", "user", "password", "database" ( 'localhost','root','', 'bugzilla' ) !!

echo "DELETE FROM mantis_user_table;";

bz2m_migration_profiles2mantis_user_table();  //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_products2mantis_project_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_bugs_activity2mantis_bug_history_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

mysql_close( $link);

*/

// ------------------------------------------------------------------------------------------
//     PHP CODE
// ------------------------------------------------------------------------------------------

function bz2m_connect( &$link ) { // BUGZILLA - connect to database
	$link = mysql_connect('localhost', 'root', '');
	if (!$link)
	{
	  die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("bugzilla",  $link);
} 

// ------------------------------------------------------------------------------------------

function bz2m_select( $sql_fields, $sql_table, &$data, &$total ) { // BUGZILLA -  read bugs

	$sql="SELECT ";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql." FROM "."`".$sql_table."`";
//	$sql = $sql." FROM "."`".$sql_table."` WHERE `bug_id`<3";
//	$sql = $sql." FROM "."`".$sql_table."`  limit 0 ,3;";

	//echo "<br />"; echo $sql;  echo "<br />";
	

	$result = mysql_query( $sql );

	$columns = array();
	$data = array();

	// Table(Rows)
	while($row = mysql_fetch_array($result))
	{
		foreach ($sql_fields as $key1=> $value1 ) {
			$columns[]= $row[$value1];
			//echo "$value1 $row[$value1] <br />";
		}
		$data[]= $columns;
		unset($columns);
	}

	$total = mysql_num_rows($result);
	mysql_free_result($result);

}

// ------------------------------------------------------------------------------------------

function bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, &$data, $callback_function ) { // MANTIS -  write bugs

	$sql="INSERT INTO `$sql_table` (";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql.") VALUES( ";

	$sql_header = $sql;

	foreach ($data as $key0 => $value0 ) {
		unset($sql);
		$sql = $sql_header;
		$row_count = $key0 + 1;

		$flag_first = true;

		foreach ($value0 as $key1 => $value1 ) {
			$sql.= call_user_func( $callback_function, $row_count, $flag_first, $sql_fields_source[$key1], $value1);
			if($flag_first )
				$flag_first = false;
			//echo "<br />"; echo "$key1 $value1"; echo "<br />";

		}
		$sql.= " );";
		echo "<br />"; echo $sql; echo "<br />";

		
	}
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table() //USERS
{
/*
USERS
#####

mysql> describe profiles;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| userid         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| login_name     | varchar(255) | NO   | UNI | NULL    |                |
| cryptpassword  | varchar(128) | YES  |     | NULL    |                |
| realname       | varchar(255) | YES  |     | NULL    |                |
| disabledtext   | mediumtext   | NO   |     | NULL    |                |
| mybugslink     | tinyint(4)   | NO   |     | 1       |                |
| refreshed_when | datetime     | NO   |     | NULL    |                |
| extern_id      | varchar(64)  | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

#####

mysql> describe mantis_user_table;
+-----------------------------+------------------+------+-----+---------------------+----------------+
| Field                       | Type             | Null | Key | Default             | Extra          |
+-----------------------------+------------------+------+-----+---------------------+----------------+
| id                          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| username                    | varchar(32)      | NO   | UNI | NULL                |                |
| realname                    | varchar(64)      | NO   |     | NULL                |                |
| email                       | varchar(64)      | NO   |     | NULL                |                |
| password                    | varchar(32)      | NO   |     | NULL                |                |
| date_created                | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| last_visit                  | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| enabled                     | tinyint(4)       | NO   | MUL | 1                   |                |
| protected                   | tinyint(4)       | NO   |     | 0                   |                |
| access_level                | smallint(6)      | NO   | MUL | 10                  |                |
| login_count                 | int(11)          | NO   |     | 0                   |                |
| lost_password_request_count | smallint(6)      | NO   |     | 0                   |                |
| failed_login_count          | smallint(6)      | NO   |     | 0                   |                |
| cookie_string               | varchar(64)      | NO   | UNI | NULL                |                |
+-----------------------------+------------------+------+-----+---------------------+----------------+
14 rows in set (0.00 sec)

*/


$sql_table_source= "profiles";
$sql_fields_source=array( "userid","login_name","cryptpassword","realname","login_name", "refreshed_when", "cryptpassword" );

$sql_table= "mantis_user_table";
$sql_fields = array("id","username","password","realname","email","date_created", "cookie_string");
$callback_function = "bz2m_migration_profiles2mantis_user_table_callback";

// ------

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

	$sql = "";
	//echo "$key $value <br />";

switch ($key)
{
	case "userid":
		$value1 = $value;
		break;
	case "login_name":
		$value1 = "'".$value."'";
		break;
	case "cryptpassword":
		$value1 = "'".$value."'";
		break;
	case "realname":
		$value1 = "'".$value."'";
		break;
	case "refreshed_when":
		$value1 = "'".$value."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

//		echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table() //PROJECTS
{

/*
PROJECTS
#####
mysql> describe products;
+-------------------+-------------+------+-----+---------+----------------+
| Field             | Type        | Null | Key | Default | Extra          |
+-------------------+-------------+------+-----+---------+----------------+
| id                | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name              | varchar(64) | NO   | UNI | NULL    |                |
| classification_id | smallint(6) | NO   |     | 1       |                |
| description       | mediumtext  | YES  |     | NULL    |                |
| milestoneurl      | tinytext    | NO   |     | NULL    |                |
| disallownew       | tinyint(4)  | NO   |     | NULL    |                |
| votesperuser      | smallint(6) | NO   |     | NULL    |                |
| maxvotesperbug    | smallint(6) | NO   |     | 10000   |                |
| votestoconfirm    | smallint(6) | NO   |     | NULL    |                |
| defaultmilestone  | varchar(20) | NO   |     | ---     |                |
+-------------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> 

#####

mysql> describe mantis_project_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(128)     | NO   | UNI | NULL    |                |
| status      | smallint(6)      | NO   |     | 10      |                |
| enabled     | tinyint(4)       | NO   |     | 1       |                |
| view_state  | smallint(6)      | NO   | MUL | 10      |                |
| access_min  | smallint(6)      | NO   |     | 10      |                |
| file_path   | varchar(250)     | NO   |     | NULL    |                |
| description | text             | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
8 rows in set (0.01 sec)

*/

$sql_table_source= "products";
$sql_fields_source= array( "id","name","description","votesperuser","votestoconfirm" );

$sql_table= "mantis_project_table";
$callback_function = "bz2m_migration_products2mantis_project_table_callback";
$sql_fields = array("id","name","description", "status", "enabled");


// ----

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "id":
		$value1 = $value;
		break;
	case "name":
	case "description":
		$value1 = "'".$value."'";
		break;
	case "votesperuser":
		$value1 = 10;
		break;
	case "votestoconfirm":
		$value1 = 1;
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

//		echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table() //BUGS
{

/*
BUGS
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| keywords            | mediumtext   | NO   |     | NULL    |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| alias               | varchar(20)  | YES  | UNI | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


#####
mysql> describe mantis_bug_table;
+-------------------+------------------+------+-----+---------------------+----------------+
| Field             | Type             | Null | Key | Default             | Extra          |
+-------------------+------------------+------+-----+---------------------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| project_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id       | int(10) unsigned | NO   |     | 0                   |                |
| handler_id        | int(10) unsigned | NO   |     | 0                   |                |
| duplicate_id      | int(10) unsigned | NO   |     | 0                   |                |
| priority          | smallint(6)      | NO   |     | 30                  |                |
| severity          | smallint(6)      | NO   |     | 50                  |                |
| reproducibility   | smallint(6)      | NO   |     | 10                  |                |
| status            | smallint(6)      | NO   | MUL | 10                  |                |
| resolution        | smallint(6)      | NO   |     | 10                  |                |
| projection        | smallint(6)      | NO   |     | 10                  |                |
| category          | varchar(64)      | NO   |     | NULL                |                |
| date_submitted    | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| last_updated      | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| eta               | smallint(6)      | NO   |     | 10                  |                |
| bug_text_id       | int(10) unsigned | NO   |     | 0                   |                |
| os                | varchar(32)      | NO   |     | NULL                |                |
| os_build          | varchar(32)      | NO   |     | NULL                |                |
| platform          | varchar(32)      | NO   |     | NULL                |                |
| version           | varchar(64)      | NO   |     | NULL                |                |
| fixed_in_version  | varchar(64)      | NO   | MUL | NULL                |                |
| build             | varchar(32)      | NO   |     | NULL                |                |
| profile_id        | int(10) unsigned | NO   |     | 0                   |                |
| view_state        | smallint(6)      | NO   |     | 10                  |                |
| summary           | varchar(128)     | NO   |     | NULL                |                |
| sponsorship_total | int(11)          | NO   | MUL | 0                   |                |
| sticky            | tinyint(4)       | NO   |     | 0                   |                |
+-------------------+------------------+------+-----+---------------------+----------------+
27 rows in set (0.00 sec)

mysql>
*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","assigned_to","bug_severity","bug_status","product_id","creation_ts","creation_ts","priority","reporter", "short_desc", "bug_id" );

$sql_table= "mantis_bug_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_table_callback";
$sql_fields = array("id","handler_id","severity","status","project_id","date_submitted","last_updated","priority","reporter_id", "summary", "bug_text_id" );

// ---

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

$bug_severity_map = array( "enhancement"=>10, "trivial"=>20, "minor"=>40, "normal"=>50, "major"=>60, "critical"=>70, "blocker"=>80 );
$bug_status_map = array( "UNCONFIRMED"=>10, "NEW"=>40, "ASSIGNED"=>50, "RESOLVED"=>80, "CLOSED"=>90 );
$priority_map = array( "P5"=>10, "P4"=>20, "P3"=>30, "P2"=>40, "P1"=>50 );

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "assigned_to":
	case "reporter":
	case "product_id":
		$value1 = $value;
		break;
	case "short_desc":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	case "creation_ts":
		$value1 = "'".$value."'";
		break;

	case "bug_severity":
		if( isset($bug_severity_map[$value]) ) {
			$value1 = $bug_severity_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "bug_status":
		if( isset($bug_status_map[$value]) ) {
			$value1 = $bug_status_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "priority":
		if( isset($priority_map[$value]) ) {
			$value1 = $priority_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

//		echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table() //BUGS HISTORY
{

/*
BUGS HISTORY
#####
mysql> describe bugs_activity;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| bug_id    | mediumint(9) | NO   | MUL | NULL    |       |
| attach_id | mediumint(9) | YES  |     | NULL    |       |
| who       | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when  | datetime     | NO   | MUL | NULL    |       |
| fieldid   | mediumint(9) | NO   | MUL | NULL    |       |
| added     | tinytext     | YES  |     | NULL    |       |
| removed   | tinytext     | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

#####

mysql> describe mantis_bug_history_table;
+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| user_id       | int(10) unsigned | NO   | MUL | 0                   |                |
| bug_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| date_modified | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| field_name    | varchar(32)      | NO   |     | NULL                |                |
| old_value     | varchar(128)     | NO   |     | NULL                |                |
| new_value     | varchar(128)     | NO   |     | NULL                |                |
| type          | smallint(6)      | NO   |     | 0                   |                |
+---------------+------------------+------+-----+---------------------+----------------+
8 rows in set (0.00 sec)

*/

$sql_table_source= "bugs_activity";
$sql_fields_source = array( "bug_id","who","bug_when","fieldid","added","removed" );

$sql_table= "mantis_bug_history_table";
$callback_function = "bz2m_migration_bugs_activity2mantis_bug_history_table_callback";
$sql_fields = array("bug_id","user_id","date_modified","field_name","old_value","new_value");

// ----

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "who":
	case "bug_when":
	case "fieldid":
	case "added":
	case "removed":
		$value1 = "'".$value."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

//		echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table() //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

#####

mysql> describe mantis_bugnote_table;
+-----------------+------------------+------+-----+---------------------+----------------+
| Field           | Type             | Null | Key | Default             | Extra          |
+-----------------+------------------+------+-----+---------------------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| bug_id          | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id     | int(10) unsigned | NO   |     | 0                   |                |
| bugnote_text_id | int(10) unsigned | NO   |     | 0                   |                |
| view_state      | smallint(6)      | NO   |     | 10                  |                |
| date_submitted  | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| last_modified   | datetime         | NO   | MUL | 1970-01-01 00:00:01 |                |
| note_type       | int(11)          | YES  |     | 0                   |                |
| note_attr       | varchar(250)     | YES  |     | NULL                |                |
+-----------------+------------------+------+-----+---------------------+----------------+
9 rows in set (0.01 sec)

mysql>

#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array( "bug_id","who","bug_when","bug_when","work_time", );

$sql_table= "mantis_bugnote_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_table_callback";
$sql_fields = array("bug_id","reporter_id","date_submitted","last_modified","bugnote_text_id",);

// ---------

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "who":
		$value1 = $value;
		break;
	case "bug_when":
		$value1 = "'".$value."'";
		break;
	case "work_time":
		$value1 = $row_count;
		break;
	case "thetext":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

//		echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table() //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)


mysql> describe mantis_bugnote_text_table;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| note  | text             | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array( "thetext" );

$sql_table= "mantis_bugnote_text_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_text_table_callback";
$sql_fields = array("note");

// ---------

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "thetext":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

//		echo "<br />"; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table() //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| keywords            | mediumtext   | NO   |     | NULL    |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| alias               | varchar(20)  | YES  | UNI | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_bug_text_table;
+------------------------+------------------+------+-----+---------+----------------+
| Field                  | Type             | Null | Key | Default | Extra          |
+------------------------+------------------+------+-----+---------+----------------+
| id                     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| description            | text             | NO   |     | NULL    |                |
| steps_to_reproduce     | text             | NO   |     | NULL    |                |
| additional_information | text             | NO   |     | NULL    |                |
+------------------------+----
mysql>

#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","short_desc","resolution" );

$sql_table= "mantis_bug_text_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_text_table_callback";
$sql_fields = array("id","description","additional_information");

// ---

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "short_desc":
	case "resolution":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

//		echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

{ // BUGZILLA - MAIN

bz2m_connect($link);

echo "DELETE FROM mantis_user_table;";
echo "<br>";

bz2m_migration_profiles2mantis_user_table();

bz2m_migration_products2mantis_project_table();

bz2m_migration_bugs2mantis_bug_table();
bz2m_migration_bugs2mantis_bug_text_table();

bz2m_migration_bugs_activity2mantis_bug_history_table();

bz2m_migration_longdescs2mantis_bugnote_table();
bz2m_migration_longdescs2mantis_bugnote_text_table();

mysql_close( $link);

}


?>

bugzilla2mantis.php (39,251 bytes)   
bugzilla2mantisV2-jyb.php (39,938 bytes)   
<?php
/*
// ------------------------------------------------------------------------------------------
// LICENSE
// ------------------------------------------------------------------------------------------

        # bugzilla2mantis.php - Development Status "Alpha" 
        # Convert bugzilla 2.20 database to mantis 1.0.5 sql data inserts
        #
        # Copyright (C) 2006  Agusti Fontquerni afontquerni -at- gmail -dot- com
        # This program is distributed under the terms and conditions 
        # of the GNU GENERAL PUBLIC LICENSE Version 2 (http://www.gnu.org/copyleft/gpl.html)

// ------------------------------------------------------------------------------------------
// README
// ------------------------------------------------------------------------------------------
On Set-2006 I was looking for Bugzilla to Mantis (Perl) by Cris Daniluk (http://www.cadencetools.org/projects/bugzilla2mantis/)     

But Nobody didn't find any thing on the web, all web page links are broken or deleted (http://freshmeat.net/redir/bugzilla2mantis/52873/url_tgz/bugzilla2mantis-0.2.tar.gz) ((http://forums.mantisbugtracker.com/viewtopic.php?p=5745&sid=1a679a9fa211306bfd51eb33f7da2579))    

At last, I decided to start my first php script to convert bugs data from bugzilla to mantis (bugzilla2mantis.php).  It is alfa script but its did basic and functional great job.  
I give mantis2bugzilla.php to mantis community to improve bugzilla migration feature

// ------------------------------------------------------------------------------------------
// INSTALL
// ------------------------------------------------------------------------------------------
1) Backup all databases
2) Copy this file to bugzilla web site ( example: /var/www/bugzilla )
3) Modify database parameters "server", "user", "password", "database" ( 'localhost','root','', 'bugzilla' ) in "bz2m_connect()" function 
4) Open "bugzilla2mantis.php" with web browser ( example: localhost:/bugzilla/bugzilla2mantis.php )
5) Check output web page
6) Select all (Ctrl+A). Copy from web browser to test editor. Save to file ( example: mantis.sql )
7) Install mantis website ( http://www.mantisbugtracker.com/ )
8) Modify mantis database from bugzilla data "bugzilla2mantis.php" ( example: mysql -u root bugtracker < mantis.sql )
9) Now, report bugs with mantis ... 

// ------------------------------------------------------------------------------------------
// CHANGELOG
// ------------------------------------------------------------------------------------------
2006.09.24 - Clean code and Documentation (afontquerni)
2006.09.16 - Resolve some major bugs (afontquerni)
2006.09.15 - Add bugnote_table and bugnote_text_table  (afontquerni)
2006.09.14 - Initial release (afontquerni)

// ------------------------------------------------------------------------------------------
// DESCRIPTION STRUCTURE - DOCUMENTATION
// ------------------------------------------------------------------------------------------

bz2m_connect($link); // !!! MODIFY: "server", "user", "password", "database" ( 'localhost','bugzilla3','1dtiYsKjQoGD', 'bugzilla3' ) !!

echo "DELETE FROM mantis_user_table;";

bz2m_migration_profiles2mantis_user_table();  //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_products2mantis_project_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_bugs_activity2mantis_bug_history_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

mysql_close( $link);

*/

// ------------------------------------------------------------------------------------------
//     PHP CODE
// ------------------------------------------------------------------------------------------

function bz2m_connect( &$link ) { // BUGZILLA - connect to database
	$link = mysql_connect('localhost', 'root', '1mac=10pc');
	if (!$link)
	{
	  die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("bugzilla3",  $link);
} 

// ------------------------------------------------------------------------------------------

function bz2m_select( $sql_fields, $sql_table, &$data, &$total ) { // BUGZILLA -  read bugs

	$sql="SELECT ";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql." FROM "."`".$sql_table."`";
//	$sql = $sql." FROM "."`".$sql_table."` WHERE `bug_id`<3";
//	$sql = $sql." FROM "."`".$sql_table."`  limit 0 ,3;";

	//echo "<br />"; echo $sql;  echo "<br />";
	

	$result = mysql_query( $sql );

	$columns = array();
	$data = array();

	// Table(Rows)
	while($row = mysql_fetch_array($result))
	{
		foreach ($sql_fields as $key1=> $value1 ) {
			$columns[]= $row[$value1];
			//echo "$value1 $row[$value1] <br />";
		}
		$data[]= $columns;
		unset($columns);
	}

	$total = mysql_num_rows($result);
	mysql_free_result($result);

}

// ------------------------------------------------------------------------------------------

function bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, &$data, $callback_function ) { // MANTIS -  write bugs

	$sql="INSERT INTO `$sql_table` (";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql.") VALUES( ";

	$sql_header = $sql;

	foreach ($data as $key0 => $value0 ) {
		unset($sql);
		$sql = $sql_header;
		$row_count = $key0 + 1;

		$flag_first = true;

		foreach ($value0 as $key1 => $value1 ) {
			$sql.= call_user_func( $callback_function, $row_count, $flag_first, $sql_fields_source[$key1], $value1);
			if($flag_first )
				$flag_first = false;
			//echo "<br />"; echo "$key1 $value1"; echo "<br />";

		}
		$sql.= " );";
		echo $sql; echo "<br />";

		
	}
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table() //USERS
{
/*
USERS
#####

mysql> describe profiles;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| userid         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| login_name     | varchar(255) | NO   | UNI | NULL    |                |
| cryptpassword  | varchar(128) | YES  |     | NULL    |                |
| realname       | varchar(255) | YES  |     | NULL    |                |
| disabledtext   | mediumtext   | NO   |     | NULL    |                |
| mybugslink     | tinyint(4)   | NO   |     | 1       |                |
| refreshed_when | datetime     | NO   |     | NULL    |                |
| extern_id      | varchar(64)  | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

#####

mysql> describe mantis_user_table;
+-----------------------------+------------------+------+-----+---------------------+----------------+
| Field                       | Type             | Null | Key | Default             | Extra          |
+-----------------------------+------------------+------+-----+---------------------+----------------+
| id                          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| username                    | varchar(32)      | NO   | UNI | NULL                |                |
| realname                    | varchar(64)      | NO   |     | NULL                |                |
| email                       | varchar(64)      | NO   |     | NULL                |                |
| password                    | varchar(32)      | NO   |     | NULL                |                |
| date_created                | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| last_visit                  | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| enabled                     | tinyint(4)       | NO   | MUL | 1                   |                |
| protected                   | tinyint(4)       | NO   |     | 0                   |                |
| access_level                | smallint(6)      | NO   | MUL | 10                  |                |
| login_count                 | int(11)          | NO   |     | 0                   |                |
| lost_password_request_count | smallint(6)      | NO   |     | 0                   |                |
| failed_login_count          | smallint(6)      | NO   |     | 0                   |                |
| cookie_string               | varchar(64)      | NO   | UNI | NULL                |                |
+-----------------------------+------------------+------+-----+---------------------+----------------+
14 rows in set (0.00 sec)

*/


$sql_table_source= "profiles";
$sql_fields_source=array( "userid","login_name","cryptpassword","realname","login_name", "refreshed_when", "cryptpassword" );

$sql_table= "mantis_user_table";
$sql_fields = array("id","username","password","realname","email","date_created", "cookie_string");
$callback_function = "bz2m_migration_profiles2mantis_user_table_callback";

// ------

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

	$sql = "";
	//echo "$key $value <br />";

switch ($key)
{
	case "userid":
		$value1 = $value;
		break;
	case "login_name":
	case "cryptpassword":
	case "realname":
	case "refreshed_when":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table() //PROJECTS
{

/*
PROJECTS
#####
mysql> describe products;
+-------------------+-------------+------+-----+---------+----------------+
| Field             | Type        | Null | Key | Default | Extra          |
+-------------------+-------------+------+-----+---------+----------------+
| id                | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name              | varchar(64) | NO   | UNI | NULL    |                |
| classification_id | smallint(6) | NO   |     | 1       |                |
| description       | mediumtext  | YES  |     | NULL    |                |
| milestoneurl      | tinytext    | NO   |     | NULL    |                |
| disallownew       | tinyint(4)  | NO   |     | NULL    |                |
| votesperuser      | smallint(6) | NO   |     | NULL    |                |
| maxvotesperbug    | smallint(6) | NO   |     | 10000   |                |
| votestoconfirm    | smallint(6) | NO   |     | NULL    |                |
| defaultmilestone  | varchar(20) | NO   |     | ---     |                |
+-------------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> 

#####

mysql> describe mantis_project_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(128)     | NO   | UNI | NULL    |                |
| status      | smallint(6)      | NO   |     | 10      |                |
| enabled     | tinyint(4)       | NO   |     | 1       |                |
| view_state  | smallint(6)      | NO   | MUL | 10      |                |
| access_min  | smallint(6)      | NO   |     | 10      |                |
| file_path   | varchar(250)     | NO   |     | NULL    |                |
| description | text             | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
8 rows in set (0.01 sec)

*/

$sql_table_source= "products";
$sql_fields_source= array( "id","name","description","votesperuser","votestoconfirm" );

$sql_table= "mantis_project_table";
$callback_function = "bz2m_migration_products2mantis_project_table_callback";
$sql_fields = array("id","name","description", "status", "enabled");


// ----

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "id":
		$value1 = $value;
		break;
	case "name":
	case "description":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	case "votesperuser":
		$value1 = 10;
		break;
	case "votestoconfirm":
		$value1 = 1;
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table() //BUGS
{

/*
BUGS
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| keywords            | mediumtext   | NO   |     | NULL    |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| alias               | varchar(20)  | YES  | UNI | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


#####
mysql> describe mantis_bug_table;
+-------------------+------------------+------+-----+---------------------+----------------+
| Field             | Type             | Null | Key | Default             | Extra          |
+-------------------+------------------+------+-----+---------------------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| project_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id       | int(10) unsigned | NO   |     | 0                   |                |
| handler_id        | int(10) unsigned | NO   |     | 0                   |                |
| duplicate_id      | int(10) unsigned | NO   |     | 0                   |                |
| priority          | smallint(6)      | NO   |     | 30                  |                |
| severity          | smallint(6)      | NO   |     | 50                  |                |
| reproducibility   | smallint(6)      | NO   |     | 10                  |                |
| status            | smallint(6)      | NO   | MUL | 10                  |                |
| resolution        | smallint(6)      | NO   |     | 10                  |                |
| projection        | smallint(6)      | NO   |     | 10                  |                |
| category          | varchar(64)      | NO   |     | NULL                |                |
| date_submitted    | int              | NO   |     | NULL                |                |
| last_updated      | int              | NO   |     | NULL                |                |
| eta               | smallint(6)      | NO   |     | 10                  |                |
| bug_text_id       | int(10) unsigned | NO   |     | 0                   |                |
| os                | varchar(32)      | NO   |     | NULL                |                |
| os_build          | varchar(32)      | NO   |     | NULL                |                |
| platform          | varchar(32)      | NO   |     | NULL                |                |
| version           | varchar(64)      | NO   |     | NULL                |                |
| fixed_in_version  | varchar(64)      | NO   | MUL | NULL                |                |
| build             | varchar(32)      | NO   |     | NULL                |                |
| profile_id        | int(10) unsigned | NO   |     | 0                   |                |
| view_state        | smallint(6)      | NO   |     | 10                  |                |
| summary           | varchar(128)     | NO   |     | NULL                |                |
| sponsorship_total | int(11)          | NO   | MUL | 0                   |                |
| sticky            | tinyint(4)       | NO   |     | 0                   |                |
+-------------------+------------------+------+-----+---------------------+----------------+
27 rows in set (0.00 sec)

mysql>
*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","assigned_to","bug_severity","bug_status","product_id","creation_ts","creation_ts","priority","reporter", "short_desc", "bug_id" );

$sql_table= "mantis_bug_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_table_callback";
$sql_fields = array("id","handler_id","severity","status","project_id","date_submitted","last_updated","priority","reporter_id", "summary", "bug_text_id" );

// ---

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

$bug_severity_map = array( "enhancement"=>10, "trivial"=>20, "Standby"=>20, "minor"=>40, "Low"=>40, "normal"=>50, "Normal"=>50, "major"=>60, "High"=>60, "critical"=>70, "Urgent"=>70, "blocker"=>80 );
$bug_status_map = array( "UNCONFIRMED"=>10, "NEW"=>40, "ASSIGNED"=>50, "RESOLVED"=>80, "CLOSED"=>90, "NON CONFIRM�"=>10, "R�OUVERT"=>40, "REOPEN"=>40, "NOUVEAU"=>40, "ASSIGN�"=>50, "V�RIFI�"=>50, "RESOLU"=>80, "FERM�"=>90 );
$priority_map = array( "Standby"=>10, "Low"=>20, "Normal"=>30, "High"=>40 );

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "assigned_to":
	case "reporter":
	case "product_id":
		$value1 = $value;
		break;
	case "short_desc":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	case "creation_ts":
		$value1 = strtotime( $value );
		break;

	case "bug_severity":
		if( isset($bug_severity_map[$value]) ) {
			$value1 = $bug_severity_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "bug_status":
		if( isset($bug_status_map[$value]) ) {
			$value1 = $bug_status_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "priority":
		if( isset($priority_map[$value]) ) {
			$value1 = $priority_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table() //BUGS HISTORY
{

/*
BUGS HISTORY
#####
mysql> describe bugs_activity;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| bug_id    | mediumint(9) | NO   | MUL | NULL    |       |
| attach_id | mediumint(9) | YES  |     | NULL    |       |
| who       | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when  | datetime     | NO   | MUL | NULL    |       |
| fieldid   | mediumint(9) | NO   | MUL | NULL    |       |
| added     | tinytext     | YES  |     | NULL    |       |
| removed   | tinytext     | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

#####

mysql> describe mantis_bug_history_table;
+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| user_id       | int(10) unsigned | NO   | MUL | 0                   |                |
| bug_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| date_modified | int              | NO   |     | NULL                |                |
| field_name    | varchar(32)      | NO   |     | NULL                |                |
| old_value     | varchar(128)     | NO   |     | NULL                |                |
| new_value     | varchar(128)     | NO   |     | NULL                |                |
| type          | smallint(6)      | NO   |     | 0                   |                |
+---------------+------------------+------+-----+---------------------+----------------+
8 rows in set (0.00 sec)

*/

$sql_table_source= "bugs_activity";
$sql_fields_source = array( "bug_id","who","bug_when","fieldid","added","removed" );

$sql_table= "mantis_bug_history_table";
$callback_function = "bz2m_migration_bugs_activity2mantis_bug_history_table_callback";
$sql_fields = array("bug_id","user_id","date_modified","field_name","old_value","new_value");

// ----

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "who":
		$value1 = $value;
		break;	    
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "fieldid":
	case "added":
	case "removed":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table() //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

#####

mysql> describe mantis_bugnote_table;
+-----------------+------------------+------+-----+---------------------+----------------+
| Field           | Type             | Null | Key | Default             | Extra          |
+-----------------+------------------+------+-----+---------------------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| bug_id          | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id     | int(10) unsigned | NO   |     | 0                   |                |
| bugnote_text_id | int(10) unsigned | NO   |     | 0                   |                |
| view_state      | smallint(6)      | NO   |     | 10                  |                |
| date_submitted  | int              | NO   |     | NULL                |                |
| last_modified   | int              | NO   | MUL | NULL                |                |
| note_type       | int(11)          | YES  |     | 0                   |                |
| note_attr       | varchar(250)     | YES  |     | NULL                |                |
+-----------------+------------------+------+-----+---------------------+----------------+
9 rows in set (0.01 sec)

mysql>

#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array( "bug_id","who","bug_when","bug_when","work_time", );

$sql_table= "mantis_bugnote_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_table_callback";
$sql_fields = array("bug_id","reporter_id","date_submitted","last_modified","bugnote_text_id",);

// ---------

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "who":
		$value1 = $value;
		break;
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "work_time":
		$value1 = $row_count;
		break;
	case "thetext":
		$value = str_replace("\"","'",$value );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table() //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)


mysql> describe mantis_bugnote_text_table;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| note  | text             | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array( "thetext" );

$sql_table= "mantis_bugnote_text_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_text_table_callback";
$sql_fields = array("note");

// ---------

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "thetext":
		$value = str_replace("\"","'",$value );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table() //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| keywords            | mediumtext   | NO   |     | NULL    |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| alias               | varchar(20)  | YES  | UNI | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_bug_text_table;
+------------------------+------------------+------+-----+---------+----------------+
| Field                  | Type             | Null | Key | Default | Extra          |
+------------------------+------------------+------+-----+---------+----------------+
| id                     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| description            | text             | NO   |     | NULL    |                |
| steps_to_reproduce     | text             | NO   |     | NULL    |                |
| additional_information | text             | NO   |     | NULL    |                |
+------------------------+------------------+------+-----+---------+----------------+
mysql>

#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","short_desc","resolution","alias");

$sql_table= "mantis_bug_text_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_text_table_callback";
$sql_fields = array("id","description","additional_information","steps_to_reproduce");

// ---

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "short_desc":
	case "resolution":
		$value = str_replace("\"","'",$value );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	case "alias":
	    $value1 = "\"\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

{ // BUGZILLA - MAIN

bz2m_connect($link);

//echo "/* NB : FIRST CREATE ALL YOUR USER ACCOUNT IN MANTIS WITH THE CORRESPONDING ID FROM BUGZILLA */";
//echo "<br>";

bz2m_migration_profiles2mantis_user_table();

bz2m_migration_products2mantis_project_table();

bz2m_migration_bugs2mantis_bug_table();
bz2m_migration_bugs2mantis_bug_text_table();

bz2m_migration_bugs_activity2mantis_bug_history_table();

bz2m_migration_longdescs2mantis_bugnote_table();
bz2m_migration_longdescs2mantis_bugnote_text_table();

mysql_close( $link);

}


?>

bugzilla2mantisV2-jyb.php (39,938 bytes)   
bugzilla2mantisV3.php (40,187 bytes)   
<?php
/*
// ------------------------------------------------------------------------------------------
// LICENSE
// ------------------------------------------------------------------------------------------

        # bugzilla2mantis.php - Development Status "Alpha" 
        # Convert bugzilla 2.20 database to mantis 1.0.5 sql data inserts
        #
        # Copyright (C) 2006  Agusti Fontquerni afontquerni -at- gmail -dot- com
        # This program is distributed under the terms and conditions 
        # of the GNU GENERAL PUBLIC LICENSE Version 2 (http://www.gnu.org/copyleft/gpl.html)

// ------------------------------------------------------------------------------------------
// README
// ------------------------------------------------------------------------------------------
On Set-2006 I was looking for Bugzilla to Mantis (Perl) by Cris Daniluk (http://www.cadencetools.org/projects/bugzilla2mantis/)     

But Nobody didn't find any thing on the web, all web page links are broken or deleted (http://freshmeat.net/redir/bugzilla2mantis/52873/url_tgz/bugzilla2mantis-0.2.tar.gz) ((http://forums.mantisbugtracker.com/viewtopic.php?p=5745&sid=1a679a9fa211306bfd51eb33f7da2579))    

At last, I decided to start my first php script to convert bugs data from bugzilla to mantis (bugzilla2mantis.php).  It is alfa script but its did basic and functional great job.  
I give mantis2bugzilla.php to mantis community to improve bugzilla migration feature

// ------------------------------------------------------------------------------------------
// INSTALL
// ------------------------------------------------------------------------------------------
1) Backup all databases
2) Copy this file to bugzilla web site ( example: /var/www/bugzilla )
3) Modify database parameters "server", "user", "password", "database" ( 'localhost','root','', 'bugzilla' ) in "bz2m_connect()" function 
4) Open "bugzilla2mantis.php" with web browser ( example: localhost:/bugzilla/bugzilla2mantis.php )
5) Check output web page
6) Select all (Ctrl+A). Copy from web browser to test editor. Save to file ( example: mantis.sql )
7) Install mantis website ( http://www.mantisbugtracker.com/ )
8) Modify mantis database from bugzilla data "bugzilla2mantis.php" ( example: mysql -u root bugtracker < mantis.sql )
9) Now, report bugs with mantis ... 

// ------------------------------------------------------------------------------------------
// CHANGELOG
// ------------------------------------------------------------------------------------------
2006.09.24 - Clean code and Documentation (afontquerni)
2006.09.16 - Resolve some major bugs (afontquerni)
2006.09.15 - Add bugnote_table and bugnote_text_table  (afontquerni)
2006.09.14 - Initial release (afontquerni)

// ------------------------------------------------------------------------------------------
// DESCRIPTION STRUCTURE - DOCUMENTATION
// ------------------------------------------------------------------------------------------

bz2m_connect($link); // !!! MODIFY: "server", "user", "password", "database" ( 'localhost','bugzilla3','pasteyourpass_a little_lower', 'bugzilla3' ) !!

echo "DELETE FROM mantis_user_table;";

bz2m_migration_profiles2mantis_user_table();  //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_products2mantis_project_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_bugs_activity2mantis_bug_history_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

mysql_close( $link);

*/

// ------------------------------------------------------------------------------------------
//     PHP CODE
// ------------------------------------------------------------------------------------------

function bz2m_connect( &$link ) { // BUGZILLA - connect to database
	$link = mysql_connect('localhost', 'bugzilla3', 'pasteyourpasshere');
	if (!$link)
	{
	  die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("bugzilla3",  $link);
} 

// ------------------------------------------------------------------------------------------

function bz2m_select( $sql_fields, $sql_table, &$data, &$total ) { // BUGZILLA -  read bugs

	$sql="SELECT ";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql." FROM "."`".$sql_table."`";
//	$sql = $sql." FROM "."`".$sql_table."` WHERE `bug_id`<3";
//	$sql = $sql." FROM "."`".$sql_table."`  limit 0 ,3;";

	//echo "<br />"; echo $sql;  echo "<br />";
	

	$result = mysql_query( $sql );

	$columns = array();
	$data = array();

	// Table(Rows)
	while($row = mysql_fetch_array($result))
	{
		foreach ($sql_fields as $key1=> $value1 ) {
			$columns[]= $row[$value1];
			//echo "$value1 $row[$value1] <br />";
		}
		$data[]= $columns;
		unset($columns);
	}

	$total = mysql_num_rows($result);
	mysql_free_result($result);

}

// ------------------------------------------------------------------------------------------

function bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, &$data, $callback_function ) { // MANTIS -  write bugs

	$sql="INSERT INTO `$sql_table` (";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql.") VALUES( ";

	$sql_header = $sql;

	foreach ($data as $key0 => $value0 ) {
		unset($sql);
		$sql = $sql_header;
		$row_count = $key0 + 1;

		$flag_first = true;

		foreach ($value0 as $key1 => $value1 ) {
			$sql.= call_user_func( $callback_function, $row_count, $flag_first, $sql_fields_source[$key1], $value1);
			if($flag_first )
				$flag_first = false;
			//echo "<br />"; echo "$key1 $value1"; echo "<br />";

		}
		$sql.= " );";
		echo $sql; echo "<br />";

		
	}
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table() //USERS
{
/*
USERS
#####

mysql> describe profiles;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| userid         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| login_name     | varchar(255) | NO   | UNI | NULL    |                |
| cryptpassword  | varchar(128) | YES  |     | NULL    |                |
| realname       | varchar(255) | YES  |     | NULL    |                |
| disabledtext   | mediumtext   | NO   |     | NULL    |                |
| mybugslink     | tinyint(4)   | NO   |     | 1       |                |
| refreshed_when | datetime     | NO   |     | NULL    |                |
| extern_id      | varchar(64)  | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

#####

mysql> describe mantis_user_table;
+-----------------------------+------------------+------+-----+---------------------+----------------+
| Field                       | Type             | Null | Key | Default             | Extra          |
+-----------------------------+------------------+------+-----+---------------------+----------------+
| id                          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| username                    | varchar(32)      | NO   | UNI | NULL                |                |
| realname                    | varchar(64)      | NO   |     | NULL                |                |
| email                       | varchar(64)      | NO   |     | NULL                |                |
| password                    | varchar(32)      | NO   |     | NULL                |                |
| date_created                | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| last_visit                  | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| enabled                     | tinyint(4)       | NO   | MUL | 1                   |                |
| protected                   | tinyint(4)       | NO   |     | 0                   |                |
| access_level                | smallint(6)      | NO   | MUL | 10                  |                |
| login_count                 | int(11)          | NO   |     | 0                   |                |
| lost_password_request_count | smallint(6)      | NO   |     | 0                   |                |
| failed_login_count          | smallint(6)      | NO   |     | 0                   |                |
| cookie_string               | varchar(64)      | NO   | UNI | NULL                |                |
+-----------------------------+------------------+------+-----+---------------------+----------------+
14 rows in set (0.00 sec)

*/


$sql_table_source= "profiles";
$sql_fields_source=array( "userid","login_name","cryptpassword","realname","login_name", "refreshed_when", "cryptpassword" );

$sql_table= "mantis_user_table";
$sql_fields = array("id","username","password","realname","email","date_created", "cookie_string");
$callback_function = "bz2m_migration_profiles2mantis_user_table_callback";

// ------

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

	$sql = "";
	//echo "$key $value <br />";

switch ($key)
{
	case "userid":
		$value1 = $value;
		break;
	case "login_name":
	case "cryptpassword":
	case "realname":
	case "refreshed_when":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table() //PROJECTS
{

/*
PROJECTS
#####
mysql> describe products;
+-------------------+-------------+------+-----+---------+----------------+
| Field             | Type        | Null | Key | Default | Extra          |
+-------------------+-------------+------+-----+---------+----------------+
| id                | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name              | varchar(64) | NO   | UNI | NULL    |                |
| classification_id | smallint(6) | NO   |     | 1       |                |
| description       | mediumtext  | YES  |     | NULL    |                |
| milestoneurl      | tinytext    | NO   |     | NULL    |                |
| disallownew       | tinyint(4)  | NO   |     | NULL    |                |
| votesperuser      | smallint(6) | NO   |     | NULL    |                |
| maxvotesperbug    | smallint(6) | NO   |     | 10000   |                |
| votestoconfirm    | smallint(6) | NO   |     | NULL    |                |
| defaultmilestone  | varchar(20) | NO   |     | ---     |                |
+-------------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> 

#####

mysql> describe mantis_project_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(128)     | NO   | UNI | NULL    |                |
| status      | smallint(6)      | NO   |     | 10      |                |
| enabled     | tinyint(4)       | NO   |     | 1       |                |
| view_state  | smallint(6)      | NO   | MUL | 10      |                |
| access_min  | smallint(6)      | NO   |     | 10      |                |
| file_path   | varchar(250)     | NO   |     | NULL    |                |
| description | text             | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
8 rows in set (0.01 sec)

*/

$sql_table_source= "products";
$sql_fields_source= array( "id","name","description","votesperuser","votestoconfirm" );

$sql_table= "mantis_project_table";
$callback_function = "bz2m_migration_products2mantis_project_table_callback";
$sql_fields = array("id","name","description", "status", "enabled");


// ----

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "id":
		$value1 = $value;
		break;
	case "name":
	case "description":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	case "votesperuser":
		$value1 = 10;
		break;
	case "votestoconfirm":
		$value1 = 1;
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table() //BUGS
{

/*
BUGS
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| keywords            | mediumtext   | NO   |     | NULL    |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| alias               | varchar(20)  | YES  | UNI | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


#####
mysql> describe mantis_bug_table;
+-------------------+------------------+------+-----+---------------------+----------------+
| Field             | Type             | Null | Key | Default             | Extra          |
+-------------------+------------------+------+-----+---------------------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| project_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id       | int(10) unsigned | NO   |     | 0                   |                |
| handler_id        | int(10) unsigned | NO   |     | 0                   |                |
| duplicate_id      | int(10) unsigned | NO   |     | 0                   |                |
| priority          | smallint(6)      | NO   |     | 30                  |                |
| severity          | smallint(6)      | NO   |     | 50                  |                |
| reproducibility   | smallint(6)      | NO   |     | 10                  |                |
| status            | smallint(6)      | NO   | MUL | 10                  |                |
| resolution        | smallint(6)      | NO   |     | 10                  |                |
| projection        | smallint(6)      | NO   |     | 10                  |                |
| category          | varchar(64)      | NO   |     | NULL                |                |
| date_submitted    | int              | NO   |     | NULL                |                |
| last_updated      | int              | NO   |     | NULL                |                |
| eta               | smallint(6)      | NO   |     | 10                  |                |
| bug_text_id       | int(10) unsigned | NO   |     | 0                   |                |
| os                | varchar(32)      | NO   |     | NULL                |                |
| os_build          | varchar(32)      | NO   |     | NULL                |                |
| platform          | varchar(32)      | NO   |     | NULL                |                |
| version           | varchar(64)      | NO   |     | NULL                |                |
| fixed_in_version  | varchar(64)      | NO   | MUL | NULL                |                |
| build             | varchar(32)      | NO   |     | NULL                |                |
| profile_id        | int(10) unsigned | NO   |     | 0                   |                |
| view_state        | smallint(6)      | NO   |     | 10                  |                |
| summary           | varchar(128)     | NO   |     | NULL                |                |
| sponsorship_total | int(11)          | NO   | MUL | 0                   |                |
| sticky            | tinyint(4)       | NO   |     | 0                   |                |
+-------------------+------------------+------+-----+---------------------+----------------+
27 rows in set (0.00 sec)

mysql>
*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","assigned_to","bug_severity","bug_status","product_id","creation_ts","creation_ts","priority","reporter", "short_desc", "bug_id" );

$sql_table= "mantis_bug_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_table_callback";
$sql_fields = array("id","handler_id","severity","status","project_id","date_submitted","last_updated","priority","reporter_id", "summary", "bug_text_id" );

// ---

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

$bug_severity_map = array( "enhancement"=>10, "trivial"=>20, "Standby"=>20, "minor"=>40, "Low"=>40, "normal"=>50, "Normal"=>50, "major"=>60, "High"=>60, "critical"=>70, "Urgent"=>70, "blocker"=>80 );
$bug_status_map = array( "UNCONFIRMED"=>10, "NEW"=>40, "ASSIGNED"=>50, "RESOLVED"=>80, "CLOSED"=>90, "NON CONFIRM�"=>10, "R�OUVERT"=>40, "REOPEN"=>40, "NOUVEAU"=>40, "ASSIGN�"=>50, "V�RIFI�"=>50, "RESOLU"=>80, "FERM�"=>90 );
$priority_map = array( "Standby"=>10, "Low"=>20, "Normal"=>30, "High"=>40 );

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "assigned_to":
	case "reporter":
	case "product_id":
		$value1 = $value;
		break;
	case "short_desc":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	case "creation_ts":
		$value1 = strtotime( $value );
		break;

	case "bug_severity":
		if( isset($bug_severity_map[$value]) ) {
			$value1 = $bug_severity_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "bug_status":
		if( isset($bug_status_map[$value]) ) {
			$value1 = $bug_status_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "priority":
		if( isset($priority_map[$value]) ) {
			$value1 = $priority_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table() //BUGS HISTORY
{

/*
BUGS HISTORY
#####
mysql> describe bugs_activity;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| bug_id    | mediumint(9) | NO   | MUL | NULL    |       |
| attach_id | mediumint(9) | YES  |     | NULL    |       |
| who       | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when  | datetime     | NO   | MUL | NULL    |       |
| fieldid   | mediumint(9) | NO   | MUL | NULL    |       |
| added     | tinytext     | YES  |     | NULL    |       |
| removed   | tinytext     | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

#####

mysql> describe mantis_bug_history_table;
+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| user_id       | int(10) unsigned | NO   | MUL | 0                   |                |
| bug_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| date_modified | int              | NO   |     | NULL                |                |
| field_name    | varchar(32)      | NO   |     | NULL                |                |
| old_value     | varchar(128)     | NO   |     | NULL                |                |
| new_value     | varchar(128)     | NO   |     | NULL                |                |
| type          | smallint(6)      | NO   |     | 0                   |                |
+---------------+------------------+------+-----+---------------------+----------------+
8 rows in set (0.00 sec)

*/

$sql_table_source= "bugs_activity";
$sql_fields_source = array( "bug_id","who","bug_when","fieldid","added","removed" );

$sql_table= "mantis_bug_history_table";
$callback_function = "bz2m_migration_bugs_activity2mantis_bug_history_table_callback";
$sql_fields = array("bug_id","user_id","date_modified","field_name","old_value","new_value");

// ----

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "who":
		$value1 = $value;
		break;	    
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "fieldid":
	case "added":
	case "removed":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table() //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

#####

mysql> describe mantis_bugnote_table;
+-----------------+------------------+------+-----+---------------------+----------------+
| Field           | Type             | Null | Key | Default             | Extra          |
+-----------------+------------------+------+-----+---------------------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL                |                |
| bug_id          | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id     | int(10) unsigned | NO   |     | 0                   |                |
| bugnote_text_id | int(10) unsigned | NO   |     | 0                   |                |
| view_state      | smallint(6)      | NO   |     | 10                  |                |
| date_submitted  | int              | NO   |     | NULL                |                |
| last_modified   | int              | NO   | MUL | NULL                |                |
| note_type       | int(11)          | YES  |     | 0                   |                |
| note_attr       | varchar(250)     | YES  |     | NULL                |                |
+-----------------+------------------+------+-----+---------------------+----------------+
9 rows in set (0.01 sec)

mysql>

#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "bug_id","who","bug_when","bug_when","work_time", );

$sql_table= "mantis_bugnote_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_table_callback";
$sql_fields = array("id", "bug_id","reporter_id","date_submitted","last_modified","bugnote_text_id",);

// ---------

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "who":
	case "comment_id":
		$value1 = $value;
		break;
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "work_time":
		$value1 = $row_count;
		break;
	case "thetext":
		$value = str_replace("\"","'",$value );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table() //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)


mysql> describe mantis_bugnote_text_table;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    |                |
| note  | text             | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "thetext" );

$sql_table= "mantis_bugnote_text_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_text_table_callback";
$sql_fields = array("id", "note");

// ---------

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "comment_id":
		$value1 = $value;
		break;
	case "thetext":
		$value = str_replace("\"","'",$value );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table() //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| keywords            | mediumtext   | NO   |     | NULL    |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| alias               | varchar(20)  | YES  | UNI | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_bug_text_table;
+------------------------+------------------+------+-----+---------+----------------+
| Field                  | Type             | Null | Key | Default | Extra          |
+------------------------+------------------+------+-----+---------+----------------+
| id                     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| description            | text             | NO   |     | NULL    |                |
| steps_to_reproduce     | text             | NO   |     | NULL    |                |
| additional_information | text             | NO   |     | NULL    |                |
+------------------------+------------------+------+-----+---------+----------------+
mysql>

#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","short_desc","resolution","alias");

$sql_table= "mantis_bug_text_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_text_table_callback";
$sql_fields = array("id","description","additional_information","steps_to_reproduce");

// ---

$data = array();
$total = 0;

bz2m_select( $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "short_desc":
	case "resolution":
		$value = str_replace("\"","'",$value );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	case "alias":
	    $value1 = "\"\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

{ // BUGZILLA - MAIN

bz2m_connect($link);

//echo "/* NB : FIRST CREATE ALL YOUR USER ACCOUNT IN MANTIS WITH THE CORRESPONDING ID FROM BUGZILLA */";
//echo "<br>";

//bz2m_migration_profiles2mantis_user_table();

//bz2m_migration_products2mantis_project_table();

//bz2m_migration_bugs2mantis_bug_table();
//bz2m_migration_bugs2mantis_bug_text_table();

//bz2m_migration_bugs_activity2mantis_bug_history_table();

bz2m_migration_longdescs2mantis_bugnote_table();
bz2m_migration_longdescs2mantis_bugnote_text_table();

mysql_close( $link);

}


?>

bugzilla2mantisV3.php (40,187 bytes)   
bugzilla2mantisV5.php (52,463 bytes)   
<?php
/*
// ------------------------------------------------------------------------------------------
// LICENSE
// ------------------------------------------------------------------------------------------

        # bugzilla2mantis.php - Development Status "Alpha" 
        # Convert bugzilla 5.0.3 database to mantis 1.3.3 sql data inserts
        #
        # Copyright (C) 2006  Agusti Fontquerni afontquerni -at- gmail -dot- com
        # This program is distributed under the terms and conditions 
        # of the GNU GENERAL PUBLIC LICENSE Version 2 (http://www.gnu.org/copyleft/gpl.html)

// ------------------------------------------------------------------------------------------
// README
// ------------------------------------------------------------------------------------------
On Set-2006 I was looking for Bugzilla to Mantis (Perl) by Cris Daniluk (http://www.cadencetools.org/projects/bugzilla2mantis/)     

But Nobody didn't find any thing on the web, all web page links are broken or deleted (http://freshmeat.net/redir/bugzilla2mantis/52873/url_tgz/bugzilla2mantis-0.2.tar.gz) ((http://forums.mantisbugtracker.com/viewtopic.php?p=5745&sid=1a679a9fa211306bfd51eb33f7da2579))    

At last, I decided to start my first php script to convert bugs data from bugzilla to mantis (bugzilla2mantis.php).  It is alfa script but its did basic and functional great job.  
I give mantis2bugzilla.php to mantis community to improve bugzilla migration feature

// ------------------------------------------------------------------------------------------
// INSTALL
// ------------------------------------------------------------------------------------------
1) Backup all databases
2) Copy this file to bugzilla web site ( example: /var/www/bugzilla )
3) Modify database parameters "server", "user", "password", "database" ( 'localhost','root','', 'bugzilla' ) in "bz2m_connect()" function 
4) Open "bugzilla2mantis.php" with web browser ( example: localhost:/bugzilla/bugzilla2mantis.php )
5) Check output web page
6) Select all (Ctrl+A). Copy from web browser to test editor. Save to file ( example: mantis.sql )
7) Install mantis website ( http://www.mantisbugtracker.com/ )
8) Modify mantis database from bugzilla data "bugzilla2mantis.php" ( example: mysql -u root bugtracker < mantis.sql )
9) Now, report bugs with mantis ... 

// ------------------------------------------------------------------------------------------
// CHANGELOG
// ------------------------------------------------------------------------------------------
2016.11.21 - Adaptation for PHP 5.6.16, bugzilla version 5.0.3 and Mantis BT version 1.3.3 - french version (groques)
2006.09.24 - Clean code and Documentation (afontquerni)
2006.09.16 - Resolve some major bugs (afontquerni)
2006.09.15 - Add bugnote_table and bugnote_text_table  (afontquerni)
2006.09.14 - Initial release (afontquerni)

// ------------------------------------------------------------------------------------------
// DESCRIPTION STRUCTURE - DOCUMENTATION
// ------------------------------------------------------------------------------------------

bz2m_connect($link); // !!! MODIFY: "server", "user", "password", "database" ( 'localhost','bugzilla3','pasteyourpass_a little_lower', 'bugzilla3' ) !!

echo "DELETE FROM mantis_user_table;";

bz2m_migration_profiles2mantis_user_table();  //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_products2mantis_project_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_bugs_activity2mantis_bug_history_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_mantis_category_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_category_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_mantis_customclient_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_customclient_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

mysql_close( $link);

*/

// ------------------------------------------------------------------------------------------
//     PHP CODE
// ------------------------------------------------------------------------------------------

function bz2m_connect( &$mysqli ) { // BUGZILLA - connect to database
/*********
// PHP version before 5.6.16
	$link = mysql_connect('localhost', 'myuser', 'mypassword');
	if (!$link)
	{
	  die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("bugs",  $link);
*********/
	$myport = 3306;
	$mysqli = new mysqli('localhost', 'myuser', 'mypassword', 'bugs', $myport);
	if ($mysqli->connect_error) {
		die('Erreur de connexion (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
	}

	echo 'Success... ' . $mysqli->host_info . "\n";
} 

// ------------------------------------------------------------------------------------------

function bz2m_select($con, $sql_fields, $sql_table, &$data, &$total ) { // BUGZILLA -  read bugs

	$sql="SELECT ";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql." FROM "."`".$sql_table."`";
//	$sql = $sql." FROM "."`".$sql_table."` WHERE `bug_id`<3";
//	$sql = $sql." FROM "."`".$sql_table."`  limit 0 ,3;";

	echo "<br />"; echo $sql;  echo "<br />";
	

	$result = mysqli_query($con, $sql );

	$columns = array();
	$data = array();

	// Table(Rows)
	while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
	{
		foreach ($sql_fields as $key1=> $value1 ) {
			$columns[]= $row[$value1];
			//echo "$value1 $row[$value1] <br />";
		}
		$data[]= $columns;
		unset($columns);
	}

	$total = mysqli_num_rows($result);
	mysqli_free_result($result);
}

// ------------------------------------------------------------------------------------------

function bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, &$data, $callback_function ) { // MANTIS -  write bugs

	$sql="INSERT INTO `$sql_table` (";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql.") VALUES( ";

	$sql_header = $sql;

	foreach ($data as $key0 => $value0 ) {
		unset($sql);
		$sql = $sql_header;
		$row_count = $key0 + 1;

		$flag_first = true;

		foreach ($value0 as $key1 => $value1 ) {
			$sql.= call_user_func( $callback_function, $row_count, $flag_first, $sql_fields_source[$key1], $value1);
			if($flag_first )
				$flag_first = false;
			//echo "<br />"; echo "$key1 $value1"; echo "<br />";

		}
		$sql.= " );";
		echo $sql; echo "<br />";

		
	}
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table($mysqli) //USERS
{
/*
USERS
#####

mysql> describe profiles;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| userid         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| login_name     | varchar(255) | NO   | UNI | NULL    |                |
| cryptpassword  | varchar(128) | YES  |     | NULL    |                |
| realname       | varchar(255) | YES  |     | NULL    |                |
| disabledtext   | mediumtext   | NO   |     | NULL    |                |
| disable_mail   | tinyint(4)   | NO   |     | 0       |                |
| mybugslink     | tinyint(4)   | NO   |     | 1       |                |
| extern_id      | varchar(64)  | YES  |     | NULL    |                |
| is_enabled 	 | tinyint(4)   | NO   |     | 1 	   |                |
| last_seen_date | datetime     | NO   |     | NULL    |                |
| email          | varchar(255) | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

#####

mysql> describe mantis_user_table;
+-----------------------------+------------------+------+-----+---------------------+----------------+
| Field                       | Type             | Null | Key | Default             | Extra          |
+-----------------------------+------------------+------+-----+---------------------+----------------+
| id                          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| username                    | varchar(32)      | NO   | UNI | NULL                |                |
| realname                    | varchar(64)      | NO   |     | NULL                |                |
| email                       | varchar(64)      | NO   |     | NULL                |                |
| password                    | varchar(32)      | NO   |     | NULL                |                |
| enabled                     | tinyint(4)       | NO   | MUL | 1                   |                |
| protected                   | tinyint(4)       | NO   |     | 0                   |                |
| access_level                | smallint(6)      | NO   | MUL | 10                  |                |
| login_count                 | int(11)          | NO   |     | 0                   |                |
| lost_password_request_count | smallint(6)      | NO   |     | 0                   |                |
| failed_login_count          | smallint(6)      | NO   |     | 0                   |                |
| cookie_string               | varchar(64)      | NO   | UNI | NULL                |                |
| last_visit                  | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| date_created                | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
+-----------------------------+------------------+------+-----+---------------------+----------------+
14 rows in set (0.00 sec)

*/


$sql_table_source= "profiles";
$sql_fields_source=array( "userid","login_name","cryptpassword","realname","login_name", "last_seen_date", "cryptpassword" );

$sql_table= "mantis_user_table";
$sql_fields = array("id","username","password","realname","email","date_created", "cookie_string");
$callback_function = "bz2m_migration_profiles2mantis_user_table_callback";

// ------

$data = array();
$total = 0;

bz2m_select($mysqli, $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

	$sql = "";
	//echo "$key $value <br />";

switch ($key)
{
	case "userid":
		$value1 = $value;
		break;
	case "login_name":
	case "cryptpassword":
	case "realname":
	case "refreshed_when":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table($mysqli) //PROJECTS
{

/*
PROJECTS
#####
mysql> describe products;
+-------------------+-------------+------+-----+---------+----------------+
| Field             | Type        | Null | Key | Default | Extra          |
+-------------------+-------------+------+-----+---------+----------------+
| id                | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name              | varchar(64) | NO   | UNI | NULL    |                |
| classification_id | smallint(6) | NO   |     | 1       |                |
| description       | mediumtext  | YES  |     | NULL    |                |
| isactive 	        | tinyint(4)  | NO   |     | 1       |                |
| votesperuser      | smallint(6) | NO   |     | NULL    |                |
| maxvotesperbug    | smallint(6) | NO   |     | 10000   |                |
| votestoconfirm    | smallint(6) | NO   |     | NULL    |                |
| defaultmilestone  | varchar(20) | NO   |     | ---     |                |
| allows_unconfirmed| tinyint(4)  | NO   |     | 1       |                |
+-------------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> 

#####

mysql> describe mantis_project_table;
+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id             | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name           | varchar(128)     | NO   | UNI | NULL    |                |
| status         | smallint(6)      | NO   |     | 10      |                |
| enabled        | tinyint(4)       | NO   |     | 1       |                |
| view_state     | smallint(6)      | NO   | MUL | 10      |                |
| access_min     | smallint(6)      | NO   |     | 10      |                |
| file_path      | varchar(250)     | NO   |     | NULL    |                |
| description    | text             | NO   |     | NULL    |                |
| category_id    | int(10) unsigned | NO   |     | 1       |                |
| inherit_global | tinyint(4)       | NO   |     | 0       |                |
+----------------+------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)

*/

$sql_table_source= "products";
$sql_fields_source= array( "id","name","description","votesperuser","votestoconfirm" );

$sql_table= "mantis_project_table";
$callback_function = "bz2m_migration_products2mantis_project_table_callback";
$sql_fields = array("id","name","description", "status", "enabled");


// ----

$data = array();
$total = 0;

bz2m_select($mysqli, $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "id":
		$value1 = $value;
		break;
	case "name":
	case "description":
		$value1 = "'".str_replace("'", "''", htmlentities($value, ENT_HTML5, 'ISO-8859-15'))."'";
		break;
	case "votesperuser":
		$value1 = 10;
		break;
	case "votestoconfirm":
		$value1 = 1;
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table($con) //BUGS
{

/*
BUGS
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     |         |                |
| cf_customer         | varchar(64)  | NO   |     |         |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


#####
mysql> describe mantis_bug_table;
+-------------------+------------------+------+-----+---------------------+----------------+
| Field             | Type             | Null | Key | Default             | Extra          |
+-------------------+------------------+------+-----+---------------------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| project_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id       | int(10) unsigned | NO   |     | 0                   |                |
| handler_id        | int(10) unsigned | NO   |     | 0                   |                |
| duplicate_id      | int(10) unsigned | NO   |     | 0                   |                |
| priority          | smallint(6)      | NO   |     | 30                  |                |
| severity          | smallint(6)      | NO   |     | 50                  |                |
| reproducibility   | smallint(6)      | NO   |     | 10                  |                |
| status            | smallint(6)      | NO   | MUL | 10                  |                |
| resolution        | smallint(6)      | NO   |     | 10                  |                |
| projection        | smallint(6)      | NO   |     | 10                  |                |
| eta               | smallint(6)      | NO   |     | 10                  |                |
| bug_text_id       | int(10) unsigned | NO   |     | 0                   |                |
| os                | varchar(32)      | NO   |     | NULL                |                |
| os_build          | varchar(32)      | NO   |     | NULL                |                |
| platform          | varchar(32)      | NO   |     | NULL                |                |
| version           | varchar(64)      | NO   |     | NULL                |                |
| fixed_in_version  | varchar(64)      | NO   | MUL | NULL                |                |
| build             | varchar(32)      | NO   |     | NULL                |                |
| profile_id        | int(10) unsigned | NO   |     | 0                   |                |
| view_state        | smallint(6)      | NO   |     | 10                  |                |
| summary           | varchar(128)     | NO   |     | NULL                |                |
| sponsorship_total | int(11)          | NO   | MUL | 0                   |                |
| sticky            | tinyint(4)       | NO   |     | 0                   |                |
| category_id       | int(10) unsigned | NO   |     | 1                   |                |
| date_submitted    | int(10) unsigned | NO   |     | 1                   |                |
| due_date          | int(10) unsigned | NO   |     | 1                   |                |
| last_updated      | int(10) unsigned | NO   |     | 1                   |                |
+-------------------+------------------+------+-----+---------------------+----------------+
29 rows in set (0.00 sec)

mysql>
*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","assigned_to","bug_severity","bug_status","product_id","creation_ts","creation_ts","priority","reporter", "short_desc", "bug_id" , "component_id","resolution","version");

$sql_table= "mantis_bug_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_table_callback";
$sql_fields = array("id","handler_id","severity","status","project_id","date_submitted","last_updated","priority","reporter_id", "summary", "bug_text_id", "category_id","resolution","version" );

// ---

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";
/*
Bugzilla
Bug, Evolution, Param�trage, Documentation, Savoir

Bas, Haut, ND, Normal, TBas, THaut

Mantis
$s_severity_enum_string = '10:fonctionnalit�,20:simple,30:texte,40:cosm�tique,50:mineur,60:majeur,70:critique,80:bloquant';
$s_resolution_enum_string = '10:ouvert,20:corrig�,30:r�ouvert,40:impossible � reproduire,50:impossible � corriger,60:doublon,70:pas un bogue,80:suspendu,90:ne sera pas corrig�';
$s_status_enum_string = '10:nouveau,20:retour  d\'informations,30:accept�,40:confirm�,50:affect�,80:trait�,90:ferm�';
$s_priority_enum_string = '10:aucune,20:basse,30:normale,40:�lev�e,50:urgente,60:imm�diate';
*/
$bug_severity_map = array( "Evolution"=>10, "Param�trage"=>20, "Documentation"=>30, "Savoir"=>40, "Bug"=>60 );
$bug_status_map = array( "UNCONFIRMED"=>10, "NEW"=>40, "ASSIGNED"=>50, "VERIFIED"=>50, "RESOLVED"=>80, "CLOSED"=>90, "NON CONFIRM�"=>10, "R�OUVERT"=>40, "REOPEN"=>40, "NOUVEAU"=>40, "ASSIGN�"=>50, "V�RIFI�"=>50, "RESOLU"=>80, "FERM�"=>90 );
$bug_resolution_map = array( ""=>10, "DUPLICATE"=>60, "FIXED"=>20, "INVALID"=>70, "TESTNEEDED"=>30, "WONTFIX"=>90, "WORKSFORME"=>40 );
$bug_priority_map = array( "ND"=>10, "TBas"=>20, "Bas"=>20, "Normal"=>30, "Haut"=>40, "THaut"=>50 );

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "assigned_to":
	case "reporter":
	case "product_id":
	case "component_id":
		$value1 = $value;
		break;
	case "short_desc":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'ISO-8859-15') );
		$value1 = "\"".$value."\"";
		break;
	case "version":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	case "creation_ts":
		$value1 = strtotime( $value );
		break;

	case "bug_severity":
		if( isset($bug_severity_map[$value]) ) {
			$value1 = $bug_severity_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "bug_status":
		if( isset($bug_status_map[$value]) ) {
			$value1 = $bug_status_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "priority":
		if( isset($bug_priority_map[$value]) ) {
			$value1 = $bug_priority_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;

	case "resolution":
		if( isset($bug_resolution_map[$value]) ) {
			$value1 = $bug_resolution_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table($con) //BUGS HISTORY
{

/*
BUGS HISTORY
#####
mysql> describe bugs_activity;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| bug_id    | mediumint(9) | NO   | MUL | NULL    |       |
| attach_id | mediumint(9) | YES  |     | NULL    |       |
| who       | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when  | datetime     | NO   | MUL | NULL    |       |
| fieldid   | mediumint(9) | NO   | MUL | NULL    |       |
| added     | tinytext     | YES  |     | NULL    |       |
| removed   | tinytext     | YES  |     | NULL    |       |
| comment_id| int(11)      | YES  |     | NULL    |       |
| id        | int(11)      | NO   |     | AUTO    |       |
+-----------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

#####

mysql> describe mantis_bug_history_table;
+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| user_id       | int(10) unsigned | NO   | MUL | 0                   |                |
| bug_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| field_name    | varchar(32)      | NO   |     | NULL                |                |
| old_value     | varchar(128)     | NO   |     | NULL                |                |
| new_value     | varchar(128)     | NO   |     | NULL                |                |
| type          | smallint(6)      | NO   |     | 0                   |                |
| date_modified | int              | NO   |     | NULL                |                |
+---------------+------------------+------+-----+---------------------+----------------+
8 rows in set (0.00 sec)

*/

$sql_table_source= "bugs_activity";
$sql_fields_source = array( "bug_id","who","bug_when","fieldid","added","removed" );

$sql_table= "mantis_bug_history_table";
$callback_function = "bz2m_migration_bugs_activity2mantis_bug_history_table_callback";
$sql_fields = array("bug_id","user_id","date_modified","field_name","old_value","new_value");

// ----

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "who":
		$value1 = $value;
		break;	    
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "fieldid":
	case "added":
	case "removed":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
| type            | smallint(6)  | NO   |     | 0       |       |
| extra_data      | varchar(255) | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

#####

mysql> describe mantis_bugnote_table;
+-----------------+------------------+------+-----+---------------------+----------------+
| Field           | Type             | Null | Key | Default             | Extra          |
+-----------------+------------------+------+-----+---------------------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL                |                |
| bug_id          | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id     | int(10) unsigned | NO   |     | 0                   |                |
| bugnote_text_id | int(10) unsigned | NO   |     | 0                   |                |
| view_state      | smallint(6)      | NO   |     | 10                  |                |
| note_type       | int(11)          | YES  |     | 0                   |                |
| note_attr       | varchar(250)     | YES  |     | NULL                |                |
| time_tracking   | int(10) unsigned | NO   |     | 0                   |                |
| last_modified   | int(10) unsigned | NO   | MUL | 1                   |                |
| date_submitted  | int(10) unsigned | NO   |     | 1                   |                |
+-----------------+------------------+------+-----+---------------------+----------------+
10 rows in set (0.01 sec)

mysql>

#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "bug_id","who","bug_when","bug_when","work_time", );

$sql_table= "mantis_bugnote_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_table_callback";
$sql_fields = array("id", "bug_id","reporter_id","date_submitted","last_modified","bugnote_text_id",);

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "who":
	case "comment_id":
		$value1 = $value;
		break;
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "work_time":
		$value1 = $row_count;
		break;
	case "thetext":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'ISO-8859-15') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
| type            | smallint(6)  | NO   |     | 0       |       |
| extra_data      | varchar(255) | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)


mysql> describe mantis_bugnote_text_table;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    |                |
| note  | text             | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "thetext" );

$sql_table= "mantis_bugnote_text_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_text_table_callback";
$sql_fields = array("id", "note");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
	case "comment_id":
		$value1 = $value;
		break;
	case "thetext":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'ISO-8859-15') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     | NULL    |                |
| cf_customer         | varchar(64)  | NO   |     | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_bug_text_table;
+------------------------+------------------+------+-----+---------+----------------+
| Field                  | Type             | Null | Key | Default | Extra          |
+------------------------+------------------+------+-----+---------+----------------+
| id                     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| description            | text             | NO   |     | NULL    |                |
| steps_to_reproduce     | text             | NO   |     | NULL    |                |
| additional_information | text             | NO   |     | NULL    |                |
+------------------------+------------------+------+-----+---------+----------------+
mysql>

#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","short_desc","resolution","votes");

$sql_table= "mantis_bug_text_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_text_table_callback";
$sql_fields = array("id","description","additional_information","steps_to_reproduce");

// ---

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "short_desc":
	case "resolution":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'ISO-8859-15') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	case "votes":
	    $value1 = "\"\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_category_table($con) //BUGS CATEGORY
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe components;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| id              | mediumint(9) | NO   | PRI | NULL    | AUTO  |
| name            | varchar(64)  | NO   | MUL | NULL    |       |
| product_id      | smallint(6)  | NO   | MUL | NULL    |       |
| initialowner    | mediumint(9) | NO   | MUL | NULL    |       |
| initialqacontact| mediumint(9) | YES  | MUL | NULL    |       |
| description     | mediumtext   | NO   |     | NULL    |       |
| isactive        | tinyint(4)   | NO   |     | 1       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)


mysql> describe mantis_category_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    |  AUTO          |
| project_id  | int(10) unsigned | NO   | MUL | 0       |                |
| user_id     | int(10) unsigned | NO   |     | 0       |                |
| name        | varchar(128)     | NO   |     |         |                |
| status      | int(10) unsigned | NO   |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)


#####

*/

$sql_table_source= "components";
$sql_fields_source = array("id", "name" , "product_id", "isactive");

$sql_table= "mantis_category_table";
$callback_function = "bz2m_migration_mantis_category_table_callback";
$sql_fields = array("id", "name", "project_id", "status");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_category_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write category

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "id":
	case "product_id":
	case "isactive":
		$value1 = $value;
		break;
	case "name":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'ISO-8859-15') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","<br />",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_customclient_table($con) //BUGS CUSTOMCLIENT
{

/*
BUGS DESCRIPTION
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     |         |                |
| cf_customer         | varchar(64)  | NO   |     |         |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_custom_field_string_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| field_id  | int(11) unsigned   | NO   | PRI | 0       |  AUTO          |
| bug_id  	| int(11) unsigned   | NO   | MUL | 0       |                |
| value     | varchar(255)       | NO   |     |         |                |
| text      | int(10) unsigned   | YES  |     |         |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array("cf_client", "bug_id", "cf_customer");

$sql_table= "mantis_custom_field_string_table";
$callback_function = "bz2m_migration_mantis_customclient_table_callback";
$sql_fields = array("field_id", "bug_id", "value");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo "<br />"; echo "total:$total"; echo "<br />";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_customclient_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write customer

$sql = "";

	//echo "$key $value <br />";

switch ($key)
{
	case "cf_client":
		$value1 = 1;
		break;
	case "bug_id":
		$value1 = $value;
		break;
	case "cf_customer":
		if ($value == '---')
			$value1 = "\"\"";
		else
			$value1 = "\"|".htmlentities($value, ENT_HTML5, 'ISO-8859-15')."|\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo "<br />"; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

{ // BUGZILLA - MAIN

//echo "/* NB : FIRST CREATE ALL YOUR USER ACCOUNT IN MANTIS WITH THE CORRESPONDING ID FROM BUGZILLA */";
//echo "<br>";

echo "<html lang='fr' xmlns='http://www.w3.org/1999/xhtml'>
	<head>
		<title>Migration bugzilla to Mantis</title>
		<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
		<meta http-equiv='Content-Language' content='fr-fr' />
    </head>
    <body>";

bz2m_connect($mysqli);
/*
bz2m_migration_profiles2mantis_user_table($mysqli);

bz2m_migration_products2mantis_project_table($mysqli);

bz2m_migration_bugs2mantis_bug_table($mysqli);
bz2m_migration_bugs2mantis_bug_text_table($mysqli);

bz2m_migration_bugs_activity2mantis_bug_history_table($mysqli);

bz2m_migration_longdescs2mantis_bugnote_table($mysqli);
bz2m_migration_longdescs2mantis_bugnote_text_table($mysqli);

bz2m_migration_mantis_category_table($mysqli);

bz2m_migration_mantis_customclient_table($mysqli);
*/
echo "</body>
        </html>";
$mysqli->close();

/*********
// PHP version before 5.6.16
bz2m_migration_profiles2mantis_user_table();

bz2m_migration_products2mantis_project_table();

bz2m_migration_bugs2mantis_bug_table();
bz2m_migration_bugs2mantis_bug_text_table();

bz2m_migration_bugs_activity2mantis_bug_history_table();

bz2m_migration_longdescs2mantis_bugnote_table();
bz2m_migration_longdescs2mantis_bugnote_text_table();

mysql_close( $link);
*********/
}


?>

bugzilla2mantisV5.php (52,463 bytes)   
bugzilla2mantis-4.php (59,497 bytes)   
<?php
header('Content-type: text/plain; charset=utf-8');

ini_set("memory_limit", "64G");
ini_set("max_execution_time", "86400");
/*
// ------------------------------------------------------------------------------------------
// LICENSE
// ------------------------------------------------------------------------------------------

        # bugzilla2mantis.php - Development Status "Alpha" 
        # Convert bugzilla 5.0.3 database to mantis 1.3.3 sql data inserts
        #
        # Copyright (C) 2006  Agusti Fontquerni afontquerni -at- gmail -dot- com
		# Copyright (C) 2017  Christoph Suess <christoph -dot- suess -at- gmail -dot- com (for the attachment migration and utf-8 support)
        # This program is distributed under the terms and conditions 
        # of the GNU GENERAL PUBLIC LICENSE Version 2 (http://www.gnu.org/copyleft/gpl.html)

// ------------------------------------------------------------------------------------------
// README
// ------------------------------------------------------------------------------------------
On Set-2006 I was looking for Bugzilla to Mantis (Perl) by Cris Daniluk (http://www.cadencetools.org/projects/bugzilla2mantis/)     

But Nobody didn't find any thing on the web, all web page links are broken or deleted (http://freshmeat.net/redir/bugzilla2mantis/52873/url_tgz/bugzilla2mantis-0.2.tar.gz) ((http://forums.mantisbugtracker.com/viewtopic.php?p=5745&sid=1a679a9fa211306bfd51eb33f7da2579))    

At last, I decided to start my first php script to convert bugs data from bugzilla to mantis (bugzilla2mantis.php).  It is alfa script but its did basic and functional great job.  
I give mantis2bugzilla.php to mantis community to improve bugzilla migration feature

// ------------------------------------------------------------------------------------------
// INSTALL
// ------------------------------------------------------------------------------------------
1) Backup all databases
2) Copy this file to bugzilla web site ( example: /var/www/bugzilla )
3) Modify database parameters "server", "user", "password", "database" ( 'localhost','root','', 'bugzilla' ) in "bz2m_connect()" function 
4) Open "bugzilla2mantis.php" with web browser ( example: localhost:/bugzilla/bugzilla2mantis.php )
5) Check output web page
6) Select all (Ctrl+A). Copy from web browser to test editor. Save to file ( example: mantis.sql )
7) Install mantis website ( http://www.mantisbugtracker.com/ )
8) Modify mantis database from bugzilla data "bugzilla2mantis.php" ( example: mysql -u root bugtracker < mantis.sql )
9) Now, report bugs with mantis ... 

// ------------------------------------------------------------------------------------------
// CHANGELOG
// ------------------------------------------------------------------------------------------
2016.11.21 - Adaptation for PHP 5.6.16, bugzilla version 5.0.3 and Mantis BT version 1.3.3 - french version (groques)
2006.09.24 - Clean code and Documentation (afontquerni)
2006.09.16 - Resolve some major bugs (afontquerni)
2006.09.15 - Add bugnote_table and bugnote_text_table  (afontquerni)
2006.09.14 - Initial release (afontquerni)

// ------------------------------------------------------------------------------------------
// DESCRIPTION STRUCTURE - DOCUMENTATION
// ------------------------------------------------------------------------------------------

bz2m_connect($link); // !!! MODIFY: "server", "user", "password", "database" ( 'localhost','bugzilla3','pasteyourpass_a little_lower', 'bugzilla3' ) !!

echo "DELETE FROM mantis_user_table;";

bz2m_migration_profiles2mantis_user_table();  //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_products2mantis_project_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_bugs_activity2mantis_bug_history_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_mantis_category_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_category_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_mantis_customclient_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_customclient_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM
	  
	  
bz2m_migration_mantis_bug_file_table(); //CUSTOM - TO USE THIS, YOU NEED TO COPY EMPTY mantis_bug_file_table TO YOUR BUGZILLA DATABASE (YOU HAVE TO DUMP AND MOVE THIS TABLE LATER BACK TO MANTIS DB)
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total, $joins ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_bug_file_table_callback( $row_count, $flag_first, $key, $value ); //COMMON

mysql_close( $link);

*/

// ------------------------------------------------------------------------------------------
//     PHP CODE
// ------------------------------------------------------------------------------------------

function bz2m_connect( &$mysqli ) { // BUGZILLA - connect to database
/*********
// PHP version before 5.6.16
	$link = mysql_connect('localhost', 'myuser', 'mypassword');
	if (!$link)
	{
	  die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("bugs",  $link);
*********/
	$myport = 3306;
	$mysqli = new mysqli('localhost', 'webuser', 'password', 'bugzilla', $myport);
	if ($mysqli->connect_error) {
		die('Erreur de connexion (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
	}

	echo 'Success... ' . $mysqli->host_info . "\n";
} 

// ------------------------------------------------------------------------------------------

function bz2m_select($con, $sql_fields, $sql_table, &$data, &$total, $joins = "", $limit = "") { // BUGZILLA -  read bugs

	$sql="SELECT ";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql." FROM "."`".$sql_table."` " . $joins . " " . $limit;
//	$sql = $sql." FROM "."`".$sql_table."` WHERE `bug_id`<3";
//	$sql = $sql." FROM "."`".$sql_table."`  limit 0 ,3;";

	echo ""; echo $sql;  echo "";
	

	$result = mysqli_query($con, $sql );

	$columns = array();
	$data = array();

	// Table(Rows)
	while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
	{
		foreach ($sql_fields as $key1=> $value1 ) {
			$columns[]= $row[$value1];
			//echo "$value1 $row[$value1] ";
		}
		$data[]= $columns;
		unset($columns);
	}

	$total = mysqli_num_rows($result);
	mysqli_free_result($result);
}

// ------------------------------------------------------------------------------------------

function bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, &$data, $callback_function ) { // MANTIS -  write bugs

	global $mysqli;
	$sql="INSERT INTO `$sql_table` (";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql.") VALUES( ";

	$sql_header = $sql;

	foreach ($data as $key0 => $value0 ) {
		unset($sql);
		$sql = $sql_header;
		$row_count = $key0 + 1;

		$flag_first = true;

		foreach ($value0 as $key1 => $value1 ) {
			$sql.= call_user_func( $callback_function, $row_count, $flag_first, $sql_fields_source[$key1], $value1);
			if($flag_first )
				$flag_first = false;
			//echo ""; echo "$key1 $value1"; echo "";

		}
		$sql.= " );";
		if($sql_table === 'mantis_bug_file_table') {
			$mysqli->query($sql);
			//echo $sql; echo "";
			echo "#";
		} else {
			echo $sql; echo "";
		}
		

		
	}
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table($mysqli) //USERS
{
/*
USERS
#####

mysql> describe profiles;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| userid         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| login_name     | varchar(255) | NO   | UNI | NULL    |                |
| cryptpassword  | varchar(128) | YES  |     | NULL    |                |
| realname       | varchar(255) | YES  |     | NULL    |                |
| disabledtext   | mediumtext   | NO   |     | NULL    |                |
| disable_mail   | tinyint(4)   | NO   |     | 0       |                |
| mybugslink     | tinyint(4)   | NO   |     | 1       |                |
| extern_id      | varchar(64)  | YES  |     | NULL    |                |
| is_enabled 	 | tinyint(4)   | NO   |     | 1 	   |                |
| last_seen_date | datetime     | NO   |     | NULL    |                |
| email          | varchar(255) | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

#####

mysql> describe mantis_user_table;
+-----------------------------+------------------+------+-----+---------------------+----------------+
| Field                       | Type             | Null | Key | Default             | Extra          |
+-----------------------------+------------------+------+-----+---------------------+----------------+
| id                          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| username                    | varchar(32)      | NO   | UNI | NULL                |                |
| realname                    | varchar(64)      | NO   |     | NULL                |                |
| email                       | varchar(64)      | NO   |     | NULL                |                |
| password                    | varchar(32)      | NO   |     | NULL                |                |
| enabled                     | tinyint(4)       | NO   | MUL | 1                   |                |
| protected                   | tinyint(4)       | NO   |     | 0                   |                |
| access_level                | smallint(6)      | NO   | MUL | 10                  |                |
| login_count                 | int(11)          | NO   |     | 0                   |                |
| lost_password_request_count | smallint(6)      | NO   |     | 0                   |                |
| failed_login_count          | smallint(6)      | NO   |     | 0                   |                |
| cookie_string               | varchar(64)      | NO   | UNI | NULL                |                |
| last_visit                  | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| date_created                | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
+-----------------------------+------------------+------+-----+---------------------+----------------+
14 rows in set (0.00 sec)

*/


$sql_table_source= "profiles";
$sql_fields_source=array( "userid","login_name","cryptpassword","realname","login_name", "last_seen_date", "cryptpassword" );

$sql_table= "mantis_user_table";
$sql_fields = array("id","username","password","realname","email","date_created", "cookie_string");
$callback_function = "bz2m_migration_profiles2mantis_user_table_callback";

// ------

$data = array();
$total = 0;

bz2m_select($mysqli, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

	$sql = "";
	//echo "$key $value ";

switch ($key)
{
	case "userid":
		$value1 = $value;
		break;
	case "login_name":
	case "cryptpassword":
	case "realname":
	case "refreshed_when":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	case "last_seen_date":
		$value1 = time(value);
		break;
	break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table($mysqli) //PROJECTS
{

/*
PROJECTS
#####
mysql> describe products;
+-------------------+-------------+------+-----+---------+----------------+
| Field             | Type        | Null | Key | Default | Extra          |
+-------------------+-------------+------+-----+---------+----------------+
| id                | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name              | varchar(64) | NO   | UNI | NULL    |                |
| classification_id | smallint(6) | NO   |     | 1       |                |
| description       | mediumtext  | YES  |     | NULL    |                |
| isactive 	        | tinyint(4)  | NO   |     | 1       |                |
| votesperuser      | smallint(6) | NO   |     | NULL    |                |
| maxvotesperbug    | smallint(6) | NO   |     | 10000   |                |
| votestoconfirm    | smallint(6) | NO   |     | NULL    |                |
| defaultmilestone  | varchar(20) | NO   |     | ---     |                |
| allows_unconfirmed| tinyint(4)  | NO   |     | 1       |                |
+-------------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> 

#####

mysql> describe mantis_project_table;
+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id             | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name           | varchar(128)     | NO   | UNI | NULL    |                |
| status         | smallint(6)      | NO   |     | 10      |                |
| enabled        | tinyint(4)       | NO   |     | 1       |                |
| view_state     | smallint(6)      | NO   | MUL | 10      |                |
| access_min     | smallint(6)      | NO   |     | 10      |                |
| file_path      | varchar(250)     | NO   |     | NULL    |                |
| description    | text             | NO   |     | NULL    |                |
| category_id    | int(10) unsigned | NO   |     | 1       |                |
| inherit_global | tinyint(4)       | NO   |     | 0       |                |
+----------------+------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)

*/

$sql_table_source= "products";
$sql_fields_source= array( "id","name","description","votesperuser","votestoconfirm" );

$sql_table= "mantis_project_table";
$callback_function = "bz2m_migration_products2mantis_project_table_callback";
$sql_fields = array("id","name","description", "status", "enabled");


// ----

$data = array();
$total = 0;

bz2m_select($mysqli, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "id":
		$value1 = $value;
		break;
	case "name":
	case "description":
		$value1 = "'".str_replace("'", "''", htmlentities($value, ENT_HTML5, 'utf-8'))."'";
		break;
	case "votesperuser":
		$value1 = 10;
		break;
	case "votestoconfirm":
		$value1 = 1;
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table($con) //BUGS
{

/*
BUGS
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     |         |                |
| cf_customer         | varchar(64)  | NO   |     |         |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


#####
mysql> describe mantis_bug_table;
+-------------------+------------------+------+-----+---------------------+----------------+
| Field             | Type             | Null | Key | Default             | Extra          |
+-------------------+------------------+------+-----+---------------------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| project_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id       | int(10) unsigned | NO   |     | 0                   |                |
| handler_id        | int(10) unsigned | NO   |     | 0                   |                |
| duplicate_id      | int(10) unsigned | NO   |     | 0                   |                |
| priority          | smallint(6)      | NO   |     | 30                  |                |
| severity          | smallint(6)      | NO   |     | 50                  |                |
| reproducibility   | smallint(6)      | NO   |     | 10                  |                |
| status            | smallint(6)      | NO   | MUL | 10                  |                |
| resolution        | smallint(6)      | NO   |     | 10                  |                |
| projection        | smallint(6)      | NO   |     | 10                  |                |
| eta               | smallint(6)      | NO   |     | 10                  |                |
| bug_text_id       | int(10) unsigned | NO   |     | 0                   |                |
| os                | varchar(32)      | NO   |     | NULL                |                |
| os_build          | varchar(32)      | NO   |     | NULL                |                |
| platform          | varchar(32)      | NO   |     | NULL                |                |
| version           | varchar(64)      | NO   |     | NULL                |                |
| fixed_in_version  | varchar(64)      | NO   | MUL | NULL                |                |
| build             | varchar(32)      | NO   |     | NULL                |                |
| profile_id        | int(10) unsigned | NO   |     | 0                   |                |
| view_state        | smallint(6)      | NO   |     | 10                  |                |
| summary           | varchar(128)     | NO   |     | NULL                |                |
| sponsorship_total | int(11)          | NO   | MUL | 0                   |                |
| sticky            | tinyint(4)       | NO   |     | 0                   |                |
| category_id       | int(10) unsigned | NO   |     | 1                   |                |
| date_submitted    | int(10) unsigned | NO   |     | 1                   |                |
| due_date          | int(10) unsigned | NO   |     | 1                   |                |
| last_updated      | int(10) unsigned | NO   |     | 1                   |                |
+-------------------+------------------+------+-----+---------------------+----------------+
29 rows in set (0.00 sec)

mysql>
*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","assigned_to","bug_severity","bug_status","product_id","creation_ts","creation_ts","priority","reporter", "short_desc", "bug_id" , "component_id","resolution","version");

$sql_table= "mantis_bug_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_table_callback";
$sql_fields = array("id","handler_id","severity","status","project_id","date_submitted","last_updated","priority","reporter_id", "summary", "bug_text_id", "category_id","resolution","version" );

// ---

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";
/*
Mantis
$s_severity_enum_string = '10:Feature-Wunsch,20:Trivial,30:Fehler im Text,40:Unsch�nheit,50:kleinerer Fehler,60:schwerer Fehler,70:Absturz,80:Blocker'
$s_status_enum_string = '10:neu,20:R�ckmeldung,30:anerkannt,40:best�tigt,50:zugewiesen,80:erledigt,90:geschlossen';
$s_resolution_enum_string = '10:offen,20:erledigt,30:wiederer�ffnet,40:nicht reproduzierbar,50:unl�sbar,60:doppelt,70:keine �nderung notwendig,80:aufgeschoben,90:wird nicht behoben';
$s_priority_enum_string = '10:keine,20:niedrig,30:normal,40:hoch,50:dringend,60:sofort';
*/       
$bug_severity_map = array( "enhancement"=>10, "trivial"=>20, "Standby"=>20, "minor"=>40, "Low"=>40, "normal"=>50, "Normal"=>50, "major"=>60, "High"=>60, "critical"=>70, "Urgent"=>70, "blocker"=>80 );
$bug_status_map = array( "UNCONFIRMED"=>10, "NEW"=>40, "ASSIGNED"=>50, "RESOLVED"=>80, "CLOSED"=>90 ); 
$bug_resolution_map = array( ""=>10, "FIXED"=>20, "INVALID"=>50, "REJECTED"=>90, "DUPLICATE"=>60, "WORKS FOR ME"=>70, "REVIEW/TESTING"=>20, "READY TO DEPLOY"=>20, "DONE/LIVE"=>20, "FREEZE"=>80, "APPROVED"=>10, "NOT REPRODUCABLE"=>40 );
$bug_priority_map = array( "Standby"=>10, "Low"=>20, "Normal"=>30, "High"=>40 );

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "assigned_to":
	case "reporter":
	case "product_id":
	case "component_id":
		$value1 = $value;
		break;
	case "short_desc":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value1 = "\"".$value."\"";
		break;
	case "version":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	case "creation_ts":
		$value1 = strtotime( $value );
		break;

	case "bug_severity":
		if( isset($bug_severity_map[$value]) ) {
			$value1 = $bug_severity_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "bug_status":
		if( isset($bug_status_map[$value]) ) {
			$value1 = $bug_status_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "priority":
		if( isset($bug_priority_map[$value]) ) {
			$value1 = $bug_priority_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;

	case "resolution":
		if( isset($bug_resolution_map[$value]) ) {
			$value1 = $bug_resolution_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table($con) //BUGS HISTORY
{

/*
BUGS HISTORY
#####
mysql> describe bugs_activity;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| bug_id    | mediumint(9) | NO   | MUL | NULL    |       |
| attach_id | mediumint(9) | YES  |     | NULL    |       |
| who       | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when  | datetime     | NO   | MUL | NULL    |       |
| fieldid   | mediumint(9) | NO   | MUL | NULL    |       |
| added     | tinytext     | YES  |     | NULL    |       |
| removed   | tinytext     | YES  |     | NULL    |       |
| comment_id| int(11)      | YES  |     | NULL    |       |
| id        | int(11)      | NO   |     | AUTO    |       |
+-----------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

#####

mysql> describe mantis_bug_history_table;
+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| user_id       | int(10) unsigned | NO   | MUL | 0                   |                |
| bug_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| field_name    | varchar(32)      | NO   |     | NULL                |                |
| old_value     | varchar(128)     | NO   |     | NULL                |                |
| new_value     | varchar(128)     | NO   |     | NULL                |                |
| type          | smallint(6)      | NO   |     | 0                   |                |
| date_modified | int              | NO   |     | NULL                |                |
+---------------+------------------+------+-----+---------------------+----------------+
8 rows in set (0.00 sec)

*/

$sql_table_source= "bugs_activity";
$sql_fields_source = array( "bug_id","who","bug_when","fieldid","added","removed" );

$sql_table= "mantis_bug_history_table";
$callback_function = "bz2m_migration_bugs_activity2mantis_bug_history_table_callback";
$sql_fields = array("bug_id","user_id","date_modified","field_name","old_value","new_value");

// ----

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "who":
		$value1 = $value;
		break;	    
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "fieldid":
	case "added":
	case "removed":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
| type            | smallint(6)  | NO   |     | 0       |       |
| extra_data      | varchar(255) | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

#####

mysql> describe mantis_bugnote_table;
+-----------------+------------------+------+-----+---------------------+----------------+
| Field           | Type             | Null | Key | Default             | Extra          |
+-----------------+------------------+------+-----+---------------------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL                |                |
| bug_id          | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id     | int(10) unsigned | NO   |     | 0                   |                |
| bugnote_text_id | int(10) unsigned | NO   |     | 0                   |                |
| view_state      | smallint(6)      | NO   |     | 10                  |                |
| note_type       | int(11)          | YES  |     | 0                   |                |
| note_attr       | varchar(250)     | YES  |     | NULL                |                |
| time_tracking   | int(10) unsigned | NO   |     | 0                   |                |
| last_modified   | int(10) unsigned | NO   | MUL | 1                   |                |
| date_submitted  | int(10) unsigned | NO   |     | 1                   |                |
+-----------------+------------------+------+-----+---------------------+----------------+
10 rows in set (0.01 sec)

mysql>

#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "bug_id","who","bug_when","bug_when","work_time", );

$sql_table= "mantis_bugnote_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_table_callback";
$sql_fields = array("id", "bug_id","reporter_id","date_submitted","last_modified","bugnote_text_id",);

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "who":
	case "comment_id":
		$value1 = $value;
		break;
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "work_time":
		$value1 = $row_count;
		break;
	case "thetext":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
| type            | smallint(6)  | NO   |     | 0       |       |
| extra_data      | varchar(255) | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)


mysql> describe mantis_bugnote_text_table;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    |                |
| note  | text             | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "thetext" );

$sql_table= "mantis_bugnote_text_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_text_table_callback";
$sql_fields = array("id", "note");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "comment_id":
		$value1 = $value;
		break;
	case "thetext":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     | NULL    |                |
| cf_customer         | varchar(64)  | NO   |     | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_bug_text_table;
+------------------------+------------------+------+-----+---------+----------------+
| Field                  | Type             | Null | Key | Default | Extra          |
+------------------------+------------------+------+-----+---------+----------------+
| id                     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| description            | text             | NO   |     | NULL    |                |
| steps_to_reproduce     | text             | NO   |     | NULL    |                |
| additional_information | text             | NO   |     | NULL    |                |
+------------------------+------------------+------+-----+---------+----------------+
mysql>

#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","short_desc","resolution","votes");

$sql_table= "mantis_bug_text_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_text_table_callback";
$sql_fields = array("id","description","additional_information","steps_to_reproduce");

// ---

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "short_desc":
	case "resolution":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	case "votes":
	    $value1 = "\"\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_category_table($con) //BUGS CATEGORY
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe components;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| id              | mediumint(9) | NO   | PRI | NULL    | AUTO  |
| name            | varchar(64)  | NO   | MUL | NULL    |       |
| product_id      | smallint(6)  | NO   | MUL | NULL    |       |
| initialowner    | mediumint(9) | NO   | MUL | NULL    |       |
| initialqacontact| mediumint(9) | YES  | MUL | NULL    |       |
| description     | mediumtext   | NO   |     | NULL    |       |
| isactive        | tinyint(4)   | NO   |     | 1       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)


mysql> describe mantis_category_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    |  AUTO          |
| project_id  | int(10) unsigned | NO   | MUL | 0       |                |
| user_id     | int(10) unsigned | NO   |     | 0       |                |
| name        | varchar(128)     | NO   |     |         |                |
| status      | int(10) unsigned | NO   |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)


#####

*/

$sql_table_source= "components";
$sql_fields_source = array("id", "name" , "product_id", "isactive");

$sql_table= "mantis_category_table";
$callback_function = "bz2m_migration_mantis_category_table_callback";
$sql_fields = array("id", "name", "project_id", "status");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_category_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write category

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "id":
	case "product_id":
	case "isactive":
		$value1 = $value;
		break;
	case "name":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_bug_file_table($con) //BUGS ATTACHMENTS
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe attachments;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| attach_id         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| bug_id            | mediumint(9) | NO   | MUL | NULL    |                |
| creation_ts       | datetime     | NO   | MUL | NULL    |                |
| modification_time | datetime     | NO   | MUL | NULL    |                |
| description       | tinytext     | NO   |     | NULL    |                |
| mimetype          | tinytext     | NO   |     | NULL    |                |
| ispatch           | tinyint(4)   | NO   |     | 0       |                |
| filename          | varchar(255) | NO   |     | NULL    |                |
| submitter_id      | mediumint(9) | NO   | MUL | NULL    |                |
| isobsolete        | tinyint(4)   | NO   |     | 0       |                |
| isprivate         | tinyint(4)   | NO   |     | 0       |                |
+-------------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
mysql> describe attach_data;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | mediumint(9) | NO   | PRI | NULL    |       |
| thedata | longblob     | NO   |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)



mysql> describe mantis_bug_file_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| bug_id      | int(10) unsigned | NO   | MUL | 0       |                |
| title       | varchar(250)     | NO   |     |         |                |
| description | varchar(250)     | NO   |     |         |                |
| diskfile    | varchar(250)     | NO   | MUL |         |                |
| filename    | varchar(250)     | NO   |     |         |                |
| folder      | varchar(250)     | NO   |     |         |                |
| filesize    | int(11)          | NO   |     | 0       |                |
| file_type   | varchar(250)     | NO   |     |         |                |
| content     | longblob         | YES  |     | NULL    |                |
| date_added  | int(10) unsigned | NO   |     | 1       |                |
| user_id     | int(10) unsigned | NO   |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)


#####

*/

$sql_table_source= "attachments";
$sql_fields_source = array("attach_id", "bug_id" , "creation_ts", "filename", "description", "mimetype", "submitter_id", "thedata");
$join = "INNER JOIN attach_data ON attach_data.id = attachments.attach_id";
//$limit = "LIMIT 10";

$sql_table= "mantis_bug_file_table";
$callback_function = "bz2m_migration_mantis_bug_file_table_callback";
$sql_fields = array("id", "bug_id", "date_added", "filename", "description", "file_type", "user_id", "content");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total, $join, $limit );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_bug_file_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write category

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "attach_id":
	case "bug_id":
	case "submitter_id":
		$value1 = $value;
		break;
	case "filename":
	case "description":
	case "mimetype":
		$value1 = "'".str_replace("'", "''", $value)."'";
	break;
	
	case "thedata":
		$value1 = "'".mysql_escape_string($value)."'";
	break;
	case "creation_ts":
		$value1 = time($value);
	break;
	
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
		
	
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_customclient_table($con) //BUGS CUSTOMCLIENT
{

/*
BUGS DESCRIPTION
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     |         |                |
| cf_customer         | varchar(64)  | NO   |     |         |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_custom_field_string_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| field_id  | int(11) unsigned   | NO   | PRI | 0       |  AUTO          |
| bug_id  	| int(11) unsigned   | NO   | MUL | 0       |                |
| value     | varchar(255)       | NO   |     |         |                |
| text      | int(10) unsigned   | YES  |     |         |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array("cf_client", "bug_id", "cf_customer");

$sql_table= "mantis_custom_field_string_table";
$callback_function = "bz2m_migration_mantis_customclient_table_callback";
$sql_fields = array("field_id", "bug_id", "value");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_customclient_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write customer

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "cf_client":
		$value1 = 1;
		break;
	case "bug_id":
		$value1 = $value;
		break;
	case "cf_customer":
		if ($value == '---')
			$value1 = "\"\"";
		else
			$value1 = "\"|".htmlentities($value, ENT_HTML5, 'utf-8')."|\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

{ // BUGZILLA - MAIN

//echo "/* NB : FIRST CREATE ALL YOUR USER ACCOUNT IN MANTIS WITH THE CORRESPONDING ID FROM BUGZILLA */";
//echo "<br>";

echo "<html lang='de' xmlns='http://www.w3.org/1999/xhtml'>
	<head>
		<title>Migration bugzilla to Mantis</title>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		<meta http-equiv='Content-Language' content='de-de' />
    </head>
    <body>";

bz2m_connect($mysqli);

bz2m_migration_profiles2mantis_user_table($mysqli);

bz2m_migration_products2mantis_project_table($mysqli);

bz2m_migration_bugs2mantis_bug_table($mysqli);
bz2m_migration_bugs2mantis_bug_text_table($mysqli);

bz2m_migration_bugs_activity2mantis_bug_history_table($mysqli);

bz2m_migration_longdescs2mantis_bugnote_table($mysqli);

bz2m_migration_longdescs2mantis_bugnote_text_table($mysqli);

bz2m_migration_mantis_category_table($mysqli);

bz2m_migration_mantis_customclient_table($mysqli);

bz2m_migration_mantis_bug_file_table($mysqli);
mysqli_query($mysqli, "update mantis_bug_file_table set filesize = LENGTH(content);");


echo "</body>
        </html>";
$mysqli->close();

/*********
// PHP version before 5.6.16
bz2m_migration_profiles2mantis_user_table();

bz2m_migration_products2mantis_project_table();

bz2m_migration_bugs2mantis_bug_table();
bz2m_migration_bugs2mantis_bug_text_table();

bz2m_migration_bugs_activity2mantis_bug_history_table();

bz2m_migration_longdescs2mantis_bugnote_table();
bz2m_migration_longdescs2mantis_bugnote_text_table();

mysql_close( $link);
*********/
}


?>

bugzilla2mantis-4.php (59,497 bytes)   

Relationships

related to 0008071 closeddregad Migration script for Bugzilla 2.16 (script by Cris Daniluk) 
related to 0009283 closedvboctor "Cris Daniluk " link is not opening 
related to 0008251 closeddregad Migration script to Mantis 

Activities

vboctor

vboctor

2006-09-25 04:04

manager   ~0013487

Last edited: 2012-08-28 03:48

Thanks for the contribution. I've added a link to this issue in the Mantis FAQ [1].

I would suggest using attachments for making this version and future versions available to users.

http://www.mantisbt.org/wiki/doku.php/mantisbt:faq#migration_to_mantisbt

EDIT: dregad fixed wiki link

afontquerni

afontquerni

2006-09-29 11:46

reporter   ~0013558

Thank you for mantis.
I can't not change " Additional Information".
I already upload file "bugzilla2mantis.php" ( Attached Files )
I hope integration with core mantis release for a easy bugzilla replacement.

marc

marc

2007-06-15 13:00

reporter   ~0014768

also see 0008071 for the script by Cris Daniluk

jybleau

jybleau

2011-01-21 17:17

reporter   ~0028024

Thnaks for the script:

I have uploaded a new version, "bugzilla2mantisV2-jyb.php", which is compatible with bugzilla3:
-Modify: timestamp string format to php int format (as expected by mantis)
-Modify: text field :
-Keeps carriage return
-Check for ' and \ characters
-Added: steps_to_reproduce field with "" value in mantis_bug_text_table

jybleau

jybleau

2011-01-21 18:11

reporter   ~0028025

Uploaded a new version 3.

orrected a bug with mantis_bugnote_table and antis_bugnote_text_table and id field.
They are now using the comment_id field from bugzilla.

(Forget about the version 2 file. it there a way it can be removed?)

dregad

dregad

2012-08-28 03:47

developer   ~0032689

Removed php code from "Additional Info" field, as it's already uploaded as part of attachment bugzilla2mantis.php uploaded 2006-09-24 17:05.

Here is a reference to another bugzilla upgrade script:
http://www.l3jane.net/wiki/factory%3Abugzilla2mantis

Source code here: https://www.l3jane.net/svn/trunk/Bz2Mantis/

groques

groques

2016-11-21 18:48

reporter   ~0054560

Hi everyone,

Your script was useful for me but I had to modify it to switch from bugzilla 5.0.3 to Mantis bt 1.3.3, so here is my file ;)

groques

groques

2016-11-22 05:33

reporter   ~0054563

Hi again,

sorry when I re-arranged my script, to remove my username and password, I wrote erroneous code, so the v5 has the correction.

I wrote the extraction of custom field too because I needed it, but the "fiel_id" is linked to value "1". I tought it would help....

dregad

dregad

2016-11-22 07:04

developer   ~0054564

@groques should the v4 attachment be deleted then ?

groques

groques

2016-11-22 07:11

reporter   ~0054565

It should , thank you.

christoph.suess

christoph.suess

2017-12-07 05:05

reporter   ~0058338

Added support for migration of attachments and changed charset to utf-8
Works great for mantis v 2.9.0 :-)

bugzilla2mantis-2.php (59,536 bytes)   
<?php
header('Content-type: text/plain; charset=utf-8');

ini_set("memory_limit", "64G");
ini_set("max_execution_time", "86400");
/*
// ------------------------------------------------------------------------------------------
// LICENSE
// ------------------------------------------------------------------------------------------

        # bugzilla2mantis.php - Development Status "Alpha" 
        # Convert bugzilla 5.0.3 database to mantis 1.3.3 sql data inserts
        #
        # Copyright (C) 2006  Agusti Fontquerni afontquerni -at- gmail -dot- com
		# Copyright (C) 2017  Christoph Suess <christoph -dot- suess -at- gmail -dot- com (for the attachment migration and utf-8 support)
        # This program is distributed under the terms and conditions 
        # of the GNU GENERAL PUBLIC LICENSE Version 2 (http://www.gnu.org/copyleft/gpl.html)

// ------------------------------------------------------------------------------------------
// README
// ------------------------------------------------------------------------------------------
On Set-2006 I was looking for Bugzilla to Mantis (Perl) by Cris Daniluk (http://www.cadencetools.org/projects/bugzilla2mantis/)     

But Nobody didn't find any thing on the web, all web page links are broken or deleted (http://freshmeat.net/redir/bugzilla2mantis/52873/url_tgz/bugzilla2mantis-0.2.tar.gz) ((http://forums.mantisbugtracker.com/viewtopic.php?p=5745&sid=1a679a9fa211306bfd51eb33f7da2579))    

At last, I decided to start my first php script to convert bugs data from bugzilla to mantis (bugzilla2mantis.php).  It is alfa script but its did basic and functional great job.  
I give mantis2bugzilla.php to mantis community to improve bugzilla migration feature

// ------------------------------------------------------------------------------------------
// INSTALL
// ------------------------------------------------------------------------------------------
1) Backup all databases
2) Copy this file to bugzilla web site ( example: /var/www/bugzilla )
3) Modify database parameters "server", "user", "password", "database" ( 'localhost','root','', 'bugzilla' ) in "bz2m_connect()" function 
4) Open "bugzilla2mantis.php" with web browser ( example: localhost:/bugzilla/bugzilla2mantis.php )
5) Check output web page
6) Select all (Ctrl+A). Copy from web browser to test editor. Save to file ( example: mantis.sql )
7) Install mantis website ( http://www.mantisbugtracker.com/ )
8) Modify mantis database from bugzilla data "bugzilla2mantis.php" ( example: mysql -u root bugtracker < mantis.sql )
9) Now, report bugs with mantis ... 

// ------------------------------------------------------------------------------------------
// CHANGELOG
// ------------------------------------------------------------------------------------------
2016.11.21 - Adaptation for PHP 5.6.16, bugzilla version 5.0.3 and Mantis BT version 1.3.3 - french version (groques)
2006.09.24 - Clean code and Documentation (afontquerni)
2006.09.16 - Resolve some major bugs (afontquerni)
2006.09.15 - Add bugnote_table and bugnote_text_table  (afontquerni)
2006.09.14 - Initial release (afontquerni)

// ------------------------------------------------------------------------------------------
// DESCRIPTION STRUCTURE - DOCUMENTATION
// ------------------------------------------------------------------------------------------

bz2m_connect($link); // !!! MODIFY: "server", "user", "password", "database" ( 'localhost','bugzilla3','pasteyourpass_a little_lower', 'bugzilla3' ) !!

echo "DELETE FROM mantis_user_table;";

bz2m_migration_profiles2mantis_user_table();  //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_products2mantis_project_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_bugs_activity2mantis_bug_history_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_mantis_category_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_category_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_mantis_customclient_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_customclient_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM
	  
	  
bz2m_migration_mantis_bug_file_table(); //CUSTOM - TO USE THIS, YOU NEED TO COPY EMPTY mantis_bug_file_table TO YOUR BUGZILLA DATABASE (YOU HAVE TO DUMP AND MOVE THIS TABLE LATER BACK TO MANTIS DB)
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total, $joins ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_bug_file_table_callback( $row_count, $flag_first, $key, $value ); //COMMON

mysql_close( $link);

*/

// ------------------------------------------------------------------------------------------
//     PHP CODE
// ------------------------------------------------------------------------------------------

function bz2m_connect( &$mysqli ) { // BUGZILLA - connect to database
/*********
// PHP version before 5.6.16
	$link = mysql_connect('localhost', 'myuser', 'mypassword');
	if (!$link)
	{
	  die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("bugs",  $link);
*********/
	$myport = 3306;
	$mysqli = new mysqli('localhost', 'webuser', 'password', 'bugzilla', $myport);
	if ($mysqli->connect_error) {
		die('Erreur de connexion (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
	}

	echo 'Success... ' . $mysqli->host_info . "\n";
} 

// ------------------------------------------------------------------------------------------

function bz2m_select($con, $sql_fields, $sql_table, &$data, &$total, $joins = "", $limit = "") { // BUGZILLA -  read bugs

	$sql="SELECT ";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql." FROM "."`".$sql_table."` " . $joins . " " . $limit;
//	$sql = $sql." FROM "."`".$sql_table."` WHERE `bug_id`<3";
//	$sql = $sql." FROM "."`".$sql_table."`  limit 0 ,3;";

	echo ""; echo $sql;  echo "";
	

	$result = mysqli_query($con, $sql );

	$columns = array();
	$data = array();

	// Table(Rows)
	while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
	{
		foreach ($sql_fields as $key1=> $value1 ) {
			$columns[]= $row[$value1];
			//echo "$value1 $row[$value1] ";
		}
		$data[]= $columns;
		unset($columns);
	}

	$total = mysqli_num_rows($result);
	mysqli_free_result($result);
}

// ------------------------------------------------------------------------------------------

function bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, &$data, $callback_function ) { // MANTIS -  write bugs

	global $mysqli;
	$sql="INSERT INTO `$sql_table` (";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql.") VALUES( ";

	$sql_header = $sql;

	foreach ($data as $key0 => $value0 ) {
		unset($sql);
		$sql = $sql_header;
		$row_count = $key0 + 1;

		$flag_first = true;

		foreach ($value0 as $key1 => $value1 ) {
			$sql.= call_user_func( $callback_function, $row_count, $flag_first, $sql_fields_source[$key1], $value1);
			if($flag_first )
				$flag_first = false;
			//echo ""; echo "$key1 $value1"; echo "";

		}
		$sql.= " );";
		if($sql_table === 'mantis_bug_file_table') {
			$mysqli->query($sql);
			//echo $sql; echo "";
			echo "#";
		} else {
			echo $sql; echo "";
		}
		

		
	}
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table($mysqli) //USERS
{
/*
USERS
#####

mysql> describe profiles;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| userid         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| login_name     | varchar(255) | NO   | UNI | NULL    |                |
| cryptpassword  | varchar(128) | YES  |     | NULL    |                |
| realname       | varchar(255) | YES  |     | NULL    |                |
| disabledtext   | mediumtext   | NO   |     | NULL    |                |
| disable_mail   | tinyint(4)   | NO   |     | 0       |                |
| mybugslink     | tinyint(4)   | NO   |     | 1       |                |
| extern_id      | varchar(64)  | YES  |     | NULL    |                |
| is_enabled 	 | tinyint(4)   | NO   |     | 1 	   |                |
| last_seen_date | datetime     | NO   |     | NULL    |                |
| email          | varchar(255) | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

#####

mysql> describe mantis_user_table;
+-----------------------------+------------------+------+-----+---------------------+----------------+
| Field                       | Type             | Null | Key | Default             | Extra          |
+-----------------------------+------------------+------+-----+---------------------+----------------+
| id                          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| username                    | varchar(32)      | NO   | UNI | NULL                |                |
| realname                    | varchar(64)      | NO   |     | NULL                |                |
| email                       | varchar(64)      | NO   |     | NULL                |                |
| password                    | varchar(32)      | NO   |     | NULL                |                |
| enabled                     | tinyint(4)       | NO   | MUL | 1                   |                |
| protected                   | tinyint(4)       | NO   |     | 0                   |                |
| access_level                | smallint(6)      | NO   | MUL | 10                  |                |
| login_count                 | int(11)          | NO   |     | 0                   |                |
| lost_password_request_count | smallint(6)      | NO   |     | 0                   |                |
| failed_login_count          | smallint(6)      | NO   |     | 0                   |                |
| cookie_string               | varchar(64)      | NO   | UNI | NULL                |                |
| last_visit                  | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| date_created                | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
+-----------------------------+------------------+------+-----+---------------------+----------------+
14 rows in set (0.00 sec)

*/


$sql_table_source= "profiles";
$sql_fields_source=array( "userid","login_name","cryptpassword","realname","login_name", "last_seen_date", "cryptpassword" );

$sql_table= "mantis_user_table";
$sql_fields = array("id","username","password","realname","email","date_created", "cookie_string");
$callback_function = "bz2m_migration_profiles2mantis_user_table_callback";

// ------

$data = array();
$total = 0;

bz2m_select($mysqli, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

	$sql = "";
	//echo "$key $value ";

switch ($key)
{
	case "userid":
		$value1 = $value;
		break;
	case "login_name":
	case "cryptpassword":
	case "realname":
	case "refreshed_when":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	case "last_seen_date":
		$value1 = time(value);
		break;
	break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table($mysqli) //PROJECTS
{

/*
PROJECTS
#####
mysql> describe products;
+-------------------+-------------+------+-----+---------+----------------+
| Field             | Type        | Null | Key | Default | Extra          |
+-------------------+-------------+------+-----+---------+----------------+
| id                | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name              | varchar(64) | NO   | UNI | NULL    |                |
| classification_id | smallint(6) | NO   |     | 1       |                |
| description       | mediumtext  | YES  |     | NULL    |                |
| isactive 	        | tinyint(4)  | NO   |     | 1       |                |
| votesperuser      | smallint(6) | NO   |     | NULL    |                |
| maxvotesperbug    | smallint(6) | NO   |     | 10000   |                |
| votestoconfirm    | smallint(6) | NO   |     | NULL    |                |
| defaultmilestone  | varchar(20) | NO   |     | ---     |                |
| allows_unconfirmed| tinyint(4)  | NO   |     | 1       |                |
+-------------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> 

#####

mysql> describe mantis_project_table;
+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id             | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name           | varchar(128)     | NO   | UNI | NULL    |                |
| status         | smallint(6)      | NO   |     | 10      |                |
| enabled        | tinyint(4)       | NO   |     | 1       |                |
| view_state     | smallint(6)      | NO   | MUL | 10      |                |
| access_min     | smallint(6)      | NO   |     | 10      |                |
| file_path      | varchar(250)     | NO   |     | NULL    |                |
| description    | text             | NO   |     | NULL    |                |
| category_id    | int(10) unsigned | NO   |     | 1       |                |
| inherit_global | tinyint(4)       | NO   |     | 0       |                |
+----------------+------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)

*/

$sql_table_source= "products";
$sql_fields_source= array( "id","name","description","votesperuser","votestoconfirm" );

$sql_table= "mantis_project_table";
$callback_function = "bz2m_migration_products2mantis_project_table_callback";
$sql_fields = array("id","name","description", "status", "enabled");


// ----

$data = array();
$total = 0;

bz2m_select($mysqli, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "id":
		$value1 = $value;
		break;
	case "name":
	case "description":
		$value1 = "'".str_replace("'", "''", htmlentities($value, ENT_HTML5, 'utf-8'))."'";
		break;
	case "votesperuser":
		$value1 = 10;
		break;
	case "votestoconfirm":
		$value1 = 1;
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table($con) //BUGS
{

/*
BUGS
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     |         |                |
| cf_customer         | varchar(64)  | NO   |     |         |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


#####
mysql> describe mantis_bug_table;
+-------------------+------------------+------+-----+---------------------+----------------+
| Field             | Type             | Null | Key | Default             | Extra          |
+-------------------+------------------+------+-----+---------------------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| project_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id       | int(10) unsigned | NO   |     | 0                   |                |
| handler_id        | int(10) unsigned | NO   |     | 0                   |                |
| duplicate_id      | int(10) unsigned | NO   |     | 0                   |                |
| priority          | smallint(6)      | NO   |     | 30                  |                |
| severity          | smallint(6)      | NO   |     | 50                  |                |
| reproducibility   | smallint(6)      | NO   |     | 10                  |                |
| status            | smallint(6)      | NO   | MUL | 10                  |                |
| resolution        | smallint(6)      | NO   |     | 10                  |                |
| projection        | smallint(6)      | NO   |     | 10                  |                |
| eta               | smallint(6)      | NO   |     | 10                  |                |
| bug_text_id       | int(10) unsigned | NO   |     | 0                   |                |
| os                | varchar(32)      | NO   |     | NULL                |                |
| os_build          | varchar(32)      | NO   |     | NULL                |                |
| platform          | varchar(32)      | NO   |     | NULL                |                |
| version           | varchar(64)      | NO   |     | NULL                |                |
| fixed_in_version  | varchar(64)      | NO   | MUL | NULL                |                |
| build             | varchar(32)      | NO   |     | NULL                |                |
| profile_id        | int(10) unsigned | NO   |     | 0                   |                |
| view_state        | smallint(6)      | NO   |     | 10                  |                |
| summary           | varchar(128)     | NO   |     | NULL                |                |
| sponsorship_total | int(11)          | NO   | MUL | 0                   |                |
| sticky            | tinyint(4)       | NO   |     | 0                   |                |
| category_id       | int(10) unsigned | NO   |     | 1                   |                |
| date_submitted    | int(10) unsigned | NO   |     | 1                   |                |
| due_date          | int(10) unsigned | NO   |     | 1                   |                |
| last_updated      | int(10) unsigned | NO   |     | 1                   |                |
+-------------------+------------------+------+-----+---------------------+----------------+
29 rows in set (0.00 sec)

mysql>
*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","assigned_to","bug_severity","bug_status","product_id","creation_ts","creation_ts","priority","reporter", "short_desc", "bug_id" , "component_id","resolution","version");

$sql_table= "mantis_bug_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_table_callback";
$sql_fields = array("id","handler_id","severity","status","project_id","date_submitted","last_updated","priority","reporter_id", "summary", "bug_text_id", "category_id","resolution","version" );

// ---

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";
/*
Bugzilla
Bug, Evolution, Param�trage, Documentation, Savoir

Bas, Haut, ND, Normal, TBas, THaut

Mantis
$s_severity_enum_string = '10:Feature-Wunsch,20:Trivial,30:Fehler im Text,40:Unsch�nheit,50:kleinerer Fehler,60:schwerer Fehler,70:Absturz,80:Blocker'
$s_status_enum_string = '10:neu,20:R�ckmeldung,30:anerkannt,40:best�tigt,50:zugewiesen,80:erledigt,90:geschlossen';
$s_resolution_enum_string = '10:offen,20:erledigt,30:wiederer�ffnet,40:nicht reproduzierbar,50:unl�sbar,60:doppelt,70:keine �nderung notwendig,80:aufgeschoben,90:wird nicht behoben';
$s_priority_enum_string = '10:keine,20:niedrig,30:normal,40:hoch,50:dringend,60:sofort';
*/
$bug_severity_map = array( "enhancement"=>10, "trivial"=>20, "Documentation"=>30, "minor"=>40, "normal"=>50, "major"=>60, "critical"=>70, "blocker"=> 80 );
$bug_status_map = array( "UNCONFIRMED"=>10, "NEW"=>10, "IN PROGRESS"=>50, "REOPENED"=>20, "RESOLVED"=>80, "CLOSED"=>90, "WAITING FOR REPLY"=>20, "CONFIRMED"=>40 );
$bug_resolution_map = array( ""=>10, "FIXED"=>20, "INVALID"=>50, "REJECTED"=>90, "DUPLICATE"=>60, "WORKS FOR ME"=>70, "REVIEW/TESTING"=>20, "READY TO DEPLOY"=>20, "DONE/LIVE"=>20, "FREEZE"=>80, "APPROVED"=>10, "NOT REPRODUCABLE"=>40 );
$bug_priority_map = array( "P5"=>10, "P4"=>20, "P3"=>30, "P2"=>40, "P1"=>60 );

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "assigned_to":
	case "reporter":
	case "product_id":
	case "component_id":
		$value1 = $value;
		break;
	case "short_desc":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value1 = "\"".$value."\"";
		break;
	case "version":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	case "creation_ts":
		$value1 = strtotime( $value );
		break;

	case "bug_severity":
		if( isset($bug_severity_map[$value]) ) {
			$value1 = $bug_severity_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "bug_status":
		if( isset($bug_status_map[$value]) ) {
			$value1 = $bug_status_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "priority":
		if( isset($bug_priority_map[$value]) ) {
			$value1 = $bug_priority_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;

	case "resolution":
		if( isset($bug_resolution_map[$value]) ) {
			$value1 = $bug_resolution_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table($con) //BUGS HISTORY
{

/*
BUGS HISTORY
#####
mysql> describe bugs_activity;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| bug_id    | mediumint(9) | NO   | MUL | NULL    |       |
| attach_id | mediumint(9) | YES  |     | NULL    |       |
| who       | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when  | datetime     | NO   | MUL | NULL    |       |
| fieldid   | mediumint(9) | NO   | MUL | NULL    |       |
| added     | tinytext     | YES  |     | NULL    |       |
| removed   | tinytext     | YES  |     | NULL    |       |
| comment_id| int(11)      | YES  |     | NULL    |       |
| id        | int(11)      | NO   |     | AUTO    |       |
+-----------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

#####

mysql> describe mantis_bug_history_table;
+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| user_id       | int(10) unsigned | NO   | MUL | 0                   |                |
| bug_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| field_name    | varchar(32)      | NO   |     | NULL                |                |
| old_value     | varchar(128)     | NO   |     | NULL                |                |
| new_value     | varchar(128)     | NO   |     | NULL                |                |
| type          | smallint(6)      | NO   |     | 0                   |                |
| date_modified | int              | NO   |     | NULL                |                |
+---------------+------------------+------+-----+---------------------+----------------+
8 rows in set (0.00 sec)

*/

$sql_table_source= "bugs_activity";
$sql_fields_source = array( "bug_id","who","bug_when","fieldid","added","removed" );

$sql_table= "mantis_bug_history_table";
$callback_function = "bz2m_migration_bugs_activity2mantis_bug_history_table_callback";
$sql_fields = array("bug_id","user_id","date_modified","field_name","old_value","new_value");

// ----

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "who":
		$value1 = $value;
		break;	    
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "fieldid":
	case "added":
	case "removed":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
| type            | smallint(6)  | NO   |     | 0       |       |
| extra_data      | varchar(255) | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

#####

mysql> describe mantis_bugnote_table;
+-----------------+------------------+------+-----+---------------------+----------------+
| Field           | Type             | Null | Key | Default             | Extra          |
+-----------------+------------------+------+-----+---------------------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL                |                |
| bug_id          | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id     | int(10) unsigned | NO   |     | 0                   |                |
| bugnote_text_id | int(10) unsigned | NO   |     | 0                   |                |
| view_state      | smallint(6)      | NO   |     | 10                  |                |
| note_type       | int(11)          | YES  |     | 0                   |                |
| note_attr       | varchar(250)     | YES  |     | NULL                |                |
| time_tracking   | int(10) unsigned | NO   |     | 0                   |                |
| last_modified   | int(10) unsigned | NO   | MUL | 1                   |                |
| date_submitted  | int(10) unsigned | NO   |     | 1                   |                |
+-----------------+------------------+------+-----+---------------------+----------------+
10 rows in set (0.01 sec)

mysql>

#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "bug_id","who","bug_when","bug_when","work_time", );

$sql_table= "mantis_bugnote_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_table_callback";
$sql_fields = array("id", "bug_id","reporter_id","date_submitted","last_modified","bugnote_text_id",);

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "who":
	case "comment_id":
		$value1 = $value;
		break;
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "work_time":
		$value1 = $row_count;
		break;
	case "thetext":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
| type            | smallint(6)  | NO   |     | 0       |       |
| extra_data      | varchar(255) | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)


mysql> describe mantis_bugnote_text_table;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    |                |
| note  | text             | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "thetext" );

$sql_table= "mantis_bugnote_text_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_text_table_callback";
$sql_fields = array("id", "note");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "comment_id":
		$value1 = $value;
		break;
	case "thetext":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     | NULL    |                |
| cf_customer         | varchar(64)  | NO   |     | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_bug_text_table;
+------------------------+------------------+------+-----+---------+----------------+
| Field                  | Type             | Null | Key | Default | Extra          |
+------------------------+------------------+------+-----+---------+----------------+
| id                     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| description            | text             | NO   |     | NULL    |                |
| steps_to_reproduce     | text             | NO   |     | NULL    |                |
| additional_information | text             | NO   |     | NULL    |                |
+------------------------+------------------+------+-----+---------+----------------+
mysql>

#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","short_desc","resolution","votes");

$sql_table= "mantis_bug_text_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_text_table_callback";
$sql_fields = array("id","description","additional_information","steps_to_reproduce");

// ---

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "short_desc":
	case "resolution":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	case "votes":
	    $value1 = "\"\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_category_table($con) //BUGS CATEGORY
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe components;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| id              | mediumint(9) | NO   | PRI | NULL    | AUTO  |
| name            | varchar(64)  | NO   | MUL | NULL    |       |
| product_id      | smallint(6)  | NO   | MUL | NULL    |       |
| initialowner    | mediumint(9) | NO   | MUL | NULL    |       |
| initialqacontact| mediumint(9) | YES  | MUL | NULL    |       |
| description     | mediumtext   | NO   |     | NULL    |       |
| isactive        | tinyint(4)   | NO   |     | 1       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)


mysql> describe mantis_category_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    |  AUTO          |
| project_id  | int(10) unsigned | NO   | MUL | 0       |                |
| user_id     | int(10) unsigned | NO   |     | 0       |                |
| name        | varchar(128)     | NO   |     |         |                |
| status      | int(10) unsigned | NO   |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)


#####

*/

$sql_table_source= "components";
$sql_fields_source = array("id", "name" , "product_id", "isactive");

$sql_table= "mantis_category_table";
$callback_function = "bz2m_migration_mantis_category_table_callback";
$sql_fields = array("id", "name", "project_id", "status");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_category_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write category

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "id":
	case "product_id":
	case "isactive":
		$value1 = $value;
		break;
	case "name":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_bug_file_table($con) //BUGS ATTACHMENTS
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe attachments;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| attach_id         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| bug_id            | mediumint(9) | NO   | MUL | NULL    |                |
| creation_ts       | datetime     | NO   | MUL | NULL    |                |
| modification_time | datetime     | NO   | MUL | NULL    |                |
| description       | tinytext     | NO   |     | NULL    |                |
| mimetype          | tinytext     | NO   |     | NULL    |                |
| ispatch           | tinyint(4)   | NO   |     | 0       |                |
| filename          | varchar(255) | NO   |     | NULL    |                |
| submitter_id      | mediumint(9) | NO   | MUL | NULL    |                |
| isobsolete        | tinyint(4)   | NO   |     | 0       |                |
| isprivate         | tinyint(4)   | NO   |     | 0       |                |
+-------------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
mysql> describe attach_data;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | mediumint(9) | NO   | PRI | NULL    |       |
| thedata | longblob     | NO   |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)



mysql> describe mantis_bug_file_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| bug_id      | int(10) unsigned | NO   | MUL | 0       |                |
| title       | varchar(250)     | NO   |     |         |                |
| description | varchar(250)     | NO   |     |         |                |
| diskfile    | varchar(250)     | NO   | MUL |         |                |
| filename    | varchar(250)     | NO   |     |         |                |
| folder      | varchar(250)     | NO   |     |         |                |
| filesize    | int(11)          | NO   |     | 0       |                |
| file_type   | varchar(250)     | NO   |     |         |                |
| content     | longblob         | YES  |     | NULL    |                |
| date_added  | int(10) unsigned | NO   |     | 1       |                |
| user_id     | int(10) unsigned | NO   |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)


#####

*/

$sql_table_source= "attachments";
$sql_fields_source = array("attach_id", "bug_id" , "creation_ts", "filename", "description", "mimetype", "submitter_id", "thedata");
$join = "INNER JOIN attach_data ON attach_data.id = attachments.attach_id";
//$limit = "LIMIT 10";

$sql_table= "mantis_bug_file_table";
$callback_function = "bz2m_migration_mantis_bug_file_table_callback";
$sql_fields = array("id", "bug_id", "date_added", "filename", "description", "file_type", "user_id", "content");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total, $join, $limit );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_bug_file_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write category

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "attach_id":
	case "bug_id":
	case "submitter_id":
		$value1 = $value;
		break;
	case "filename":
	case "description":
	case "mimetype":
		$value1 = "'".str_replace("'", "''", $value)."'";
	break;
	
	case "thedata":
		$value1 = "'".mysql_escape_string($value)."'";
	break;
	case "creation_ts":
		$value1 = time($value);
	break;
	
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
		
	
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_customclient_table($con) //BUGS CUSTOMCLIENT
{

/*
BUGS DESCRIPTION
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     |         |                |
| cf_customer         | varchar(64)  | NO   |     |         |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_custom_field_string_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| field_id  | int(11) unsigned   | NO   | PRI | 0       |  AUTO          |
| bug_id  	| int(11) unsigned   | NO   | MUL | 0       |                |
| value     | varchar(255)       | NO   |     |         |                |
| text      | int(10) unsigned   | YES  |     |         |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array("cf_client", "bug_id", "cf_customer");

$sql_table= "mantis_custom_field_string_table";
$callback_function = "bz2m_migration_mantis_customclient_table_callback";
$sql_fields = array("field_id", "bug_id", "value");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_customclient_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write customer

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "cf_client":
		$value1 = 1;
		break;
	case "bug_id":
		$value1 = $value;
		break;
	case "cf_customer":
		if ($value == '---')
			$value1 = "\"\"";
		else
			$value1 = "\"|".htmlentities($value, ENT_HTML5, 'utf-8')."|\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

{ // BUGZILLA - MAIN

//echo "/* NB : FIRST CREATE ALL YOUR USER ACCOUNT IN MANTIS WITH THE CORRESPONDING ID FROM BUGZILLA */";
//echo "<br>";

echo "<html lang='de' xmlns='http://www.w3.org/1999/xhtml'>
	<head>
		<title>Migration bugzilla to Mantis</title>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		<meta http-equiv='Content-Language' content='de-de' />
    </head>
    <body>";

bz2m_connect($mysqli);


//bz2m_migration_profiles2mantis_user_table($mysqli);

//bz2m_migration_products2mantis_project_table($mysqli);

//bz2m_migration_bugs2mantis_bug_table($mysqli);
//bz2m_migration_bugs2mantis_bug_text_table($mysqli);

//bz2m_migration_bugs_activity2mantis_bug_history_table($mysqli);

//bz2m_migration_longdescs2mantis_bugnote_table($mysqli);

//bz2m_migration_longdescs2mantis_bugnote_text_table($mysqli);

//bz2m_migration_mantis_category_table($mysqli);

//bz2m_migration_mantis_customclient_table($mysqli);

bz2m_migration_mantis_bug_file_table($mysqli);

echo "</body>
        </html>";
$mysqli->close();

/*********
// PHP version before 5.6.16
bz2m_migration_profiles2mantis_user_table();

bz2m_migration_products2mantis_project_table();

bz2m_migration_bugs2mantis_bug_table();
bz2m_migration_bugs2mantis_bug_text_table();

bz2m_migration_bugs_activity2mantis_bug_history_table();

bz2m_migration_longdescs2mantis_bugnote_table();
bz2m_migration_longdescs2mantis_bugnote_text_table();

mysql_close( $link);
*********/
}


?>

bugzilla2mantis-2.php (59,536 bytes)   
christoph.suess

christoph.suess

2017-12-07 05:46

reporter   ~0058339

Latest Version.
Forgot to add one important bugfix :)

bugzilla2mantis-3.php (59,626 bytes)   
<?php
header('Content-type: text/plain; charset=utf-8');

ini_set("memory_limit", "64G");
ini_set("max_execution_time", "86400");
/*
// ------------------------------------------------------------------------------------------
// LICENSE
// ------------------------------------------------------------------------------------------

        # bugzilla2mantis.php - Development Status "Alpha" 
        # Convert bugzilla 5.0.3 database to mantis 1.3.3 sql data inserts
        #
        # Copyright (C) 2006  Agusti Fontquerni afontquerni -at- gmail -dot- com
		# Copyright (C) 2017  Christoph Suess <christoph -dot- suess -at- gmail -dot- com (for the attachment migration and utf-8 support)
        # This program is distributed under the terms and conditions 
        # of the GNU GENERAL PUBLIC LICENSE Version 2 (http://www.gnu.org/copyleft/gpl.html)

// ------------------------------------------------------------------------------------------
// README
// ------------------------------------------------------------------------------------------
On Set-2006 I was looking for Bugzilla to Mantis (Perl) by Cris Daniluk (http://www.cadencetools.org/projects/bugzilla2mantis/)     

But Nobody didn't find any thing on the web, all web page links are broken or deleted (http://freshmeat.net/redir/bugzilla2mantis/52873/url_tgz/bugzilla2mantis-0.2.tar.gz) ((http://forums.mantisbugtracker.com/viewtopic.php?p=5745&sid=1a679a9fa211306bfd51eb33f7da2579))    

At last, I decided to start my first php script to convert bugs data from bugzilla to mantis (bugzilla2mantis.php).  It is alfa script but its did basic and functional great job.  
I give mantis2bugzilla.php to mantis community to improve bugzilla migration feature

// ------------------------------------------------------------------------------------------
// INSTALL
// ------------------------------------------------------------------------------------------
1) Backup all databases
2) Copy this file to bugzilla web site ( example: /var/www/bugzilla )
3) Modify database parameters "server", "user", "password", "database" ( 'localhost','root','', 'bugzilla' ) in "bz2m_connect()" function 
4) Open "bugzilla2mantis.php" with web browser ( example: localhost:/bugzilla/bugzilla2mantis.php )
5) Check output web page
6) Select all (Ctrl+A). Copy from web browser to test editor. Save to file ( example: mantis.sql )
7) Install mantis website ( http://www.mantisbugtracker.com/ )
8) Modify mantis database from bugzilla data "bugzilla2mantis.php" ( example: mysql -u root bugtracker < mantis.sql )
9) Now, report bugs with mantis ... 

// ------------------------------------------------------------------------------------------
// CHANGELOG
// ------------------------------------------------------------------------------------------
2016.11.21 - Adaptation for PHP 5.6.16, bugzilla version 5.0.3 and Mantis BT version 1.3.3 - french version (groques)
2006.09.24 - Clean code and Documentation (afontquerni)
2006.09.16 - Resolve some major bugs (afontquerni)
2006.09.15 - Add bugnote_table and bugnote_text_table  (afontquerni)
2006.09.14 - Initial release (afontquerni)

// ------------------------------------------------------------------------------------------
// DESCRIPTION STRUCTURE - DOCUMENTATION
// ------------------------------------------------------------------------------------------

bz2m_connect($link); // !!! MODIFY: "server", "user", "password", "database" ( 'localhost','bugzilla3','pasteyourpass_a little_lower', 'bugzilla3' ) !!

echo "DELETE FROM mantis_user_table;";

bz2m_migration_profiles2mantis_user_table();  //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_products2mantis_project_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value );

bz2m_migration_bugs2mantis_bug_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_bugs_activity2mantis_bug_history_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_longdescs2mantis_bugnote_text_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_mantis_category_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_category_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM

bz2m_migration_mantis_customclient_table(); //CUSTOM
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_customclient_table_callback( $row_count, $flag_first, $key, $value ); //CUSTOM
	  
	  
bz2m_migration_mantis_bug_file_table(); //CUSTOM - TO USE THIS, YOU NEED TO COPY EMPTY mantis_bug_file_table TO YOUR BUGZILLA DATABASE (YOU HAVE TO DUMP AND MOVE THIS TABLE LATER BACK TO MANTIS DB)
 \-> bz2m_select( $sql_fields_source, $sql_table_source, $data, $total, $joins ); //COMMON
 \-> bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function ); //COMMON
      \-> bz2m_migration_mantis_bug_file_table_callback( $row_count, $flag_first, $key, $value ); //COMMON

mysql_close( $link);

*/

// ------------------------------------------------------------------------------------------
//     PHP CODE
// ------------------------------------------------------------------------------------------

function bz2m_connect( &$mysqli ) { // BUGZILLA - connect to database
/*********
// PHP version before 5.6.16
	$link = mysql_connect('localhost', 'myuser', 'mypassword');
	if (!$link)
	{
	  die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("bugs",  $link);
*********/
	$myport = 3306;
	$mysqli = new mysqli('localhost', 'webuser', 'password', 'bugzilla', $myport);
	if ($mysqli->connect_error) {
		die('Erreur de connexion (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
	}

	echo 'Success... ' . $mysqli->host_info . "\n";
} 

// ------------------------------------------------------------------------------------------

function bz2m_select($con, $sql_fields, $sql_table, &$data, &$total, $joins = "", $limit = "") { // BUGZILLA -  read bugs

	$sql="SELECT ";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql." FROM "."`".$sql_table."` " . $joins . " " . $limit;
//	$sql = $sql." FROM "."`".$sql_table."` WHERE `bug_id`<3";
//	$sql = $sql." FROM "."`".$sql_table."`  limit 0 ,3;";

	echo ""; echo $sql;  echo "";
	

	$result = mysqli_query($con, $sql );

	$columns = array();
	$data = array();

	// Table(Rows)
	while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
	{
		foreach ($sql_fields as $key1=> $value1 ) {
			$columns[]= $row[$value1];
			//echo "$value1 $row[$value1] ";
		}
		$data[]= $columns;
		unset($columns);
	}

	$total = mysqli_num_rows($result);
	mysqli_free_result($result);
}

// ------------------------------------------------------------------------------------------

function bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, &$data, $callback_function ) { // MANTIS -  write bugs

	global $mysqli;
	$sql="INSERT INTO `$sql_table` (";

	$flag_first = true;

	foreach ($sql_fields as $key1=> $value1 ) {
		if($flag_first ) {
			$flag_first = false;
			$sql = $sql."`".$value1."`";
		}
		else {
			$sql = $sql.","."`".$value1."`";
		}
	}
	$sql = $sql.") VALUES( ";

	$sql_header = $sql;

	foreach ($data as $key0 => $value0 ) {
		unset($sql);
		$sql = $sql_header;
		$row_count = $key0 + 1;

		$flag_first = true;

		foreach ($value0 as $key1 => $value1 ) {
			$sql.= call_user_func( $callback_function, $row_count, $flag_first, $sql_fields_source[$key1], $value1);
			if($flag_first )
				$flag_first = false;
			//echo ""; echo "$key1 $value1"; echo "";

		}
		$sql.= " );";
		if($sql_table === 'mantis_bug_file_table') {
			$mysqli->query($sql);
			//echo $sql; echo "";
			echo "#";
		} else {
			echo $sql; echo "";
		}
		

		
	}
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table($mysqli) //USERS
{
/*
USERS
#####

mysql> describe profiles;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| userid         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| login_name     | varchar(255) | NO   | UNI | NULL    |                |
| cryptpassword  | varchar(128) | YES  |     | NULL    |                |
| realname       | varchar(255) | YES  |     | NULL    |                |
| disabledtext   | mediumtext   | NO   |     | NULL    |                |
| disable_mail   | tinyint(4)   | NO   |     | 0       |                |
| mybugslink     | tinyint(4)   | NO   |     | 1       |                |
| extern_id      | varchar(64)  | YES  |     | NULL    |                |
| is_enabled 	 | tinyint(4)   | NO   |     | 1 	   |                |
| last_seen_date | datetime     | NO   |     | NULL    |                |
| email          | varchar(255) | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

#####

mysql> describe mantis_user_table;
+-----------------------------+------------------+------+-----+---------------------+----------------+
| Field                       | Type             | Null | Key | Default             | Extra          |
+-----------------------------+------------------+------+-----+---------------------+----------------+
| id                          | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| username                    | varchar(32)      | NO   | UNI | NULL                |                |
| realname                    | varchar(64)      | NO   |     | NULL                |                |
| email                       | varchar(64)      | NO   |     | NULL                |                |
| password                    | varchar(32)      | NO   |     | NULL                |                |
| enabled                     | tinyint(4)       | NO   | MUL | 1                   |                |
| protected                   | tinyint(4)       | NO   |     | 0                   |                |
| access_level                | smallint(6)      | NO   | MUL | 10                  |                |
| login_count                 | int(11)          | NO   |     | 0                   |                |
| lost_password_request_count | smallint(6)      | NO   |     | 0                   |                |
| failed_login_count          | smallint(6)      | NO   |     | 0                   |                |
| cookie_string               | varchar(64)      | NO   | UNI | NULL                |                |
| last_visit                  | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
| date_created                | datetime         | NO   |     | 1970-01-01 00:00:01 |                |
+-----------------------------+------------------+------+-----+---------------------+----------------+
14 rows in set (0.00 sec)

*/


$sql_table_source= "profiles";
$sql_fields_source=array( "userid","login_name","cryptpassword","realname","login_name", "last_seen_date", "cryptpassword" );

$sql_table= "mantis_user_table";
$sql_fields = array("id","username","password","realname","email","date_created", "cookie_string");
$callback_function = "bz2m_migration_profiles2mantis_user_table_callback";

// ------

$data = array();
$total = 0;

bz2m_select($mysqli, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_profiles2mantis_user_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

	$sql = "";
	//echo "$key $value ";

switch ($key)
{
	case "userid":
		$value1 = $value;
		break;
	case "login_name":
	case "cryptpassword":
	case "realname":
	case "refreshed_when":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	case "last_seen_date":
		$value1 = time(value);
		break;
	break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table($mysqli) //PROJECTS
{

/*
PROJECTS
#####
mysql> describe products;
+-------------------+-------------+------+-----+---------+----------------+
| Field             | Type        | Null | Key | Default | Extra          |
+-------------------+-------------+------+-----+---------+----------------+
| id                | smallint(6) | NO   | PRI | NULL    | auto_increment |
| name              | varchar(64) | NO   | UNI | NULL    |                |
| classification_id | smallint(6) | NO   |     | 1       |                |
| description       | mediumtext  | YES  |     | NULL    |                |
| isactive 	        | tinyint(4)  | NO   |     | 1       |                |
| votesperuser      | smallint(6) | NO   |     | NULL    |                |
| maxvotesperbug    | smallint(6) | NO   |     | 10000   |                |
| votestoconfirm    | smallint(6) | NO   |     | NULL    |                |
| defaultmilestone  | varchar(20) | NO   |     | ---     |                |
| allows_unconfirmed| tinyint(4)  | NO   |     | 1       |                |
+-------------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> 

#####

mysql> describe mantis_project_table;
+----------------+------------------+------+-----+---------+----------------+
| Field          | Type             | Null | Key | Default | Extra          |
+----------------+------------------+------+-----+---------+----------------+
| id             | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name           | varchar(128)     | NO   | UNI | NULL    |                |
| status         | smallint(6)      | NO   |     | 10      |                |
| enabled        | tinyint(4)       | NO   |     | 1       |                |
| view_state     | smallint(6)      | NO   | MUL | 10      |                |
| access_min     | smallint(6)      | NO   |     | 10      |                |
| file_path      | varchar(250)     | NO   |     | NULL    |                |
| description    | text             | NO   |     | NULL    |                |
| category_id    | int(10) unsigned | NO   |     | 1       |                |
| inherit_global | tinyint(4)       | NO   |     | 0       |                |
+----------------+------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)

*/

$sql_table_source= "products";
$sql_fields_source= array( "id","name","description","votesperuser","votestoconfirm" );

$sql_table= "mantis_project_table";
$callback_function = "bz2m_migration_products2mantis_project_table_callback";
$sql_fields = array("id","name","description", "status", "enabled");


// ----

$data = array();
$total = 0;

bz2m_select($mysqli, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_products2mantis_project_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "id":
		$value1 = $value;
		break;
	case "name":
	case "description":
		$value1 = "'".str_replace("'", "''", htmlentities($value, ENT_HTML5, 'utf-8'))."'";
		break;
	case "votesperuser":
		$value1 = 10;
		break;
	case "votestoconfirm":
		$value1 = 1;
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table($con) //BUGS
{

/*
BUGS
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     |         |                |
| cf_customer         | varchar(64)  | NO   |     |         |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


#####
mysql> describe mantis_bug_table;
+-------------------+------------------+------+-----+---------------------+----------------+
| Field             | Type             | Null | Key | Default             | Extra          |
+-------------------+------------------+------+-----+---------------------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| project_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id       | int(10) unsigned | NO   |     | 0                   |                |
| handler_id        | int(10) unsigned | NO   |     | 0                   |                |
| duplicate_id      | int(10) unsigned | NO   |     | 0                   |                |
| priority          | smallint(6)      | NO   |     | 30                  |                |
| severity          | smallint(6)      | NO   |     | 50                  |                |
| reproducibility   | smallint(6)      | NO   |     | 10                  |                |
| status            | smallint(6)      | NO   | MUL | 10                  |                |
| resolution        | smallint(6)      | NO   |     | 10                  |                |
| projection        | smallint(6)      | NO   |     | 10                  |                |
| eta               | smallint(6)      | NO   |     | 10                  |                |
| bug_text_id       | int(10) unsigned | NO   |     | 0                   |                |
| os                | varchar(32)      | NO   |     | NULL                |                |
| os_build          | varchar(32)      | NO   |     | NULL                |                |
| platform          | varchar(32)      | NO   |     | NULL                |                |
| version           | varchar(64)      | NO   |     | NULL                |                |
| fixed_in_version  | varchar(64)      | NO   | MUL | NULL                |                |
| build             | varchar(32)      | NO   |     | NULL                |                |
| profile_id        | int(10) unsigned | NO   |     | 0                   |                |
| view_state        | smallint(6)      | NO   |     | 10                  |                |
| summary           | varchar(128)     | NO   |     | NULL                |                |
| sponsorship_total | int(11)          | NO   | MUL | 0                   |                |
| sticky            | tinyint(4)       | NO   |     | 0                   |                |
| category_id       | int(10) unsigned | NO   |     | 1                   |                |
| date_submitted    | int(10) unsigned | NO   |     | 1                   |                |
| due_date          | int(10) unsigned | NO   |     | 1                   |                |
| last_updated      | int(10) unsigned | NO   |     | 1                   |                |
+-------------------+------------------+------+-----+---------------------+----------------+
29 rows in set (0.00 sec)

mysql>
*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","assigned_to","bug_severity","bug_status","product_id","creation_ts","creation_ts","priority","reporter", "short_desc", "bug_id" , "component_id","resolution","version");

$sql_table= "mantis_bug_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_table_callback";
$sql_fields = array("id","handler_id","severity","status","project_id","date_submitted","last_updated","priority","reporter_id", "summary", "bug_text_id", "category_id","resolution","version" );

// ---

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";
/*
Bugzilla
Bug, Evolution, Param�trage, Documentation, Savoir

Bas, Haut, ND, Normal, TBas, THaut

Mantis
$s_severity_enum_string = '10:Feature-Wunsch,20:Trivial,30:Fehler im Text,40:Unsch�nheit,50:kleinerer Fehler,60:schwerer Fehler,70:Absturz,80:Blocker'
$s_status_enum_string = '10:neu,20:R�ckmeldung,30:anerkannt,40:best�tigt,50:zugewiesen,80:erledigt,90:geschlossen';
$s_resolution_enum_string = '10:offen,20:erledigt,30:wiederer�ffnet,40:nicht reproduzierbar,50:unl�sbar,60:doppelt,70:keine �nderung notwendig,80:aufgeschoben,90:wird nicht behoben';
$s_priority_enum_string = '10:keine,20:niedrig,30:normal,40:hoch,50:dringend,60:sofort';
*/
$bug_severity_map = array( "enhancement"=>10, "trivial"=>20, "Documentation"=>30, "minor"=>40, "normal"=>50, "major"=>60, "critical"=>70, "blocker"=> 80 );
$bug_status_map = array( "UNCONFIRMED"=>10, "NEW"=>10, "IN PROGRESS"=>50, "REOPENED"=>20, "RESOLVED"=>80, "CLOSED"=>90, "WAITING FOR REPLY"=>20, "CONFIRMED"=>40 );
$bug_resolution_map = array( ""=>10, "FIXED"=>20, "INVALID"=>50, "REJECTED"=>90, "DUPLICATE"=>60, "WORKS FOR ME"=>70, "REVIEW/TESTING"=>20, "READY TO DEPLOY"=>20, "DONE/LIVE"=>20, "FREEZE"=>80, "APPROVED"=>10, "NOT REPRODUCABLE"=>40 );
$bug_priority_map = array( "P5"=>10, "P4"=>20, "P3"=>30, "P2"=>40, "P1"=>60 );

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "assigned_to":
	case "reporter":
	case "product_id":
	case "component_id":
		$value1 = $value;
		break;
	case "short_desc":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value1 = "\"".$value."\"";
		break;
	case "version":
		$value = str_replace("\"","'",$value );
		$value1 = "\"".$value."\"";
		break;
	case "creation_ts":
		$value1 = strtotime( $value );
		break;

	case "bug_severity":
		if( isset($bug_severity_map[$value]) ) {
			$value1 = $bug_severity_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "bug_status":
		if( isset($bug_status_map[$value]) ) {
			$value1 = $bug_status_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	case "priority":
		if( isset($bug_priority_map[$value]) ) {
			$value1 = $bug_priority_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;

	case "resolution":
		if( isset($bug_resolution_map[$value]) ) {
			$value1 = $bug_resolution_map[$value];
		}
		else {
			$value1 = "VALUE_ERROR_".$value."_VALUE_ERROR";
		}
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table($con) //BUGS HISTORY
{

/*
BUGS HISTORY
#####
mysql> describe bugs_activity;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| bug_id    | mediumint(9) | NO   | MUL | NULL    |       |
| attach_id | mediumint(9) | YES  |     | NULL    |       |
| who       | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when  | datetime     | NO   | MUL | NULL    |       |
| fieldid   | mediumint(9) | NO   | MUL | NULL    |       |
| added     | tinytext     | YES  |     | NULL    |       |
| removed   | tinytext     | YES  |     | NULL    |       |
| comment_id| int(11)      | YES  |     | NULL    |       |
| id        | int(11)      | NO   |     | AUTO    |       |
+-----------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

#####

mysql> describe mantis_bug_history_table;
+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| user_id       | int(10) unsigned | NO   | MUL | 0                   |                |
| bug_id        | int(10) unsigned | NO   | MUL | 0                   |                |
| field_name    | varchar(32)      | NO   |     | NULL                |                |
| old_value     | varchar(128)     | NO   |     | NULL                |                |
| new_value     | varchar(128)     | NO   |     | NULL                |                |
| type          | smallint(6)      | NO   |     | 0                   |                |
| date_modified | int              | NO   |     | NULL                |                |
+---------------+------------------+------+-----+---------------------+----------------+
8 rows in set (0.00 sec)

*/

$sql_table_source= "bugs_activity";
$sql_fields_source = array( "bug_id","who","bug_when","fieldid","added","removed" );

$sql_table= "mantis_bug_history_table";
$callback_function = "bz2m_migration_bugs_activity2mantis_bug_history_table_callback";
$sql_fields = array("bug_id","user_id","date_modified","field_name","old_value","new_value");

// ----

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs_activity2mantis_bug_history_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "who":
		$value1 = $value;
		break;	    
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "fieldid":
	case "added":
	case "removed":
		$value1 = "'".str_replace("'", "''", $value)."'";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
| type            | smallint(6)  | NO   |     | 0       |       |
| extra_data      | varchar(255) | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

#####

mysql> describe mantis_bugnote_table;
+-----------------+------------------+------+-----+---------------------+----------------+
| Field           | Type             | Null | Key | Default             | Extra          |
+-----------------+------------------+------+-----+---------------------+----------------+
| id              | int(10) unsigned | NO   | PRI | NULL                |                |
| bug_id          | int(10) unsigned | NO   | MUL | 0                   |                |
| reporter_id     | int(10) unsigned | NO   |     | 0                   |                |
| bugnote_text_id | int(10) unsigned | NO   |     | 0                   |                |
| view_state      | smallint(6)      | NO   |     | 10                  |                |
| note_type       | int(11)          | YES  |     | 0                   |                |
| note_attr       | varchar(250)     | YES  |     | NULL                |                |
| time_tracking   | int(10) unsigned | NO   |     | 0                   |                |
| last_modified   | int(10) unsigned | NO   | MUL | 1                   |                |
| date_submitted  | int(10) unsigned | NO   |     | 1                   |                |
+-----------------+------------------+------+-----+---------------------+----------------+
10 rows in set (0.01 sec)

mysql>

#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "bug_id","who","bug_when","bug_when","work_time", );

$sql_table= "mantis_bugnote_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_table_callback";
$sql_fields = array("id", "bug_id","reporter_id","date_submitted","last_modified","bugnote_text_id",);

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "who":
	case "comment_id":
		$value1 = $value;
		break;
	case "bug_when":
		$value1 = strtotime( $value );
		break;
	case "work_time":
		$value1 = $row_count;
		break;
	case "thetext":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe longdescs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| comment_id      | mediumint(9) | NO   | MUL | NULL    |       |
| bug_id          | mediumint(9) | NO   | MUL | NULL    |       |
| who             | mediumint(9) | NO   | MUL | NULL    |       |
| bug_when        | datetime     | NO   | MUL | NULL    |       |
| work_time       | decimal(5,2) | NO   |     | 0.00    |       |
| thetext         | mediumtext   | YES  | MUL | NULL    |       |
| isprivate       | tinyint(4)   | NO   |     | 0       |       |
| already_wrapped | tinyint(4)   | NO   |     | 0       |       |
| type            | smallint(6)  | NO   |     | 0       |       |
| extra_data      | varchar(255) | NO   |     | 0       |       |
+-----------------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)


mysql> describe mantis_bugnote_text_table;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    |                |
| note  | text             | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)


#####

*/

$sql_table_source= "longdescs";
$sql_fields_source = array("comment_id", "thetext" );

$sql_table= "mantis_bugnote_text_table";
$callback_function = "bz2m_migration_longdescs2mantis_bugnote_text_table_callback";
$sql_fields = array("id", "note");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_longdescs2mantis_bugnote_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
	case "comment_id":
		$value1 = $value;
		break;
	case "thetext":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table($con) //BUGS DESCRIPTION
{

/*
BUGS DESCRIPTION
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     | NULL    |                |
| cf_customer         | varchar(64)  | NO   |     | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_bug_text_table;
+------------------------+------------------+------+-----+---------+----------------+
| Field                  | Type             | Null | Key | Default | Extra          |
+------------------------+------------------+------+-----+---------+----------------+
| id                     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| description            | text             | NO   |     | NULL    |                |
| steps_to_reproduce     | text             | NO   |     | NULL    |                |
| additional_information | text             | NO   |     | NULL    |                |
+------------------------+------------------+------+-----+---------+----------------+
mysql>

#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array( "bug_id","short_desc","resolution","votes");

$sql_table= "mantis_bug_text_table";
$callback_function = "bz2m_migration_bugs2mantis_bug_text_table_callback";
$sql_fields = array("id","description","additional_information","steps_to_reproduce");

// ---

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_bugs2mantis_bug_text_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write bugs

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "bug_id":
		$value1 = $value;
		break;
	case "short_desc":
	case "resolution":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	case "votes":
	    $value1 = "\"\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_category_table($con) //BUGS CATEGORY
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe components;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| id              | mediumint(9) | NO   | PRI | NULL    | AUTO  |
| name            | varchar(64)  | NO   | MUL | NULL    |       |
| product_id      | smallint(6)  | NO   | MUL | NULL    |       |
| initialowner    | mediumint(9) | NO   | MUL | NULL    |       |
| initialqacontact| mediumint(9) | YES  | MUL | NULL    |       |
| description     | mediumtext   | NO   |     | NULL    |       |
| isactive        | tinyint(4)   | NO   |     | 1       |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)


mysql> describe mantis_category_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    |  AUTO          |
| project_id  | int(10) unsigned | NO   | MUL | 0       |                |
| user_id     | int(10) unsigned | NO   |     | 0       |                |
| name        | varchar(128)     | NO   |     |         |                |
| status      | int(10) unsigned | NO   |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)


#####

*/

$sql_table_source= "components";
$sql_fields_source = array("id", "name" , "product_id", "isactive");

$sql_table= "mantis_category_table";
$callback_function = "bz2m_migration_mantis_category_table_callback";
$sql_fields = array("id", "name", "project_id", "status");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_category_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write category

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "id":
	case "product_id":
	case "isactive":
		$value1 = $value;
		break;
	case "name":
		$value = str_replace("\"","'",htmlentities($value, ENT_HTML5, 'utf-8') );
		$value = str_replace("\\","\\\\",$value );
		$value = str_replace("\n","",$value );
		$value1 = "\"".$value."\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}
// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_bug_file_table($con) //BUGS ATTACHMENTS
{

/*
BUGS DESCRIPTION
#####
                                       |
mysql> describe attachments;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| attach_id         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| bug_id            | mediumint(9) | NO   | MUL | NULL    |                |
| creation_ts       | datetime     | NO   | MUL | NULL    |                |
| modification_time | datetime     | NO   | MUL | NULL    |                |
| description       | tinytext     | NO   |     | NULL    |                |
| mimetype          | tinytext     | NO   |     | NULL    |                |
| ispatch           | tinyint(4)   | NO   |     | 0       |                |
| filename          | varchar(255) | NO   |     | NULL    |                |
| submitter_id      | mediumint(9) | NO   | MUL | NULL    |                |
| isobsolete        | tinyint(4)   | NO   |     | 0       |                |
| isprivate         | tinyint(4)   | NO   |     | 0       |                |
+-------------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
mysql> describe attach_data;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | mediumint(9) | NO   | PRI | NULL    |       |
| thedata | longblob     | NO   |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)



mysql> describe mantis_bug_file_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| bug_id      | int(10) unsigned | NO   | MUL | 0       |                |
| title       | varchar(250)     | NO   |     |         |                |
| description | varchar(250)     | NO   |     |         |                |
| diskfile    | varchar(250)     | NO   | MUL |         |                |
| filename    | varchar(250)     | NO   |     |         |                |
| folder      | varchar(250)     | NO   |     |         |                |
| filesize    | int(11)          | NO   |     | 0       |                |
| file_type   | varchar(250)     | NO   |     |         |                |
| content     | longblob         | YES  |     | NULL    |                |
| date_added  | int(10) unsigned | NO   |     | 1       |                |
| user_id     | int(10) unsigned | NO   |     | 0       |                |
+-------------+------------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)


#####

*/

$sql_table_source= "attachments";
$sql_fields_source = array("attach_id", "bug_id" , "creation_ts", "filename", "description", "mimetype", "submitter_id", "thedata");
$join = "INNER JOIN attach_data ON attach_data.id = attachments.attach_id";
//$limit = "LIMIT 10";

$sql_table= "mantis_bug_file_table";
$callback_function = "bz2m_migration_mantis_bug_file_table_callback";
$sql_fields = array("id", "bug_id", "date_added", "filename", "description", "file_type", "user_id", "content");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total, $join, $limit );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_bug_file_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write category

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "attach_id":
	case "bug_id":
	case "submitter_id":
		$value1 = $value;
		break;
	case "filename":
	case "description":
	case "mimetype":
		$value1 = "'".str_replace("'", "''", $value)."'";
	break;
	
	case "thedata":
		$value1 = "'".mysql_escape_string($value)."'";
	break;
	case "creation_ts":
		$value1 = time($value);
	break;
	
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
		
	
	return $sql;
}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_customclient_table($con) //BUGS CUSTOMCLIENT
{

/*
BUGS DESCRIPTION
#####
mysql> describe bugs;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| bug_id              | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| assigned_to         | mediumint(9) | NO   | MUL | NULL    |                |
| bug_file_loc        | text         | YES  |     | NULL    |                |
| bug_severity        | varchar(64)  | NO   | MUL | NULL    |                |
| bug_status          | varchar(64)  | NO   | MUL | NULL    |                |
| creation_ts         | datetime     | YES  | MUL | NULL    |                |
| delta_ts            | datetime     | NO   | MUL | NULL    |                |
| short_desc          | mediumtext   | NO   | MUL | NULL    |                |
| op_sys              | varchar(64)  | NO   | MUL | NULL    |                |
| priority            | varchar(64)  | NO   | MUL | NULL    |                |
| product_id          | smallint(6)  | NO   | MUL | NULL    |                |
| rep_platform        | varchar(64)  | NO   |     | NULL    |                |
| reporter            | mediumint(9) | NO   | MUL | NULL    |                |
| version             | varchar(64)  | NO   | MUL | NULL    |                |
| component_id        | smallint(6)  | NO   | MUL | NULL    |                |
| resolution          | varchar(64)  | NO   | MUL | NULL    |                |
| target_milestone    | varchar(20)  | NO   | MUL | ---     |                |
| qa_contact          | mediumint(9) | YES  | MUL | NULL    |                |
| status_whiteboard   | mediumtext   | NO   |     | NULL    |                |
| votes               | mediumint(9) | NO   | MUL | 0       |                |
| lastdiffed          | datetime     | YES  |     | NULL    |                |
| everconfirmed       | tinyint(4)   | NO   |     | NULL    |                |
| reporter_accessible | tinyint(4)   | NO   |     | 1       |                |
| cclist_accessible   | tinyint(4)   | NO   |     | 1       |                |
| estimated_time      | decimal(5,2) | NO   |     | 0.00    |                |
| remaining_time      | decimal(5,2) | NO   |     | 0.00    |                |
| deadline            | datetime     | YES  |     | NULL    |                |
| cf_client           | varchar(255) | NO   |     |         |                |
| cf_customer         | varchar(64)  | NO   |     |         |                |
+---------------------+--------------+------+-----+---------+----------------+
29 rows in set (0.00 sec)


mysql> describe mantis_custom_field_string_table;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| field_id  | int(11) unsigned   | NO   | PRI | 0       |  AUTO          |
| bug_id  	| int(11) unsigned   | NO   | MUL | 0       |                |
| value     | varchar(255)       | NO   |     |         |                |
| text      | int(10) unsigned   | YES  |     |         |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


#####

*/

$sql_table_source= "bugs";
$sql_fields_source = array("cf_client", "bug_id", "cf_customer");

$sql_table= "mantis_custom_field_string_table";
$callback_function = "bz2m_migration_mantis_customclient_table_callback";
$sql_fields = array("field_id", "bug_id", "value");

// ---------

$data = array();
$total = 0;

bz2m_select($con, $sql_fields_source, $sql_table_source, $data, $total );
//echo ""; echo "total:$total"; echo "";
bz2m_migration( $sql_fields_source, $sql_fields, $sql_table, $data, $callback_function );

}

// ------------------------------------------------------------------------------------------

function bz2m_migration_mantis_customclient_table_callback( $row_count, $flag_first, $key, $value ) { // MANTIS -  write customer

$sql = "";

	//echo "$key $value ";

switch ($key)
{
	case "cf_client":
		$value1 = 1;
		break;
	case "bug_id":
		$value1 = $value;
		break;
	case "cf_customer":
		if ($value == '---')
			$value1 = "\"\"";
		else
			$value1 = "\"|".htmlentities($value, ENT_HTML5, 'utf-8')."|\"";
		break;
	default:
		$value1 = "DEFAULT_".$key."=".$value."_DEFAULT";
		break;
}


	if( isset($value1)  ) {

		if($flag_first ) {
			$sql = $value1;
		}
		else {
			$sql = ",".$value1;
		}
	}

		//echo ""; echo $sql;
	return $sql;
}

// ------------------------------------------------------------------------------------------

{ // BUGZILLA - MAIN

//echo "/* NB : FIRST CREATE ALL YOUR USER ACCOUNT IN MANTIS WITH THE CORRESPONDING ID FROM BUGZILLA */";
//echo "<br>";

echo "<html lang='de' xmlns='http://www.w3.org/1999/xhtml'>
	<head>
		<title>Migration bugzilla to Mantis</title>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		<meta http-equiv='Content-Language' content='de-de' />
    </head>
    <body>";

bz2m_connect($mysqli);


//bz2m_migration_profiles2mantis_user_table($mysqli);

//bz2m_migration_products2mantis_project_table($mysqli);

//bz2m_migration_bugs2mantis_bug_table($mysqli);
//bz2m_migration_bugs2mantis_bug_text_table($mysqli);

//bz2m_migration_bugs_activity2mantis_bug_history_table($mysqli);

//bz2m_migration_longdescs2mantis_bugnote_table($mysqli);

//bz2m_migration_longdescs2mantis_bugnote_text_table($mysqli);

//bz2m_migration_mantis_category_table($mysqli);

//bz2m_migration_mantis_customclient_table($mysqli);

bz2m_migration_mantis_bug_file_table($mysqli);
mysqli_query($mysqli, "update mantis_bug_file_table set filesize = LENGTH(content);");


echo "</body>
        </html>";
$mysqli->close();

/*********
// PHP version before 5.6.16
bz2m_migration_profiles2mantis_user_table();

bz2m_migration_products2mantis_project_table();

bz2m_migration_bugs2mantis_bug_table();
bz2m_migration_bugs2mantis_bug_text_table();

bz2m_migration_bugs_activity2mantis_bug_history_table();

bz2m_migration_longdescs2mantis_bugnote_table();
bz2m_migration_longdescs2mantis_bugnote_text_table();

mysql_close( $link);
*********/
}


?>

bugzilla2mantis-3.php (59,626 bytes)   
groques

groques

2017-12-07 06:06

reporter   ~0058342

Thanks !
But a lot of code is always commented is it good ? (lines 1461-1476)

I corrected it but the map arrays in the procedure "bz2m_migration_bugs2mantis_bug_table_callback" need to be check

christoph.suess

christoph.suess

2017-12-07 06:30

reporter   ~0058344

Oh, well 1461-1476 are commented out because i ran each command once at a time.
I changed all mappings (severity, priority, status, resolution) in order to fit my bugzilla settings, so everybody has to feel free to change that to his/her own mappings. :-)

groques

groques

2017-12-07 07:52

reporter   ~0058348

:D you're right .

tperrindell

tperrindell

2023-07-21 04:17

reporter   ~0067898

Hello there,

I'm trying to migrate an existing bugzilla installation to mantisbt, and this script seems like the go-to plan, but I am hitting a bit of roadblocks, and I'm not sure on how to address them.

Would anyone be able to help with a more recent version?

Thanks

dregad

dregad

2023-07-24 04:14

developer   ~0067903

This script is maintained by the community, and not supported by the MantisBT team.

I'm therefore resolving this issue, as there are no plans to change that.

@tperrindell your best bet is probably to fix the script yourself. If you end up doing this, it would be nice if you could upload the new version here for the benefit of other users. Should you need some help, I suggest you ask questions on the forums (https://mantisbt.org/forums).