3 * @file src/Modules/Settings/UserExport.php
6 namespace Friendica\Module\Settings;
9 use Friendica\Core\Hook;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Renderer;
12 use Friendica\Database\DBA;
13 use Friendica\Database\DBStructure;
15 use Friendica\Module\BaseSettingsModule;
18 * Module to export user data
20 class UserExport extends BaseSettingsModule
23 * Handle the request to export data.
24 * At the moment one can export three different data set
25 * 1. The profile data that can be used by uimport to resettle
26 * to a different Friendica instance
27 * 2. The entire data-set, profile plus postings
28 * 3. A list of contacts as CSV file similar to the export of Mastodon
30 * If there is an action required through the URL / path, react
31 * accordingly and export the requested data.
33 public static function content(array $parameters = [])
35 parent::content($parameters);
38 * options shown on "Export personal data" page
39 * list of array( 'link url', 'link text', 'help text' )
42 ['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.')],
43 ['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")],
44 ['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.")],
46 Hook::callAll('uexport_options', $options);
48 $tpl = Renderer::getMarkupTemplate("settings/userexport.tpl");
49 return Renderer::replaceMacros($tpl, [
50 '$title' => L10n::t('Export personal data'),
51 '$options' => $options
55 * raw content generated for the different choices made
56 * by the user. At the moment this returns a JSON file
57 * to the browser which then offers a save / open dialog
60 public static function rawContent(array $parameters = [])
63 if ($args->getArgc() == 3) {
64 // @TODO Replace with router-provided arguments
65 $action = $args->get(2);
66 $user = DI::app()->user;
69 header("Content-type: application/json");
70 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
71 self::exportAll(DI::app());
75 header("Content-type: application/json");
76 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '.' . $action . '"');
77 self::exportAccount(DI::app());
81 header("Content-type: application/csv");
82 header('Content-Disposition: attachment; filename="' . $user['nickname'] . '-contacts.csv'. '"');
83 self::exportContactsAsCSV();
91 private static function exportMultiRow(string $query)
93 $dbStructure = DBStructure::definition(DI::app()->getBasePath(), false);
95 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
99 $rows = DBA::p($query);
100 while ($row = DBA::fetch($rows)) {
102 foreach ($row as $k => $v) {
103 switch ($dbStructure[$table]['fields'][$k]['type']) {
105 $p[$k] = $v ?? DBA::NULL_DATETIME;
118 private static function exportRow(string $query)
120 $dbStructure = DBStructure::definition(DI::app()->getBasePath(), false);
122 preg_match("/\s+from\s+`?([a-z\d_]+)`?/i", $query, $match);
127 if (DBA::isResult($r)) {
129 foreach ($r as $rr) {
130 foreach ($rr as $k => $v) {
131 switch ($dbStructure[$table]['fields'][$k]['type']) {
133 $result[$k] = $v ?? DBA::NULL_DATETIME;
146 * Export a list of the contacts as CSV file as e.g. Mastodon and Pleroma are doing.
148 private static function exportContactsAsCSV()
150 // write the table header (like Mastodon)
151 echo "Account address, Show boosts\n";
152 // get all the contacts
153 $contacts = DBA::select('contact', ['addr'], ['uid' => $_SESSION['uid'], 'self' => false, 'rel' => [1,3], 'deleted' => false]);
154 while ($contact = DBA::fetch($contacts)) {
155 echo $contact['addr'] . ", true\n";
157 DBA::close($contacts);
159 private static function exportAccount(App $a)
161 $user = self::exportRow(
162 sprintf("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()))
165 $contact = self::exportMultiRow(
166 sprintf("SELECT * FROM `contact` WHERE `uid` = %d ", intval(local_user()))
170 $profile = self::exportMultiRow(
171 sprintf("SELECT * FROM `profile` WHERE `uid` = %d ", intval(local_user()))
174 $photo = self::exportMultiRow(
175 sprintf("SELECT * FROM `photo` WHERE uid = %d AND profile = 1", intval(local_user()))
177 foreach ($photo as &$p) {
178 $p['data'] = bin2hex($p['data']);
181 $pconfig = self::exportMultiRow(
182 sprintf("SELECT * FROM `pconfig` WHERE uid = %d", intval(local_user()))
185 $group = self::exportMultiRow(
186 sprintf("SELECT * FROM `group` WHERE uid = %d", intval(local_user()))
189 $group_member = self::exportMultiRow(
190 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()))
194 'version' => FRIENDICA_VERSION,
195 'schema' => DB_UPDATE_VERSION,
196 'baseurl' => DI::baseUrl(),
198 'contact' => $contact,
199 'profile' => $profile,
201 'pconfig' => $pconfig,
203 'group_member' => $group_member,
206 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR);
210 * echoes account data and items as separated json, one per line
215 private static function exportAll(App $a)
217 self::exportAccount($a);
220 $total = DBA::count('item', ['uid' => local_user()]);
221 // chunk the output to avoid exhausting memory
223 for ($x = 0; $x < $total; $x += 500) {
224 $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
225 intval(local_user()),
230 $output = ['item' => $r];
231 echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";