Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 26 | class DB |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * MySQL credentials |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | static protected $mysql_credentials = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * PDO object |
||
| 37 | * |
||
| 38 | * @var PDO |
||
| 39 | */ |
||
| 40 | static protected $pdo; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Table prefix |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | static protected $table_prefix; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Telegram class object |
||
| 51 | * |
||
| 52 | * @var \Longman\TelegramBot\Telegram |
||
| 53 | */ |
||
| 54 | static protected $telegram; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Initialize |
||
| 58 | * |
||
| 59 | * @param array $credentials Database connection details |
||
| 60 | * @param \Longman\TelegramBot\Telegram $telegram Telegram object to connect with this object |
||
| 61 | * @param string $table_prefix Table prefix |
||
| 62 | * @param string $encoding Database character encoding |
||
| 63 | * |
||
| 64 | * @return PDO PDO database object |
||
| 65 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 66 | */ |
||
| 67 | 9 | public static function initialize( |
|
| 95 | |||
| 96 | /** |
||
| 97 | * External Initialize |
||
| 98 | * |
||
| 99 | * Let you use the class with an external already existing Pdo Mysql connection. |
||
| 100 | * |
||
| 101 | * @param PDO $external_pdo_connection PDO database object |
||
| 102 | * @param \Longman\TelegramBot\Telegram $telegram Telegram object to connect with this object |
||
| 103 | * @param string $table_prefix Table prefix |
||
| 104 | * |
||
| 105 | * @return PDO PDO database object |
||
| 106 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 107 | */ |
||
| 108 | public static function externalInitialize( |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Define all the tables with the proper prefix |
||
| 129 | */ |
||
| 130 | 9 | protected static function defineTables() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Check if database connection has been created |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | 9 | public static function isDbConnected() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get the PDO object of the connected database |
||
| 164 | * |
||
| 165 | * @return \PDO |
||
| 166 | */ |
||
| 167 | public static function getPdo() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Fetch update(s) from DB |
||
| 174 | * |
||
| 175 | * @param int $limit Limit the number of updates to fetch |
||
| 176 | * @param int $id Check for unique update id |
||
| 177 | * |
||
| 178 | * @return array|bool Fetched data or false if not connected |
||
| 179 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 180 | */ |
||
| 181 | public static function selectTelegramUpdate($limit = null, $id = null) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Fetch message(s) from DB |
||
| 217 | * |
||
| 218 | * @param int $limit Limit the number of messages to fetch |
||
| 219 | * |
||
| 220 | * @return array|bool Fetched data or false if not connected |
||
| 221 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 222 | */ |
||
| 223 | public static function selectMessages($limit = null) |
||
| 224 | { |
||
| 225 | if (!self::isDbConnected()) { |
||
| 226 | return false; |
||
| 227 | } |
||
| 228 | |||
| 229 | try { |
||
| 230 | $sql = ' |
||
| 231 | SELECT * |
||
| 232 | FROM `' . TB_MESSAGE . '` |
||
| 233 | WHERE `update_id` != 0 |
||
| 234 | ORDER BY `message_id` DESC |
||
| 235 | '; |
||
| 236 | |||
| 237 | if ($limit !== null) { |
||
| 238 | $sql .= 'LIMIT :limit'; |
||
| 239 | } |
||
| 240 | |||
| 241 | $sth = self::$pdo->prepare($sql); |
||
| 242 | $sth->bindParam(':limit', $limit, PDO::PARAM_INT); |
||
| 243 | $sth->execute(); |
||
| 244 | |||
| 245 | return $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 246 | } catch (PDOException $e) { |
||
| 247 | throw new TelegramException($e->getMessage()); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Convert from unix timestamp to timestamp |
||
| 253 | * |
||
| 254 | * @param int $time Unix timestamp (if null, current timestamp is used) |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | 7 | protected static function getTimestamp($time = null) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Convert array of Entity items to a JSON array |
||
| 269 | * |
||
| 270 | * @todo Find a better way, as json_* functions are very heavy |
||
| 271 | * |
||
| 272 | * @param array|null $entities |
||
| 273 | * @param mixed $default |
||
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | 6 | public static function entitiesArrayToJson($entities, $default = null) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Insert entry to telegram_update table |
||
| 293 | * |
||
| 294 | * @param int $id |
||
| 295 | * @param int $chat_id |
||
| 296 | * @param int $message_id |
||
| 297 | * @param int $inline_query_id |
||
| 298 | * @param int $chosen_inline_result_id |
||
| 299 | * @param int $callback_query_id |
||
| 300 | * @param int $edited_message_id |
||
| 301 | * |
||
| 302 | * @return bool If the insert was successful |
||
| 303 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 304 | */ |
||
| 305 | public static function insertTelegramUpdate( |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Insert users and save their connection to chats |
||
| 346 | * |
||
| 347 | * @param \Longman\TelegramBot\Entities\User $user |
||
| 348 | * @param string $date |
||
| 349 | * @param \Longman\TelegramBot\Entities\Chat $chat |
||
| 350 | * |
||
| 351 | * @return bool If the insert was successful |
||
| 352 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 353 | */ |
||
| 354 | 6 | public static function insertUser(User $user, $date, Chat $chat = null) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Insert chat |
||
| 418 | * |
||
| 419 | * @param \Longman\TelegramBot\Entities\Chat $chat |
||
| 420 | * @param string $date |
||
| 421 | * @param int $migrate_to_chat_id |
||
| 422 | * |
||
| 423 | * @return bool If the insert was successful |
||
| 424 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 425 | */ |
||
| 426 | 6 | public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Insert request into database |
||
| 477 | * |
||
| 478 | * @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails? |
||
| 479 | * |
||
| 480 | * @param \Longman\TelegramBot\Entities\Update $update |
||
| 481 | * |
||
| 482 | * @return bool |
||
| 483 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 484 | */ |
||
| 485 | public static function insertRequest(Update $update) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Insert inline query request into database |
||
| 589 | * |
||
| 590 | * @param \Longman\TelegramBot\Entities\InlineQuery $inline_query |
||
| 591 | * |
||
| 592 | * @return bool If the insert was successful |
||
| 593 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 594 | */ |
||
| 595 | View Code Duplication | public static function insertInlineQueryRequest(InlineQuery $inline_query) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Insert chosen inline result request into database |
||
| 637 | * |
||
| 638 | * @param \Longman\TelegramBot\Entities\ChosenInlineResult $chosen_inline_result |
||
| 639 | * |
||
| 640 | * @return bool If the insert was successful |
||
| 641 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 642 | */ |
||
| 643 | View Code Duplication | public static function insertChosenInlineResultRequest(ChosenInlineResult $chosen_inline_result) |
|
| 682 | |||
| 683 | /** |
||
| 684 | * Insert callback query request into database |
||
| 685 | * |
||
| 686 | * @param \Longman\TelegramBot\Entities\CallbackQuery $callback_query |
||
| 687 | * |
||
| 688 | * @return bool If the insert was successful |
||
| 689 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 690 | */ |
||
| 691 | public static function insertCallbackQueryRequest(CallbackQuery $callback_query) |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Insert Message request in db |
||
| 755 | * |
||
| 756 | * @param \Longman\TelegramBot\Entities\Message $message |
||
| 757 | * |
||
| 758 | * @return bool If the insert was successful |
||
| 759 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 760 | */ |
||
| 761 | 6 | public static function insertMessageRequest(Message $message) |
|
| 762 | { |
||
| 763 | 6 | if (!self::isDbConnected()) { |
|
| 764 | return false; |
||
| 765 | } |
||
| 766 | |||
| 767 | 6 | $from = $message->getFrom(); |
|
| 768 | 6 | $chat = $message->getChat(); |
|
| 769 | |||
| 770 | 6 | $chat_id = $chat->getId(); |
|
| 771 | |||
| 772 | 6 | $date = self::getTimestamp($message->getDate()); |
|
| 773 | |||
| 774 | 6 | $forward_from = $message->getForwardFrom(); |
|
| 775 | 6 | $forward_from_chat = $message->getForwardFromChat(); |
|
| 776 | 6 | $forward_from_message_id = $message->getForwardFromMessageId(); |
|
| 777 | 6 | $photo = self::entitiesArrayToJson($message->getPhoto(), ''); |
|
| 778 | 6 | $entities = self::entitiesArrayToJson($message->getEntities(), null); |
|
| 779 | 6 | $new_chat_members = $message->getNewChatMembers(); |
|
| 780 | 6 | $left_chat_member = $message->getLeftChatMember(); |
|
| 781 | 6 | $new_chat_photo = self::entitiesArrayToJson($message->getNewChatPhoto(), ''); |
|
| 782 | 6 | $migrate_to_chat_id = $message->getMigrateToChatId(); |
|
| 783 | |||
| 784 | //Insert chat, update chat id in case it migrated |
||
| 785 | 6 | self::insertChat($chat, $date, $migrate_to_chat_id); |
|
| 786 | |||
| 787 | //Insert user and the relation with the chat |
||
| 788 | 6 | if (is_object($from)) { |
|
| 789 | 6 | self::insertUser($from, $date, $chat); |
|
| 790 | } |
||
| 791 | |||
| 792 | //Insert the forwarded message user in users table |
||
| 793 | 6 | if ($forward_from instanceof User) { |
|
| 794 | $forward_date = self::getTimestamp($message->getForwardDate()); |
||
| 795 | self::insertUser($forward_from, $forward_date); |
||
| 796 | $forward_from = $forward_from->getId(); |
||
| 797 | } |
||
| 798 | |||
| 799 | 6 | if ($forward_from_chat instanceof Chat) { |
|
| 800 | $forward_date = self::getTimestamp($message->getForwardDate()); |
||
| 801 | self::insertChat($forward_from_chat, $forward_date); |
||
| 802 | $forward_from_chat = $forward_from_chat->getId(); |
||
| 803 | } |
||
| 804 | |||
| 805 | //New and left chat member |
||
| 806 | 6 | if (!empty($new_chat_members)) { |
|
| 807 | $new_chat_members_ids = []; |
||
| 808 | foreach ($new_chat_members as $new_chat_member) { |
||
| 809 | if ($new_chat_member instanceof User) { |
||
| 810 | //Insert the new chat user |
||
| 811 | self::insertUser($new_chat_member, $date, $chat); |
||
| 812 | $new_chat_members_ids[] = $new_chat_member->getId(); |
||
| 813 | } |
||
| 814 | } |
||
| 815 | $new_chat_members_ids = implode(',', $new_chat_members_ids); |
||
| 816 | 6 | } elseif ($left_chat_member instanceof User) { |
|
| 817 | //Insert the left chat user |
||
| 818 | self::insertUser($left_chat_member, $date, $chat); |
||
| 819 | $left_chat_member = $left_chat_member->getId(); |
||
| 820 | } |
||
| 821 | |||
| 822 | try { |
||
| 823 | 6 | $sth = self::$pdo->prepare(' |
|
| 824 | 6 | INSERT IGNORE INTO `' . TB_MESSAGE . '` |
|
| 825 | ( |
||
| 826 | `id`, `user_id`, `chat_id`, `date`, `forward_from`, `forward_from_chat`, `forward_from_message_id`, |
||
| 827 | `forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`, |
||
| 828 | `photo`, `sticker`, `video`, `voice`, `video_note`, `caption`, `contact`, |
||
| 829 | `location`, `venue`, `new_chat_members`, `left_chat_member`, |
||
| 830 | `new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`, |
||
| 831 | `supergroup_chat_created`, `channel_chat_created`, |
||
| 832 | `migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message` |
||
| 833 | ) VALUES ( |
||
| 834 | :message_id, :user_id, :chat_id, :date, :forward_from, :forward_from_chat, :forward_from_message_id, |
||
| 835 | :forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document, |
||
| 836 | :photo, :sticker, :video, :voice, :video_note, :caption, :contact, |
||
| 837 | :location, :venue, :new_chat_members, :left_chat_member, |
||
| 838 | :new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created, |
||
| 839 | :supergroup_chat_created, :channel_chat_created, |
||
| 840 | :migrate_from_chat_id, :migrate_to_chat_id, :pinned_message |
||
| 841 | ) |
||
| 842 | '); |
||
| 843 | |||
| 844 | 6 | $message_id = $message->getMessageId(); |
|
| 845 | |||
| 846 | 6 | if (is_object($from)) { |
|
| 847 | 6 | $from_id = $from->getId(); |
|
| 848 | } else { |
||
| 849 | $from_id = null; |
||
| 850 | } |
||
| 851 | |||
| 852 | 6 | $reply_to_message = $message->getReplyToMessage(); |
|
| 853 | 6 | $reply_to_message_id = null; |
|
| 854 | 6 | if ($reply_to_message instanceof ReplyToMessage) { |
|
| 855 | $reply_to_message_id = $reply_to_message->getMessageId(); |
||
| 856 | // please notice that, as explained in the documentation, reply_to_message don't contain other |
||
| 857 | // reply_to_message field so recursion deep is 1 |
||
| 858 | self::insertMessageRequest($reply_to_message); |
||
| 859 | } |
||
| 860 | |||
| 861 | 6 | $text = $message->getText(); |
|
| 862 | 6 | $audio = $message->getAudio(); |
|
| 863 | 6 | $document = $message->getDocument(); |
|
| 864 | 6 | $sticker = $message->getSticker(); |
|
| 865 | 6 | $video = $message->getVideo(); |
|
| 866 | 6 | $voice = $message->getVoice(); |
|
| 867 | 6 | $video_note = $message->getVideoNote(); |
|
| 868 | 6 | $caption = $message->getCaption(); |
|
| 869 | 6 | $contact = $message->getContact(); |
|
| 870 | 6 | $location = $message->getLocation(); |
|
| 871 | 6 | $venue = $message->getVenue(); |
|
| 872 | 6 | $new_chat_title = $message->getNewChatTitle(); |
|
| 873 | 6 | $delete_chat_photo = $message->getDeleteChatPhoto(); |
|
| 874 | 6 | $group_chat_created = $message->getGroupChatCreated(); |
|
| 875 | 6 | $supergroup_chat_created = $message->getSupergroupChatCreated(); |
|
| 876 | 6 | $channel_chat_created = $message->getChannelChatCreated(); |
|
| 877 | 6 | $migrate_from_chat_id = $message->getMigrateFromChatId(); |
|
| 878 | 6 | $migrate_to_chat_id = $message->getMigrateToChatId(); |
|
| 879 | 6 | $pinned_message = $message->getPinnedMessage(); |
|
| 880 | |||
| 881 | 6 | $sth->bindParam(':chat_id', $chat_id, PDO::PARAM_STR); |
|
| 882 | 6 | $sth->bindParam(':message_id', $message_id, PDO::PARAM_STR); |
|
| 883 | 6 | $sth->bindParam(':user_id', $from_id, PDO::PARAM_STR); |
|
| 884 | 6 | $sth->bindParam(':date', $date, PDO::PARAM_STR); |
|
| 885 | 6 | $sth->bindParam(':forward_from', $forward_from, PDO::PARAM_STR); |
|
| 886 | 6 | $sth->bindParam(':forward_from_chat', $forward_from_chat, PDO::PARAM_STR); |
|
| 887 | 6 | $sth->bindParam(':forward_from_message_id', $forward_from_message_id, PDO::PARAM_STR); |
|
| 888 | 6 | $sth->bindParam(':forward_date', $forward_date, PDO::PARAM_STR); |
|
| 889 | |||
| 890 | 6 | $reply_to_chat_id = null; |
|
| 891 | 6 | if ($reply_to_message_id) { |
|
| 892 | $reply_to_chat_id = $chat_id; |
||
| 893 | } |
||
| 894 | |||
| 895 | 6 | $sth->bindParam(':reply_to_chat', $reply_to_chat_id, PDO::PARAM_STR); |
|
| 896 | 6 | $sth->bindParam(':reply_to_message', $reply_to_message_id, PDO::PARAM_STR); |
|
| 897 | 6 | $sth->bindParam(':text', $text, PDO::PARAM_STR); |
|
| 898 | 6 | $sth->bindParam(':entities', $entities, PDO::PARAM_STR); |
|
| 899 | 6 | $sth->bindParam(':audio', $audio, PDO::PARAM_STR); |
|
| 900 | 6 | $sth->bindParam(':document', $document, PDO::PARAM_STR); |
|
| 901 | 6 | $sth->bindParam(':photo', $photo, PDO::PARAM_STR); |
|
| 902 | 6 | $sth->bindParam(':sticker', $sticker, PDO::PARAM_STR); |
|
| 903 | 6 | $sth->bindParam(':video', $video, PDO::PARAM_STR); |
|
| 904 | 6 | $sth->bindParam(':voice', $voice, PDO::PARAM_STR); |
|
| 905 | 6 | $sth->bindParam(':video_note', $video_note, PDO::PARAM_STR); |
|
| 906 | 6 | $sth->bindParam(':caption', $caption, PDO::PARAM_STR); |
|
| 907 | 6 | $sth->bindParam(':contact', $contact, PDO::PARAM_STR); |
|
| 908 | 6 | $sth->bindParam(':location', $location, PDO::PARAM_STR); |
|
| 909 | 6 | $sth->bindParam(':venue', $venue, PDO::PARAM_STR); |
|
| 910 | 6 | $sth->bindParam(':new_chat_members', $new_chat_members_ids, PDO::PARAM_STR); |
|
| 911 | 6 | $sth->bindParam(':left_chat_member', $left_chat_member, PDO::PARAM_STR); |
|
| 912 | 6 | $sth->bindParam(':new_chat_title', $new_chat_title, PDO::PARAM_STR); |
|
| 913 | 6 | $sth->bindParam(':new_chat_photo', $new_chat_photo, PDO::PARAM_STR); |
|
| 914 | 6 | $sth->bindParam(':delete_chat_photo', $delete_chat_photo, PDO::PARAM_INT); |
|
| 915 | 6 | $sth->bindParam(':group_chat_created', $group_chat_created, PDO::PARAM_INT); |
|
| 916 | 6 | $sth->bindParam(':supergroup_chat_created', $supergroup_chat_created, PDO::PARAM_INT); |
|
| 917 | 6 | $sth->bindParam(':channel_chat_created', $channel_chat_created, PDO::PARAM_INT); |
|
| 918 | 6 | $sth->bindParam(':migrate_from_chat_id', $migrate_from_chat_id, PDO::PARAM_STR); |
|
| 919 | 6 | $sth->bindParam(':migrate_to_chat_id', $migrate_to_chat_id, PDO::PARAM_STR); |
|
| 920 | 6 | $sth->bindParam(':pinned_message', $pinned_message, PDO::PARAM_STR); |
|
| 921 | |||
| 922 | 6 | return $sth->execute(); |
|
| 923 | } catch (PDOException $e) { |
||
| 924 | throw new TelegramException($e->getMessage()); |
||
| 925 | } |
||
| 926 | } |
||
| 927 | |||
| 928 | /** |
||
| 929 | * Insert Edited Message request in db |
||
| 930 | * |
||
| 931 | * @param \Longman\TelegramBot\Entities\Message $edited_message |
||
| 932 | * |
||
| 933 | * @return bool If the insert was successful |
||
| 934 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 935 | */ |
||
| 936 | public static function insertEditedMessageRequest(Message $edited_message) |
||
| 937 | { |
||
| 938 | if (!self::isDbConnected()) { |
||
| 939 | return false; |
||
| 940 | } |
||
| 941 | |||
| 942 | $from = $edited_message->getFrom(); |
||
| 943 | $chat = $edited_message->getChat(); |
||
| 944 | |||
| 945 | $chat_id = $chat->getId(); |
||
| 946 | |||
| 947 | $edit_date = self::getTimestamp($edited_message->getEditDate()); |
||
| 948 | |||
| 949 | $entities = self::entitiesArrayToJson($edited_message->getEntities(), null); |
||
| 950 | |||
| 951 | //Insert chat |
||
| 952 | self::insertChat($chat, $edit_date); |
||
| 953 | |||
| 954 | //Insert user and the relation with the chat |
||
| 955 | if (is_object($from)) { |
||
| 956 | self::insertUser($from, $edit_date, $chat); |
||
| 957 | } |
||
| 958 | |||
| 959 | try { |
||
| 960 | $sth = self::$pdo->prepare(' |
||
| 961 | INSERT IGNORE INTO `' . TB_EDITED_MESSAGE . '` |
||
| 962 | (`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`) |
||
| 963 | VALUES |
||
| 964 | (:chat_id, :message_id, :user_id, :edit_date, :text, :entities, :caption) |
||
| 965 | '); |
||
| 966 | |||
| 967 | $message_id = $edited_message->getMessageId(); |
||
| 968 | |||
| 969 | if (is_object($from)) { |
||
| 970 | $from_id = $from->getId(); |
||
| 971 | } else { |
||
| 972 | $from_id = null; |
||
| 973 | } |
||
| 974 | |||
| 975 | $text = $edited_message->getText(); |
||
| 976 | $caption = $edited_message->getCaption(); |
||
| 977 | |||
| 978 | $sth->bindParam(':chat_id', $chat_id, PDO::PARAM_STR); |
||
| 979 | $sth->bindParam(':message_id', $message_id, PDO::PARAM_STR); |
||
| 980 | $sth->bindParam(':user_id', $from_id, PDO::PARAM_STR); |
||
| 981 | $sth->bindParam(':edit_date', $edit_date, PDO::PARAM_STR); |
||
| 982 | $sth->bindParam(':text', $text, PDO::PARAM_STR); |
||
| 983 | $sth->bindParam(':entities', $entities, PDO::PARAM_STR); |
||
| 984 | $sth->bindParam(':caption', $caption, PDO::PARAM_STR); |
||
| 985 | |||
| 986 | return $sth->execute(); |
||
| 987 | } catch (PDOException $e) { |
||
| 988 | throw new TelegramException($e->getMessage()); |
||
| 989 | } |
||
| 990 | } |
||
| 991 | |||
| 992 | /** |
||
| 993 | * Select Groups, Supergroups, Channels and/or single user Chats (also by ID or text) |
||
| 994 | * |
||
| 995 | * @param $select_chats_params |
||
| 996 | * |
||
| 997 | * @return array|bool |
||
| 998 | * @throws TelegramException |
||
| 999 | */ |
||
| 1000 | public static function selectChats($select_chats_params) |
||
| 1001 | { |
||
| 1002 | if (!self::isDbConnected()) { |
||
| 1003 | return false; |
||
| 1004 | } |
||
| 1005 | |||
| 1006 | // Set defaults for omitted values. |
||
| 1007 | $select = array_merge([ |
||
| 1008 | 'groups' => true, |
||
| 1009 | 'supergroups' => true, |
||
| 1010 | 'channels' => true, |
||
| 1011 | 'users' => true, |
||
| 1012 | 'date_from' => null, |
||
| 1013 | 'date_to' => null, |
||
| 1014 | 'chat_id' => null, |
||
| 1015 | 'text' => null, |
||
| 1016 | ], $select_chats_params); |
||
| 1017 | |||
| 1018 | if (!$select['groups'] && !$select['users'] && !$select['supergroups']) { |
||
| 1019 | return false; |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | try { |
||
| 1023 | $query = ' |
||
| 1024 | SELECT * , |
||
| 1025 | ' . TB_CHAT . '.`id` AS `chat_id`, |
||
| 1026 | ' . TB_CHAT . '.`username` AS `chat_username`, |
||
| 1027 | ' . TB_CHAT . '.`created_at` AS `chat_created_at`, |
||
| 1028 | ' . TB_CHAT . '.`updated_at` AS `chat_updated_at` |
||
| 1029 | '; |
||
| 1030 | if ($select['users']) { |
||
| 1031 | $query .= ' |
||
| 1032 | , ' . TB_USER . '.`id` AS `user_id` |
||
| 1033 | FROM `' . TB_CHAT . '` |
||
| 1034 | LEFT JOIN `' . TB_USER . '` |
||
| 1035 | ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id` |
||
| 1036 | '; |
||
| 1037 | } else { |
||
| 1038 | $query .= 'FROM `' . TB_CHAT . '`'; |
||
| 1039 | } |
||
| 1040 | |||
| 1041 | //Building parts of query |
||
| 1042 | $where = []; |
||
| 1043 | $tokens = []; |
||
| 1044 | |||
| 1045 | if (!$select['groups'] || !$select['users'] || !$select['supergroups']) { |
||
| 1046 | $chat_or_user = []; |
||
| 1047 | |||
| 1048 | $select['groups'] && $chat_or_user[] = TB_CHAT . '.`type` = "group"'; |
||
| 1049 | $select['supergroups'] && $chat_or_user[] = TB_CHAT . '.`type` = "supergroup"'; |
||
| 1050 | $select['channels'] && $chat_or_user[] = TB_CHAT . '.`type` = "channel"'; |
||
| 1051 | $select['users'] && $chat_or_user[] = TB_CHAT . '.`type` = "private"'; |
||
| 1052 | |||
| 1053 | $where[] = '(' . implode(' OR ', $chat_or_user) . ')'; |
||
| 1054 | } |
||
| 1055 | |||
| 1056 | if (null !== $select['date_from']) { |
||
| 1057 | $where[] = TB_CHAT . '.`updated_at` >= :date_from'; |
||
| 1058 | $tokens[':date_from'] = $select['date_from']; |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | if (null !== $select['date_to']) { |
||
| 1062 | $where[] = TB_CHAT . '.`updated_at` <= :date_to'; |
||
| 1063 | $tokens[':date_to'] = $select['date_to']; |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | if (null !== $select['chat_id']) { |
||
| 1067 | $where[] = TB_CHAT . '.`id` = :chat_id'; |
||
| 1068 | $tokens[':chat_id'] = $select['chat_id']; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | if (null !== $select['text']) { |
||
| 1072 | if ($select['users']) { |
||
| 1073 | $where[] = '( |
||
| 1074 | LOWER(' . TB_CHAT . '.`title`) LIKE :text |
||
| 1075 | OR LOWER(' . TB_USER . '.`first_name`) LIKE :text |
||
| 1076 | OR LOWER(' . TB_USER . '.`last_name`) LIKE :text |
||
| 1077 | OR LOWER(' . TB_USER . '.`username`) LIKE :text |
||
| 1078 | )'; |
||
| 1079 | } else { |
||
| 1080 | $where[] = 'LOWER(' . TB_CHAT . '.`title`) LIKE :text'; |
||
| 1081 | } |
||
| 1082 | $tokens[':text'] = '%' . strtolower($select['text']) . '%'; |
||
| 1083 | } |
||
| 1084 | |||
| 1085 | if (!empty($where)) { |
||
| 1086 | $query .= ' WHERE ' . implode(' AND ', $where); |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | $query .= ' ORDER BY ' . TB_CHAT . '.`updated_at` ASC'; |
||
| 1090 | |||
| 1091 | $sth = self::$pdo->prepare($query); |
||
| 1092 | $sth->execute($tokens); |
||
| 1093 | |||
| 1094 | return $sth->fetchAll(PDO::FETCH_ASSOC); |
||
| 1095 | } catch (PDOException $e) { |
||
| 1096 | throw new TelegramException($e->getMessage()); |
||
| 1097 | } |
||
| 1098 | } |
||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Get Telegram API request count for current chat / message |
||
| 1102 | * |
||
| 1103 | * @param integer $chat_id |
||
| 1104 | * @param string $inline_message_id |
||
| 1105 | * |
||
| 1106 | * @return array|bool (Array containing TOTAL and CURRENT fields or false on invalid arguments) |
||
| 1107 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 1108 | */ |
||
| 1109 | public static function getTelegramRequestCount($chat_id = null, $inline_message_id = null) |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * Insert Telegram API request in db |
||
| 1142 | * |
||
| 1143 | * @param string $method |
||
| 1144 | * @param array $data |
||
| 1145 | * |
||
| 1146 | * @return bool If the insert was successful |
||
| 1147 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
| 1148 | */ |
||
| 1149 | public static function insertTelegramRequest($method, $data) |
||
| 1180 | } |
||
| 1181 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: