<?php
/*****************************************************************************
 * import_albums.php
 * Imports albums from a Gallery 3 database.
 *
 * Kabuki CMS (C) 2013-2016, Aaron van Geffen
 *****************************************************************************/

// Include the project's configuration.
require_once 'config.php';

// Set up the autoloader.
require_once 'vendor/autoload.php';

// Initialise the database.
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
$pdb = new Database(DB_SERVER, DB_USER, DB_PASS, "hashru_gallery");
Registry::set('db', $db);

// Do some authentication checks.
Session::start();
Registry::set('user', Authentication::isLoggedIn() ? Member::fromId($_SESSION['user_id']) : new Guest());

// Enable debugging.
//set_error_handler('ErrorHandler::handleError');
ini_set("display_errors", DEBUG ? "On" : "Off");

/*******************************
 * STEP 0: USERS
 *******************************/

$num_users = $pdb->queryValue('
	SELECT COUNT(*)
	FROM users');

echo $num_users, ' users to import.', "\n";

$rs_users = $pdb->query('
	SELECT id, name, full_name, password, last_login, email, admin
	FROM users
	WHERE id > 1
	ORDER BY id ASC');

$old_user_id_to_new_user_id = [];

while ($user = $pdb->fetch_assoc($rs_users))
{
	// Check whether a user already exists for this e-mail address.
	if (!($id_user = Authentication::getUserId($user['email'])))
	{
		$bool = $db->insert('insert', 'users', [
			'first_name' => 'string-30',
			'surname' => 'string-60',
			'slug' => 'string-90',
			'emailaddress' => 'string-255',
			'password_hash' => 'string-255',
			'creation_time' => 'int',
			'last_action_time' => 'int',
			'ip_address' => 'string-15',
			'is_admin' => 'int',
		], [
			'first_name' => substr($user['full_name'], 0, strpos($user['full_name'], ' ')),
			'surname' => substr($user['full_name'], strpos($user['full_name'], ' ') + 1),
			'slug' => $user['name'],
			'emailaddress' => $user['email'],
			'password_hash' => $user['password'],
			'creation_time' => 0,
			'last_action_time' => $user['last_login'],
			'ip_address' => '0.0.0.0',
			'is_admin' => $user['admin'],
		], ['id_user']);

		if ($bool)
			$id_user = $db->insert_id();
		else
			die("User creation failed!");
	}

	$old_user_id_to_new_user_id[$user['id']] = $id_user;
}

$pdb->free_result($rs_users);

/*******************************
 * STEP 1: ALBUMS
 *******************************/

$num_albums = $pdb->queryValue('
	SELECT COUNT(*)
	FROM items
	WHERE type = {string:album}
	ORDER BY id ASC',
	['album' => 'album']);

echo $num_albums, ' albums to import.', "\n";

$albums = $pdb->query('
	SELECT id, album_cover_item_id, parent_id, title, description, relative_path_cache, relative_url_cache
	FROM items
	WHERE type = {string:album}
	ORDER BY id ASC',
	['album' => 'album']);

$tags = [];
$old_album_id_to_new_tag_id = [];
$dirnames_by_old_album_id = [];
$old_thumb_id_by_tag_id = [];

while ($album = $pdb->fetch_assoc($albums))
{
	$tag = Tag::createNew([
		'tag' => $album['title'],
		'slug' => $album['relative_url_cache'],
		'kind' => 'Album',
		'description' => $album['description'],
	]);

	if (!empty($album['parent_id']))
		$parent_to_set[$tag->id_tag] = $album['parent_id'];

	$tags[$tag->id_tag] = $tag;
	$old_album_id_to_new_tag_id[$album['id']] = $tag->id_tag;
	$dirnames_by_old_album_id[$album['id']] = str_replace('#', '', urldecode($album['relative_path_cache']));
	$old_thumb_id_by_tag_id[$tag->id_tag] = $album['album_cover_item_id'];
}

$pdb->free_result($albums);

foreach ($parent_to_set as $id_tag => $old_album_id)
{
	$id_parent = $old_album_id_to_new_tag_id[$old_album_id];
	$db->query('
		UPDATE tags
		SET id_parent = ' . $id_parent . '
		WHERE id_tag = ' . $id_tag);
}

unset($parent_to_set);

/*******************************
 * STEP 2: PHOTOS
 *******************************/

$num_photos = $pdb->queryValue('
	SELECT COUNT(*)
	FROM items
	WHERE type = {string:photo}',
	['photo' => "photo"]);

echo $num_photos, " photos to import.\n";

$old_photo_id_to_asset_id = [];
for ($i = 0; $i < $num_photos; $i += 50)
{
	echo 'Offset ' . $i . "...\n";

	$photos = $pdb->query('
		SELECT id, owner_id, parent_id, captured, created, name, title, description, relative_url_cache, width, height, mime_type, weight
		FROM items
		WHERE type = {string:photo}
		ORDER BY id ASC
		LIMIT ' . $i . ', 50',
		['photo' => 'photo']);

	while ($photo = $pdb->fetch_assoc($photos))
	{
		$res = $db->query('
			INSERT INTO assets
			(id_user_uploaded, subdir, filename, title, slug, mimetype, image_width, image_height, date_captured, priority)
			VALUES
			({int:id_user_uploaded}, {string:subdir}, {string:filename}, {string:title}, {string:slug}, {string:mimetype},
			 {int:image_width}, {int:image_height},
			 IF({int:date_captured} > 0, FROM_UNIXTIME({int:date_captured}), NULL),
			 {int:priority})',
			[
				'id_user_uploaded' => $old_user_id_to_new_user_id[$photo['owner_id']],
				'subdir' => $dirnames_by_old_album_id[$photo['parent_id']],
				'filename' => str_replace('#', '', $photo['name']),
				'title' => $photo['title'],
				'slug' => str_replace('#', '', urldecode($photo['relative_url_cache'])),
				'mimetype' => $photo['mime_type'],
				'image_width' => !empty($photo['width']) ? $photo['width'] : 'NULL',
				'image_height' => !empty($photo['height']) ? $photo['height'] : 'NULL',
				'date_captured' => !empty($photo['captured']) ? $photo['captured'] : $photo['created'],
				'priority' => !empty($photo['weight']) ? (int) $photo['weight'] : 0,
			]);

		$id_asset = $db->insert_id();
		$old_photo_id_to_asset_id[$photo['id']] = $id_asset;

		// Link to album.
		$db->query('
			INSERT INTO assets_tags
			(id_asset, id_tag)
			VALUES
			({int:id_asset}, {int:id_tag})',
			[
				'id_asset' => $id_asset,
				'id_tag' => $old_album_id_to_new_tag_id[$photo['parent_id']],
			]);
	}
}

/*******************************
 * STEP 3: TAGS
 *******************************/

$num_tags = $pdb->queryValue('
	SELECT COUNT(*)
	FROM tags');

echo $num_tags, " tags to import.\n";

$rs_tags = $pdb->query('
	SELECT id, name, count
	FROM tags');

$old_tag_id_to_new_tag_id = [];
while ($person = $pdb->fetch_assoc($rs_tags))
{
	$tag = Tag::createNew([
		'tag' => $person['name'],
		'slug' => $person['name'],
		'kind' => 'Person',
		'description' => '',
		'count' => $person['count'],
	]);

	$tags[$tag->id_tag] = $tag;
	$old_tag_id_to_new_tag_id[$person['id']] = $tag->id_tag;
}

$pdb->free_result($rs_tags);

/*******************************
 * STEP 4: TAGGED PHOTOS
 *******************************/

$num_tagged = $pdb->queryValue('
	SELECT COUNT(*)
	FROM items_tags
	WHERE item_id IN(
		SELECT id
		FROM items
		WHERE type = {string:photo}
	)',
	['photo' => 'photo']);

echo $num_tagged, " photo tags to import.\n";

$rs_tags = $pdb->query('
	SELECT item_id, tag_id
	FROM items_tags
	WHERE item_id IN(
		SELECT id
		FROM items
		WHERE type = {string:photo}
	)',
	['photo' => 'photo']);

while ($tag = $pdb->fetch_assoc($rs_tags))
{
	if (!isset($old_tag_id_to_new_tag_id[$tag['tag_id']], $old_photo_id_to_asset_id[$tag['item_id']]))
		continue;

	$id_asset = $old_photo_id_to_asset_id[$tag['item_id']];
	$id_tag = $old_tag_id_to_new_tag_id[$tag['tag_id']];

	// Link up.
	$db->query('
		INSERT IGNORE INTO assets_tags
		(id_asset, id_tag)
		VALUES
		({int:id_asset}, {int:id_tag})',
		[
			'id_asset' => $id_asset,
			'id_tag' => $id_tag,
		]);
}

$pdb->free_result($rs_tags);

/*******************************
 * STEP 5: THUMBNAIL IDS
 *******************************/

foreach ($old_thumb_id_by_tag_id as $id_tag => $old_thumb_id)
{
	if (!isset($old_photo_id_to_asset_id[$old_thumb_id]))
		continue;

	$id_asset = $old_photo_id_to_asset_id[$old_thumb_id];
	$db->query('
		UPDATE tags
		SET id_asset_thumb = ' . $id_asset . '
		WHERE id_tag = ' . $id_tag);
}

/*******************************
 * STEP 6: THUMBNAILS FOR PEOPLE
 *******************************/

$db->query('
	UPDATE tags AS t
	SET id_asset_thumb = (
		SELECT id_asset
		FROM assets_tags AS a
		WHERE a.id_tag = t.id_tag
		ORDER BY RAND()
		LIMIT 1
	)
	WHERE kind = {string:person}',
	['person' => 'Person']);

/*******************************
 * STEP 7: CLEANING UP
 *******************************/

Tag::recount();