From 6d0e95670b732bc7643eb52a1f914eafd71360b4 Mon Sep 17 00:00:00 2001 From: Martin Ambrus Date: Fri, 17 Nov 2017 21:00:00 +0100 Subject: [PATCH] refactor: messages and reports only loaded on pages where needed #313 --- GameEngine/Database.php | 26 +++++++- GameEngine/Message.php | 142 ++++++++++++++++++++++++---------------- sql_updates.txt | 2 + var/db/struct.sql | 6 +- 4 files changed, 116 insertions(+), 60 deletions(-) diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 4357da1e..45128d79 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -677,8 +677,12 @@ class MYSQLi_DB implements IDbConnection { * @return string Returns a sanitized string, safe for SQL queries. */ function escape($value) { - $value = stripslashes($value); - return mysqli_real_escape_string($this->dblink, $value); + if (is_string($value)) { + $value = stripslashes( $value ); + return mysqli_real_escape_string($this->dblink, $value); + } else { + return $value; + } } /** @@ -3636,6 +3640,24 @@ class MYSQLi_DB implements IDbConnection { return mysqli_query($this->dblink,$q); } + // no need to cache this method + function getUnreadMessagesCount($uid) { + $uid = (int) $uid; + + return mysqli_fetch_array(mysqli_query($this->dblink, ' + SELECT Count(*) as numUnread FROM '.TB_PREFIX.'mdata WHERE target = '.$uid.' AND viewed = 0' + ), MYSQLI_ASSOC)['numUnread']; + } + + // no need to cache this method + function getUnreadNoticesCount($uid) { + $uid = (int) $uid; + + return mysqli_fetch_array(mysqli_query($this->dblink, ' + SELECT Count(*) as numUnread FROM '.TB_PREFIX.'ndata WHERE uid = '.$uid.' AND viewed = 0' + ), MYSQLI_ASSOC)['numUnread']; + } + function sendMessage($client, $owner, $topic, $message, $send, $alliance, $player, $coor, $report, $skip_escaping = false) { if (!$skip_escaping) { list($client, $owner, $topic, $message, $send, $alliance, $player, $coor, $report) = $this->escape_input((int) $client, (int) $owner, $topic, $message, (int) $send, (int) $alliance, (int) $player, (int) $coor, (int) $report); diff --git a/GameEngine/Message.php b/GameEngine/Message.php index fc1d8dfe..55ea1b10 100755 --- a/GameEngine/Message.php +++ b/GameEngine/Message.php @@ -18,14 +18,33 @@ class Message { private $allNotice = array(); function __construct() { - $this->getMessages(); - $this->getNotice(); - if($this->totalMessage > 0) { - $this->unread = $this->checkUnread(); - } - if($this->totalNotice > 0) { - $this->nunread = $this->checkNUnread(); - } + $req_file = basename($_SERVER['PHP_SELF']); + $this->unread = $this->checkUnread(); + $this->nunread = $this->checkNUnread(); + + if ($req_file == 'nachrichten.php') { + if ( isset( $_GET['t'] ) ) { + switch ( $_GET['t'] ) { + // send messages page + case 2: + $this->getMessages( 2 ); + break; + + // archived messages page + case 3: + $this->getMessages( 3 ); + break; + } + } else { + // inbox - received messages page + $this->getMessages( 1 ); + } + } + + if ($req_file == 'berichte.php') { + $this->getNotice(); + } + if(isset($_SESSION['reply'])) { $this->reply = $_SESSION['reply']; unset($_SESSION['reply']); @@ -218,17 +237,18 @@ class Message { global $database,$session; $post = $database->escape($post); for($i = 1; $i <= 10; $i++) { - if(isset($post['n' . $i])) { - $message1 = mysqli_query($GLOBALS['link'],"SELECT target, owner FROM " . TB_PREFIX . "mdata where id = ".(int) $post['n' . $i].""); - $message = mysqli_fetch_array($message1); - if($message['target'] == $session->uid && $message['owner'] == $session->uid){ - $database->getMessage($post['n' . $i], 8); - }else if($message['target'] == $session->uid){ - $database->getMessage($post['n' . $i], 5); - }else if($message['owner'] == $session->uid){ - $database->getMessage($post['n' . $i], 7); - } - } + if ( isset( $post[ 'n' . $i ] ) ) { + $message1 = mysqli_query( $GLOBALS['link'], "SELECT target, owner FROM " . TB_PREFIX . "mdata where id = " . (int) $post[ 'n' . $i ] . "" ); + $message = mysqli_fetch_array( $message1 ); + + if ( $message['target'] == $session->uid && $message['owner'] == $session->uid ) { + $database->getMessage( $post[ 'n' . $i ], 8 ); + } else if ( $message['target'] == $session->uid ) { + $database->getMessage( $post[ 'n' . $i ], 5 ); + } else if ( $message['owner'] == $session->uid ) { + $database->getMessage( $post[ 'n' . $i ], 7 ); + } + } } header("Location: nachrichten.php"); exit; @@ -317,17 +337,24 @@ class Message { } } - private function getMessages() { + private function getMessages($which) { global $database, $session; - $this->inbox = $database->getMessage($session->uid, 1); - $this->sent = $database->getMessage($session->uid, 2); - $this->inbox1 = $database->getMessage($session->uid, 9); - $this->sent1 = $database->getMessage($session->uid, 10); - if($session->plus) { - $this->archived = $database->getMessage($session->uid, 6); - $this->archived1 = $database->getMessage($session->uid, 11); - } - $this->totalMessage = count($this->inbox) + count($this->sent); + + switch ($which) { + case 1: $this->inbox = $database->getMessage($session->uid, 1); + $this->inbox1 = $database->getMessage($session->uid, 9); + break; + + case 2: $this->sent = $database->getMessage($session->uid, 2); + $this->sent1 = $database->getMessage($session->uid, 10); + break; + + case 3: if($session->plus) { + $this->archived = $database->getMessage($session->uid, 6); + $this->archived1 = $database->getMessage($session->uid, 11); + } + break; + } } private function sendAMessage($topic,$text) { @@ -546,47 +573,50 @@ class Message { } private function checkUnread() { - foreach($this->inbox as $message) { - if($message['viewed'] == 0) { - return true; - } - } - return false; + global $database, $session; + + return $database->getUnreadMessagesCount($session->uid); } private function checkNUnread() { - foreach($this->allNotice as $notice) { - if($notice['viewed'] == 0) { - return true; - } - } - return false; + global $database, $session; + + return $database->getUnreadNoticesCount($session->uid); } private function findInbox($id) { - foreach($this->inbox as $message) { - if($message['id'] == $id) { - return true; - } - } + if (count($this->inbox)) { + foreach ( $this->inbox as $message ) { + if ( $message['id'] == $id ) { + return true; + } + } + } + return false; } private function findSent($id) { - foreach($this->sent as $message) { - if($message['id'] == $id) { - return true; - } - } + if (count($this->sent)) { + foreach ( $this->sent as $message ) { + if ( $message['id'] == $id ) { + return true; + } + } + } + return false; } private function findArchive($id) { - foreach($this->archived as $message) { - if($message['id'] == $id) { - return true; - } - } + if (count($this->archived)) { + foreach ( $this->archived as $message ) { + if ( $message['id'] == $id ) { + return true; + } + } + } + return false; } diff --git a/sql_updates.txt b/sql_updates.txt index 47b018db..0dc9dcfc 100644 --- a/sql_updates.txt +++ b/sql_updates.txt @@ -3,6 +3,8 @@ ALTER TABLE `s1_prisoners` DROP INDEX `from`, ADD INDEX `from-t11` (`from`, `t11 ALTER TABLE `s1_enforcement` DROP INDEX `from`, ADD INDEX `from` (`from`, `hero`) USING BTREE; ALTER TABLE `s1_forum_post` CHANGE `date` `date` INT NULL DEFAULT NULL; ALTER TABLE `s1_market` DROP INDEX `vref-accept`, ADD INDEX `vref-accept-merchant` (`vref`, `accept`, `merchant`) USING BTREE; +ALTER TABLE `s1_mdata` ADD INDEX `target-viewed` (`target`, `viewed`); +ALTER TABLE `s1_ndata` ADD INDEX `uid-viewed` (`uid`, `viewed`); -- 8.11.2017 -> query optimizations and cleanups diff --git a/var/db/struct.sql b/var/db/struct.sql index 34ccf42f..bb472ef5 100644 --- a/var/db/struct.sql +++ b/var/db/struct.sql @@ -1033,7 +1033,8 @@ CREATE TABLE IF NOT EXISTS `%PREFIX%mdata` ( `report` int(11) NULL, PRIMARY KEY (`id`), KEY `target-time` (`target`,`time`) USING BTREE, - KEY `owner` (`owner`) + KEY `owner` (`owner`), + KEY `target-viewed` (`target`,`viewed`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- @@ -1119,7 +1120,8 @@ CREATE TABLE IF NOT EXISTS `%PREFIX%ndata` ( KEY `time` (`time`), KEY `uid-time` (`uid`,`time`) USING BTREE, KEY `del` (`del`), - KEY `toWref` (`toWref`) + KEY `toWref` (`toWref`), + KEY `uid-viewed` (`uid`,`viewed`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; --