From 2be0ceac6befc1d20a7fb545d298d54af5b842f5 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 14:33:17 +0000 Subject: [PATCH 1/9] Added "selectToArray" functions in DBA and Item --- src/Database/DBA.php | 17 +++++++++++++++++ src/Model/Item.php | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 6e9bc89be..937693a79 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -408,6 +408,23 @@ class DBA return self::$database->selectFirst($table, $fields, $condition, $params); } + /** + * @brief Select rows from a table and fills an array with the data + * + * @param string $table Table name + * @param array $fields Array of selected fields, empty for all + * @param array $condition Array of fields for condition + * @param array $params Array of several parameters + * + * @return array Data array + * @throws \Exception + * @see self::select + */ + public static function selectToArray($table, array $fields = [], array $condition = [], array $params = []) + { + return self::$database->toArray(self::$database->select($table, $fields, $condition, $params)); + } + /** * @brief Select rows from a table * diff --git a/src/Model/Item.php b/src/Model/Item.php index 81c60b30b..c2bb6c892 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -368,6 +368,33 @@ class Item extends BaseObject } } + /** + * @brief Select rows from the item table and returns them as an array + * + * @param array $selected Array of selected fields, empty for all + * @param array $condition Array of fields for condition + * @param array $params Array of several parameters + * + * @return array + * @throws \Exception + */ + public static function selectToArray(array $fields = [], array $condition = [], $params = []) + { + $result = self::select($fields, $condition, $params); + + if (is_bool($result)) { + return $result; + } + + $data = []; + while ($row = self::fetch($result)) { + $data[] = $row; + } + DBA::close($result); + + return $data; + } + /** * @brief Select rows from the item table * From 975ff86baea5602e9b83f94fbd29f5faf8eb0ce4 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 15:40:43 +0000 Subject: [PATCH 2/9] Fix: The $contact error is expected to be a single row array --- src/Protocol/DFRN.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 5fceab326..12c93ea2f 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -2176,7 +2176,7 @@ class DFRN if (($entrytype == DFRN::TOP_LEVEL) && !empty($importer['id'])) { // The filling of the the "contact" variable is done for legcy reasons // The functions below are partly used by ostatus.php as well - where we have this variable - $contact = Contact::select([], ['id' => $importer['id']]); + $contact = Contact::selectFirst([], ['id' => $importer['id']]); // Big question: Do we need these functions? They were part of the "consume_feed" function. // This function once was responsible for DFRN and OStatus. From ac6bfd3e4c0b49d3f719a2f5220222001e1bbbce Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 15:47:17 +0000 Subject: [PATCH 3/9] Changed Attach::select to Attach::selectToArray --- src/Model/Attach.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Model/Attach.php b/src/Model/Attach.php index d65e67fe3..716004ab3 100644 --- a/src/Model/Attach.php +++ b/src/Model/Attach.php @@ -38,7 +38,7 @@ class Attach extends BaseObject } /** - * @brief Select rows from the attach table + * @brief Select rows from the attach table and return them as array * * @param array $fields Array of selected fields, empty for all * @param array $conditions Array of fields for conditions @@ -47,9 +47,9 @@ class Attach extends BaseObject * @return boolean|array * * @throws \Exception - * @see \Friendica\Database\DBA::select + * @see \Friendica\Database\DBA::selectToArray */ - public static function select(array $fields = [], array $conditions = [], array $params = []) + public static function selectToArray(array $fields = [], array $conditions = [], array $params = []) { if (empty($fields)) { $fields = self::getFields(); @@ -264,7 +264,7 @@ class Attach extends BaseObject { if (!is_null($img)) { // get items to update - $items = self::select(['backend-class','backend-ref'], $conditions); + $items = self::selectToArray(['backend-class','backend-ref'], $conditions); foreach($items as $item) { /** @var IStorage $backend_class */ @@ -297,7 +297,7 @@ class Attach extends BaseObject public static function delete(array $conditions, array $options = []) { // get items to delete data info - $items = self::select(['backend-class','backend-ref'], $conditions); + $items = self::selectToArray(['backend-class','backend-ref'], $conditions); foreach($items as $item) { /** @var IStorage $backend_class */ From 89454fabdb64283f415b1af2c5114c71d2e716ed Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 15:52:02 +0000 Subject: [PATCH 4/9] Changed "Photo::select(" to "Photo::selectToArray(" --- src/Model/Attach.php | 3 +-- src/Model/Photo.php | 15 +++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Model/Attach.php b/src/Model/Attach.php index 716004ab3..758d3ae83 100644 --- a/src/Model/Attach.php +++ b/src/Model/Attach.php @@ -55,8 +55,7 @@ class Attach extends BaseObject $fields = self::getFields(); } - $r = DBA::select('attach', $fields, $conditions, $params); - return DBA::toArray($r); + $r = DBA::selectToArray('attach', $fields, $conditions, $params); } /** diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 0e3661b0f..a1c53fbbe 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -29,7 +29,7 @@ require_once "include/dba.php"; class Photo extends BaseObject { /** - * @brief Select rows from the photo table + * @brief Select rows from the photo table and returns them as array * * @param array $fields Array of selected fields, empty for all * @param array $conditions Array of fields for conditions @@ -38,16 +38,15 @@ class Photo extends BaseObject * @return boolean|array * * @throws \Exception - * @see \Friendica\Database\DBA::select + * @see \Friendica\Database\DBA::selectToArray */ - public static function select(array $fields = [], array $conditions = [], array $params = []) + public static function selectToArray(array $fields = [], array $conditions = [], array $params = []) { if (empty($fields)) { $fields = self::getFields(); } - $r = DBA::select("photo", $fields, $conditions, $params); - return DBA::toArray($r); + return DBA::selectToArray("photo", $fields, $conditions, $params); } /** @@ -89,7 +88,7 @@ class Photo extends BaseObject $conditions["resource-id"] = $resourceid; $conditions["uid"] = $uid; - return self::select([], $conditions, $params); + return self::selectToArray([], $conditions, $params); } /** @@ -350,7 +349,7 @@ class Photo extends BaseObject public static function delete(array $conditions, array $options = []) { // get photo to delete data info - $photos = self::select(["backend-class","backend-ref"], $conditions); + $photos = self::selectToArray(["backend-class","backend-ref"], $conditions); foreach($photos as $photo) { /** @var IStorage $backend_class */ @@ -380,7 +379,7 @@ class Photo extends BaseObject { if (!is_null($img)) { // get photo to update - $photos = self::select(["backend-class","backend-ref"], $conditions); + $photos = self::selectToArray(["backend-class","backend-ref"], $conditions); foreach($photos as $photo) { /** @var IStorage $backend_class */ From 1a3bf05dfbff72a243224485a83926d046eb2375 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 15:57:23 +0000 Subject: [PATCH 5/9] Changed "Contact::select" to "Contact::selectToArray" --- src/Model/Contact.php | 6 ++---- src/Module/Admin/Blocklist/Contact.php | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 398acc2d7..f01cb7360 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -117,11 +117,9 @@ class Contact extends BaseObject * @return array * @throws \Exception */ - public static function select(array $fields = [], array $condition = [], array $params = []) + public static function selectToArray(array $fields = [], array $condition = [], array $params = []) { - $statement = DBA::select('contact', $fields, $condition, $params); - - return DBA::toArray($statement); + return DBA::selectToArray('contact', $fields, $condition, $params); } /** diff --git a/src/Module/Admin/Blocklist/Contact.php b/src/Module/Admin/Blocklist/Contact.php index 60fe04bf7..de3c717e3 100644 --- a/src/Module/Admin/Blocklist/Contact.php +++ b/src/Module/Admin/Blocklist/Contact.php @@ -53,7 +53,7 @@ class Contact extends BaseAdminModule $pager = new Pager($a->query_string, 30); - $contacts = Model\Contact::select([], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]); + $contacts = Model\Contact::selectToArray([], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]); $t = Renderer::getMarkupTemplate('admin/blocklist/contact.tpl'); $o = Renderer::replaceMacros($t, [ From 555c444b4e283261d2efe376b5803e0adbf5e214 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 16:08:48 +0000 Subject: [PATCH 6/9] Moved function to Database.php --- src/Database/DBA.php | 2 +- src/Database/Database.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 937693a79..4619bc38b 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -422,7 +422,7 @@ class DBA */ public static function selectToArray($table, array $fields = [], array $condition = [], array $params = []) { - return self::$database->toArray(self::$database->select($table, $fields, $condition, $params)); + return self::$database->selectToArray($table, $fields, $condition, $params); } /** diff --git a/src/Database/Database.php b/src/Database/Database.php index c50341691..566a757b5 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1407,6 +1407,23 @@ class Database } } + /** + * @brief Select rows from a table and fills an array with the data + * + * @param string $table Table name + * @param array $fields Array of selected fields, empty for all + * @param array $condition Array of fields for condition + * @param array $params Array of several parameters + * + * @return array Data array + * @throws \Exception + * @see self::select + */ + public function selectToArray($table, array $fields = [], array $condition = [], array $params = []) + { + return $this->toArray($this->select($table, $fields, $condition, $params)); + } + /** * @brief Select rows from a table * From 580f9cf6c7498485a4db8dc43794afb72ff0d8d5 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 16:53:48 +0000 Subject: [PATCH 7/9] Replaced some " with ' --- src/Model/Photo.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Model/Photo.php b/src/Model/Photo.php index a1c53fbbe..0ea6f06b1 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -46,7 +46,7 @@ class Photo extends BaseObject $fields = self::getFields(); } - return DBA::selectToArray("photo", $fields, $conditions, $params); + return DBA::selectToArray('photo', $fields, $conditions, $params); } /** @@ -349,7 +349,7 @@ class Photo extends BaseObject public static function delete(array $conditions, array $options = []) { // get photo to delete data info - $photos = self::selectToArray(["backend-class","backend-ref"], $conditions); + $photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions); foreach($photos as $photo) { /** @var IStorage $backend_class */ From f48452dd291296baa9096841d7880e1623c8ba1b Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 16:57:00 +0000 Subject: [PATCH 8/9] Some more --- src/Model/Photo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 0ea6f06b1..9fa279cf8 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -379,7 +379,7 @@ class Photo extends BaseObject { if (!is_null($img)) { // get photo to update - $photos = self::selectToArray(["backend-class","backend-ref"], $conditions); + $photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions); foreach($photos as $photo) { /** @var IStorage $backend_class */ From 5d9472dccf5ab62a0054e1a31aee22c7cd02e5e3 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 17:00:06 +0000 Subject: [PATCH 9/9] Type hints --- src/Database/DBA.php | 2 +- src/Database/Database.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 4619bc38b..9bc314c9c 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -420,7 +420,7 @@ class DBA * @throws \Exception * @see self::select */ - public static function selectToArray($table, array $fields = [], array $condition = [], array $params = []) + public static function selectToArray(string $table, array $fields = [], array $condition = [], array $params = []) { return self::$database->selectToArray($table, $fields, $condition, $params); } diff --git a/src/Database/Database.php b/src/Database/Database.php index 566a757b5..29d1ac8cc 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1419,7 +1419,7 @@ class Database * @throws \Exception * @see self::select */ - public function selectToArray($table, array $fields = [], array $condition = [], array $params = []) + public function selectToArray(string $table, array $fields = [], array $condition = [], array $params = []) { return $this->toArray($this->select($table, $fields, $condition, $params)); }