Simplify parameter binding, to bind values directly instead of references.

parent 28bafb15
...@@ -288,7 +288,7 @@ class DB ...@@ -288,7 +288,7 @@ class DB
return $default; return $default;
} }
//Convert each Entity item into an object based on its JSON reflection // Convert each Entity item into an object based on its JSON reflection
$json_entities = array_map(function ($entity) { $json_entities = array_map(function ($entity) {
return json_decode($entity, true); return json_decode($entity, true);
}, $entities); }, $entities);
...@@ -337,13 +337,13 @@ class DB ...@@ -337,13 +337,13 @@ class DB
(:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id) (:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id)
'); ');
$sth->bindParam(':id', $id, PDO::PARAM_STR); $sth->bindValue(':id', $id);
$sth->bindParam(':chat_id', $chat_id, PDO::PARAM_STR); $sth->bindValue(':chat_id', $chat_id);
$sth->bindParam(':message_id', $message_id, PDO::PARAM_STR); $sth->bindValue(':message_id', $message_id);
$sth->bindParam(':inline_query_id', $inline_query_id, PDO::PARAM_STR); $sth->bindValue(':edited_message_id', $edited_message_id);
$sth->bindParam(':chosen_inline_result_id', $chosen_inline_result_id, PDO::PARAM_STR); $sth->bindValue(':inline_query_id', $inline_query_id);
$sth->bindParam(':callback_query_id', $callback_query_id, PDO::PARAM_STR); $sth->bindValue(':chosen_inline_result_id', $chosen_inline_result_id);
$sth->bindParam(':edited_message_id', $edited_message_id, PDO::PARAM_STR); $sth->bindValue(':callback_query_id', $callback_query_id);
return $sth->execute(); return $sth->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
...@@ -367,19 +367,12 @@ class DB ...@@ -367,19 +367,12 @@ class DB
return false; return false;
} }
$user_id = $user->getId();
$is_bot = $user->getIsBot();
$username = $user->getUsername();
$first_name = $user->getFirstName();
$last_name = $user->getLastName();
$language_code = $user->getLanguageCode();
try { try {
$sth = self::$pdo->prepare(' $sth = self::$pdo->prepare('
INSERT INTO `' . TB_USER . '` INSERT INTO `' . TB_USER . '`
(`id`, `is_bot`, `username`, `first_name`, `last_name`, `language_code`, `created_at`, `updated_at`) (`id`, `is_bot`, `username`, `first_name`, `last_name`, `language_code`, `created_at`, `updated_at`)
VALUES VALUES
(:id, :is_bot,:username, :first_name, :last_name, :language_code, :created_at, :updated_at) (:id, :is_bot, :username, :first_name, :last_name, :language_code, :created_at, :updated_at)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
`is_bot` = VALUES(`is_bot`), `is_bot` = VALUES(`is_bot`),
`username` = VALUES(`username`), `username` = VALUES(`username`),
...@@ -389,14 +382,14 @@ class DB ...@@ -389,14 +382,14 @@ class DB
`updated_at` = VALUES(`updated_at`) `updated_at` = VALUES(`updated_at`)
'); ');
$sth->bindParam(':id', $user_id, PDO::PARAM_STR); $sth->bindValue(':id', $user->getId());
$sth->bindParam(':is_bot', $is_bot, PDO::PARAM_INT); $sth->bindValue(':is_bot', $user->getIsBot(), PDO::PARAM_INT);
$sth->bindParam(':username', $username, PDO::PARAM_STR, 255); $sth->bindValue(':username', $user->getUsername());
$sth->bindParam(':first_name', $first_name, PDO::PARAM_STR, 255); $sth->bindValue(':first_name', $user->getFirstName());
$sth->bindParam(':last_name', $last_name, PDO::PARAM_STR, 255); $sth->bindValue(':last_name', $user->getLastName());
$sth->bindParam(':language_code', $language_code, PDO::PARAM_STR, 10); $sth->bindValue(':language_code', $user->getLanguageCode());
$sth->bindParam(':created_at', $date, PDO::PARAM_STR); $sth->bindValue(':created_at', $date);
$sth->bindParam(':updated_at', $date, PDO::PARAM_STR); $sth->bindValue(':updated_at', $date);
$status = $sth->execute(); $status = $sth->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
...@@ -405,7 +398,6 @@ class DB ...@@ -405,7 +398,6 @@ class DB
// Also insert the relationship to the chat into the user_chat table // Also insert the relationship to the chat into the user_chat table
if ($chat instanceof Chat) { if ($chat instanceof Chat) {
$chat_id = $chat->getId();
try { try {
$sth = self::$pdo->prepare(' $sth = self::$pdo->prepare('
INSERT IGNORE INTO `' . TB_USER_CHAT . '` INSERT IGNORE INTO `' . TB_USER_CHAT . '`
...@@ -414,8 +406,8 @@ class DB ...@@ -414,8 +406,8 @@ class DB
(:user_id, :chat_id) (:user_id, :chat_id)
'); ');
$sth->bindParam(':user_id', $user_id, PDO::PARAM_STR); $sth->bindValue(':user_id', $user->getId());
$sth->bindParam(':chat_id', $chat_id, PDO::PARAM_STR); $sth->bindValue(':chat_id', $chat->getId());
$status = $sth->execute(); $status = $sth->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
...@@ -442,18 +434,12 @@ class DB ...@@ -442,18 +434,12 @@ class DB
return false; return false;
} }
$chat_id = $chat->getId();
$chat_title = $chat->getTitle();
$chat_username = $chat->getUsername();
$chat_type = $chat->getType();
$chat_all_members_are_administrators = $chat->getAllMembersAreAdministrators();
try { try {
$sth = self::$pdo->prepare(' $sth = self::$pdo->prepare('
INSERT IGNORE INTO `' . TB_CHAT . '` INSERT IGNORE INTO `' . TB_CHAT . '`
(`id`, `type`, `title`, `username`, `all_members_are_administrators`, `created_at` ,`updated_at`, `old_id`) (`id`, `type`, `title`, `username`, `all_members_are_administrators`, `created_at` ,`updated_at`, `old_id`)
VALUES VALUES
(:id, :type, :title, :username, :all_members_are_administrators, :created_at, :updated_at, :oldid) (:id, :type, :title, :username, :all_members_are_administrators, :created_at, :updated_at, :old_id)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
`type` = VALUES(`type`), `type` = VALUES(`type`),
`title` = VALUES(`title`), `title` = VALUES(`title`),
...@@ -462,22 +448,25 @@ class DB ...@@ -462,22 +448,25 @@ class DB
`updated_at` = VALUES(`updated_at`) `updated_at` = VALUES(`updated_at`)
'); ');
$chat_id = $chat->getId();
$chat_type = $chat->getType();
if ($migrate_to_chat_id) { if ($migrate_to_chat_id) {
$chat_type = 'supergroup'; $chat_type = 'supergroup';
$sth->bindParam(':id', $migrate_to_chat_id, PDO::PARAM_STR); $sth->bindValue(':id', $migrate_to_chat_id);
$sth->bindParam(':oldid', $chat_id, PDO::PARAM_STR); $sth->bindValue(':old_id', $chat_id);
} else { } else {
$sth->bindParam(':id', $chat_id, PDO::PARAM_STR); $sth->bindValue(':id', $chat_id);
$sth->bindParam(':oldid', $migrate_to_chat_id, PDO::PARAM_STR); $sth->bindValue(':old_id', $migrate_to_chat_id);
} }
$sth->bindParam(':type', $chat_type, PDO::PARAM_STR); $sth->bindValue(':type', $chat_type);
$sth->bindParam(':title', $chat_title, PDO::PARAM_STR, 255); $sth->bindValue(':title', $chat->getTitle());
$sth->bindParam(':username', $chat_username, PDO::PARAM_STR, 255); $sth->bindValue(':username', $chat->getUsername());
$sth->bindParam(':all_members_are_administrators', $chat_all_members_are_administrators, PDO::PARAM_INT); $sth->bindValue(':all_members_are_administrators', $chat->getAllMembersAreAdministrators(), PDO::PARAM_INT);
$sth->bindParam(':created_at', $date, PDO::PARAM_STR); $sth->bindValue(':created_at', $date);
$sth->bindParam(':updated_at', $date, PDO::PARAM_STR); $sth->bindValue(':updated_at', $date);
return $sth->execute(); return $sth->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
...@@ -635,28 +624,24 @@ class DB ...@@ -635,28 +624,24 @@ class DB
INSERT IGNORE INTO `' . TB_INLINE_QUERY . '` INSERT IGNORE INTO `' . TB_INLINE_QUERY . '`
(`id`, `user_id`, `location`, `query`, `offset`, `created_at`) (`id`, `user_id`, `location`, `query`, `offset`, `created_at`)
VALUES VALUES
(:inline_query_id, :user_id, :location, :query, :param_offset, :created_at) (:id, :user_id, :location, :query, :offset, :created_at)
'); ');
$date = self::getTimestamp(); $date = self::getTimestamp();
$inline_query_id = $inline_query->getId();
$from = $inline_query->getFrom();
$user_id = null; $user_id = null;
if ($from instanceof User) {
$user_id = $from->getId();
self::insertUser($from, $date);
}
$location = $inline_query->getLocation(); $user = $inline_query->getFrom();
$query = $inline_query->getQuery(); if ($user instanceof User) {
$offset = $inline_query->getOffset(); $user_id = $user->getId();
self::insertUser($user, $date);
}
$sth->bindParam(':inline_query_id', $inline_query_id, PDO::PARAM_STR); $sth->bindValue(':id', $inline_query->getId());
$sth->bindParam(':user_id', $user_id, PDO::PARAM_STR); $sth->bindValue(':user_id', $user_id);
$sth->bindParam(':location', $location, PDO::PARAM_STR); $sth->bindValue(':location', $inline_query->getLocation());
$sth->bindParam(':query', $query, PDO::PARAM_STR); $sth->bindValue(':query', $inline_query->getQuery());
$sth->bindParam(':param_offset', $offset, PDO::PARAM_STR); $sth->bindValue(':offset', $inline_query->getOffset());
$sth->bindParam(':created_at', $date, PDO::PARAM_STR); $sth->bindValue(':created_at', $date);
return $sth->execute(); return $sth->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
...@@ -687,24 +672,20 @@ class DB ...@@ -687,24 +672,20 @@ class DB
'); ');
$date = self::getTimestamp(); $date = self::getTimestamp();
$result_id = $chosen_inline_result->getResultId();
$from = $chosen_inline_result->getFrom();
$user_id = null; $user_id = null;
if ($from instanceof User) {
$user_id = $from->getId();
self::insertUser($from, $date);
}
$location = $chosen_inline_result->getLocation(); $user = $chosen_inline_result->getFrom();
$inline_message_id = $chosen_inline_result->getInlineMessageId(); if ($user instanceof User) {
$query = $chosen_inline_result->getQuery(); $user_id = $user->getId();
self::insertUser($user, $date);
}
$sth->bindParam(':result_id', $result_id, PDO::PARAM_STR); $sth->bindValue(':result_id', $chosen_inline_result->getResultId());
$sth->bindParam(':user_id', $user_id, PDO::PARAM_STR); $sth->bindValue(':user_id', $user_id);
$sth->bindParam(':location', $location, PDO::PARAM_STR); $sth->bindValue(':location', $chosen_inline_result->getLocation());
$sth->bindParam(':inline_message_id', $inline_message_id, PDO::PARAM_STR); $sth->bindValue(':inline_message_id', $chosen_inline_result->getInlineMessageId());
$sth->bindParam(':query', $query, PDO::PARAM_STR); $sth->bindValue(':query', $chosen_inline_result->getQuery());
$sth->bindParam(':created_at', $date, PDO::PARAM_STR); $sth->bindValue(':created_at', $date);
return $sth->execute(); return $sth->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
...@@ -731,16 +712,16 @@ class DB ...@@ -731,16 +712,16 @@ class DB
INSERT IGNORE INTO `' . TB_CALLBACK_QUERY . '` INSERT IGNORE INTO `' . TB_CALLBACK_QUERY . '`
(`id`, `user_id`, `chat_id`, `message_id`, `inline_message_id`, `data`, `created_at`) (`id`, `user_id`, `chat_id`, `message_id`, `inline_message_id`, `data`, `created_at`)
VALUES VALUES
(:callback_query_id, :user_id, :chat_id, :message_id, :inline_message_id, :data, :created_at) (:id, :user_id, :chat_id, :message_id, :inline_message_id, :data, :created_at)
'); ');
$date = self::getTimestamp(); $date = self::getTimestamp();
$callback_query_id = $callback_query->getId();
$from = $callback_query->getFrom();
$user_id = null; $user_id = null;
if ($from instanceof User) {
$user_id = $from->getId(); $user = $callback_query->getFrom();
self::insertUser($from, $date); if ($user instanceof User) {
$user_id = $user->getId();
self::insertUser($user, $date);
} }
$message = $callback_query->getMessage(); $message = $callback_query->getMessage();
...@@ -765,16 +746,13 @@ class DB ...@@ -765,16 +746,13 @@ class DB
} }
} }
$inline_message_id = $callback_query->getInlineMessageId(); $sth->bindValue(':id', $callback_query->getId());
$data = $callback_query->getData(); $sth->bindValue(':user_id', $user_id);
$sth->bindValue(':chat_id', $chat_id);
$sth->bindParam(':callback_query_id', $callback_query_id, PDO::PARAM_STR); $sth->bindValue(':message_id', $message_id);
$sth->bindParam(':user_id', $user_id, PDO::PARAM_STR); $sth->bindValue(':inline_message_id', $callback_query->getInlineMessageId());
$sth->bindParam(':chat_id', $chat_id, PDO::PARAM_STR); $sth->bindValue(':data', $callback_query->getData());
$sth->bindParam(':message_id', $message_id, PDO::PARAM_STR); $sth->bindValue(':created_at', $date);
$sth->bindParam(':inline_message_id', $inline_message_id, PDO::PARAM_STR);
$sth->bindParam(':data', $data, PDO::PARAM_STR);
$sth->bindParam(':created_at', $date, PDO::PARAM_STR);
return $sth->execute(); return $sth->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
...@@ -785,6 +763,8 @@ class DB ...@@ -785,6 +763,8 @@ class DB
/** /**
* Insert Message request in db * Insert Message request in db
* *
* @todo Complete with new fields: https://core.telegram.org/bots/api#message
*
* @param Message $message * @param Message $message
* *
* @return bool If the insert was successful * @return bool If the insert was successful
...@@ -796,47 +776,40 @@ class DB ...@@ -796,47 +776,40 @@ class DB
return false; return false;
} }
$from = $message->getFrom();
$chat = $message->getChat();
$chat_id = $chat->getId();
$date = self::getTimestamp($message->getDate()); $date = self::getTimestamp($message->getDate());
$forward_from = $message->getForwardFrom(); // Insert chat, update chat id in case it migrated
$forward_from_chat = $message->getForwardFromChat(); $chat = $message->getChat();
$forward_from_message_id = $message->getForwardFromMessageId(); self::insertChat($chat, $date, $message->getMigrateToChatId());
$photo = self::entitiesArrayToJson($message->getPhoto(), '');
$entities = self::entitiesArrayToJson($message->getEntities(), null);
$new_chat_members = $message->getNewChatMembers();
$left_chat_member = $message->getLeftChatMember();
$new_chat_photo = self::entitiesArrayToJson($message->getNewChatPhoto(), '');
$migrate_to_chat_id = $message->getMigrateToChatId();
//Insert chat, update chat id in case it migrated
self::insertChat($chat, $date, $migrate_to_chat_id);
//Insert user and the relation with the chat // Insert user and the relation with the chat
if (is_object($from)) { $user = $message->getFrom();
self::insertUser($from, $date, $chat); if ($user instanceof User) {
self::insertUser($user, $date, $chat);
} }
// Insert the forwarded message user in users table // Insert the forwarded message user in users table
$forward_date = null;
$forward_from = $message->getForwardFrom();
if ($forward_from instanceof User) { if ($forward_from instanceof User) {
$forward_date = self::getTimestamp($message->getForwardDate());
self::insertUser($forward_from, $forward_date); self::insertUser($forward_from, $forward_date);
$forward_from = $forward_from->getId(); $forward_from = $forward_from->getId();
$forward_date = self::getTimestamp($message->getForwardDate());
} }
$forward_from_chat = $message->getForwardFromChat();
if ($forward_from_chat instanceof Chat) { if ($forward_from_chat instanceof Chat) {
$forward_date = self::getTimestamp($message->getForwardDate());
self::insertChat($forward_from_chat, $forward_date); self::insertChat($forward_from_chat, $forward_date);
$forward_from_chat = $forward_from_chat->getId(); $forward_from_chat = $forward_from_chat->getId();
$forward_date = self::getTimestamp($message->getForwardDate());
} }
// New and left chat member // New and left chat member
$new_chat_members_ids = null;
$left_chat_member_id = null;
$new_chat_members = $message->getNewChatMembers();
$left_chat_member = $message->getLeftChatMember();
if (!empty($new_chat_members)) { if (!empty($new_chat_members)) {
$new_chat_members_ids = [];
foreach ($new_chat_members as $new_chat_member) { foreach ($new_chat_members as $new_chat_member) {
if ($new_chat_member instanceof User) { if ($new_chat_member instanceof User) {
// Insert the new chat user // Insert the new chat user
...@@ -848,7 +821,7 @@ class DB ...@@ -848,7 +821,7 @@ class DB
} elseif ($left_chat_member instanceof User) { } elseif ($left_chat_member instanceof User) {
// Insert the left chat user // Insert the left chat user
self::insertUser($left_chat_member, $date, $chat); self::insertUser($left_chat_member, $date, $chat);
$left_chat_member = $left_chat_member->getId(); $left_chat_member_id = $left_chat_member->getId();
} }
try { try {
...@@ -873,13 +846,11 @@ class DB ...@@ -873,13 +846,11 @@ class DB
) )
'); ');
$message_id = $message->getMessageId(); $user_id = null;
if ($user instanceof User) {
if (is_object($from)) { $user_id = $user->getId();
$from_id = $from->getId();
} else {
$from_id = null;
} }
$chat_id = $chat->getId();
$reply_to_message = $message->getReplyToMessage(); $reply_to_message = $message->getReplyToMessage();
$reply_to_message_id = null; $reply_to_message_id = null;
...@@ -890,66 +861,46 @@ class DB ...@@ -890,66 +861,46 @@ class DB
self::insertMessageRequest($reply_to_message); self::insertMessageRequest($reply_to_message);
} }
$text = $message->getText(); $sth->bindValue(':message_id', $message->getMessageId());
$audio = $message->getAudio(); $sth->bindValue(':chat_id', $chat_id);
$document = $message->getDocument(); $sth->bindValue(':user_id', $user_id);
$sticker = $message->getSticker(); $sth->bindValue(':date', $date);
$video = $message->getVideo(); $sth->bindValue(':forward_from', $forward_from);
$voice = $message->getVoice(); $sth->bindValue(':forward_from_chat', $forward_from_chat);
$video_note = $message->getVideoNote(); $sth->bindValue(':forward_from_message_id', $message->getForwardFromMessageId());
$caption = $message->getCaption(); $sth->bindValue(':forward_date', $forward_date);
$contact = $message->getContact();
$location = $message->getLocation();
$venue = $message->getVenue();
$new_chat_title = $message->getNewChatTitle();
$delete_chat_photo = $message->getDeleteChatPhoto();
$group_chat_created = $message->getGroupChatCreated();
$supergroup_chat_created = $message->getSupergroupChatCreated();
$channel_chat_created = $message->getChannelChatCreated();
$migrate_from_chat_id = $message->getMigrateFromChatId();
$migrate_to_chat_id = $message->getMigrateToChatId();
$pinned_message = $message->getPinnedMessage();
$sth->bindParam(':chat_id', $chat_id, PDO::PARAM_STR);
$sth->bindParam(':message_id', $message_id, PDO::PARAM_STR);
$sth->bindParam(':user_id', $from_id, PDO::PARAM_STR);
$sth->bindParam(':date', $date, PDO::PARAM_STR);
$sth->bindParam(':forward_from', $forward_from, PDO::PARAM_STR);
$sth->bindParam(':forward_from_chat', $forward_from_chat, PDO::PARAM_STR);
$sth->bindParam(':forward_from_message_id', $forward_from_message_id, PDO::PARAM_STR);
$sth->bindParam(':forward_date', $forward_date, PDO::PARAM_STR);
$reply_to_chat_id = null; $reply_to_chat_id = null;
if ($reply_to_message_id) { if ($reply_to_message_id) {
$reply_to_chat_id = $chat_id; $reply_to_chat_id = $chat_id;
} }
$sth->bindValue(':reply_to_chat', $reply_to_chat_id);
$sth->bindParam(':reply_to_chat', $reply_to_chat_id, PDO::PARAM_STR); $sth->bindValue(':reply_to_message', $reply_to_message_id);
$sth->bindParam(':reply_to_message', $reply_to_message_id, PDO::PARAM_STR);
$sth->bindParam(':text', $text, PDO::PARAM_STR); $sth->bindValue(':text', $message->getText());
$sth->bindParam(':entities', $entities, PDO::PARAM_STR); $sth->bindValue(':entities', $t = self::entitiesArrayToJson($message->getEntities(), null));
$sth->bindParam(':audio', $audio, PDO::PARAM_STR); $sth->bindValue(':audio', $message->getAudio());
$sth->bindParam(':document', $document, PDO::PARAM_STR); $sth->bindValue(':document', $message->getDocument());
$sth->bindParam(':photo', $photo, PDO::PARAM_STR); $sth->bindValue(':photo', $t = self::entitiesArrayToJson($message->getPhoto(), null));
$sth->bindParam(':sticker', $sticker, PDO::PARAM_STR); $sth->bindValue(':sticker', $message->getSticker());
$sth->bindParam(':video', $video, PDO::PARAM_STR); $sth->bindValue(':video', $message->getVideo());
$sth->bindParam(':voice', $voice, PDO::PARAM_STR); $sth->bindValue(':voice', $message->getVoice());
$sth->bindParam(':video_note', $video_note, PDO::PARAM_STR); $sth->bindValue(':video_note', $message->getVideoNote());
$sth->bindParam(':caption', $caption, PDO::PARAM_STR); $sth->bindValue(':caption', $message->getCaption());
$sth->bindParam(':contact', $contact, PDO::PARAM_STR); $sth->bindValue(':contact', $message->getContact());
$sth->bindParam(':location', $location, PDO::PARAM_STR); $sth->bindValue(':location', $message->getLocation());
$sth->bindParam(':venue', $venue, PDO::PARAM_STR); $sth->bindValue(':venue', $message->getVenue());
$sth->bindParam(':new_chat_members', $new_chat_members_ids, PDO::PARAM_STR); $sth->bindValue(':new_chat_members', $new_chat_members_ids);
$sth->bindParam(':left_chat_member', $left_chat_member, PDO::PARAM_STR); $sth->bindValue(':left_chat_member', $left_chat_member_id);
$sth->bindParam(':new_chat_title', $new_chat_title, PDO::PARAM_STR); $sth->bindValue(':new_chat_title', $message->getNewChatTitle());
$sth->bindParam(':new_chat_photo', $new_chat_photo, PDO::PARAM_STR); $sth->bindValue(':new_chat_photo', $t = self::entitiesArrayToJson($message->getNewChatPhoto(), null));
$sth->bindParam(':delete_chat_photo', $delete_chat_photo, PDO::PARAM_INT); $sth->bindValue(':delete_chat_photo', $message->getDeleteChatPhoto());
$sth->bindParam(':group_chat_created', $group_chat_created, PDO::PARAM_INT); $sth->bindValue(':group_chat_created', $message->getGroupChatCreated());
$sth->bindParam(':supergroup_chat_created', $supergroup_chat_created, PDO::PARAM_INT); $sth->bindValue(':supergroup_chat_created', $message->getSupergroupChatCreated());
$sth->bindParam(':channel_chat_created', $channel_chat_created, PDO::PARAM_INT); $sth->bindValue(':channel_chat_created', $message->getChannelChatCreated());
$sth->bindParam(':migrate_from_chat_id', $migrate_from_chat_id, PDO::PARAM_STR); $sth->bindValue(':migrate_from_chat_id', $message->getMigrateFromChatId());
$sth->bindParam(':migrate_to_chat_id', $migrate_to_chat_id, PDO::PARAM_STR); $sth->bindValue(':migrate_to_chat_id', $message->getMigrateToChatId());
$sth->bindParam(':pinned_message', $pinned_message, PDO::PARAM_STR); $sth->bindValue(':pinned_message', $message->getPinnedMessage());
return $sth->execute(); return $sth->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
...@@ -971,24 +922,19 @@ class DB ...@@ -971,24 +922,19 @@ class DB
return false; return false;
} }
$from = $edited_message->getFrom(); try {
$chat = $edited_message->getChat();
$chat_id = $chat->getId();
$edit_date = self::getTimestamp($edited_message->getEditDate()); $edit_date = self::getTimestamp($edited_message->getEditDate());
$entities = self::entitiesArrayToJson($edited_message->getEntities(), null); // Insert chat
$chat = $edited_message->getChat();
//Insert chat
self::insertChat($chat, $edit_date); self::insertChat($chat, $edit_date);
//Insert user and the relation with the chat // Insert user and the relation with the chat
if (is_object($from)) { $user = $edited_message->getFrom();
self::insertUser($from, $edit_date, $chat); if ($user instanceof User) {
self::insertUser($user, $edit_date, $chat);
} }
try {
$sth = self::$pdo->prepare(' $sth = self::$pdo->prepare('
INSERT IGNORE INTO `' . TB_EDITED_MESSAGE . '` INSERT IGNORE INTO `' . TB_EDITED_MESSAGE . '`
(`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`) (`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`)
...@@ -996,24 +942,18 @@ class DB ...@@ -996,24 +942,18 @@ class DB
(:chat_id, :message_id, :user_id, :edit_date, :text, :entities, :caption) (:chat_id, :message_id, :user_id, :edit_date, :text, :entities, :caption)
'); ');
$message_id = $edited_message->getMessageId(); $user_id = null;
if ($user instanceof User) {
if (is_object($from)) { $user_id = $user->getId();
$from_id = $from->getId();
} else {
$from_id = null;
} }
$text = $edited_message->getText(); $sth->bindValue(':chat_id', $chat->getId());
$caption = $edited_message->getCaption(); $sth->bindValue(':message_id', $edited_message->getMessageId());
$sth->bindValue(':user_id', $user_id);
$sth->bindParam(':chat_id', $chat_id, PDO::PARAM_STR); $sth->bindValue(':edit_date', $edit_date);
$sth->bindParam(':message_id', $message_id, PDO::PARAM_STR); $sth->bindValue(':text', $edited_message->getText());
$sth->bindParam(':user_id', $from_id, PDO::PARAM_STR); $sth->bindValue(':entities', self::entitiesArrayToJson($edited_message->getEntities(), null));
$sth->bindParam(':edit_date', $edit_date, PDO::PARAM_STR); $sth->bindValue(':caption', $edited_message->getCaption());
$sth->bindParam(':text', $text, PDO::PARAM_STR);
$sth->bindParam(':entities', $entities, PDO::PARAM_STR);
$sth->bindParam(':caption', $caption, PDO::PARAM_STR);
return $sth->execute(); return $sth->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
...@@ -1151,20 +1091,20 @@ class DB ...@@ -1151,20 +1091,20 @@ class DB
try { try {
$sth = self::$pdo->prepare('SELECT $sth = self::$pdo->prepare('SELECT
(SELECT COUNT(DISTINCT `chat_id`) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :created_at_1) as LIMIT_PER_SEC_ALL, (SELECT COUNT(DISTINCT `chat_id`) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :created_at_1) AS LIMIT_PER_SEC_ALL,
(SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :created_at_2 AND ((`chat_id` = :chat_id_1 AND `inline_message_id` IS NULL) OR (`inline_message_id` = :inline_message_id AND `chat_id` IS NULL))) as LIMIT_PER_SEC, (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :created_at_2 AND ((`chat_id` = :chat_id_1 AND `inline_message_id` IS NULL) OR (`inline_message_id` = :inline_message_id AND `chat_id` IS NULL))) AS LIMIT_PER_SEC,
(SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :created_at_minute AND `chat_id` = :chat_id_2) as LIMIT_PER_MINUTE (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :created_at_minute AND `chat_id` = :chat_id_2) AS LIMIT_PER_MINUTE
'); ');
$date = self::getTimestamp(); $date = self::getTimestamp();
$date_minute = self::getTimestamp(strtotime('-1 minute')); $date_minute = self::getTimestamp(strtotime('-1 minute'));
$sth->bindParam(':chat_id_1', $chat_id, \PDO::PARAM_STR); $sth->bindValue(':chat_id_1', $chat_id);
$sth->bindParam(':chat_id_2', $chat_id, \PDO::PARAM_STR); $sth->bindValue(':chat_id_2', $chat_id);
$sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR); $sth->bindValue(':inline_message_id', $inline_message_id);
$sth->bindParam(':created_at_1', $date, \PDO::PARAM_STR); $sth->bindValue(':created_at_1', $date);
$sth->bindParam(':created_at_2', $date, \PDO::PARAM_STR); $sth->bindValue(':created_at_2', $date);
$sth->bindParam(':created_at_minute', $date_minute, \PDO::PARAM_STR); $sth->bindValue(':created_at_minute', $date_minute);
$sth->execute(); $sth->execute();
...@@ -1189,25 +1129,20 @@ class DB ...@@ -1189,25 +1129,20 @@ class DB
return false; return false;
} }
$chat_id = ((isset($data['chat_id'])) ? $data['chat_id'] : null);
$inline_message_id = (isset($data['inline_message_id']) ? $data['inline_message_id'] : null);
try { try {
$sth = self::$pdo->prepare('INSERT INTO `' . TB_REQUEST_LIMITER . '` $sth = self::$pdo->prepare('INSERT INTO `' . TB_REQUEST_LIMITER . '`
( (`method`, `chat_id`, `inline_message_id`, `created_at`)
`method`, `chat_id`, `inline_message_id`, `created_at` VALUES
) (:method, :chat_id, :inline_message_id, :created_at);
VALUES (
:method, :chat_id, :inline_message_id, :created_at
);
'); ');
$created_at = self::getTimestamp(); $chat_id = isset($data['chat_id']) ? $data['chat_id'] : null;
$inline_message_id = isset($data['inline_message_id']) ? $data['inline_message_id'] : null;
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR); $sth->bindValue(':chat_id', $chat_id);
$sth->bindParam(':inline_message_id', $inline_message_id, \PDO::PARAM_STR); $sth->bindValue(':inline_message_id', $inline_message_id);
$sth->bindParam(':method', $method, \PDO::PARAM_STR); $sth->bindValue(':method', $method);
$sth->bindParam(':created_at', $created_at, \PDO::PARAM_STR); $sth->bindValue(':created_at', self::getTimestamp());
return $sth->execute(); return $sth->execute();
} catch (\Exception $e) { } catch (\Exception $e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment