From 962fbc916647a0ab8fa3a6d73a3886741cdbe707 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Tue, 19 Jun 2018 21:33:07 +0000
Subject: [PATCH] Many "fetch_first" had been replaced

---
 include/security.php       | 16 +++++++---------
 mod/item.php               |  8 ++++----
 src/Core/Worker.php        | 15 ++++-----------
 src/Model/Conversation.php |  4 ++--
 src/Model/User.php         | 17 +++++------------
 src/Protocol/DFRN.php      | 20 +++++++++-----------
 src/Protocol/OStatus.php   |  9 ++++-----
 7 files changed, 35 insertions(+), 54 deletions(-)

diff --git a/include/security.php b/include/security.php
index bcfddf8872..e8a03ad0fe 100644
--- a/include/security.php
+++ b/include/security.php
@@ -99,11 +99,9 @@ function authenticate_success($user_record, $login_initial = false, $interactive
 	$master_record = $a->user;
 
 	if ((x($_SESSION, 'submanage')) && intval($_SESSION['submanage'])) {
-		$r = dba::fetch_first("SELECT * FROM `user` WHERE `uid` = ? LIMIT 1",
-			intval($_SESSION['submanage'])
-		);
-		if (DBM::is_result($r)) {
-			$master_record = $r;
+		$user = dba::selectFirst('user', [], ['uid' => $_SESSION['submanage']]);
+		if (DBM::is_result($user)) {
+			$master_record = $user;
 		}
 	}
 
@@ -155,10 +153,10 @@ function authenticate_success($user_record, $login_initial = false, $interactive
 		logger('auth_identities refresh: ' . print_r($a->identities, true), LOGGER_DEBUG);
 	}
 
-	$r = dba::fetch_first("SELECT * FROM `contact` WHERE `uid` = ? AND `self` LIMIT 1", $_SESSION['uid']);
-	if (DBM::is_result($r)) {
-		$a->contact = $r;
-		$a->cid = $r['id'];
+	$contact = dba::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => true]);
+	if (DBM::is_result($contact)) {
+		$a->contact = $contact;
+		$a->cid = $contact['id'];
 		$_SESSION['cid'] = $a->cid;
 	}
 
diff --git a/mod/item.php b/mod/item.php
index f164fb0ae2..4c78af6ca1 100644
--- a/mod/item.php
+++ b/mod/item.php
@@ -647,13 +647,13 @@ function item_post(App $a) {
 	// This field is for storing the raw conversation data
 	$datarray['protocol'] = PROTOCOL_DFRN;
 
-	$r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $datarray['parent-uri']);
-	if (DBM::is_result($r)) {
+	$conversation = dba::selectFirst('conversation', ['conversation-uri', 'conversation-href'], ['item-uri' => $datarray['parent-uri']]);
+	if (DBM::is_result($conversation)) {
 		if ($r['conversation-uri'] != '') {
-			$datarray['conversation-uri'] = $r['conversation-uri'];
+			$datarray['conversation-uri'] = $conversation['conversation-uri'];
 		}
 		if ($r['conversation-href'] != '') {
-			$datarray['conversation-href'] = $r['conversation-href'];
+			$datarray['conversation-href'] = $conversation['conversation-href'];
 		}
 	}
 
diff --git a/src/Core/Worker.php b/src/Core/Worker.php
index 4659d22832..8960780e55 100644
--- a/src/Core/Worker.php
+++ b/src/Core/Worker.php
@@ -155,12 +155,7 @@ class Worker
 	 */
 	private static function totalEntries()
 	{
-		$s = dba::fetch_first("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` <= ? AND NOT `done`", NULL_DATE);
-		if (DBM::is_result($s)) {
-			return $s["total"];
-		} else {
-			return 0;
-		}
+		return dba::count('workerqueue', ["`executed` <= ? AND NOT `done`", NULL_DATE]);
 	}
 
 	/**
@@ -718,9 +713,7 @@ class Worker
 	 */
 	private static function activeWorkers()
 	{
-		$workers = dba::fetch_first("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'Worker.php'");
-
-		return $workers["processes"];
+		return dba::count('process', ['command' => 'Worker.php']);
 	}
 
 	/**
@@ -973,9 +966,9 @@ class Worker
 
 		self::clearProcesses();
 
-		$workers = dba::fetch_first("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
+		$workers = self::activeWorkers();
 
-		if ($workers["processes"] == 0) {
+		if ($workers == 0) {
 			self::callWorker();
 		}
 	}
diff --git a/src/Model/Conversation.php b/src/Model/Conversation.php
index cd51a537ed..17f151fe03 100644
--- a/src/Model/Conversation.php
+++ b/src/Model/Conversation.php
@@ -52,8 +52,8 @@ class Conversation
 				$conversation['source'] = $arr['source'];
 			}
 
-			$old_conv = dba::fetch_first("SELECT `item-uri`, `reply-to-uri`, `conversation-uri`, `conversation-href`, `protocol`, `source`
-					FROM `conversation` WHERE `item-uri` = ?", $conversation['item-uri']);
+			$fields = ['item-uri', 'reply-to-uri', 'conversation-uri', 'conversation-href', 'protocol', 'source'];
+			$old_conv = dba::selectFirst('conversation', $fields, ['item-uri' => $conversation['item-uri']]);
 			if (DBM::is_result($old_conv)) {
 				// Don't update when only the source has changed.
 				// Only do this when there had been no source before.
diff --git a/src/Model/User.php b/src/Model/User.php
index 76769e14c7..6754f22070 100644
--- a/src/Model/User.php
+++ b/src/Model/User.php
@@ -211,18 +211,11 @@ class User
 					]
 				);
 			} else {
-				$user = dba::fetch_first('SELECT `uid`, `password`, `legacy_password`
-					FROM `user`
-					WHERE (`email` = ? OR `username` = ? OR `nickname` = ?)
-					AND `blocked` = 0
-					AND `account_expired` = 0
-					AND `account_removed` = 0
-					AND `verified` = 1
-					LIMIT 1',
-					$user_info,
-					$user_info,
-					$user_info
-				);
+				$fields = ['uid', 'password', 'legacy_password'];
+				$condition = ["(`email` = ? OR `username` = ? OR `nickname` = ?)
+					AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified`",
+					$user_info, $user_info, $user_info];
+				$user = dba::selectFirst('user', $fields, $condition);
 			}
 
 			if (!DBM::is_result($user)) {
diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php
index 9a24c75e46..a9e836499f 100644
--- a/src/Protocol/DFRN.php
+++ b/src/Protocol/DFRN.php
@@ -950,13 +950,13 @@ class DFRN
 		$conversation_uri = $conversation_href;
 
 		if (isset($parent_item)) {
-			$r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $item['parent-uri']);
-			if (DBM::is_result($r)) {
+			$conversation = dba::selectFirst('conversation', ['conversation-uri', 'conversation-href'], ['item-uri' => $item['parent-uri']]);
+			if (DBM::is_result($conversation)) {
 				if ($r['conversation-uri'] != '') {
-					$conversation_uri = $r['conversation-uri'];
+					$conversation_uri = $conversation['conversation-uri'];
 				}
 				if ($r['conversation-href'] != '') {
-					$conversation_href = $r['conversation-href'];
+					$conversation_href = $conversation['conversation-href'];
 				}
 			}
 		}
@@ -1537,13 +1537,11 @@ class DFRN
 		$author["name"] = $xpath->evaluate($element."/atom:name/text()", $context)->item(0)->nodeValue;
 		$author["link"] = $xpath->evaluate($element."/atom:uri/text()", $context)->item(0)->nodeValue;
 
-		$contact_old = dba::fetch_first("SELECT `id`, `uid`, `url`, `network`, `avatar-date`, `avatar`, `name-date`, `uri-date`, `addr`,
-				`name`, `nick`, `about`, `location`, `keywords`, `xmpp`, `bdyear`, `bd`, `hidden`, `contact-type`
-				FROM `contact` WHERE `uid` = ? AND `nurl` = ? AND `network` != ?",
-			$importer["importer_uid"],
-			normalise_link($author["link"]),
-			NETWORK_STATUSNET
-		);
+		$fields = ['id', 'uid', 'url', 'network', 'avatar-date', 'avatar', 'name-date', 'uri-date', 'addr',
+			'name', 'nick', 'about', 'location', 'keywords', 'xmpp', 'bdyear', 'bd', 'hidden', 'contact-type'];
+		$condition = ["`uid` = ? AND `nurl` = ? AND `network` != ?",
+			$importer["importer_uid"], normalise_link($author["link"]), NETWORK_STATUSNET];
+		$contact_old = dba::selectFirst('contact', $fields, $condition);
 
 		if (DBM::is_result($contact_old)) {
 			$author["contact-id"] = $contact_old["id"];
diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php
index e84dddceaf..957f60a2ce 100644
--- a/src/Protocol/OStatus.php
+++ b/src/Protocol/OStatus.php
@@ -1971,14 +1971,13 @@ class OStatus
 			$conversation_uri = $conversation_href;
 
 			if (isset($parent_item)) {
-				$r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $parent_item);
-
-				if (DBM::is_result($r)) {
+				$conversation = dba::selectFirst('conversation', ['conversation-uri', 'conversation-href'], ['item-uri' => $parent_item]);
+				if (DBM::is_result($conversation)) {
 					if ($r['conversation-uri'] != '') {
-						$conversation_uri = $r['conversation-uri'];
+						$conversation_uri = $conversation['conversation-uri'];
 					}
 					if ($r['conversation-href'] != '') {
-						$conversation_href = $r['conversation-href'];
+						$conversation_href = $conversation['conversation-href'];
 					}
 				}
 			}
-- 
2.39.5