]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Settings/UserExport.php
Merge pull request #10094 from urbalazs/license-20210328
[friendica.git] / src / Module / Settings / UserExport.php
index 0eaa72ffe6a518270e49f39c8b63639e87e66e55..2b668182e42148bf6a060b87dfdc8e3cded6e29d 100644 (file)
@@ -27,6 +27,8 @@ use Friendica\Core\Renderer;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
+use Friendica\Model\Item;
+use Friendica\Model\Post;
 use Friendica\Module\BaseSettings;
 
 /**
@@ -114,14 +116,14 @@ class UserExport extends BaseSettings
                $rows = DBA::p($query);
                while ($row = DBA::fetch($rows)) {
                        $p = [];
-                       foreach ($row as $k => $v) {
-                               switch ($dbStructure[$table]['fields'][$k]['type']) {
-                                       case 'datetime':
-                                               $p[$k] = $v ?? DBA::NULL_DATETIME;
-                                               break;
-                                       default:
-                                               $p[$k] = $v;
-                                               break;
+                       foreach ($dbStructure[$table]['fields'] as $column => $field) {
+                               if (!isset($row[$column])) {
+                                       continue;
+                               }
+                               if ($field['type'] == 'datetime') {
+                                       $p[$column] = $row[$column] ?? DBA::NULL_DATETIME;
+                               } else {
+                                       $p[$column] = $row[$column];
                                }
                        }
                        $result[] = $p;
@@ -143,6 +145,9 @@ class UserExport extends BaseSettings
 
                        foreach ($r as $rr) {
                                foreach ($rr as $k => $v) {
+                                       if (empty($dbStructure[$table]['fields'][$k])) {
+                                               continue;
+                                       }
                                        switch ($dbStructure[$table]['fields'][$k]['type']) {
                                                case 'datetime':
                                                        $result[$k] = $v ?? DBA::NULL_DATETIME;
@@ -165,9 +170,9 @@ class UserExport extends BaseSettings
                // write the table header (like Mastodon)
                echo "Account address, Show boosts\n";
                // get all the contacts
-               $contacts = DBA::select('contact', ['addr'], ['uid' => $_SESSION['uid'], 'self' => false, 'rel' => [1,3], 'deleted' => false]);
+               $contacts = DBA::select('contact', ['addr', 'url'], ['uid' => $_SESSION['uid'], 'self' => false, 'rel' => [1,3], 'deleted' => false]);
                while ($contact = DBA::fetch($contacts)) {
-                       echo $contact['addr'] . ", true\n";
+                       echo ($contact['addr'] ?: $contact['url']) . ", true\n";
                }
                DBA::close($contacts);
        }
@@ -237,17 +242,12 @@ class UserExport extends BaseSettings
                self::exportAccount($a);
                echo "\n";
 
-               $total = DBA::count('item', ['uid' => local_user()]);
+               $total = Post::count(['uid' => local_user()]);
                // chunk the output to avoid exhausting memory
 
                for ($x = 0; $x < $total; $x += 500) {
-                       $r = q("SELECT * FROM `item` WHERE `uid` = %d LIMIT %d, %d",
-                               intval(local_user()),
-                               intval($x),
-                               intval(500)
-                       );
-
-                       $output = ['item' => $r];
+                       $items = Post::selectToArray(Item::ITEM_FIELDLIST, ['uid' => local_user()], ['limit' => [$x, 500]]);
+                       $output = ['item' => $items];
                        echo json_encode($output, JSON_PARTIAL_OUTPUT_ON_ERROR). "\n";
                }
        }