<?xml version="1.0"?>
<importer xmlns="http://www.elkarte.net/xml/importer" xmlns:smf="http://www.elkarte.net/">
	<general>
		<name>phpBB3</name>
		<version>Wedge 0.1</version>
		<className>phpBB3</className>
	</general>
	<step type="mandatory">
		<title>Importing Members</title>
		<detect>{$from_prefix}users</detect>
		<destination>{$to_prefix}members</destination>
		<presql>
			TRUNCATE {$to_prefix}members;
		</presql>
		<preparsecode>
			$row['signature'] = phpbb_replace_bbc($row['signature']);
			$row['signature'] = str_replace($row['tmp_bbc_replace'], '', $row['signature']);
			unset($row['tmp_bbc_replace']);
		</preparsecode>
		<query>
			SELECT
				u.user_id AS id_member, SUBSTRING(u.username, 1, 80) AS member_name,
				SUBSTRING(u.username, 1, 255) AS real_name,
				SUBSTRING(u.user_password, 1, 64) AS passwd, u.user_lastvisit AS last_login,
				u.user_regdate AS date_registered, '' AS buddy_list,
				SUBSTRING(u.user_from, 1, 255) AS location,
				u.user_posts AS posts, IF(u.user_rank = 1, 1, IFNULL(mg.id_group, 0)) AS id_group,
				u.user_new_privmsg AS instant_messages, '' AS message_labels,
				SUBSTRING(u.user_email, 1, 255) AS email_address,
				u.user_unread_privmsg AS unread_messages, '' AS ignore_boards,
				SUBSTRING(u.user_website, 1, 255) AS website_title,
				SUBSTRING(u.user_website, 1, 255) AS website_url,
				u.user_allow_viewonline AS show_online, u.user_timezone AS time_offset,
				IF(u.user_allow_viewemail = 1, 0, 1) AS hide_email, u.user_avatar AS avatar,
				u.user_sig AS signature, u.user_sig_bbcode_uid AS tmp_bbc_replace,
				u.user_notify_pm AS pm_email_notify,
				CASE u.user_inactive_reason WHEN 0 THEN 1 ELSE 0 END AS is_activated,
				u.user_ip AS member_ip
			FROM {$from_prefix}users AS u
				LEFT JOIN {$from_prefix}ranks AS r ON (r.rank_id = u.user_rank AND r.rank_special = 1)
				LEFT JOIN {$to_prefix}membergroups AS mg ON (mg.group_name = CONCAT('phpBB ', r.rank_title))
			WHERE u.group_id NOT IN (1, 6)
			GROUP BY u.user_id;
		</query>
	</step>
	<step>
		<title>Importing Categories</title>
		<detect>{$from_prefix}forums</detect>
		<destination>{$to_prefix}categories</destination>
		<presql>TRUNCATE {$to_prefix}categories;</presql>
		<query>
			SELECT
				forum_id AS id_cat, SUBSTRING(forum_name, 1, 255) AS name, left_id AS cat_order
			FROM {$from_prefix}forums
			WHERE forum_type = 0;
		</query>
	</step>
	<step>
		<title>Importing Boards</title>
		<detect>{$from_prefix}forums</detect>
		<destination>{$to_prefix}boards</destination>
		<presql>
			TRUNCATE {$to_prefix}boards;
			DELETE FROM {$to_prefix}board_permissions WHERE id_profile > 4;
		</presql>
		<preparsecode>
			$row['url'] = substr($this->config->boardurl, strpos($this->config->boardurl, '://') + 3) . '/board' . $row['url'];
			$row['urllen'] = strlen($row['url']);

			if (!empty($row['member_groups']))
			{
				$permissions = explode(',', $row['member_groups']);
				foreach ($permissions as $allowed)
					$db->query("
						REPLACE INTO {$to_prefix}board_groups
							(id_group, id_board, view_perm, enter_perm)
						VALUES ('$allowed', $row[id_board], 'allow', 'allow')
					");
			}
		</preparsecode>
		<query>
			SELECT
				f.forum_id AS id_board, f.parent_id AS id_parent, f.left_id AS board_order,
				f.forum_posts AS num_posts, f.forum_id AS url, f.forum_last_post_id AS id_last_msg,
				SUBSTRING(f.forum_name, 1, 255) AS name, f.parent_id AS id_cat, '-1,0' AS member_groups,
				SUBSTRING(f.forum_desc, 1, 65534) AS description, f.forum_topics AS num_topics,
				f.forum_last_post_id AS id_last_msg
			FROM {$from_prefix}forums AS f
			WHERE forum_type = 1
			GROUP BY id_board;
		</query>
	</step>
	<step>
		<title>Importing Topics</title>
		<detect>{$from_prefix}topics</detect>
		<destination>{$to_prefix}topics</destination>
		<presql>
			TRUNCATE {$to_prefix}topics;
			TRUNCATE {$to_prefix}log_topics;
			TRUNCATE {$to_prefix}log_mark_read;
		</presql>
		<query>
			SELECT
				t.topic_id AS id_topic, t.forum_id AS id_board, t.topic_first_post_id AS id_first_msg,
				CASE t.topic_type 
					WHEN 1 THEN 1 
					WHEN 2 THEN 1 
					ELSE 0 
				END AS is_pinned,
				t.topic_last_post_id AS id_last_msg, t.topic_poster AS id_member_started,
				t.topic_last_poster_id AS id_member_updated, IF(po.topic_id > 0, po.topic_id, 0) AS id_poll,
				t.topic_replies AS num_replies, t.topic_views AS num_views,
				CASE t.topic_status 
					WHEN 1 THEN 1 
					ELSE 0 
				END AS locked
			FROM {$from_prefix}topics AS t
				LEFT JOIN {$from_prefix}poll_options AS po ON (po.topic_id = t.topic_id);
		</query>
	</step>
	<step>
		<title>Importing Messages</title>
		<detect> {$from_prefix}posts</detect>
		<destination>{$to_prefix}messages</destination>
		<presql>TRUNCATE {$to_prefix}messages;</presql>
		<options>
			<ignore>true</ignore>
			<slashes>true</slashes>
			<limit>100</limit>
		</options>
		<preparsecode>
			$row['body'] = phpbb_replace_bbc($row['body']);
			$row['body'] = str_replace($row['tmp_bbc_replace'], '', $row['body']);
			unset($row['tmp_bbc_replace']);
		</preparsecode>
		<query>
			SELECT
				p.post_id AS id_msg, p.topic_id AS id_topic, p.forum_id AS id_board,
				p.post_time AS poster_time, p.poster_id AS id_member, p.post_subject AS subject,
				IFNULL(m.username, 'Guest') AS poster_name,	IFNULL(m.user_email, 'Unknown') AS poster_email,
				IFNULL(p.poster_ip, '0.0.0.0') AS poster_ip, p.enable_smilies AS smileys_enabled,
				IFNULL(p.post_edit_time,0) AS modified_time, p.bbcode_uid AS tmp_bbc_replace,
				IFNULL(CASE p.post_edit_user WHEN 0 THEN 'Guest' ELSE m2.username END, 0) AS modified_name,
				p.post_text AS body
			FROM {$from_prefix}posts AS p
				LEFT JOIN {$from_prefix}users AS m ON (m.user_id = p.poster_id)
				LEFT JOIN {$from_prefix}users AS m2 ON (m2.user_id = p.post_edit_user);
		</query>
	</step>
		<step>
		<title>Importing polls</title>
		<detect>{$from_prefix}topics</detect>
		<destination>{$to_prefix}polls</destination>
		<presql>
			TRUNCATE {$to_prefix}polls;
			TRUNCATE {$to_prefix}poll_choices;
			TRUNCATE {$to_prefix}log_polls;
		</presql>
		<query>
			SELECT
				t.topic_id AS id_poll, t.poll_title AS question, t.poll_max_options AS max_votes,
				IFNULL((t.poll_start + t.poll_length), 0) AS expire_time, t.poll_vote_change AS change_vote,
				t.topic_poster AS id_member, IFNULL(m.username, 0) AS poster_name
			FROM {$from_prefix}topics AS t
				LEFT JOIN {$from_prefix}users AS m ON (m.user_id = t.topic_poster)
			WHERE t.poll_title != '';
		</query>
	</step>
	<step>
		<title>Importing poll choices</title>
		<detect>{$from_prefix}poll_options</detect>
		<destination>{$to_prefix}poll_choices</destination>
		<options>
			<ignore>true</ignore>
		</options>
		<query>
			SELECT
				topic_id AS id_poll, poll_option_id AS id_choice,
				SUBSTRING(poll_option_text, 1, 255) AS label, poll_option_total AS votes
			FROM {$from_prefix}poll_options;
		</query>
	</step>
	<step>
		<title>Importing poll votes</title>
		<detect>{$from_prefix}poll_votes</detect>
		<destination>{$to_prefix}log_polls</destination>
		<options>
			<ignore>true</ignore>
		</options>
		<query>
			SELECT 
				topic_id AS id_poll, vote_user_id AS id_member, poll_option_id AS id_choice
			FROM {$from_prefix}poll_votes
			WHERE vote_user_id > 0;
		</query>
	</step>
		<step>
		<title>Importing personal messages</title>
		<detect>{$from_prefix}privmsgs</detect>
		<destination>{$to_prefix}personal_messages</destination>
		<presql>
			TRUNCATE {$to_prefix}personal_messages;
			TRUNCATE {$to_prefix}pm_recipients;
		</presql>
		<preparsecode>
			$row['body'] = phpbb_replace_bbc($row['body']);
			$row['body'] = str_replace($row['tmp_bbc_replace'], '', $row['body']);
			unset($row['tmp_bbc_replace']);
		</preparsecode>
		<query><![CDATA[
			SELECT
				pm.msg_id AS id_pm, pm.author_id AS id_member_from, pm.message_time AS msgtime,
				SUBSTRING(uf.username, 1, 255) AS from_name, SUBSTRING(pm.message_subject, 1, 255) AS subject,
				pm.bbcode_uid AS tmp_bbc_replace,
				SUBSTRING(REPLACE(IF(pm.bbcode_uid = '', pm.message_text, 
				REPLACE(REPLACE(pm.message_text, CONCAT(':1:', pm.bbcode_uid), ''), CONCAT(':', pm.bbcode_uid), '')), '\n', '<br>'), 1, 65534) AS body
			FROM {$from_prefix}privmsgs AS pm
				LEFT JOIN {$from_prefix}users AS uf ON (uf.user_id = pm.author_id);
		]]></query>
	</step>
	<step>
		<title>Importing pm recipients</title>
		<detect>{$from_prefix}privmsgs_to</detect>
		<destination>{$to_prefix}pm_recipients</destination>
		<query>
			SELECT
				pm.msg_id AS id_pm, pm.user_id AS id_member, '-1' AS labels,
				CASE pm.pm_unread WHEN 1 THEN 0 ELSE 1 END AS is_read, pm.pm_deleted AS deleted
			FROM {$from_prefix}privmsgs_to AS pm
			GROUP BY pm.msg_id;
		</query>
	</step>
</importer>