Commit 1dfe672c authored by Marco Boretto's avatar Marco Boretto

Merge pull request #78 from noplanman/some_code_cleanup

Initial code cleanup of `DB.php`.
parents 9fc1d8ec 6eef53dc
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -8,14 +7,15 @@ ...@@ -8,14 +7,15 @@
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
* Written by Marco Boretto <marco.bore@gmail.com> * Written by Marco Boretto <marco.bore@gmail.com>
*/ */
namespace Longman\TelegramBot; namespace Longman\TelegramBot;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\InlineQuery; use Longman\TelegramBot\Entities\InlineQuery;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\User; use Longman\TelegramBot\Entities\User;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
/** /**
...@@ -38,12 +38,12 @@ class DB ...@@ -38,12 +38,12 @@ class DB
/** /**
* PDO object * PDO object
* *
* @var \PDO * @var PDO
*/ */
static protected $pdo; static protected $pdo;
/** /**
* table prefix * Table prefix
* *
* @var string * @var string
*/ */
...@@ -52,26 +52,29 @@ class DB ...@@ -52,26 +52,29 @@ class DB
/** /**
* Telegram class object * Telegram class object
* *
* @var \Telegram * @var Telegram
*/ */
static protected $telegram; static protected $telegram;
/** /**
* Initialize * Initialize
* *
* @param array credential, string table_prefix * @param array $credentials Database connection details
* @param Telegram $telegram Telegram object to connect with this object
* @param string $table_prefix Table prefix
* @return PDO PDO database object
*/ */
public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null) public static function initialize(array $credentials, Telegram $telegram, $table_prefix = null)
{ {
self::$telegram = $telegram;
if (empty($credentials)) { if (empty($credentials)) {
throw new TelegramException('MySQL credentials not provided!'); throw new TelegramException('MySQL credentials not provided!');
} }
self::$telegram = $telegram;
self::$mysql_credentials = $credentials; self::$mysql_credentials = $credentials;
self::$table_prefix = $table_prefix; self::$table_prefix = $table_prefix;
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database']; $dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['database'];
$options = array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',); $options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
try { try {
$pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options); $pdo = new \PDO($dsn, $credentials['user'], $credentials['password'], $options);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
...@@ -109,11 +112,10 @@ class DB ...@@ -109,11 +112,10 @@ class DB
} }
/** /**
* check if database Connection has been created * Check if database connection has been created
* *
* @return bool * @return bool
*/ */
public static function isDbConnected() public static function isDbConnected()
{ {
if (empty(self::$pdo)) { if (empty(self::$pdo)) {
...@@ -124,22 +126,19 @@ class DB ...@@ -124,22 +126,19 @@ class DB
} }
/** /**
* fetch update from DB * Fetch update(s) from DB
*
* @param fetch Message from the DB
* *
* @return bool/ array with data * @param int $limit Limit the number of updates to fetch
* @return array|bool Fetched data or false if not connected
*/ */
public static function selectTelegramUpdate($limit = null) public static function selectTelegramUpdate($limit = null)
{ {
if (!self::isDbConnected()) { if (!self::isDbConnected()) {
return false; return false;
} }
try { try {
$query = 'SELECT `id` FROM `'.TB_TELEGRAM_UPDATE.'` '; $query = 'SELECT `id` FROM `' . TB_TELEGRAM_UPDATE . '` ';
$query .= 'ORDER BY `id` DESC'; $query .= 'ORDER BY `id` DESC';
if (!is_null($limit)) { if (!is_null($limit)) {
...@@ -154,28 +153,27 @@ class DB ...@@ -154,28 +153,27 @@ class DB
} catch (PDOException $e) { } catch (PDOException $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $results; return $results;
} }
/** /**
* fetch message from DB * Fetch message(s) from DB
*
* @param fetch Message from the DB
* *
* @return bool/ array with data * @param int $limit Limit the number of messages to fetch
* @return array|bool Fetched data or false if not connected
*/ */
public static function selectMessages($limit = null) public static function selectMessages($limit = null)
{ {
if (!self::isDbConnected()) { if (!self::isDbConnected()) {
return false; return false;
} }
try { try {
$query = 'SELECT * FROM `'.TB_MESSAGE.'` '; //message table
$query .= 'WHERE '.TB_MESSAGE.'.`update_id` != 0 '; $query = 'SELECT * FROM `' . TB_MESSAGE . '` ';
$query .= 'ORDER BY '.TB_MESSAGE.'.`message_id` DESC'; $query .= 'WHERE ' . TB_MESSAGE . '.`update_id` != 0 ';
$query .= 'ORDER BY ' . TB_MESSAGE . '.`message_id` DESC';
if (!is_null($limit)) { if (!is_null($limit)) {
$query .=' LIMIT :limit '; $query .=' LIMIT :limit ';
...@@ -189,15 +187,16 @@ class DB ...@@ -189,15 +187,16 @@ class DB
} catch (PDOException $e) { } catch (PDOException $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $results; return $results;
} }
/** /**
* Convert from unix timestamp to timestamp * Convert from unix timestamp to timestamp
* *
* @return string * @param int $time Unix timestamp
* @return null|string Timestamp if a time has been passed, else null
*/ */
protected static function getTimestamp($time = null) protected static function getTimestamp($time = null)
{ {
if (is_null($time)) { if (is_null($time)) {
...@@ -207,16 +206,20 @@ class DB ...@@ -207,16 +206,20 @@ class DB
} }
/** /**
* Insert telegram_update table * Insert entry to telegram_update table
* *
* @return bool * @todo Needs to return something if successful
*
* @param int $id
* @param int $message_id
* @param int $inline_query_id
* @param int $chosen_inline_query_id
* @return bool|null
*/ */
public static function insertTelegramUpdate($id, $message_id, $inline_query_id, $chosen_inline_query_id) public static function insertTelegramUpdate($id, $message_id, $inline_query_id, $chosen_inline_query_id)
{ {
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_query_id)) { if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_query_id)) {
throw new TelegramException("Error both query_id and message_id are null"); throw new TelegramException('Error both query_id and message_id are null');
} }
if (!self::isDbConnected()) { if (!self::isDbConnected()) {
...@@ -224,8 +227,8 @@ class DB ...@@ -224,8 +227,8 @@ class DB
} }
try { try {
//Users table //telegram_update table
$sth_insert_telegram_update = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_TELEGRAM_UPDATE.'` $sth_insert_telegram_update = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
( (
`id`, `message_id`, `inline_query_id`, `chosen_inline_query_id` `id`, `message_id`, `inline_query_id`, `chosen_inline_query_id`
) )
...@@ -247,11 +250,15 @@ class DB ...@@ -247,11 +250,15 @@ class DB
} }
/** /**
* Insert users eventually connection to chats * Insert users and save their connection to chats
* *
* @return bool * @todo Needs to return something if successful
*
* @param Entities\User $user
* @param string $date
* @param Entities\Chat $chat
* @return bool|null
*/ */
public static function insertUser(User $user, $date, Chat $chat = null) public static function insertUser(User $user, $date, Chat $chat = null)
{ {
if (!self::isDbConnected()) { if (!self::isDbConnected()) {
...@@ -264,8 +271,8 @@ class DB ...@@ -264,8 +271,8 @@ class DB
$last_name = $user->getLastName(); $last_name = $user->getLastName();
try { try {
//Users table //user table
$sth1 = self::$pdo->prepare('INSERT INTO `'.TB_USER.'` $sth1 = self::$pdo->prepare('INSERT INTO `' . TB_USER . '`
( (
`id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at` `id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at`
) )
...@@ -292,12 +299,13 @@ class DB ...@@ -292,12 +299,13 @@ class DB
$chat_id = $chat->getId(); $chat_id = $chat->getId();
try { try {
//user_chat table //user_chat table
$sth3 = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_USER_CHAT.'` $sth3 = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_USER_CHAT . '`
( (
`user_id`, `chat_id` `user_id`, `chat_id`
) )
VALUES (:user_id, :chat_id) VALUES (
'); :user_id, :chat_id
)');
$sth3->bindParam(':user_id', $user_id, \PDO::PARAM_INT); $sth3->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth3->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT); $sth3->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
...@@ -311,8 +319,9 @@ class DB ...@@ -311,8 +319,9 @@ class DB
} }
/** /**
* Insert request in db * Insert request into database
* *
* @param Entities\Update &$update
* @return bool * @return bool
*/ */
public static function insertRequest(Update &$update) public static function insertRequest(Update &$update)
...@@ -336,12 +345,12 @@ class DB ...@@ -336,12 +345,12 @@ class DB
} }
try { try {
//Inline query Table //Inline query Table
$mysql_query = 'INSERT INTO `'.TB_CHOSEN_INLINE_QUERY.'` $mysql_query = 'INSERT INTO `' . TB_CHOSEN_INLINE_QUERY . '`
( (
`result_id`, `user_id`, `query`, `created_at` `result_id`, `user_id`, `query`, `created_at`
) )
VALUES ( VALUES (
:result_id, :user_id, :query , :created_at :result_id, :user_id, :query, :created_at
)'; )';
$sth_insert_chosen_inline_query = self::$pdo->prepare($mysql_query); $sth_insert_chosen_inline_query = self::$pdo->prepare($mysql_query);
...@@ -374,8 +383,11 @@ class DB ...@@ -374,8 +383,11 @@ class DB
} }
/** /**
* Insert Message request in db * Insert inline query request into database
*
* @todo No return value at the end. Just return true?
* *
* @param Entities\InlineQuery &$inline_query
* @return bool * @return bool
*/ */
public static function insertInlineQueryRequest(InlineQuery &$inline_query) public static function insertInlineQueryRequest(InlineQuery &$inline_query)
...@@ -385,12 +397,12 @@ class DB ...@@ -385,12 +397,12 @@ class DB
} }
try { try {
//Inline query Table //Inline query Table
$mysql_query = 'INSERT IGNORE INTO `'.TB_INLINE_QUERY.'` $mysql_query = 'INSERT IGNORE INTO `' . TB_INLINE_QUERY . '`
( (
`id`, `user_id`, `query`, `offset`, `created_at` `id`, `user_id`, `query`, `offset`, `created_at`
) )
VALUES ( VALUES (
:inline_query_id , :user_id, :query , :param_offset , :created_at :inline_query_id, :user_id, :query, :param_offset, :created_at
)'; )';
$sth_insert_inline_query = self::$pdo->prepare($mysql_query); $sth_insert_inline_query = self::$pdo->prepare($mysql_query);
...@@ -414,6 +426,7 @@ class DB ...@@ -414,6 +426,7 @@ class DB
$sth_insert_inline_query->bindParam(':created_at', $date, \PDO::PARAM_STR); $sth_insert_inline_query->bindParam(':created_at', $date, \PDO::PARAM_STR);
$status = $sth_insert_inline_query->execute(); $status = $sth_insert_inline_query->execute();
} catch (PDOException $e) { } catch (PDOException $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
...@@ -422,7 +435,8 @@ class DB ...@@ -422,7 +435,8 @@ class DB
/** /**
* Insert Message request in db * Insert Message request in db
* *
* @return bool * @param Entities\Message &$message
* @return bool If the insert was successful
*/ */
public static function insertMessageRequest(Message &$message) public static function insertMessageRequest(Message &$message)
{ {
...@@ -448,11 +462,16 @@ class DB ...@@ -448,11 +462,16 @@ class DB
$migrate_from_chat_id = $message->getMigrateFromChatId(); $migrate_from_chat_id = $message->getMigrateFromChatId();
try { try {
//chats table //chat table
$sth2 = self::$pdo->prepare('INSERT INTO `'.TB_CHAT.'` $sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '`
(`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`) (
VALUES (:id, :type, :title, :date, :date, :oldid) `id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`
ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date'); )
VALUES (
:id, :type, :title, :date, :date, :oldid
)
ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date
');
$chat_title = $chat->getTitle(); $chat_title = $chat->getTitle();
$type = $chat->getType(); $type = $chat->getType();
...@@ -469,38 +488,34 @@ class DB ...@@ -469,38 +488,34 @@ class DB
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
//Insert user and the relation with the chat
//insert user and the relation with the chat
self::insertUser($from, $date, $chat); self::insertUser($from, $date, $chat);
//Insert the forwarded message user in users table //Insert the forwarded message user in users table
$forward_from = null;
if (is_object($forward_from)) { if (is_object($forward_from)) {
self::insertUser($forward_from, $forward_date); self::insertUser($forward_from, $forward_date);
$forward_from = $forward_from->getId(); $forward_from = $forward_from->getId();
} else {
$forward_from = null;
} }
//Insert the new chat user //Insert the new chat user
$new_chat_participant = '';
if (is_object($new_chat_participant)) { if (is_object($new_chat_participant)) {
self::insertUser($new_chat_participant, $date, $chat); self::insertUser($new_chat_participant, $date, $chat);
$new_chat_participant = $new_chat_participant->getId(); $new_chat_participant = $new_chat_participant->getId();
} else {
$new_chat_participant = '';
} }
//Insert the left chat user //Insert the left chat user
$left_chat_participant = '';
if (is_object($left_chat_participant)) { if (is_object($left_chat_participant)) {
self::insertUser($left_chat_participant, $date, $chat); self::insertUser($left_chat_participant, $date, $chat);
$left_chat_participant = $left_chat_participant->getId(); $left_chat_participant = $left_chat_participant->getId();
} else {
$left_chat_participant = '';
} }
try { try {
//Messages Table //message Table
$sth = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_MESSAGE.'` $sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '`
( (
`id`, `user_id`, `date`, `chat_id`, `forward_from`, `id`, `user_id`, `date`, `chat_id`, `forward_from`,
`forward_date`, `reply_to_message`, `text`, `audio`, `document`, `forward_date`, `reply_to_message`, `text`, `audio`, `document`,
...@@ -510,7 +525,8 @@ class DB ...@@ -510,7 +525,8 @@ class DB
`supergroup_chat_created`, `channel_chat_created`, `supergroup_chat_created`, `channel_chat_created`,
`migrate_from_chat_id`, `migrate_to_chat_id` `migrate_from_chat_id`, `migrate_to_chat_id`
) )
VALUES (:message_id, :user_id, :date, :chat_id, :forward_from, VALUES (
:message_id, :user_id, :date, :chat_id, :forward_from,
:forward_date, :reply_to_message, :text, :audio, :document, :forward_date, :reply_to_message, :text, :audio, :document,
:photo, :sticker, :video, :voice, :caption, :contact, :photo, :sticker, :video, :voice, :caption, :contact,
:location, :new_chat_participant, :left_chat_participant, :location, :new_chat_participant, :left_chat_participant,
...@@ -608,13 +624,15 @@ class DB ...@@ -608,13 +624,15 @@ class DB
return true; return true;
} }
/** /**
* Select Group and single Chats * Select Group and single Chats
* *
* @param date string yyyy-mm-dd hh:mm:ss * @param bool $select_groups
* * @param bool $select_super_groups
* @return array selected rows * @param bool $select_users
* @param string $date_from
* @param string $date_to
* @return array|bool Selected chats or false if invalid arguments
*/ */
public static function selectChats( public static function selectChats(
$select_groups = true, $select_groups = true,
...@@ -630,13 +648,14 @@ class DB ...@@ -630,13 +648,14 @@ class DB
if (!$select_groups & !$select_users & !$select_super_groups) { if (!$select_groups & !$select_users & !$select_super_groups) {
return false; return false;
} }
try { try {
$query = 'SELECT * , $query = 'SELECT * ,
'.TB_CHAT.'.`id` AS `chat_id`, ' . TB_CHAT . '.`id` AS `chat_id`,
'.TB_CHAT.'.`updated_at` AS `chat_updated_at`, ' . TB_CHAT . '.`updated_at` AS `chat_updated_at`,
'.TB_USER.'.`id` AS `user_id` ' . TB_USER . '.`id` AS `user_id`
FROM `'.TB_CHAT.'` LEFT JOIN `'.TB_USER.'` FROM `' . TB_CHAT . '` LEFT JOIN `' . TB_USER . '`
ON '.TB_CHAT.'.`id`='.TB_USER.'.`id`'; ON ' . TB_CHAT . '.`id`=' . TB_USER . '.`id`';
//Building parts of query //Building parts of query
$chat_or_user = ''; $chat_or_user = '';
...@@ -645,37 +664,37 @@ class DB ...@@ -645,37 +664,37 @@ class DB
if (!$select_groups || !$select_users || !$select_super_groups) { if (!$select_groups || !$select_users || !$select_super_groups) {
if ($select_groups) { if ($select_groups) {
$where[] = TB_CHAT.'.`type` = "group"'; $where[] = TB_CHAT . '.`type` = "group"';
} }
if ($select_super_groups) { if ($select_super_groups) {
$where[] = TB_CHAT.'.`type` = "supergroup"'; $where[] = TB_CHAT . '.`type` = "supergroup"';
} }
if ($select_users) { if ($select_users) {
$where[] = TB_CHAT.'.`type` = "private"'; $where[] = TB_CHAT . '.`type` = "private"';
} }
} }
if (! is_null($date_from)) { if (! is_null($date_from)) {
$where[] = TB_CHAT.'.`updated_at` >= :date_from'; $where[] = TB_CHAT . '.`updated_at` >= :date_from';
$tokens[':date_from'] = $date_from; $tokens[':date_from'] = $date_from;
} }
if (! is_null($date_to)) { if (! is_null($date_to)) {
$where[] = TB_CHAT.'.`updated_at` <= :date_to'; $where[] = TB_CHAT . '.`updated_at` <= :date_to';
$tokens[':date_to'] = $date_to; $tokens[':date_to'] = $date_to;
} }
$a=0; $a=0;
foreach ($where as $part) { foreach ($where as $part) {
if ($a) { if ($a) {
$query .= ' AND '.$part; $query .= ' AND ' . $part;
} else { } else {
$query .= ' WHERE '.$part; $query .= ' WHERE ' . $part;
++$a; ++$a;
} }
} }
$query .= ' ORDER BY '.TB_CHAT.'.`updated_at` ASC'; $query .= ' ORDER BY ' . TB_CHAT . '.`updated_at` ASC';
$sth = self::$pdo->prepare($query); $sth = self::$pdo->prepare($query);
$sth->execute($tokens); $sth->execute($tokens);
...@@ -685,6 +704,7 @@ class DB ...@@ -685,6 +704,7 @@ class DB
} catch (PDOException $e) { } catch (PDOException $e) {
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return $result; return $result;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment