| Total Complexity | 221 |
| Total Lines | 1687 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 1 |
Complex classes like DB often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DB, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class DB |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * MySQL credentials |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected static $mysql_credentials = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * PDO object |
||
| 47 | * |
||
| 48 | * @var PDO |
||
| 49 | */ |
||
| 50 | protected static $pdo; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Table prefix |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected static $table_prefix; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Telegram class object |
||
| 61 | * |
||
| 62 | * @var Telegram |
||
| 63 | */ |
||
| 64 | protected static $telegram; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Initialize |
||
| 68 | * |
||
| 69 | * @param array $credentials Database connection details |
||
| 70 | * @param Telegram $telegram Telegram object to connect with this object |
||
| 71 | * @param string $table_prefix Table prefix |
||
| 72 | * @param string $encoding Database character encoding |
||
| 73 | * |
||
| 74 | * @return PDO PDO database object |
||
| 75 | * @throws TelegramException |
||
| 76 | */ |
||
| 77 | public static function initialize( |
||
| 78 | array $credentials, |
||
| 79 | Telegram $telegram, |
||
| 80 | $table_prefix = '', |
||
| 81 | $encoding = 'utf8mb4' |
||
| 82 | ): PDO { |
||
| 83 | if (empty($credentials)) { |
||
| 84 | throw new TelegramException('MySQL credentials not provided!'); |
||
| 85 | } |
||
| 86 | if (isset($credentials['unix_socket'])) { |
||
| 87 | $dsn = 'mysql:unix_socket=' . $credentials['unix_socket']; |
||
| 88 | } else { |
||
| 89 | $dsn = 'mysql:host=' . $credentials['host']; |
||
| 90 | } |
||
| 91 | $dsn .= ';dbname=' . $credentials['database']; |
||
| 92 | |||
| 93 | if (!empty($credentials['port'])) { |
||
| 94 | $dsn .= ';port=' . $credentials['port']; |
||
| 95 | } |
||
| 96 | |||
| 97 | $options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $encoding]; |
||
| 98 | try { |
||
| 99 | $pdo = new PDO($dsn, $credentials['user'], $credentials['password'], $options); |
||
| 100 | $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); |
||
| 101 | } catch (PDOException $e) { |
||
| 102 | throw new TelegramException($e->getMessage()); |
||
| 103 | } |
||
| 104 | |||
| 105 | self::$pdo = $pdo; |
||
| 106 | self::$telegram = $telegram; |
||
| 107 | self::$mysql_credentials = $credentials; |
||
| 108 | self::$table_prefix = $table_prefix; |
||
| 109 | |||
| 110 | self::defineTables(); |
||
| 111 | |||
| 112 | return self::$pdo; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * External Initialize |
||
| 117 | * |
||
| 118 | * Let you use the class with an external already existing Pdo Mysql connection. |
||
| 119 | * |
||
| 120 | * @param PDO $external_pdo_connection PDO database object |
||
| 121 | * @param Telegram $telegram Telegram object to connect with this object |
||
| 122 | * @param string $table_prefix Table prefix |
||
| 123 | * |
||
| 124 | * @return PDO PDO database object |
||
| 125 | * @throws TelegramException |
||
| 126 | */ |
||
| 127 | public static function externalInitialize( |
||
| 128 | PDO $external_pdo_connection, |
||
| 129 | Telegram $telegram, |
||
| 130 | string $table_prefix = '' |
||
| 131 | ): PDO { |
||
| 132 | if ($external_pdo_connection === null) { |
||
| 133 | throw new TelegramException('MySQL external connection not provided!'); |
||
| 134 | } |
||
| 135 | |||
| 136 | self::$pdo = $external_pdo_connection; |
||
| 137 | self::$telegram = $telegram; |
||
| 138 | self::$mysql_credentials = []; |
||
| 139 | self::$table_prefix = $table_prefix; |
||
| 140 | |||
| 141 | self::defineTables(); |
||
| 142 | |||
| 143 | return self::$pdo; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Define all the tables with the proper prefix |
||
| 148 | */ |
||
| 149 | protected static function defineTables(): void |
||
| 150 | { |
||
| 151 | $tables = [ |
||
| 152 | 'callback_query', |
||
| 153 | 'chat', |
||
| 154 | 'chat_boost_updated', |
||
| 155 | 'chat_boost_removed', |
||
| 156 | 'chat_join_request', |
||
| 157 | 'chat_member_updated', |
||
| 158 | 'chosen_inline_result', |
||
| 159 | 'edited_message', |
||
| 160 | 'inline_query', |
||
| 161 | 'message', |
||
| 162 | 'message_reaction', |
||
| 163 | 'message_reaction_count', |
||
| 164 | 'poll', |
||
| 165 | 'poll_answer', |
||
| 166 | 'pre_checkout_query', |
||
| 167 | 'request_limiter', |
||
| 168 | 'shipping_query', |
||
| 169 | 'telegram_update', |
||
| 170 | 'user', |
||
| 171 | 'user_chat', |
||
| 172 | ]; |
||
| 173 | foreach ($tables as $table) { |
||
| 174 | $table_name = 'TB_' . strtoupper($table); |
||
| 175 | if (!defined($table_name)) { |
||
| 176 | define($table_name, self::$table_prefix . $table); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Check if database connection has been created |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public static function isDbConnected(): bool |
||
| 187 | { |
||
| 188 | return self::$pdo !== null; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the PDO object of the connected database |
||
| 193 | * |
||
| 194 | * @return PDO|null |
||
| 195 | */ |
||
| 196 | public static function getPdo(): ?PDO |
||
| 197 | { |
||
| 198 | return self::$pdo; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Fetch update(s) from DB |
||
| 203 | * |
||
| 204 | * @param int $limit Limit the number of updates to fetch |
||
| 205 | * @param string $id Check for unique update id |
||
| 206 | * |
||
| 207 | * @return array|bool Fetched data or false if not connected |
||
| 208 | * @throws TelegramException |
||
| 209 | */ |
||
| 210 | public static function selectTelegramUpdate(int $limit = 0, string $id = '') |
||
| 211 | { |
||
| 212 | if (!self::isDbConnected()) { |
||
| 213 | return false; |
||
| 214 | } |
||
| 215 | |||
| 216 | try { |
||
| 217 | $sql = ' |
||
| 218 | SELECT `id` |
||
| 219 | FROM `' . TB_TELEGRAM_UPDATE . '` |
||
| 220 | '; |
||
| 221 | |||
| 222 | if ($id !== '') { |
||
| 223 | $sql .= ' WHERE `id` = :id'; |
||
| 224 | } else { |
||
| 225 | $sql .= ' ORDER BY `id` DESC'; |
||
| 226 | } |
||
| 227 | |||
| 228 | if ($limit > 0) { |
||
| 229 | $sql .= ' LIMIT :limit'; |
||
| 230 | } |
||
| 231 | |||
| 232 | $sth = self::$pdo->prepare($sql); |
||
| 233 | |||
| 234 | if ($limit > 0) { |
||
| 235 | $sth->bindValue(':limit', $limit, PDO::PARAM_INT); |
||
| 236 | } |
||
| 237 | if ($id !== '') { |
||
| 238 | $sth->bindValue(':id', $id); |
||
| 239 | } |
||
| 240 | |||
| 241 | $sth->execute(); |
||
| 242 | |||
| 243 | return $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 244 | } catch (PDOException $e) { |
||
| 245 | throw new TelegramException($e->getMessage()); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Fetch message(s) from DB |
||
| 251 | * |
||
| 252 | * @param int $limit Limit the number of messages to fetch |
||
| 253 | * |
||
| 254 | * @return array|bool Fetched data or false if not connected |
||
| 255 | * @throws TelegramException |
||
| 256 | */ |
||
| 257 | public static function selectMessages(int $limit = 0) |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Convert from unix timestamp to timestamp |
||
| 290 | * |
||
| 291 | * @param ?int $unixtime Unix timestamp (if empty, current timestamp is used) |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | protected static function getTimestamp(?int $unixtime = null): string |
||
| 296 | { |
||
| 297 | return date('Y-m-d H:i:s', $unixtime ?? time()); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Convert array of Entity items to a JSON array |
||
| 302 | * |
||
| 303 | * @todo Find a better way, as json_* functions are very heavy |
||
| 304 | * |
||
| 305 | * @param array $entities |
||
| 306 | * @param mixed $default |
||
| 307 | * |
||
| 308 | * @return mixed |
||
| 309 | */ |
||
| 310 | public static function entitiesArrayToJson(array $entities, $default = null) |
||
| 311 | { |
||
| 312 | if (empty($entities)) { |
||
| 313 | return $default; |
||
| 314 | } |
||
| 315 | |||
| 316 | // Convert each Entity item into an object based on its JSON reflection |
||
| 317 | $json_entities = array_map(function ($entity) { |
||
| 318 | return json_decode($entity, true); |
||
| 319 | }, $entities); |
||
| 320 | |||
| 321 | return json_encode($json_entities); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Insert entry to telegram_update table |
||
| 326 | * |
||
| 327 | * @throws TelegramException |
||
| 328 | */ |
||
| 329 | protected static function insertTelegramUpdate( |
||
| 330 | int $update_id, |
||
| 331 | ?int $chat_id = null, |
||
| 332 | ?int $message_id = null, |
||
| 333 | ?int $edited_message_id = null, |
||
| 334 | ?int $channel_post_id = null, |
||
| 335 | ?int $edited_channel_post_id = null, |
||
| 336 | ?string $message_reaction_id = null, |
||
| 337 | ?string $message_reaction_count_id = null, |
||
| 338 | ?string $inline_query_id = null, |
||
| 339 | ?string $chosen_inline_result_id = null, |
||
| 340 | ?string $callback_query_id = null, |
||
| 341 | ?string $shipping_query_id = null, |
||
| 342 | ?string $pre_checkout_query_id = null, |
||
| 343 | ?string $poll_id = null, |
||
| 344 | ?string $poll_answer_poll_id = null, |
||
| 345 | ?string $my_chat_member_updated_id = null, |
||
| 346 | ?string $chat_member_updated_id = null, |
||
| 347 | ?string $chat_join_request_id = null, |
||
| 348 | ?string $chat_boost_updated_id = null, |
||
| 349 | ?string $chat_boost_removed_id = null |
||
| 350 | ): ?bool { |
||
| 351 | if ($message_id === null && $edited_message_id === null && $channel_post_id === null && $edited_channel_post_id === null && $message_reaction_id === null && $message_reaction_count_id === null && $inline_query_id === null && $chosen_inline_result_id === null && $callback_query_id === null && $shipping_query_id === null && $pre_checkout_query_id === null && $poll_id === null && $poll_answer_poll_id === null && $my_chat_member_updated_id === null && $chat_member_updated_id === null && $chat_join_request_id === null && $chat_boost_updated_id === null && $chat_boost_removed_id === null) { |
||
| 352 | throw new TelegramException('message_id, edited_message_id, channel_post_id, edited_channel_post_id, message_reaction_id, message_reaction_count_id, inline_query_id, chosen_inline_result_id, callback_query_id, shipping_query_id, pre_checkout_query_id, poll_id, poll_answer_poll_id, my_chat_member_updated_id, chat_member_updated_id, chat_join_request_id, chat_boost_updated_id, chat_boost_removed_id are all null'); |
||
| 353 | } |
||
| 354 | |||
| 355 | if (!self::isDbConnected()) { |
||
| 356 | return false; |
||
| 357 | } |
||
| 358 | |||
| 359 | try { |
||
| 360 | $sth = self::$pdo->prepare(' |
||
| 361 | INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '` |
||
| 362 | ( |
||
| 363 | `id`, `chat_id`, `message_id`, `edited_message_id`, |
||
| 364 | `channel_post_id`, `edited_channel_post_id`, `message_reaction_id`, `message_reaction_count_id`, |
||
| 365 | `inline_query_id`, `chosen_inline_result_id`, |
||
| 366 | `callback_query_id`, `shipping_query_id`, `pre_checkout_query_id`, |
||
| 367 | `poll_id`, `poll_answer_poll_id`, `my_chat_member_updated_id`, `chat_member_updated_id`, |
||
| 368 | `chat_join_request_id`, `chat_boost_updated_id`, `chat_boost_removed_id` |
||
| 369 | ) VALUES ( |
||
| 370 | :id, :chat_id, :message_id, :edited_message_id, |
||
| 371 | :channel_post_id, :edited_channel_post_id, :message_reaction_id, :message_reaction_count_id, |
||
| 372 | :inline_query_id, :chosen_inline_result_id, |
||
| 373 | :callback_query_id, :shipping_query_id, :pre_checkout_query_id, |
||
| 374 | :poll_id, :poll_answer_poll_id, :my_chat_member_updated_id, :chat_member_updated_id, |
||
| 375 | :chat_join_request_id, :chat_boost_updated_id, :chat_boost_removed_id |
||
| 376 | ) |
||
| 377 | '); |
||
| 378 | |||
| 379 | $sth->bindValue(':id', $update_id); |
||
| 380 | $sth->bindValue(':chat_id', $chat_id); |
||
| 381 | $sth->bindValue(':message_id', $message_id); |
||
| 382 | $sth->bindValue(':edited_message_id', $edited_message_id); |
||
| 383 | $sth->bindValue(':channel_post_id', $channel_post_id); |
||
| 384 | $sth->bindValue(':edited_channel_post_id', $edited_channel_post_id); |
||
| 385 | $sth->bindValue(':message_reaction_id', $message_reaction_id); |
||
| 386 | $sth->bindValue(':message_reaction_count_id', $message_reaction_count_id); |
||
| 387 | $sth->bindValue(':inline_query_id', $inline_query_id); |
||
| 388 | $sth->bindValue(':chosen_inline_result_id', $chosen_inline_result_id); |
||
| 389 | $sth->bindValue(':callback_query_id', $callback_query_id); |
||
| 390 | $sth->bindValue(':shipping_query_id', $shipping_query_id); |
||
| 391 | $sth->bindValue(':pre_checkout_query_id', $pre_checkout_query_id); |
||
| 392 | $sth->bindValue(':poll_id', $poll_id); |
||
| 393 | $sth->bindValue(':poll_answer_poll_id', $poll_answer_poll_id); |
||
| 394 | $sth->bindValue(':my_chat_member_updated_id', $my_chat_member_updated_id); |
||
| 395 | $sth->bindValue(':chat_member_updated_id', $chat_member_updated_id); |
||
| 396 | $sth->bindValue(':chat_join_request_id', $chat_join_request_id); |
||
| 397 | $sth->bindValue(':chat_boost_updated_id', $chat_boost_updated_id); |
||
| 398 | $sth->bindValue(':chat_boost_removed_id', $chat_boost_removed_id); |
||
| 399 | |||
| 400 | return $sth->execute(); |
||
| 401 | } catch (PDOException $e) { |
||
| 402 | throw new TelegramException($e->getMessage()); |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Insert users and save their connection to chats |
||
| 408 | * |
||
| 409 | * @param User $user |
||
| 410 | * @param string|null $date |
||
| 411 | * @param Chat|null $chat |
||
| 412 | * |
||
| 413 | * @return bool If the insert was successful |
||
| 414 | * @throws TelegramException |
||
| 415 | */ |
||
| 416 | public static function insertUser(User $user, ?string $date = null, ?Chat $chat = null): bool |
||
| 417 | { |
||
| 418 | if (!self::isDbConnected()) { |
||
| 419 | return false; |
||
| 420 | } |
||
| 421 | |||
| 422 | try { |
||
| 423 | $sth = self::$pdo->prepare(' |
||
| 424 | INSERT INTO `' . TB_USER . '` |
||
| 425 | (`id`, `is_bot`, `username`, `first_name`, `last_name`, `language_code`, `is_premium`, `added_to_attachment_menu`, `created_at`, `updated_at`) |
||
| 426 | VALUES |
||
| 427 | (:id, :is_bot, :username, :first_name, :last_name, :language_code, :is_premium, :added_to_attachment_menu, :created_at, :updated_at) |
||
| 428 | ON DUPLICATE KEY UPDATE |
||
| 429 | `is_bot` = VALUES(`is_bot`), |
||
| 430 | `username` = VALUES(`username`), |
||
| 431 | `first_name` = VALUES(`first_name`), |
||
| 432 | `last_name` = VALUES(`last_name`), |
||
| 433 | `language_code` = VALUES(`language_code`), |
||
| 434 | `is_premium` = VALUES(`is_premium`), |
||
| 435 | `added_to_attachment_menu` = VALUES(`added_to_attachment_menu`), |
||
| 436 | `updated_at` = VALUES(`updated_at`) |
||
| 437 | '); |
||
| 438 | |||
| 439 | $sth->bindValue(':id', $user->getId()); |
||
| 440 | $sth->bindValue(':is_bot', $user->getIsBot(), PDO::PARAM_INT); |
||
| 441 | $sth->bindValue(':username', $user->getUsername()); |
||
| 442 | $sth->bindValue(':first_name', $user->getFirstName()); |
||
| 443 | $sth->bindValue(':last_name', $user->getLastName()); |
||
| 444 | $sth->bindValue(':language_code', $user->getLanguageCode()); |
||
| 445 | $sth->bindValue(':is_premium', $user->getIsPremium(), PDO::PARAM_INT); |
||
| 446 | $sth->bindValue(':added_to_attachment_menu', $user->getAddedToAttachmentMenu(), PDO::PARAM_INT); |
||
| 447 | $date = $date ?: self::getTimestamp(); |
||
| 448 | $sth->bindValue(':created_at', $date); |
||
| 449 | $sth->bindValue(':updated_at', $date); |
||
| 450 | |||
| 451 | $status = $sth->execute(); |
||
| 452 | } catch (PDOException $e) { |
||
| 453 | throw new TelegramException($e->getMessage()); |
||
| 454 | } |
||
| 455 | |||
| 456 | // Also insert the relationship to the chat into the user_chat table |
||
| 457 | if ($chat) { |
||
| 458 | try { |
||
| 459 | $sth = self::$pdo->prepare(' |
||
| 460 | INSERT IGNORE INTO `' . TB_USER_CHAT . '` |
||
| 461 | (`user_id`, `chat_id`) |
||
| 462 | VALUES |
||
| 463 | (:user_id, :chat_id) |
||
| 464 | '); |
||
| 465 | |||
| 466 | $sth->bindValue(':user_id', $user->getId()); |
||
| 467 | $sth->bindValue(':chat_id', $chat->getId()); |
||
| 468 | |||
| 469 | $status = $sth->execute(); |
||
| 470 | } catch (PDOException $e) { |
||
| 471 | throw new TelegramException($e->getMessage()); |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | return $status; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Insert chat |
||
| 480 | * |
||
| 481 | * @param Chat $chat |
||
| 482 | * @param string|null $date |
||
| 483 | * @param int|null $migrate_to_chat_id |
||
| 484 | * |
||
| 485 | * @return bool If the insert was successful |
||
| 486 | * @throws TelegramException |
||
| 487 | */ |
||
| 488 | public static function insertChat(Chat $chat, ?string $date = null, ?int $migrate_to_chat_id = null): ?bool |
||
| 489 | { |
||
| 490 | if (!self::isDbConnected()) { |
||
| 491 | return false; |
||
| 492 | } |
||
| 493 | |||
| 494 | try { |
||
| 495 | $sth = self::$pdo->prepare(' |
||
| 496 | INSERT IGNORE INTO `' . TB_CHAT . '` |
||
| 497 | (`id`, `type`, `title`, `username`, `first_name`, `last_name`, `is_forum`, `created_at` ,`updated_at`, `old_id`) |
||
| 498 | VALUES |
||
| 499 | (:id, :type, :title, :username, :first_name, :last_name, :is_forum, :created_at, :updated_at, :old_id) |
||
| 500 | ON DUPLICATE KEY UPDATE |
||
| 501 | `type` = VALUES(`type`), |
||
| 502 | `title` = VALUES(`title`), |
||
| 503 | `username` = VALUES(`username`), |
||
| 504 | `first_name` = VALUES(`first_name`), |
||
| 505 | `last_name` = VALUES(`last_name`), |
||
| 506 | `is_forum` = VALUES(`is_forum`), |
||
| 507 | `updated_at` = VALUES(`updated_at`) |
||
| 508 | '); |
||
| 509 | |||
| 510 | $chat_id = $chat->getId(); |
||
| 511 | $chat_type = $chat->getType(); |
||
| 512 | |||
| 513 | if ($migrate_to_chat_id !== null) { |
||
| 514 | $chat_type = 'supergroup'; |
||
| 515 | |||
| 516 | $sth->bindValue(':id', $migrate_to_chat_id); |
||
| 517 | $sth->bindValue(':old_id', $chat_id); |
||
| 518 | } else { |
||
| 519 | $sth->bindValue(':id', $chat_id); |
||
| 520 | $sth->bindValue(':old_id', $migrate_to_chat_id); |
||
| 521 | } |
||
| 522 | |||
| 523 | $sth->bindValue(':type', $chat_type); |
||
| 524 | $sth->bindValue(':title', $chat->getTitle()); |
||
| 525 | $sth->bindValue(':username', $chat->getUsername()); |
||
| 526 | $sth->bindValue(':first_name', $chat->getFirstName()); |
||
| 527 | $sth->bindValue(':last_name', $chat->getLastName()); |
||
| 528 | $sth->bindValue(':is_forum', $chat->getIsForum()); |
||
| 529 | $date = $date ?: self::getTimestamp(); |
||
| 530 | $sth->bindValue(':created_at', $date); |
||
| 531 | $sth->bindValue(':updated_at', $date); |
||
| 532 | |||
| 533 | return $sth->execute(); |
||
| 534 | } catch (PDOException $e) { |
||
| 535 | throw new TelegramException($e->getMessage()); |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Insert request into database |
||
| 541 | * |
||
| 542 | * @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails? |
||
| 543 | * |
||
| 544 | * @param Update $update |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | * @throws TelegramException |
||
| 548 | */ |
||
| 549 | public static function insertRequest(Update $update): bool |
||
| 550 | { |
||
| 551 | if (!self::isDbConnected()) { |
||
| 552 | return false; |
||
| 553 | } |
||
| 554 | |||
| 555 | $chat_id = null; |
||
| 556 | $message_id = null; |
||
| 557 | $edited_message_id = null; |
||
| 558 | $channel_post_id = null; |
||
| 559 | $edited_channel_post_id = null; |
||
| 560 | $message_reaction_id = null; |
||
| 561 | $message_reaction_count_id = null; |
||
| 562 | $inline_query_id = null; |
||
| 563 | $chosen_inline_result_id = null; |
||
| 564 | $callback_query_id = null; |
||
| 565 | $shipping_query_id = null; |
||
| 566 | $pre_checkout_query_id = null; |
||
| 567 | $poll_id = null; |
||
| 568 | $poll_answer_poll_id = null; |
||
| 569 | $my_chat_member_updated_id = null; |
||
| 570 | $chat_member_updated_id = null; |
||
| 571 | $chat_join_request_id = null; |
||
| 572 | $chat_boost_updated_id = null; |
||
| 573 | $chat_boost_removed_id = null; |
||
| 574 | |||
| 575 | if (($message = $update->getMessage()) && self::insertMessageRequest($message)) { |
||
| 576 | $chat_id = $message->getChat()->getId(); |
||
| 577 | $message_id = $message->getMessageId(); |
||
| 578 | } elseif (($edited_message = $update->getEditedMessage()) && self::insertEditedMessageRequest($edited_message)) { |
||
| 579 | $chat_id = $edited_message->getChat()->getId(); |
||
| 580 | $edited_message_id = (int) self::$pdo->lastInsertId(); |
||
| 581 | } elseif (($channel_post = $update->getChannelPost()) && self::insertMessageRequest($channel_post)) { |
||
| 582 | $chat_id = $channel_post->getChat()->getId(); |
||
| 583 | $channel_post_id = $channel_post->getMessageId(); |
||
| 584 | } elseif (($edited_channel_post = $update->getEditedChannelPost()) && self::insertEditedMessageRequest($edited_channel_post)) { |
||
| 585 | $chat_id = $edited_channel_post->getChat()->getId(); |
||
| 586 | $edited_channel_post_id = (int) self::$pdo->lastInsertId(); |
||
| 587 | } elseif (($message_reaction = $update->getMessageReaction()) && self::insertMessageReaction($message_reaction)) { |
||
| 588 | $chat_id = $message_reaction->getChat()->getId(); |
||
| 589 | $message_id = $message_reaction->getMessageId(); |
||
| 590 | $message_reaction_id = self::$pdo->lastInsertId(); |
||
| 591 | } elseif (($message_reaction_count = $update->getMessageReactionCount()) && self::insertMessageReactionCount($message_reaction_count)) { |
||
| 592 | $chat_id = $message_reaction_count->getChat()->getId(); |
||
| 593 | $message_id = $message_reaction_count->getMessageId(); |
||
| 594 | $message_reaction_count_id = self::$pdo->lastInsertId(); |
||
| 595 | } elseif (($inline_query = $update->getInlineQuery()) && self::insertInlineQueryRequest($inline_query)) { |
||
| 596 | $inline_query_id = $inline_query->getId(); |
||
| 597 | } elseif (($chosen_inline_result = $update->getChosenInlineResult()) && self::insertChosenInlineResultRequest($chosen_inline_result)) { |
||
| 598 | $chosen_inline_result_id = self::$pdo->lastInsertId(); |
||
| 599 | } elseif (($callback_query = $update->getCallbackQuery()) && self::insertCallbackQueryRequest($callback_query)) { |
||
| 600 | $callback_query_id = $callback_query->getId(); |
||
| 601 | } elseif (($shipping_query = $update->getShippingQuery()) && self::insertShippingQueryRequest($shipping_query)) { |
||
| 602 | $shipping_query_id = $shipping_query->getId(); |
||
| 603 | } elseif (($pre_checkout_query = $update->getPreCheckoutQuery()) && self::insertPreCheckoutQueryRequest($pre_checkout_query)) { |
||
| 604 | $pre_checkout_query_id = $pre_checkout_query->getId(); |
||
| 605 | } elseif (($poll = $update->getPoll()) && self::insertPollRequest($poll)) { |
||
| 606 | $poll_id = $poll->getId(); |
||
| 607 | } elseif (($poll_answer = $update->getPollAnswer()) && self::insertPollAnswerRequest($poll_answer)) { |
||
| 608 | $poll_answer_poll_id = $poll_answer->getPollId(); |
||
| 609 | } elseif (($my_chat_member = $update->getMyChatMember()) && self::insertChatMemberUpdatedRequest($my_chat_member)) { |
||
| 610 | $my_chat_member_updated_id = self::$pdo->lastInsertId(); |
||
| 611 | } elseif (($chat_member = $update->getChatMember()) && self::insertChatMemberUpdatedRequest($chat_member)) { |
||
| 612 | $chat_member_updated_id = self::$pdo->lastInsertId(); |
||
| 613 | } elseif (($chat_join_request = $update->getChatJoinRequest()) && self::insertChatJoinRequestRequest($chat_join_request)) { |
||
| 614 | $chat_join_request_id = self::$pdo->lastInsertId(); |
||
| 615 | } elseif (($chat_boost_updated = $update->getChatBoost()) && self::insertChatBoostUpdatedRequest($chat_boost_updated)) { |
||
| 616 | $chat_boost_updated_id = self::$pdo->lastInsertId(); |
||
| 617 | } elseif (($chat_boost_removed = $update->getRemovedChatBoost()) && self::insertChatBoostRemovedRequest($chat_boost_removed)) { |
||
| 618 | $chat_boost_removed_id = self::$pdo->lastInsertId(); |
||
| 619 | } else { |
||
| 620 | return false; |
||
| 621 | } |
||
| 622 | |||
| 623 | return self::insertTelegramUpdate( |
||
| 624 | $update->getUpdateId(), |
||
| 625 | $chat_id, |
||
| 626 | $message_id, |
||
| 627 | $edited_message_id, |
||
| 628 | $channel_post_id, |
||
| 629 | $edited_channel_post_id, |
||
| 630 | $message_reaction_id, |
||
| 631 | $message_reaction_count_id, |
||
| 632 | $inline_query_id, |
||
| 633 | $chosen_inline_result_id, |
||
| 634 | $callback_query_id, |
||
| 635 | $shipping_query_id, |
||
| 636 | $pre_checkout_query_id, |
||
| 637 | $poll_id, |
||
| 638 | $poll_answer_poll_id, |
||
| 639 | $my_chat_member_updated_id, |
||
| 640 | $chat_member_updated_id, |
||
| 641 | $chat_join_request_id, |
||
| 642 | $chat_boost_updated_id, |
||
| 643 | $chat_boost_removed_id |
||
| 644 | ); |
||
| 645 | } |
||
| 646 | |||
| 647 | public static function insertMessageReaction(MessageReactionUpdated $message_reaction): bool |
||
| 648 | { |
||
| 649 | if (!self::isDbConnected()) { |
||
| 650 | return false; |
||
| 651 | } |
||
| 652 | |||
| 653 | try { |
||
| 654 | $sth = self::$pdo->prepare(' |
||
| 655 | INSERT IGNORE INTO `' . TB_MESSAGE_REACTION . '` |
||
|
|
|||
| 656 | (`chat_id`, `message_id`, `user_id`, `actor_chat_id`, `old_reaction`, `new_reaction`, `created_at`) |
||
| 657 | VALUES |
||
| 658 | (:chat_id, :message_id, :user_id, :actor_chat_id, :old_reaction, :new_reaction, :created_at) |
||
| 659 | '); |
||
| 660 | |||
| 661 | $date = self::getTimestamp($message_reaction->getDate()); |
||
| 662 | $chat_id = null; |
||
| 663 | $user_id = null; |
||
| 664 | $actor_chat_id = null; |
||
| 665 | |||
| 666 | if ($chat = $message_reaction->getChat()) { |
||
| 667 | $chat_id = $chat->getId(); |
||
| 668 | self::insertChat($chat, $date); |
||
| 669 | } |
||
| 670 | if ($user = $message_reaction->getUser()) { |
||
| 671 | $user_id = $user->getId(); |
||
| 672 | self::insertUser($user, $date); |
||
| 673 | } |
||
| 674 | if ($actor_chat = $message_reaction->getActorChat()) { |
||
| 675 | $actor_chat_id = $actor_chat->getId(); |
||
| 676 | self::insertChat($actor_chat, $date); |
||
| 677 | } |
||
| 678 | |||
| 679 | $sth->bindValue(':chat_id', $chat_id); |
||
| 680 | $sth->bindValue(':message_id', $message_reaction->getMessageId()); |
||
| 681 | $sth->bindValue(':user_id', $user_id); |
||
| 682 | $sth->bindValue(':actor_chat_id', $actor_chat_id); |
||
| 683 | $sth->bindValue(':old_reaction', self::entitiesArrayToJson($message_reaction->getOldReaction() ?: [])); |
||
| 684 | $sth->bindValue(':new_reaction', self::entitiesArrayToJson($message_reaction->getNewReaction() ?: [])); |
||
| 685 | $sth->bindValue(':created_at', $date); |
||
| 686 | |||
| 687 | return $sth->execute(); |
||
| 688 | } catch (PDOException $e) { |
||
| 689 | throw new TelegramException($e->getMessage()); |
||
| 690 | } |
||
| 691 | } |
||
| 692 | |||
| 693 | public static function insertMessageReactionCount(MessageReactionCountUpdated $message_reaction_count): bool |
||
| 694 | { |
||
| 695 | if (!self::isDbConnected()) { |
||
| 696 | return false; |
||
| 697 | } |
||
| 698 | |||
| 699 | try { |
||
| 700 | $sth = self::$pdo->prepare(' |
||
| 701 | INSERT IGNORE INTO `' . TB_MESSAGE_REACTION_COUNT . '` |
||
| 702 | (`chat_id`, `message_id`, `reactions`, `created_at`) |
||
| 703 | VALUES |
||
| 704 | (:chat_id, :message_id, :reactions, :created_at) |
||
| 705 | '); |
||
| 706 | |||
| 707 | $date = self::getTimestamp($message_reaction_count->getDate()); |
||
| 708 | $chat_id = null; |
||
| 709 | |||
| 710 | if ($chat = $message_reaction->getChat()) { |
||
| 711 | $chat_id = $chat->getId(); |
||
| 712 | self::insertChat($chat, $date); |
||
| 713 | } |
||
| 714 | |||
| 715 | $sth->bindValue(':chat_id', $chat_id); |
||
| 716 | $sth->bindValue(':message_id', $message_reaction_count->getMessageId()); |
||
| 717 | $sth->bindValue(':reactions', $message_reaction_count->getReactions()); |
||
| 718 | $sth->bindValue(':created_at', $date); |
||
| 719 | |||
| 720 | return $sth->execute(); |
||
| 721 | } catch (PDOException $e) { |
||
| 722 | throw new TelegramException($e->getMessage()); |
||
| 723 | } |
||
| 724 | } |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Insert inline query request into database |
||
| 728 | * |
||
| 729 | * @param InlineQuery $inline_query |
||
| 730 | * |
||
| 731 | * @return bool If the insert was successful |
||
| 732 | * @throws TelegramException |
||
| 733 | */ |
||
| 734 | public static function insertInlineQueryRequest(InlineQuery $inline_query): bool |
||
| 735 | { |
||
| 736 | if (!self::isDbConnected()) { |
||
| 737 | return false; |
||
| 738 | } |
||
| 739 | |||
| 740 | try { |
||
| 741 | $sth = self::$pdo->prepare(' |
||
| 742 | INSERT IGNORE INTO `' . TB_INLINE_QUERY . '` |
||
| 743 | (`id`, `user_id`, `location`, `query`, `offset`, `chat_type`, `created_at`) |
||
| 744 | VALUES |
||
| 745 | (:id, :user_id, :location, :query, :offset, :chat_type, :created_at) |
||
| 746 | '); |
||
| 747 | |||
| 748 | $date = self::getTimestamp(); |
||
| 749 | $user_id = null; |
||
| 750 | |||
| 751 | if ($user = $inline_query->getFrom()) { |
||
| 752 | $user_id = $user->getId(); |
||
| 753 | self::insertUser($user, $date); |
||
| 754 | } |
||
| 755 | |||
| 756 | $sth->bindValue(':id', $inline_query->getId()); |
||
| 757 | $sth->bindValue(':user_id', $user_id); |
||
| 758 | $sth->bindValue(':location', $inline_query->getLocation()); |
||
| 759 | $sth->bindValue(':query', $inline_query->getQuery()); |
||
| 760 | $sth->bindValue(':offset', $inline_query->getOffset()); |
||
| 761 | $sth->bindValue(':chat_type', $inline_query->getChatType()); |
||
| 762 | $sth->bindValue(':created_at', $date); |
||
| 763 | |||
| 764 | return $sth->execute(); |
||
| 765 | } catch (PDOException $e) { |
||
| 766 | throw new TelegramException($e->getMessage()); |
||
| 767 | } |
||
| 768 | } |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Insert chosen inline result request into database |
||
| 772 | * |
||
| 773 | * @param ChosenInlineResult $chosen_inline_result |
||
| 774 | * |
||
| 775 | * @return bool If the insert was successful |
||
| 776 | * @throws TelegramException |
||
| 777 | */ |
||
| 778 | public static function insertChosenInlineResultRequest(ChosenInlineResult $chosen_inline_result): bool |
||
| 779 | { |
||
| 780 | if (!self::isDbConnected()) { |
||
| 781 | return false; |
||
| 782 | } |
||
| 783 | |||
| 784 | try { |
||
| 785 | $sth = self::$pdo->prepare(' |
||
| 786 | INSERT INTO `' . TB_CHOSEN_INLINE_RESULT . '` |
||
| 787 | (`result_id`, `user_id`, `location`, `inline_message_id`, `query`, `created_at`) |
||
| 788 | VALUES |
||
| 789 | (:result_id, :user_id, :location, :inline_message_id, :query, :created_at) |
||
| 790 | '); |
||
| 791 | |||
| 792 | $date = self::getTimestamp(); |
||
| 793 | $user_id = null; |
||
| 794 | |||
| 795 | if ($user = $chosen_inline_result->getFrom()) { |
||
| 796 | $user_id = $user->getId(); |
||
| 797 | self::insertUser($user, $date); |
||
| 798 | } |
||
| 799 | |||
| 800 | $sth->bindValue(':result_id', $chosen_inline_result->getResultId()); |
||
| 801 | $sth->bindValue(':user_id', $user_id); |
||
| 802 | $sth->bindValue(':location', $chosen_inline_result->getLocation()); |
||
| 803 | $sth->bindValue(':inline_message_id', $chosen_inline_result->getInlineMessageId()); |
||
| 804 | $sth->bindValue(':query', $chosen_inline_result->getQuery()); |
||
| 805 | $sth->bindValue(':created_at', $date); |
||
| 806 | |||
| 807 | return $sth->execute(); |
||
| 808 | } catch (PDOException $e) { |
||
| 809 | throw new TelegramException($e->getMessage()); |
||
| 810 | } |
||
| 811 | } |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Insert callback query request into database |
||
| 815 | * |
||
| 816 | * @param CallbackQuery $callback_query |
||
| 817 | * |
||
| 818 | * @return bool If the insert was successful |
||
| 819 | * @throws TelegramException |
||
| 820 | */ |
||
| 821 | public static function insertCallbackQueryRequest(CallbackQuery $callback_query): bool |
||
| 822 | { |
||
| 823 | if (!self::isDbConnected()) { |
||
| 824 | return false; |
||
| 825 | } |
||
| 826 | |||
| 827 | try { |
||
| 828 | $sth = self::$pdo->prepare(' |
||
| 829 | INSERT IGNORE INTO `' . TB_CALLBACK_QUERY . '` |
||
| 830 | (`id`, `user_id`, `chat_id`, `message_id`, `inline_message_id`, `chat_instance`, `data`, `game_short_name`, `created_at`) |
||
| 831 | VALUES |
||
| 832 | (:id, :user_id, :chat_id, :message_id, :inline_message_id, :chat_instance, :data, :game_short_name, :created_at) |
||
| 833 | '); |
||
| 834 | |||
| 835 | $date = self::getTimestamp(); |
||
| 836 | $user_id = null; |
||
| 837 | |||
| 838 | if ($user = $callback_query->getFrom()) { |
||
| 839 | $user_id = $user->getId(); |
||
| 840 | self::insertUser($user, $date); |
||
| 841 | } |
||
| 842 | |||
| 843 | $chat_id = null; |
||
| 844 | $message_id = null; |
||
| 845 | if ($message = $callback_query->getMessage()) { |
||
| 846 | $chat_id = $message->getChat()->getId(); |
||
| 847 | $message_id = $message->getMessageId(); |
||
| 848 | |||
| 849 | $is_message = self::$pdo->query(' |
||
| 850 | SELECT * |
||
| 851 | FROM `' . TB_MESSAGE . '` |
||
| 852 | WHERE `id` = ' . $message_id . ' |
||
| 853 | AND `chat_id` = ' . $chat_id . ' |
||
| 854 | LIMIT 1 |
||
| 855 | ')->rowCount(); |
||
| 856 | |||
| 857 | if ($is_message) { |
||
| 858 | self::insertEditedMessageRequest($message); |
||
| 859 | } else { |
||
| 860 | self::insertMessageRequest($message); |
||
| 861 | } |
||
| 862 | } |
||
| 863 | |||
| 864 | $sth->bindValue(':id', $callback_query->getId()); |
||
| 865 | $sth->bindValue(':user_id', $user_id); |
||
| 866 | $sth->bindValue(':chat_id', $chat_id); |
||
| 867 | $sth->bindValue(':message_id', $message_id); |
||
| 868 | $sth->bindValue(':inline_message_id', $callback_query->getInlineMessageId()); |
||
| 869 | $sth->bindValue(':chat_instance', $callback_query->getChatInstance()); |
||
| 870 | $sth->bindValue(':data', $callback_query->getData()); |
||
| 871 | $sth->bindValue(':game_short_name', $callback_query->getGameShortName()); |
||
| 872 | $sth->bindValue(':created_at', $date); |
||
| 873 | |||
| 874 | return $sth->execute(); |
||
| 875 | } catch (PDOException $e) { |
||
| 876 | throw new TelegramException($e->getMessage()); |
||
| 877 | } |
||
| 878 | } |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Insert shipping query request into database |
||
| 882 | * |
||
| 883 | * @param ShippingQuery $shipping_query |
||
| 884 | * |
||
| 885 | * @return bool If the insert was successful |
||
| 886 | * @throws TelegramException |
||
| 887 | */ |
||
| 888 | public static function insertShippingQueryRequest(ShippingQuery $shipping_query): bool |
||
| 889 | { |
||
| 890 | if (!self::isDbConnected()) { |
||
| 891 | return false; |
||
| 892 | } |
||
| 893 | |||
| 894 | try { |
||
| 895 | $sth = self::$pdo->prepare(' |
||
| 896 | INSERT IGNORE INTO `' . TB_SHIPPING_QUERY . '` |
||
| 897 | (`id`, `user_id`, `invoice_payload`, `shipping_address`, `created_at`) |
||
| 898 | VALUES |
||
| 899 | (:id, :user_id, :invoice_payload, :shipping_address, :created_at) |
||
| 900 | '); |
||
| 901 | |||
| 902 | $date = self::getTimestamp(); |
||
| 903 | $user_id = null; |
||
| 904 | |||
| 905 | if ($user = $shipping_query->getFrom()) { |
||
| 906 | $user_id = $user->getId(); |
||
| 907 | self::insertUser($user, $date); |
||
| 908 | } |
||
| 909 | |||
| 910 | $sth->bindValue(':id', $shipping_query->getId()); |
||
| 911 | $sth->bindValue(':user_id', $user_id); |
||
| 912 | $sth->bindValue(':invoice_payload', $shipping_query->getInvoicePayload()); |
||
| 913 | $sth->bindValue(':shipping_address', $shipping_query->getShippingAddress()); |
||
| 914 | $sth->bindValue(':created_at', $date); |
||
| 915 | |||
| 916 | return $sth->execute(); |
||
| 917 | } catch (PDOException $e) { |
||
| 918 | throw new TelegramException($e->getMessage()); |
||
| 919 | } |
||
| 920 | } |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Insert pre checkout query request into database |
||
| 924 | * |
||
| 925 | * @param PreCheckoutQuery $pre_checkout_query |
||
| 926 | * |
||
| 927 | * @return bool If the insert was successful |
||
| 928 | * @throws TelegramException |
||
| 929 | */ |
||
| 930 | public static function insertPreCheckoutQueryRequest(PreCheckoutQuery $pre_checkout_query): bool |
||
| 931 | { |
||
| 932 | if (!self::isDbConnected()) { |
||
| 933 | return false; |
||
| 934 | } |
||
| 935 | |||
| 936 | try { |
||
| 937 | $sth = self::$pdo->prepare(' |
||
| 938 | INSERT IGNORE INTO `' . TB_PRE_CHECKOUT_QUERY . '` |
||
| 939 | (`id`, `user_id`, `currency`, `total_amount`, `invoice_payload`, `shipping_option_id`, `order_info`, `created_at`) |
||
| 940 | VALUES |
||
| 941 | (:id, :user_id, :currency, :total_amount, :invoice_payload, :shipping_option_id, :order_info, :created_at) |
||
| 942 | '); |
||
| 943 | |||
| 944 | $date = self::getTimestamp(); |
||
| 945 | $user_id = null; |
||
| 946 | |||
| 947 | if ($user = $pre_checkout_query->getFrom()) { |
||
| 948 | $user_id = $user->getId(); |
||
| 949 | self::insertUser($user, $date); |
||
| 950 | } |
||
| 951 | |||
| 952 | $sth->bindValue(':id', $pre_checkout_query->getId()); |
||
| 953 | $sth->bindValue(':user_id', $user_id); |
||
| 954 | $sth->bindValue(':currency', $pre_checkout_query->getCurrency()); |
||
| 955 | $sth->bindValue(':total_amount', $pre_checkout_query->getTotalAmount()); |
||
| 956 | $sth->bindValue(':invoice_payload', $pre_checkout_query->getInvoicePayload()); |
||
| 957 | $sth->bindValue(':shipping_option_id', $pre_checkout_query->getShippingOptionId()); |
||
| 958 | $sth->bindValue(':order_info', $pre_checkout_query->getOrderInfo()); |
||
| 959 | $sth->bindValue(':created_at', $date); |
||
| 960 | |||
| 961 | return $sth->execute(); |
||
| 962 | } catch (PDOException $e) { |
||
| 963 | throw new TelegramException($e->getMessage()); |
||
| 964 | } |
||
| 965 | } |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Insert poll request into database |
||
| 969 | * |
||
| 970 | * @param Poll $poll |
||
| 971 | * |
||
| 972 | * @return bool If the insert was successful |
||
| 973 | * @throws TelegramException |
||
| 974 | */ |
||
| 975 | public static function insertPollRequest(Poll $poll): bool |
||
| 976 | { |
||
| 977 | if (!self::isDbConnected()) { |
||
| 978 | return false; |
||
| 979 | } |
||
| 980 | |||
| 981 | try { |
||
| 982 | $sth = self::$pdo->prepare(' |
||
| 983 | INSERT INTO `' . TB_POLL . '` |
||
| 984 | (`id`, `question`, `options`, `total_voter_count`, `is_closed`, `is_anonymous`, `type`, `allows_multiple_answers`, `correct_option_id`, `explanation`, `explanation_entities`, `open_period`, `close_date`, `created_at`) |
||
| 985 | VALUES |
||
| 986 | (:id, :question, :options, :total_voter_count, :is_closed, :is_anonymous, :type, :allows_multiple_answers, :correct_option_id, :explanation, :explanation_entities, :open_period, :close_date, :created_at) |
||
| 987 | ON DUPLICATE KEY UPDATE |
||
| 988 | `options` = VALUES(`options`), |
||
| 989 | `total_voter_count` = VALUES(`total_voter_count`), |
||
| 990 | `is_closed` = VALUES(`is_closed`), |
||
| 991 | `is_anonymous` = VALUES(`is_anonymous`), |
||
| 992 | `type` = VALUES(`type`), |
||
| 993 | `allows_multiple_answers` = VALUES(`allows_multiple_answers`), |
||
| 994 | `correct_option_id` = VALUES(`correct_option_id`), |
||
| 995 | `explanation` = VALUES(`explanation`), |
||
| 996 | `explanation_entities` = VALUES(`explanation_entities`), |
||
| 997 | `open_period` = VALUES(`open_period`), |
||
| 998 | `close_date` = VALUES(`close_date`) |
||
| 999 | '); |
||
| 1000 | |||
| 1001 | $sth->bindValue(':id', $poll->getId()); |
||
| 1002 | $sth->bindValue(':question', $poll->getQuestion()); |
||
| 1003 | $sth->bindValue(':options', self::entitiesArrayToJson($poll->getOptions() ?: [])); |
||
| 1004 | $sth->bindValue(':total_voter_count', $poll->getTotalVoterCount()); |
||
| 1005 | $sth->bindValue(':is_closed', $poll->getIsClosed(), PDO::PARAM_INT); |
||
| 1006 | $sth->bindValue(':is_anonymous', $poll->getIsAnonymous(), PDO::PARAM_INT); |
||
| 1007 | $sth->bindValue(':type', $poll->getType()); |
||
| 1008 | $sth->bindValue(':allows_multiple_answers', $poll->getAllowsMultipleAnswers(), PDO::PARAM_INT); |
||
| 1009 | $sth->bindValue(':correct_option_id', $poll->getCorrectOptionId()); |
||
| 1010 | $sth->bindValue(':explanation', $poll->getExplanation()); |
||
| 1011 | $sth->bindValue(':explanation_entities', self::entitiesArrayToJson($poll->getExplanationEntities() ?: [])); |
||
| 1012 | $sth->bindValue(':open_period', $poll->getOpenPeriod()); |
||
| 1013 | $sth->bindValue(':close_date', self::getTimestamp($poll->getCloseDate())); |
||
| 1014 | $sth->bindValue(':created_at', self::getTimestamp()); |
||
| 1015 | |||
| 1016 | return $sth->execute(); |
||
| 1017 | } catch (PDOException $e) { |
||
| 1018 | throw new TelegramException($e->getMessage()); |
||
| 1019 | } |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * Insert poll answer request into database |
||
| 1024 | * |
||
| 1025 | * @param PollAnswer $poll_answer |
||
| 1026 | * |
||
| 1027 | * @return bool If the insert was successful |
||
| 1028 | * @throws TelegramException |
||
| 1029 | */ |
||
| 1030 | public static function insertPollAnswerRequest(PollAnswer $poll_answer): bool |
||
| 1031 | { |
||
| 1032 | if (!self::isDbConnected()) { |
||
| 1033 | return false; |
||
| 1034 | } |
||
| 1035 | |||
| 1036 | try { |
||
| 1037 | $sth = self::$pdo->prepare(' |
||
| 1038 | INSERT INTO `' . TB_POLL_ANSWER . '` |
||
| 1039 | (`poll_id`, `user_id`, `option_ids`, `created_at`) |
||
| 1040 | VALUES |
||
| 1041 | (:poll_id, :user_id, :option_ids, :created_at) |
||
| 1042 | ON DUPLICATE KEY UPDATE |
||
| 1043 | `option_ids` = VALUES(`option_ids`) |
||
| 1044 | '); |
||
| 1045 | |||
| 1046 | $date = self::getTimestamp(); |
||
| 1047 | $user_id = null; |
||
| 1048 | |||
| 1049 | if ($user = $poll_answer->getUser()) { |
||
| 1050 | $user_id = $user->getId(); |
||
| 1051 | self::insertUser($user, $date); |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | $sth->bindValue(':poll_id', $poll_answer->getPollId()); |
||
| 1055 | $sth->bindValue(':user_id', $user_id); |
||
| 1056 | $sth->bindValue(':option_ids', json_encode($poll_answer->getOptionIds())); |
||
| 1057 | $sth->bindValue(':created_at', $date); |
||
| 1058 | |||
| 1059 | return $sth->execute(); |
||
| 1060 | } catch (PDOException $e) { |
||
| 1061 | throw new TelegramException($e->getMessage()); |
||
| 1062 | } |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Insert chat member updated request into database |
||
| 1067 | * |
||
| 1068 | * @param ChatMemberUpdated $chat_member_updated |
||
| 1069 | * |
||
| 1070 | * @return bool If the insert was successful |
||
| 1071 | * @throws TelegramException |
||
| 1072 | */ |
||
| 1073 | public static function insertChatMemberUpdatedRequest(ChatMemberUpdated $chat_member_updated): bool |
||
| 1074 | { |
||
| 1075 | if (!self::isDbConnected()) { |
||
| 1076 | return false; |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | try { |
||
| 1080 | $sth = self::$pdo->prepare(' |
||
| 1081 | INSERT INTO `' . TB_CHAT_MEMBER_UPDATED . '` |
||
| 1082 | (`chat_id`, `user_id`, `date`, `old_chat_member`, `new_chat_member`, `invite_link`, `created_at`) |
||
| 1083 | VALUES |
||
| 1084 | (:chat_id, :user_id, :date, :old_chat_member, :new_chat_member, :invite_link, :created_at) |
||
| 1085 | '); |
||
| 1086 | |||
| 1087 | $date = self::getTimestamp(); |
||
| 1088 | $chat_id = null; |
||
| 1089 | $user_id = null; |
||
| 1090 | |||
| 1091 | if ($chat = $chat_member_updated->getChat()) { |
||
| 1092 | $chat_id = $chat->getId(); |
||
| 1093 | self::insertChat($chat, $date); |
||
| 1094 | } |
||
| 1095 | if ($user = $chat_member_updated->getFrom()) { |
||
| 1096 | $user_id = $user->getId(); |
||
| 1097 | self::insertUser($user, $date); |
||
| 1098 | } |
||
| 1099 | |||
| 1100 | $sth->bindValue(':chat_id', $chat_id); |
||
| 1101 | $sth->bindValue(':user_id', $user_id); |
||
| 1102 | $sth->bindValue(':date', self::getTimestamp($chat_member_updated->getDate())); |
||
| 1103 | $sth->bindValue(':old_chat_member', $chat_member_updated->getOldChatMember()); |
||
| 1104 | $sth->bindValue(':new_chat_member', $chat_member_updated->getNewChatMember()); |
||
| 1105 | $sth->bindValue(':invite_link', $chat_member_updated->getInviteLink()); |
||
| 1106 | $sth->bindValue(':created_at', $date); |
||
| 1107 | |||
| 1108 | return $sth->execute(); |
||
| 1109 | } catch (PDOException $e) { |
||
| 1110 | throw new TelegramException($e->getMessage()); |
||
| 1111 | } |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Insert chat join request into database |
||
| 1116 | * |
||
| 1117 | * @param ChatJoinRequest $chat_join_request |
||
| 1118 | * |
||
| 1119 | * @return bool If the insert was successful |
||
| 1120 | * @throws TelegramException |
||
| 1121 | */ |
||
| 1122 | public static function insertChatJoinRequestRequest(ChatJoinRequest $chat_join_request): bool |
||
| 1123 | { |
||
| 1124 | if (!self::isDbConnected()) { |
||
| 1125 | return false; |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | try { |
||
| 1129 | $sth = self::$pdo->prepare(' |
||
| 1130 | INSERT INTO `' . TB_CHAT_JOIN_REQUEST . '` |
||
| 1131 | (`chat_id`, `user_id`, `date`, `bio`, `invite_link`, `created_at`) |
||
| 1132 | VALUES |
||
| 1133 | (:chat_id, :user_id, :date, :bio, :invite_link, :created_at) |
||
| 1134 | '); |
||
| 1135 | |||
| 1136 | $date = self::getTimestamp(); |
||
| 1137 | $chat_id = null; |
||
| 1138 | $user_id = null; |
||
| 1139 | |||
| 1140 | if ($chat = $chat_join_request->getChat()) { |
||
| 1141 | $chat_id = $chat->getId(); |
||
| 1142 | self::insertChat($chat, $date); |
||
| 1143 | } |
||
| 1144 | if ($user = $chat_join_request->getFrom()) { |
||
| 1145 | $user_id = $user->getId(); |
||
| 1146 | self::insertUser($user, $date); |
||
| 1147 | } |
||
| 1148 | |||
| 1149 | $sth->bindValue(':chat_id', $chat_id); |
||
| 1150 | $sth->bindValue(':user_id', $user_id); |
||
| 1151 | $sth->bindValue(':date', self::getTimestamp($chat_join_request->getDate())); |
||
| 1152 | $sth->bindValue(':bio', $chat_join_request->getBio()); |
||
| 1153 | $sth->bindValue(':invite_link', $chat_join_request->getInviteLink()); |
||
| 1154 | $sth->bindValue(':created_at', $date); |
||
| 1155 | |||
| 1156 | return $sth->execute(); |
||
| 1157 | } catch (PDOException $e) { |
||
| 1158 | throw new TelegramException($e->getMessage()); |
||
| 1159 | } |
||
| 1160 | } |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Insert chat boost updated into database |
||
| 1164 | * |
||
| 1165 | * @param ChatBoostUpdated $chat_boost_updated |
||
| 1166 | * |
||
| 1167 | * @return bool If the insert was successful |
||
| 1168 | * @throws TelegramException |
||
| 1169 | */ |
||
| 1170 | public static function insertChatBoostUpdatedRequest(ChatBoostUpdated $chat_boost_updated): bool |
||
| 1171 | { |
||
| 1172 | if (!self::isDbConnected()) { |
||
| 1173 | return false; |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | try { |
||
| 1177 | $sth = self::$pdo->prepare(' |
||
| 1178 | INSERT INTO `' . TB_CHAT_BOOST_UPDATED . '` |
||
| 1179 | (`chat_id`, `boost`, `created_at`) |
||
| 1180 | VALUES |
||
| 1181 | (:chat_id, :boost, :created_at) |
||
| 1182 | '); |
||
| 1183 | |||
| 1184 | $date = self::getTimestamp(); |
||
| 1185 | $chat_id = null; |
||
| 1186 | |||
| 1187 | if ($chat = $chat_boost_updated->getChat()) { |
||
| 1188 | $chat_id = $chat->getId(); |
||
| 1189 | self::insertChat($chat, $date); |
||
| 1190 | } |
||
| 1191 | |||
| 1192 | $sth->bindValue(':chat_id', $chat_id); |
||
| 1193 | $sth->bindValue(':boost', $chat_boost_updated->getBoost()); |
||
| 1194 | $sth->bindValue(':created_at', $date); |
||
| 1195 | |||
| 1196 | return $sth->execute(); |
||
| 1197 | } catch (PDOException $e) { |
||
| 1198 | throw new TelegramException($e->getMessage()); |
||
| 1199 | } |
||
| 1200 | } |
||
| 1201 | |||
| 1202 | /** |
||
| 1203 | * Insert chat boost removed into database |
||
| 1204 | * |
||
| 1205 | * @param ChatBoostRemoved $chat_boost_removed |
||
| 1206 | * |
||
| 1207 | * @return bool If the insert was successful |
||
| 1208 | * @throws TelegramException |
||
| 1209 | */ |
||
| 1210 | public static function insertChatBoostRemovedRequest(ChatBoostRemoved $chat_boost_removed): bool |
||
| 1211 | { |
||
| 1212 | if (!self::isDbConnected()) { |
||
| 1213 | return false; |
||
| 1214 | } |
||
| 1215 | |||
| 1216 | try { |
||
| 1217 | $sth = self::$pdo->prepare(' |
||
| 1218 | INSERT INTO `' . TB_CHAT_BOOST_REMOVED . '` |
||
| 1219 | (`chat_id`, `boost_id`, `remove_date`, `source`, `created_at`) |
||
| 1220 | VALUES |
||
| 1221 | (:chat_id, :boost_id, :remove_date, :source, :created_at) |
||
| 1222 | '); |
||
| 1223 | |||
| 1224 | $date = self::getTimestamp(); |
||
| 1225 | $chat_id = null; |
||
| 1226 | |||
| 1227 | if ($chat = $chat_boost_removed->getChat()) { |
||
| 1228 | $chat_id = $chat->getId(); |
||
| 1229 | self::insertChat($chat, $date); |
||
| 1230 | } |
||
| 1231 | |||
| 1232 | $sth->bindValue(':chat_id', $chat_id); |
||
| 1233 | $sth->bindValue(':boost_id', $chat_boost_removed->getBoostId()); |
||
| 1234 | $sth->bindValue(':remove_date', self::getTimestamp($chat_boost_removed->getRemoveDate())); |
||
| 1235 | $sth->bindValue(':source', $chat_boost_removed->getSource()); |
||
| 1236 | $sth->bindValue(':created_at', $date); |
||
| 1237 | |||
| 1238 | return $sth->execute(); |
||
| 1239 | } catch (PDOException $e) { |
||
| 1240 | throw new TelegramException($e->getMessage()); |
||
| 1241 | } |
||
| 1242 | } |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Insert Message request in db |
||
| 1246 | * |
||
| 1247 | * @param Message $message |
||
| 1248 | * |
||
| 1249 | * @return bool If the insert was successful |
||
| 1250 | * @throws TelegramException |
||
| 1251 | */ |
||
| 1252 | public static function insertMessageRequest(Message $message): bool |
||
| 1253 | { |
||
| 1254 | if (!self::isDbConnected()) { |
||
| 1255 | return false; |
||
| 1256 | } |
||
| 1257 | |||
| 1258 | $date = self::getTimestamp($message->getDate()); |
||
| 1259 | |||
| 1260 | // Insert chat, update chat id in case it migrated |
||
| 1261 | $chat = $message->getChat(); |
||
| 1262 | self::insertChat($chat, $date, $message->getMigrateToChatId()); |
||
| 1263 | |||
| 1264 | $sender_chat_id = null; |
||
| 1265 | if ($sender_chat = $message->getSenderChat()) { |
||
| 1266 | self::insertChat($sender_chat); |
||
| 1267 | $sender_chat_id = $sender_chat->getId(); |
||
| 1268 | } |
||
| 1269 | |||
| 1270 | // Insert user and the relation with the chat |
||
| 1271 | if ($user = $message->getFrom()) { |
||
| 1272 | self::insertUser($user, $date, $chat); |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | // Insert the forwarded message user in users table |
||
| 1276 | $forward_date = $message->getForwardDate() ? self::getTimestamp($message->getForwardDate()) : null; |
||
| 1277 | |||
| 1278 | if ($forward_from = $message->getForwardFrom()) { |
||
| 1279 | self::insertUser($forward_from); |
||
| 1280 | $forward_from = $forward_from->getId(); |
||
| 1281 | } |
||
| 1282 | if ($forward_from_chat = $message->getForwardFromChat()) { |
||
| 1283 | self::insertChat($forward_from_chat); |
||
| 1284 | $forward_from_chat = $forward_from_chat->getId(); |
||
| 1285 | } |
||
| 1286 | |||
| 1287 | $via_bot_id = null; |
||
| 1288 | if ($via_bot = $message->getViaBot()) { |
||
| 1289 | self::insertUser($via_bot); |
||
| 1290 | $via_bot_id = $via_bot->getId(); |
||
| 1291 | } |
||
| 1292 | |||
| 1293 | // New and left chat member |
||
| 1294 | $new_chat_members_ids = null; |
||
| 1295 | $left_chat_member_id = null; |
||
| 1296 | |||
| 1297 | $new_chat_members = $message->getNewChatMembers(); |
||
| 1298 | $left_chat_member = $message->getLeftChatMember(); |
||
| 1299 | if (!empty($new_chat_members)) { |
||
| 1300 | foreach ($new_chat_members as $new_chat_member) { |
||
| 1301 | if ($new_chat_member instanceof User) { |
||
| 1302 | // Insert the new chat user |
||
| 1303 | self::insertUser($new_chat_member, $date, $chat); |
||
| 1304 | $new_chat_members_ids[] = $new_chat_member->getId(); |
||
| 1305 | } |
||
| 1306 | } |
||
| 1307 | $new_chat_members_ids = implode(',', $new_chat_members_ids); |
||
| 1308 | } elseif ($left_chat_member) { |
||
| 1309 | // Insert the left chat user |
||
| 1310 | self::insertUser($left_chat_member, $date, $chat); |
||
| 1311 | $left_chat_member_id = $left_chat_member->getId(); |
||
| 1312 | } |
||
| 1313 | |||
| 1314 | try { |
||
| 1315 | $sth = self::$pdo->prepare(' |
||
| 1316 | INSERT IGNORE INTO `' . TB_MESSAGE . '` |
||
| 1317 | ( |
||
| 1318 | `id`, `user_id`, `chat_id`, `message_thread_id`, `sender_chat_id`, `date`, `forward_from`, `forward_from_chat`, `forward_from_message_id`, |
||
| 1319 | `forward_signature`, `forward_sender_name`, `forward_date`, `is_topic_message`, |
||
| 1320 | `reply_to_chat`, `reply_to_message`, `external_reply`, `via_bot`, `link_preview_options`, `edit_date`, `media_group_id`, `author_signature`, `text`, `entities`, `caption_entities`, |
||
| 1321 | `audio`, `document`, `animation`, `game`, `photo`, `sticker`, `story`, `video`, `voice`, `video_note`, `caption`, `has_media_spoiler`, `contact`, |
||
| 1322 | `location`, `venue`, `poll`, `dice`, `new_chat_members`, `left_chat_member`, |
||
| 1323 | `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`, |
||
| 1324 | `supergroup_chat_created`, `channel_chat_created`, `message_auto_delete_timer_changed`, `migrate_to_chat_id`, `migrate_from_chat_id`, |
||
| 1325 | `pinned_message`, `invoice`, `successful_payment`, `user_shared`, `chat_shared`, `connected_website`, `write_access_allowed`, `passport_data`, `proximity_alert_triggered`, |
||
| 1326 | `forum_topic_created`, `forum_topic_edited`, `forum_topic_closed`, `forum_topic_reopened`, `general_forum_topic_hidden`, `general_forum_topic_unhidden`, |
||
| 1327 | `video_chat_scheduled`, `video_chat_started`, `video_chat_ended`, `video_chat_participants_invited`, `web_app_data`, `reply_markup` |
||
| 1328 | ) VALUES ( |
||
| 1329 | :message_id, :user_id, :chat_id, :message_thread_id, :sender_chat_id, :date, :forward_from, :forward_from_chat, :forward_from_message_id, |
||
| 1330 | :forward_signature, :forward_sender_name, :forward_date, :is_topic_message, |
||
| 1331 | :reply_to_chat, :reply_to_message, :external_reply, :via_bot, :link_preview_options, :edit_date, :media_group_id, :author_signature, :text, :entities, :caption_entities, |
||
| 1332 | :audio, :document, :animation, :game, :photo, :sticker, :story, :video, :voice, :video_note, :caption, :has_media_spoiler, :contact, |
||
| 1333 | :location, :venue, :poll, :dice, :new_chat_members, :left_chat_member, |
||
| 1334 | :new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created, |
||
| 1335 | :supergroup_chat_created, :channel_chat_created, :message_auto_delete_timer_changed, :migrate_to_chat_id, :migrate_from_chat_id, |
||
| 1336 | :pinned_message, :invoice, :successful_payment, :user_shared, :chat_shared, :connected_website, :write_access_allowed, :passport_data, :proximity_alert_triggered, |
||
| 1337 | :forum_topic_created, :forum_topic_edited, :forum_topic_closed, :forum_topic_reopened, :general_forum_topic_hidden, :general_forum_topic_unhidden, |
||
| 1338 | :video_chat_scheduled, :video_chat_started, :video_chat_ended, :video_chat_participants_invited, :web_app_data, :reply_markup |
||
| 1339 | ) |
||
| 1340 | '); |
||
| 1341 | |||
| 1342 | $user_id = $user ? $user->getId() : null; |
||
| 1343 | $chat_id = $chat->getId(); |
||
| 1344 | |||
| 1345 | $reply_to_message_id = null; |
||
| 1346 | if ($reply_to_message = $message->getReplyToMessage()) { |
||
| 1347 | $reply_to_message_id = $reply_to_message->getMessageId(); |
||
| 1348 | // please notice that, as explained in the documentation, reply_to_message don't contain other |
||
| 1349 | // reply_to_message field so recursion deep is 1 |
||
| 1350 | self::insertMessageRequest($reply_to_message); |
||
| 1351 | } |
||
| 1352 | |||
| 1353 | $sth->bindValue(':message_id', $message->getMessageId()); |
||
| 1354 | $sth->bindValue(':chat_id', $chat_id); |
||
| 1355 | $sth->bindValue(':sender_chat_id', $sender_chat_id); |
||
| 1356 | $sth->bindValue(':message_thread_id', $message->getMessageThreadId()); |
||
| 1357 | $sth->bindValue(':user_id', $user_id); |
||
| 1358 | $sth->bindValue(':date', $date); |
||
| 1359 | $sth->bindValue(':forward_from', $forward_from); |
||
| 1360 | $sth->bindValue(':forward_from_chat', $forward_from_chat); |
||
| 1361 | $sth->bindValue(':forward_from_message_id', $message->getForwardFromMessageId()); |
||
| 1362 | $sth->bindValue(':forward_signature', $message->getForwardSignature()); |
||
| 1363 | $sth->bindValue(':forward_sender_name', $message->getForwardSenderName()); |
||
| 1364 | $sth->bindValue(':forward_date', $forward_date); |
||
| 1365 | $sth->bindValue(':is_topic_message', $message->getIsTopicMessage()); |
||
| 1366 | |||
| 1367 | $reply_to_chat_id = null; |
||
| 1368 | if ($reply_to_message_id !== null) { |
||
| 1369 | $reply_to_chat_id = $chat_id; |
||
| 1370 | } |
||
| 1371 | $sth->bindValue(':reply_to_chat', $reply_to_chat_id); |
||
| 1372 | $sth->bindValue(':reply_to_message', $reply_to_message_id); |
||
| 1373 | $sth->bindValue(':external_reply', $message->getExternalReply()); |
||
| 1374 | |||
| 1375 | $sth->bindValue(':via_bot', $via_bot_id); |
||
| 1376 | $sth->bindValue(':link_preview_options', $message->getLinkPreviewOptions()); |
||
| 1377 | $sth->bindValue(':edit_date', self::getTimestamp($message->getEditDate())); |
||
| 1378 | $sth->bindValue(':media_group_id', $message->getMediaGroupId()); |
||
| 1379 | $sth->bindValue(':author_signature', $message->getAuthorSignature()); |
||
| 1380 | $sth->bindValue(':text', $message->getText()); |
||
| 1381 | $sth->bindValue(':entities', self::entitiesArrayToJson($message->getEntities() ?: [])); |
||
| 1382 | $sth->bindValue(':caption_entities', self::entitiesArrayToJson($message->getCaptionEntities() ?: [])); |
||
| 1383 | $sth->bindValue(':audio', $message->getAudio()); |
||
| 1384 | $sth->bindValue(':document', $message->getDocument()); |
||
| 1385 | $sth->bindValue(':animation', $message->getAnimation()); |
||
| 1386 | $sth->bindValue(':game', $message->getGame()); |
||
| 1387 | $sth->bindValue(':photo', self::entitiesArrayToJson($message->getPhoto() ?: [])); |
||
| 1388 | $sth->bindValue(':sticker', $message->getSticker()); |
||
| 1389 | $sth->bindValue(':story', $message->getStory()); |
||
| 1390 | $sth->bindValue(':video', $message->getVideo()); |
||
| 1391 | $sth->bindValue(':voice', $message->getVoice()); |
||
| 1392 | $sth->bindValue(':video_note', $message->getVideoNote()); |
||
| 1393 | $sth->bindValue(':caption', $message->getCaption()); |
||
| 1394 | $sth->bindValue(':has_media_spoiler', $message->getHasMediaSpoiler()); |
||
| 1395 | $sth->bindValue(':contact', $message->getContact()); |
||
| 1396 | $sth->bindValue(':location', $message->getLocation()); |
||
| 1397 | $sth->bindValue(':venue', $message->getVenue()); |
||
| 1398 | $sth->bindValue(':poll', $message->getPoll()); |
||
| 1399 | $sth->bindValue(':dice', $message->getDice()); |
||
| 1400 | $sth->bindValue(':new_chat_members', $new_chat_members_ids); |
||
| 1401 | $sth->bindValue(':left_chat_member', $left_chat_member_id); |
||
| 1402 | $sth->bindValue(':new_chat_title', $message->getNewChatTitle()); |
||
| 1403 | $sth->bindValue(':new_chat_photo', self::entitiesArrayToJson($message->getNewChatPhoto() ?: [])); |
||
| 1404 | $sth->bindValue(':delete_chat_photo', $message->getDeleteChatPhoto()); |
||
| 1405 | $sth->bindValue(':group_chat_created', $message->getGroupChatCreated()); |
||
| 1406 | $sth->bindValue(':supergroup_chat_created', $message->getSupergroupChatCreated()); |
||
| 1407 | $sth->bindValue(':channel_chat_created', $message->getChannelChatCreated()); |
||
| 1408 | $sth->bindValue(':message_auto_delete_timer_changed', $message->getMessageAutoDeleteTimerChanged()); |
||
| 1409 | $sth->bindValue(':migrate_to_chat_id', $message->getMigrateToChatId()); |
||
| 1410 | $sth->bindValue(':migrate_from_chat_id', $message->getMigrateFromChatId()); |
||
| 1411 | $sth->bindValue(':pinned_message', $message->getPinnedMessage()); |
||
| 1412 | $sth->bindValue(':invoice', $message->getInvoice()); |
||
| 1413 | $sth->bindValue(':successful_payment', $message->getSuccessfulPayment()); |
||
| 1414 | $sth->bindValue(':user_shared', $message->getUserShared()); |
||
| 1415 | $sth->bindValue(':chat_shared', $message->getChatShared()); |
||
| 1416 | $sth->bindValue(':connected_website', $message->getConnectedWebsite()); |
||
| 1417 | $sth->bindValue(':write_access_allowed', $message->getWriteAccessAllowed()); |
||
| 1418 | $sth->bindValue(':passport_data', $message->getPassportData()); |
||
| 1419 | $sth->bindValue(':proximity_alert_triggered', $message->getProximityAlertTriggered()); |
||
| 1420 | $sth->bindValue(':forum_topic_created', $message->getForumTopicCreated()); |
||
| 1421 | $sth->bindValue(':forum_topic_edited', $message->getForumTopicEdited()); |
||
| 1422 | $sth->bindValue(':forum_topic_closed', $message->getForumTopicClosed()); |
||
| 1423 | $sth->bindValue(':forum_topic_reopened', $message->getForumTopicReopened()); |
||
| 1424 | $sth->bindValue(':general_forum_topic_hidden', $message->getGeneralForumTopicHidden()); |
||
| 1425 | $sth->bindValue(':general_forum_topic_unhidden', $message->getGeneralForumTopicUnhidden()); |
||
| 1426 | $sth->bindValue(':video_chat_scheduled', $message->getVideoChatScheduled()); |
||
| 1427 | $sth->bindValue(':video_chat_started', $message->getVideoChatStarted()); |
||
| 1428 | $sth->bindValue(':video_chat_ended', $message->getVideoChatEnded()); |
||
| 1429 | $sth->bindValue(':video_chat_participants_invited', $message->getVideoChatParticipantsInvited()); |
||
| 1430 | $sth->bindValue(':web_app_data', $message->getWebAppData()); |
||
| 1431 | $sth->bindValue(':reply_markup', $message->getReplyMarkup()); |
||
| 1432 | |||
| 1433 | return $sth->execute(); |
||
| 1434 | } catch (PDOException $e) { |
||
| 1435 | throw new TelegramException($e->getMessage()); |
||
| 1436 | } |
||
| 1437 | } |
||
| 1438 | |||
| 1439 | /** |
||
| 1440 | * Insert Edited Message request in db |
||
| 1441 | * |
||
| 1442 | * @param Message $edited_message |
||
| 1443 | * |
||
| 1444 | * @return bool If the insert was successful |
||
| 1445 | * @throws TelegramException |
||
| 1446 | */ |
||
| 1447 | public static function insertEditedMessageRequest(Message $edited_message): bool |
||
| 1448 | { |
||
| 1449 | if (!self::isDbConnected()) { |
||
| 1450 | return false; |
||
| 1451 | } |
||
| 1452 | |||
| 1453 | try { |
||
| 1454 | $edit_date = self::getTimestamp($edited_message->getEditDate()); |
||
| 1455 | |||
| 1456 | // Insert chat |
||
| 1457 | $chat = $edited_message->getChat(); |
||
| 1458 | self::insertChat($chat, $edit_date); |
||
| 1459 | |||
| 1460 | // Insert user and the relation with the chat |
||
| 1461 | if ($user = $edited_message->getFrom()) { |
||
| 1462 | self::insertUser($user, $edit_date, $chat); |
||
| 1463 | } |
||
| 1464 | |||
| 1465 | $sth = self::$pdo->prepare(' |
||
| 1466 | INSERT IGNORE INTO `' . TB_EDITED_MESSAGE . '` |
||
| 1467 | (`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`) |
||
| 1468 | VALUES |
||
| 1469 | (:chat_id, :message_id, :user_id, :edit_date, :text, :entities, :caption) |
||
| 1470 | '); |
||
| 1471 | |||
| 1472 | $user_id = $user ? $user->getId() : null; |
||
| 1473 | |||
| 1474 | $sth->bindValue(':chat_id', $chat->getId()); |
||
| 1475 | $sth->bindValue(':message_id', $edited_message->getMessageId()); |
||
| 1476 | $sth->bindValue(':user_id', $user_id); |
||
| 1477 | $sth->bindValue(':edit_date', $edit_date); |
||
| 1478 | $sth->bindValue(':text', $edited_message->getText()); |
||
| 1479 | $sth->bindValue(':entities', self::entitiesArrayToJson($edited_message->getEntities() ?: [])); |
||
| 1480 | $sth->bindValue(':caption', $edited_message->getCaption()); |
||
| 1481 | |||
| 1482 | return $sth->execute(); |
||
| 1483 | } catch (PDOException $e) { |
||
| 1484 | throw new TelegramException($e->getMessage()); |
||
| 1485 | } |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Select Groups, Supergroups, Channels and/or single user Chats (also by ID or text) |
||
| 1490 | * |
||
| 1491 | * @param $select_chats_params |
||
| 1492 | * |
||
| 1493 | * @return array|bool |
||
| 1494 | * @throws TelegramException |
||
| 1495 | */ |
||
| 1496 | public static function selectChats($select_chats_params) |
||
| 1497 | { |
||
| 1498 | if (!self::isDbConnected()) { |
||
| 1499 | return false; |
||
| 1500 | } |
||
| 1501 | |||
| 1502 | // Set defaults for omitted values. |
||
| 1503 | $select = array_merge([ |
||
| 1504 | 'groups' => true, |
||
| 1505 | 'supergroups' => true, |
||
| 1506 | 'channels' => true, |
||
| 1507 | 'users' => true, |
||
| 1508 | 'date_from' => null, |
||
| 1509 | 'date_to' => null, |
||
| 1510 | 'chat_id' => null, |
||
| 1511 | 'text' => null, |
||
| 1512 | 'language' => null, |
||
| 1513 | ], $select_chats_params); |
||
| 1514 | |||
| 1515 | if (!$select['groups'] && !$select['users'] && !$select['supergroups'] && !$select['channels']) { |
||
| 1516 | return false; |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | try { |
||
| 1520 | $query = ' |
||
| 1521 | SELECT * , |
||
| 1522 | ' . TB_CHAT . '.`id` AS `chat_id`, |
||
| 1523 | ' . TB_CHAT . '.`username` AS `chat_username`, |
||
| 1524 | ' . TB_CHAT . '.`created_at` AS `chat_created_at`, |
||
| 1525 | ' . TB_CHAT . '.`updated_at` AS `chat_updated_at` |
||
| 1526 | '; |
||
| 1527 | if ($select['users']) { |
||
| 1528 | $query .= ' |
||
| 1529 | , ' . TB_USER . '.`id` AS `user_id` |
||
| 1530 | FROM `' . TB_CHAT . '` |
||
| 1531 | LEFT JOIN `' . TB_USER . '` |
||
| 1532 | ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id` |
||
| 1533 | '; |
||
| 1534 | } else { |
||
| 1535 | $query .= 'FROM `' . TB_CHAT . '`'; |
||
| 1536 | } |
||
| 1537 | |||
| 1538 | // Building parts of query |
||
| 1539 | $where = []; |
||
| 1540 | $tokens = []; |
||
| 1541 | |||
| 1542 | if (!$select['groups'] || !$select['users'] || !$select['supergroups'] || !$select['channels']) { |
||
| 1543 | $chat_or_user = []; |
||
| 1544 | |||
| 1545 | $select['groups'] && $chat_or_user[] = TB_CHAT . '.`type` = "group"'; |
||
| 1546 | $select['supergroups'] && $chat_or_user[] = TB_CHAT . '.`type` = "supergroup"'; |
||
| 1547 | $select['channels'] && $chat_or_user[] = TB_CHAT . '.`type` = "channel"'; |
||
| 1548 | $select['users'] && $chat_or_user[] = TB_CHAT . '.`type` = "private"'; |
||
| 1549 | |||
| 1550 | $where[] = '(' . implode(' OR ', $chat_or_user) . ')'; |
||
| 1551 | } |
||
| 1552 | |||
| 1553 | if (null !== $select['date_from']) { |
||
| 1554 | $where[] = TB_CHAT . '.`updated_at` >= :date_from'; |
||
| 1555 | $tokens[':date_from'] = $select['date_from']; |
||
| 1556 | } |
||
| 1557 | |||
| 1558 | if (null !== $select['date_to']) { |
||
| 1559 | $where[] = TB_CHAT . '.`updated_at` <= :date_to'; |
||
| 1560 | $tokens[':date_to'] = $select['date_to']; |
||
| 1561 | } |
||
| 1562 | |||
| 1563 | if (null !== $select['chat_id']) { |
||
| 1564 | $where[] = TB_CHAT . '.`id` = :chat_id'; |
||
| 1565 | $tokens[':chat_id'] = $select['chat_id']; |
||
| 1566 | } |
||
| 1567 | |||
| 1568 | if ($select['users'] && null !== $select['language']) { |
||
| 1569 | $where[] = TB_USER . '.`language_code` = :language'; |
||
| 1570 | $tokens[':language'] = $select['language']; |
||
| 1571 | } |
||
| 1572 | |||
| 1573 | if (null !== $select['text']) { |
||
| 1574 | $text_like = '%' . strtolower($select['text']) . '%'; |
||
| 1575 | if ($select['users']) { |
||
| 1576 | $where[] = '( |
||
| 1577 | LOWER(' . TB_CHAT . '.`title`) LIKE :text1 |
||
| 1578 | OR LOWER(' . TB_USER . '.`first_name`) LIKE :text2 |
||
| 1579 | OR LOWER(' . TB_USER . '.`last_name`) LIKE :text3 |
||
| 1580 | OR LOWER(' . TB_USER . '.`username`) LIKE :text4 |
||
| 1581 | )'; |
||
| 1582 | $tokens[':text1'] = $text_like; |
||
| 1583 | $tokens[':text2'] = $text_like; |
||
| 1584 | $tokens[':text3'] = $text_like; |
||
| 1585 | $tokens[':text4'] = $text_like; |
||
| 1586 | } else { |
||
| 1587 | $where[] = 'LOWER(' . TB_CHAT . '.`title`) LIKE :text'; |
||
| 1588 | $tokens[':text'] = $text_like; |
||
| 1589 | } |
||
| 1590 | } |
||
| 1591 | |||
| 1592 | if (!empty($where)) { |
||
| 1593 | $query .= ' WHERE ' . implode(' AND ', $where); |
||
| 1594 | } |
||
| 1595 | |||
| 1596 | $query .= ' ORDER BY ' . TB_CHAT . '.`updated_at` ASC'; |
||
| 1597 | |||
| 1598 | $sth = self::$pdo->prepare($query); |
||
| 1599 | $sth->execute($tokens); |
||
| 1600 | |||
| 1601 | return $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 1602 | } catch (PDOException $e) { |
||
| 1603 | throw new TelegramException($e->getMessage()); |
||
| 1604 | } |
||
| 1605 | } |
||
| 1606 | |||
| 1607 | /** |
||
| 1608 | * Get Telegram API request count for current chat / message |
||
| 1609 | * |
||
| 1610 | * @param int|string|null $chat_id |
||
| 1611 | * @param string|null $inline_message_id |
||
| 1612 | * |
||
| 1613 | * @return array|bool Array containing TOTAL and CURRENT fields or false on invalid arguments |
||
| 1614 | * @throws TelegramException |
||
| 1615 | */ |
||
| 1616 | public static function getTelegramRequestCount($chat_id = null, string $inline_message_id = null) |
||
| 1617 | { |
||
| 1618 | if (!self::isDbConnected()) { |
||
| 1619 | return false; |
||
| 1620 | } |
||
| 1621 | |||
| 1622 | try { |
||
| 1623 | $sth = self::$pdo->prepare('SELECT |
||
| 1624 | (SELECT COUNT(DISTINCT `chat_id`) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :created_at_1) AS LIMIT_PER_SEC_ALL, |
||
| 1625 | (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, |
||
| 1626 | (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :created_at_minute AND `chat_id` = :chat_id_2) AS LIMIT_PER_MINUTE |
||
| 1627 | '); |
||
| 1628 | |||
| 1629 | $date = self::getTimestamp(); |
||
| 1630 | $date_minute = self::getTimestamp(strtotime('-1 minute')); |
||
| 1631 | |||
| 1632 | $sth->bindValue(':chat_id_1', $chat_id); |
||
| 1633 | $sth->bindValue(':chat_id_2', $chat_id); |
||
| 1634 | $sth->bindValue(':inline_message_id', $inline_message_id); |
||
| 1635 | $sth->bindValue(':created_at_1', $date); |
||
| 1636 | $sth->bindValue(':created_at_2', $date); |
||
| 1637 | $sth->bindValue(':created_at_minute', $date_minute); |
||
| 1638 | |||
| 1639 | $sth->execute(); |
||
| 1640 | |||
| 1641 | return $sth->fetch(); |
||
| 1642 | } catch (PDOException $e) { |
||
| 1643 | throw new TelegramException($e->getMessage()); |
||
| 1644 | } |
||
| 1645 | } |
||
| 1646 | |||
| 1647 | /** |
||
| 1648 | * Insert Telegram API request in db |
||
| 1649 | * |
||
| 1650 | * @param string $method |
||
| 1651 | * @param array $data |
||
| 1652 | * |
||
| 1653 | * @return bool If the insert was successful |
||
| 1654 | * @throws TelegramException |
||
| 1655 | */ |
||
| 1656 | public static function insertTelegramRequest(string $method, array $data): bool |
||
| 1680 | } |
||
| 1681 | } |
||
| 1682 | |||
| 1683 | /** |
||
| 1684 | * Bulk update the entries of any table |
||
| 1685 | * |
||
| 1686 | * @param string $table |
||
| 1687 | * @param array $fields_values |
||
| 1688 | * @param array $where_fields_values |
||
| 1689 | * |
||
| 1690 | * @return bool |
||
| 1691 | * @throws TelegramException |
||
| 1692 | */ |
||
| 1693 | public static function update(string $table, array $fields_values, array $where_fields_values): bool |
||
| 1694 | { |
||
| 1695 | if (empty($fields_values) || !self::isDbConnected()) { |
||
| 1696 | return false; |
||
| 1697 | } |
||
| 1698 | |||
| 1699 | try { |
||
| 1700 | // Building parts of query |
||
| 1701 | $tokens = $fields = $where = []; |
||
| 1702 | |||
| 1703 | // Fields with values to update |
||
| 1704 | foreach ($fields_values as $field => $value) { |
||
| 1705 | $token = ':' . count($tokens); |
||
| 1706 | $fields[] = "`{$field}` = {$token}"; |
||
| 1707 | $tokens[$token] = $value; |
||
| 1708 | } |
||
| 1709 | |||
| 1726 |