3 * @file src/Modules/Settings/UserExport.php
6 namespace Friendica\Module\Settings;
9 use Friendica\App\Arguments;
10 use Friendica\BaseModule;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
16 use Friendica\Database\DBStructure;
17 use Friendica\Module\BaseSettingsModule;
20 * Module to export user data
22 class UserExport extends BaseSettingsModule
25 * Handle the request to export data.
26 * At the moment one can export three different data set
27 * 1. The profile data that can be used by uimport to resettle
28 * to a different Friendica instance
29 * 2. The entire data-set, profile plus postings
30 * 3. A list of contacts as CSV file similar to the export of Mastodon
32 * If there is an action required through the URL / path, react
33 * accordingly and export the requested data.
35 public static function content($parameters)
37 parent::content($parameters);
40 * options shown on "Export personal data" page
41 * list of array( 'link url', 'link text', 'help text' )
44 ['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.')],
45 ['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")],
46 ['settings/userexport/contact', 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.")],
48 Hook::callAll('uexport_options', $options);
50 $tpl = Renderer::getMarkupTemplate("settings/userexport.tpl");
51 return Renderer::replaceMacros($tpl, [
52 '$title' => L10n::t('Export personal data'),
53 '$options' => $options
57 * raw content generated for the different choices made
58 * by the user. At the moment this returns a JSON file
59 * to the browser which then offers a save / open dialog
62 public static function rawContent($parameters)
64 $args = self::getClass(Arguments::class);
65 if ($args->getArgc() == 3) {
66 // @TODO Replace with router-provided arguments
67 $action = $args->get(2);
68 $user = self::getApp()->user;
71 header("Content-type: application/json");
72 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
73 self::exportAll(self::getApp());
77 header("Content-type: application/json");
78 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
79 self::exportAccount(self::getApp());
83 header("Content-type: application/csv");
84 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '-contacts.csv'. '"');
85 self::exportContactsAsCSV();
93 private static function exportMultiRow(string $query)
95 $dbStructure = DBStructure::definition(self::getApp()->getBasePath(), false);
97 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
101 $rows = DBA::p($query);
102 while ($row = DBA::fetch($rows)) {
104 foreach ($row as $k => $v) {
105 switch ($dbStructure[$table]['fields'][$k]['type']) {
107 $p[$k] = $v ?? DBA::NULL_DATETIME;
120 private static function exportRow(string $query)
122 $dbStructure = DBStructure::definition(self::getApp()->getBasePath(), false);
124 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
129 if (DBA::isResult($r)) {
131 foreach ($r as $rr) {
132 foreach ($rr as $k => $v) {
133 switch ($dbStructure[$table]['fields'][$k]['type']) {
135 $result[$k] = $v ?? DBA::NULL_DATETIME;
148 * Export a list of the contacts as CSV file as e.g. Mastodon and Pleroma are doing.
150 private static function exportContactsAsCSV()
152 // write the table header (like Mastodon)
153 echo "Account address, Show boosts\n";
154 // get all the contacts
155 $contacts = DBA::select('contact', ['addr'], ['uid' => $_SESSION['uid'], 'self' => false, 'rel' => [1,3], 'deleted' => false]);
156 while ($contact = DBA::fetch($contacts)) {
157 echo $contact['addr'] . ", true\n";
159 DBA::close($contacts);
161 private static function exportAccount(App $a)
163 $user = self::exportRow(
164 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
167 $contact = self::exportMultiRow(
168 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
172 $profile = self::exportMultiRow(
173 sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
176 $photo = self::exportMultiRow(
177 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
179 foreach ($photo as &$p) {
180 $p['data'] = bin2hex($p['data']);
183 $pconfig = self::exportMultiRow(
184 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
187 $group = self::exportMultiRow(
188 sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
191 $group_member = self::exportMultiRow(
192 sprintf("SELECT `group_member`.`gid`, `group_member`.`contact-id` FROM `group_member` INNER JOIN `group` ON `group`.`id` = `group_member`.`gid` WHERE `group`.`uid` = %d", intval(local_user()))
196 'version' => FRIENDICA_VERSION,
197 'schema' => DB_UPDATE_VERSION,
198 'baseurl' => System::baseUrl(),
200 'contact' => $contact,
201 'profile' => $profile,
203 'pconfig' => $pconfig,
205 'group_member' => $group_member,
208 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
212 * echoes account data and items as separated json, one per line
217 private static function exportAll(App $a)
219 self::exportAccount($a);
222 $total = DBA::count('item', ['uid' => local_user()]);
223 // chunk the output to avoid exhausting memory
225 for ($x = 0; $x < $total; $x += 500) {
226 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
227 intval(local_user()),
232 $output = ['item' => $r];
233 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";