getContainer()->get('db'); if (!$linkDb) { die("Erreur : base de données non accessible.\n"); } // Récupérer tous les liens avec le tag shaarli-todo $allLinks = $linkDb->getLinks(); $todosToMigrate = []; foreach ($allLinks as $link) { $tags = explode(' ', $link->getTags()); $hasTodoTag = in_array('shaarli-todo', $tags, true); if ($hasTodoTag) { $todosToMigrate[] = $link; } } if (empty($todosToMigrate)) { echo "Aucun todo à migrer.\n"; exit(0); } echo "Trouvé " . count($todosToMigrate) . " todo(s) à migrer.\n"; // Fonction pour générer un UUID court (style Android) function generateTodoUuid() { if (function_exists('random_bytes')) { $bytes = random_bytes(9); $out = ''; for ($i = 0; $i < 9; $i++) { $out .= str_pad(base_convert(ord($bytes[$i]), 10, 36), 2, '0', STR_PAD_LEFT); } return substr($out, 0, 12); } return substr(base_convert(time(), 10, 36) . base_convert(mt_rand(), 10, 36), 0, 12); } // Migrer chaque todo $migrated = 0; $errors = 0; foreach ($todosToMigrate as $link) { try { $tags = explode(' ', $link->getTags()); // 1. Remplacer shaarli-todo par todo $tags = array_filter($tags, function ($t) { return $t !== 'shaarli-todo'; }); $tags = array_values($tags); if (!in_array('todo', $tags, true)) { $tags[] = 'todo'; } $link->setTags($tags); // 2. Générer URL Android si vide ou legacy $url = $link->getUrl(); if (empty($url) || strpos($url, 'http://shaarli-todo') === 0 || strpos($url, 'https://shaarli-todo') === 0) { $uuid = generateTodoUuid(); $link->setUrl('https://shaarit.app/todo/' . $uuid); } // Sauvegarder $linkDb->save($link); $migrated++; echo "✓ Migré : {$link->getTitle()} ({$link->getId()})\n"; } catch (Exception $e) { $errors++; echo "✗ Erreur : {$link->getTitle()} - {$e->getMessage()}\n"; } } echo "\n=== Résumé ===\n"; echo "Migrés : $migrated\n"; echo "Erreurs : $errors\n"; echo "Total : " . count($todosToMigrate) . "\n"; if ($errors === 0) { echo "\n✓ Migration terminée avec succès !\n"; exit(0); } else { echo "\n⚠ Migration terminée avec $errors erreur(s).\n"; exit(1); }