added export and import of followed contacts to and from CSV files

This commit is contained in:
Tobias Diekershoff 2019-11-03 00:12:16 +01:00
parent b543ee8ac7
commit 955a84a266
4 changed files with 99 additions and 4 deletions

View file

@ -23,10 +23,11 @@ class UserExport extends BaseSettingsModule
{
/**
* Handle the request to export data.
* At the moment one can export two different data set
* At the moment one can export three different data set
* 1. The profile data that can be used by uimport to resettle
* to a different Friendica instance
* 2. The entire data-set, profile plus postings
* 3. A list of contacts as CSV file similar to the export of Mastodon
*
* If there is an action required through the URL / path, react
* accordingly and export the requested data.
@ -42,6 +43,7 @@ class UserExport extends BaseSettingsModule
$options = [
['settings/userexport/account', L10n::t('Export account'), L10n::t('Export your account info and contacts. Use this to make a backup of your account and/or to move it to another server.')],
['settings/userexport/backup', L10n::t('Export all'), L10n::t("Export your accout info, contacts and all your items as json. Could be a very big file, and could take a lot of time. Use this to make a full backup of your account \x28photos are not exported\x29")],
['settings/userexport/contactcsv', L10n::t('Export Contacts to CSV'), L10n::t("Export the list of the accounts you are following as CSV file. Compatible to e.g. Mastodon.")],
];
Hook::callAll('uexport_options', $options);
@ -64,17 +66,25 @@ class UserExport extends BaseSettingsModule
// @TODO Replace with router-provided arguments
$action = $args->get(2);
$user = self::getApp()->user;
header("Content-type: application/json");
header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
switch ($action) {
case "backup":
header("Content-type: application/json");
header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
self::exportAll(self::getApp());
exit();
break;
case "account":
header("Content-type: application/json");
header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
self::exportAccount(self::getApp());
exit();
break;
case "contactcsv":
header("Content-type: application/csv");
header('Content-Disposition: attachment; filename="' . $user['nickname'] . '-contacts.csv'. '"');
self::exportContactsAsCSV(self::getApp());
exit();
break;
default:
exit();
}
@ -134,6 +144,25 @@ class UserExport extends BaseSettingsModule
return $result;
}
/**
* Export a list of the contacts as CSV file as e.g. Mastodon and Pleroma are doing.
*
* @param App $a the app data
**/
private static function exportContactsAsCSV(App $a)
{
// write the table header (like Mastodon)
echo "Account address, Show boosts\n";
// get all the contacts
$query = sprintf("SELECT addr FROM `contact` WHERE `uid` = %d AND `self` = 0 AND rel IN (1,3) AND NOT `deleted`;", intval($_SESSION['uid']));
$contacts = q($query);
if (DBA::isResult($contacts)) {
foreach ($contacts as $contact) {
// export row, set Show boosts to true
echo $contact['addr'] . ", true\n";
}
}
}
private static function exportAccount(App $a)
{
$user = self::exportRow(