Remove old import and upgrade scripts

This commit is contained in:
Aaron van Geffen 2023-03-21 22:48:18 +01:00
parent af0c8990a6
commit 9a8a91343b
4 changed files with 0 additions and 407 deletions

View File

@ -1,319 +0,0 @@
<?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();

View File

@ -1,20 +0,0 @@
#!/bin/bash
# ALBUM UPDATE
# Hashes uit filenames.
find . -name '*#*' -exec rename -v "s/#//" {} \;
# Orientatie-tags goedzetten.
find public/assets/borrel/april-2015/ -type f -exec exiftool -n -Orientation=1 "{}" \;
find public/assets/Eetpartijtjes/ruwinterbbq/ -type f -exec exiftool -n -Orientation=1 "{}" \;
find public/assets/Eetpartijtjes/Tapasavond-oktober-2011/ -type f -exec exiftool -n -Orientation=1 "{}" \;
find public/assets/Eetpartijtjes/Verjaardag-IV-bij-Wally/ -type f -exec exiftool -n -Orientation=1 "{}" \;
find public/assets/Uitstapjes/Final-Symphony-Wuppertal-2013-05-11/ -type f -exec exiftool -n -Orientation=1 "{}" \;
find public/assets/Universiteit/Oude-sneeuwfoto\'s/ -type f -exec exiftool -n -Orientation=1 "{}" \;
find public/assets/Weekenden/Susteren-2012 -type f -exec exiftool -n -Orientation=1 "{}" \;
find public/assets/Weekenden/Susteren-2013 -type f -exec exiftool -n -Orientation=1 "{}" \;
find public/assets/Weekenden/Wijhe-2016/ -type f -exec exiftool -n -Orientation=1 "{}" \;
# Remove backup files.
find public/assets/ -type f -name '*_original' -delete

View File

@ -1,53 +0,0 @@
<?php
/*****************************************************************************
* migrate_thumbs.php
* Migrates old-style thumbnails (meta) to new table.
*
* Kabuki CMS (C) 2013-2015, 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);
Registry::set('db', $db);
// Do some authentication checks.
Session::start();
Registry::set('user', Authentication::isLoggedIn() ? Member::fromId($_SESSION['user_id']) : new Guest());
$res = $db->query('
SELECT id_asset, variable, value
FROM assets_meta
WHERE variable LIKE {string:thumbs}',
['thumbs' => 'thumb_%']);
while ($row = $db->fetch_assoc($res))
{
if (!preg_match('~^thumb_(?<width>\d+)x(?<height>\d+)(?:_(?<mode>c[best]?))?$~', $row['variable'], $match))
continue;
echo 'Migrating ... ', $row['value'], '(#', $row['id_asset'], ")\r";
$db->insert('replace', 'assets_thumbs', [
'id_asset' => 'int',
'width' => 'int',
'height' => 'int',
'mode' => 'string-3',
'filename' => 'string-255',
], [
'id_asset' => $row['id_asset'],
'width' => $match['width'],
'height' => $match['height'],
'mode' => $match['mode'] ?? '',
'filename' => $row['value'],
]);
}
echo "\nDone\n";

View File

@ -1,15 +0,0 @@
/* 2023-03-11 Allow designating an owner for each tag */
ALTER TABLE `tags` ADD `id_user_owner` INT NULL DEFAULT NULL AFTER `id_asset_thumb`;
/* 2023-03-11 Try to assign tag owners automagically */
UPDATE tags AS t
SET id_user_owner = (
SELECT id_user
FROM users AS u
WHERE LOWER(u.first_name) = LOWER(t.slug) OR
LOWER(u.first_name) = LOWER(t.tag) OR
LOWER(u.slug) = LOWER(t.slug) OR
LOWER(u.slug) = LOWER(t.tag)
)
WHERE t.kind = 'Person' AND
(t.id_user_owner = 0 OR t.id_user_owner IS NULL);