3 * @file src/Modules/Settings/UserExport.php
6 namespace Friendica\Module\Settings;
9 use Friendica\Core\Hook;
10 use Friendica\Core\Renderer;
11 use Friendica\Database\DBA;
12 use Friendica\Database\DBStructure;
14 use Friendica\Module\BaseSettingsModule;
17 * Module to export user data
19 class UserExport extends BaseSettingsModule
22 * Handle the request to export data.
23 * At the moment one can export three different data set
24 * 1. The profile data that can be used by uimport to resettle
25 * to a different Friendica instance
26 * 2. The entire data-set, profile plus postings
27 * 3. A list of contacts as CSV file similar to the export of Mastodon
29 * If there is an action required through the URL / path, react
30 * accordingly and export the requested data.
32 public static function content(array $parameters = [])
34 parent::content($parameters);
37 * options shown on "Export personal data" page
38 * list of array( 'link url', 'link text', 'help text' )
41 ['settings/userexport/account', DI::l10n()->t('Export account'), DI::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.')],
42 ['settings/userexport/backup', DI::l10n()->t('Export all'), DI::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")],
43 ['settings/userexport/contact', DI::l10n()->t('Export Contacts to CSV'), DI::l10n()->t("Export the list of the accounts you are following as CSV file. Compatible to e.g. Mastodon.")],
45 Hook::callAll('uexport_options', $options);
47 $tpl = Renderer::getMarkupTemplate("settings/userexport.tpl");
48 return Renderer::replaceMacros($tpl, [
49 '$title' => DI::l10n()->t('Export personal data'),
50 '$options' => $options
54 * raw content generated for the different choices made
55 * by the user. At the moment this returns a JSON file
56 * to the browser which then offers a save / open dialog
59 public static function rawContent(array $parameters = [])
62 if ($args->getArgc() == 3) {
63 // @TODO Replace with router-provided arguments
64 $action = $args->get(2);
65 $user = DI::app()->user;
68 header("Content-type: application/json");
69 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
70 self::exportAll(DI::app());
74 header("Content-type: application/json");
75 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
76 self::exportAccount(DI::app());
80 header("Content-type: application/csv");
81 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '-contacts.csv'. '"');
82 self::exportContactsAsCSV();
90 private static function exportMultiRow(string $query)
92 $dbStructure = DBStructure::definition(DI::app()->getBasePath(), false);
94 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
98 $rows = DBA::p($query);
99 while ($row = DBA::fetch($rows)) {
101 foreach ($row as $k => $v) {
102 switch ($dbStructure[$table]['fields'][$k]['type']) {
104 $p[$k] = $v ?? DBA::NULL_DATETIME;
117 private static function exportRow(string $query)
119 $dbStructure = DBStructure::definition(DI::app()->getBasePath(), false);
121 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
126 if (DBA::isResult($r)) {
128 foreach ($r as $rr) {
129 foreach ($rr as $k => $v) {
130 switch ($dbStructure[$table]['fields'][$k]['type']) {
132 $result[$k] = $v ?? DBA::NULL_DATETIME;
145 * Export a list of the contacts as CSV file as e.g. Mastodon and Pleroma are doing.
147 private static function exportContactsAsCSV()
149 // write the table header (like Mastodon)
150 echo "Account address, Show boosts\n";
151 // get all the contacts
152 $contacts = DBA::select('contact', ['addr'], ['uid' => $_SESSION['uid'], 'self' => false, 'rel' => [1,3], 'deleted' => false]);
153 while ($contact = DBA::fetch($contacts)) {
154 echo $contact['addr'] . ", true\n";
156 DBA::close($contacts);
158 private static function exportAccount(App $a)
160 $user = self::exportRow(
161 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
164 $contact = self::exportMultiRow(
165 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
169 $profile = self::exportMultiRow(
170 sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
173 $photo = self::exportMultiRow(
174 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
176 foreach ($photo as &$p) {
177 $p['data'] = bin2hex($p['data']);
180 $pconfig = self::exportMultiRow(
181 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
184 $group = self::exportMultiRow(
185 sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
188 $group_member = self::exportMultiRow(
189 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()))
193 'version' => FRIENDICA_VERSION,
194 'schema' => DB_UPDATE_VERSION,
195 'baseurl' => DI::baseUrl(),
197 'contact' => $contact,
198 'profile' => $profile,
200 'pconfig' => $pconfig,
202 'group_member' => $group_member,
205 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
209 * echoes account data and items as separated json, one per line
214 private static function exportAll(App $a)
216 self::exportAccount($a);
219 $total = DBA::count('item', ['uid' => local_user()]);
220 // chunk the output to avoid exhausting memory
222 for ($x = 0; $x < $total; $x += 500) {
223 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
224 intval(local_user()),
229 $output = ['item' => $r];
230 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";