<?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);

}


?>

