From e679b342bd766c5ad977972f4205bd849bbfbcd5 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 6 Aug 2016 16:09:01 +0200 Subject: [PATCH] cleaned up some queries --- app/ActionCode.php | 12 +++++------- app/Comment.php | 4 +--- app/File.php | 9 ++++++--- app/Forum/Forum.php | 11 +++++------ app/Forum/Post.php | 3 +-- app/Forum/Topic.php | 11 +++++------ app/News/Post.php | 4 +--- app/Notification.php | 4 +--- app/Rank.php | 3 +-- app/Session.php | 8 ++++---- app/Template.php | 2 +- app/User.php | 6 ++---- 12 files changed, 33 insertions(+), 44 deletions(-) diff --git a/app/ActionCode.php b/app/ActionCode.php index fd80c0c..097ca06 100644 --- a/app/ActionCode.php +++ b/app/ActionCode.php @@ -26,13 +26,11 @@ class ActionCode // Insert it DB::table('actioncodes') - ->insert( - [ - 'code_action' => $action, - 'user_id' => $user, - 'action_code' => $code, - ] - ); + ->insert([ + 'code_action' => $action, + 'user_id' => $user, + 'action_code' => $code, + ]); // Return the code return $code; diff --git a/app/Comment.php b/app/Comment.php index 0dd9c7c..2eaf835 100644 --- a/app/Comment.php +++ b/app/Comment.php @@ -82,12 +82,10 @@ class Comment // Get comment data from the database $data = DB::table('comments') ->where('comment_id', $id) - ->get(); + ->first(); // Check if anything was returned and assign data if ($data) { - $data = $data[0]; - $this->id = $data->comment_id; $this->category = $data->comment_category; $this->time = $data->comment_timestamp; diff --git a/app/File.php b/app/File.php index a82572f..c098307 100644 --- a/app/File.php +++ b/app/File.php @@ -96,11 +96,10 @@ class File // Attempt to get the database row $fileRow = DB::table('uploads') ->where('file_id', $fileId) - ->get(); + ->first(); // If anything was returned populate the variables if ($fileRow) { - $fileRow = $fileRow[0]; $this->id = $fileRow->file_id; $this->user = User::construct($fileRow->user_id); $this->data = file_get_contents(ROOT . config('file.uploads_dir') . $fileRow->file_id . ".bin"); @@ -116,7 +115,11 @@ class File */ public function delete() { - unlink(ROOT . config('file.uploads_dir') . $this->id . ".bin"); + $filename = ROOT . config('file.uploads_dir') . $this->id . ".bin"; + + if (file_exists($filename)) { + unlink($filename); + } DB::table('uploads') ->where('file_id', $this->id) diff --git a/app/Forum/Forum.php b/app/Forum/Forum.php index 058a196..0904f67 100644 --- a/app/Forum/Forum.php +++ b/app/Forum/Forum.php @@ -103,14 +103,13 @@ class Forum // Get the row from the database $forumRow = DB::table('forums') ->where('forum_id', $forumId) - ->get(); + ->first(); // Create permissions object $this->permissionsCache = new Perms(Perms::FORUM); // Populate the variables if ($forumRow) { - $forumRow = $forumRow[0]; $this->id = intval($forumRow->forum_id); $this->order = intval($forumRow->forum_order); $this->name = $forumRow->forum_name; @@ -223,10 +222,10 @@ class Forum ->where('forum_id', $this->id) ->orderBy('post_id') ->limit(1) - ->get(['post_id']); + ->first(['post_id']); // Create the post object - $post = new Post(empty($firstPost) ? 0 : $firstPost[0]->post_id); + $post = new Post($firstPost->post_id ?? 0); // Assign it to a "cache" variable $this->firstPostCache = $post; @@ -251,10 +250,10 @@ class Forum ->where('forum_id', $this->id) ->orderBy('post_id', 'desc') ->limit(1) - ->get(['post_id']); + ->first(['post_id']); // Create the post object - $post = new Post(empty($lastPost) ? 0 : $lastPost[0]->post_id); + $post = new Post($lastPost->post_id ?? 0); // Assign it to a "cache" variable $this->lastPostCache = $post; diff --git a/app/Forum/Post.php b/app/Forum/Post.php index 8b2f0dd..6c31f86 100644 --- a/app/Forum/Post.php +++ b/app/Forum/Post.php @@ -100,11 +100,10 @@ class Post // Attempt to get the database row $postRow = DB::table('posts') ->where('post_id', $postId) - ->get(); + ->first(); // Assign data if a row was returned if ($postRow) { - $postRow = $postRow[0]; $this->id = intval($postRow->post_id); $this->topic = intval($postRow->topic_id); $this->forum = intval($postRow->forum_id); diff --git a/app/Forum/Topic.php b/app/Forum/Topic.php index 491dfa4..8d443de 100644 --- a/app/Forum/Topic.php +++ b/app/Forum/Topic.php @@ -113,11 +113,10 @@ class Topic // Attempt to get the database row $topicRow = DB::table('topics') ->where('topic_id', $topicId) - ->get(); + ->first(); // Assign data if a row was returned if ($topicRow) { - $topicRow = $topicRow[0]; $this->id = intval($topicRow->topic_id); $this->forum = intval($topicRow->forum_id); $this->hidden = boolval($topicRow->topic_hidden); @@ -262,10 +261,10 @@ class Topic ->where('topic_id', $this->id) ->orderBy('post_id') ->limit(1) - ->get(['post_id']); + ->first(['post_id']); // Create the post class - $post = new Post($post ? $post[0]->post_id : 0); + $post = new Post($post->post_id ?? 0); // Assign it to the cache var $this->firstPostCache = $post; @@ -290,10 +289,10 @@ class Topic ->where('topic_id', $this->id) ->orderBy('post_id', 'desc') ->limit(1) - ->get(['post_id']); + ->first(['post_id']); // Create the post class - $post = new Post($post ? $post[0]->post_id : 0); + $post = new Post($post->post_id ?? 0); // Assign it to the cache var $this->lastPostCache = $post; diff --git a/app/News/Post.php b/app/News/Post.php index 07c1c6e..2113830 100644 --- a/app/News/Post.php +++ b/app/News/Post.php @@ -79,12 +79,10 @@ class Post // Get comment data from the database $data = DB::table('news') ->where('news_id', $id) - ->get(); + ->first(); // Check if anything was returned and assign data if ($data) { - $data = $data[0]; - $this->id = $data->news_id; $this->category = $data->news_category; $this->user = $data->user_id; diff --git a/app/Notification.php b/app/Notification.php index afbeeff..65b82dd 100644 --- a/app/Notification.php +++ b/app/Notification.php @@ -76,12 +76,10 @@ class Notification // Get notification data from the database $data = DB::table('notifications') ->where('alert_id', $id) - ->get(); + ->first(); // Check if anything was returned and assign data if ($data) { - $data = $data[0]; - $this->id = intval($data->alert_id); $this->user = intval($data->user_id); $this->time = intval($data->alert_timestamp); diff --git a/app/Rank.php b/app/Rank.php index dea47ba..ed9c9fe 100644 --- a/app/Rank.php +++ b/app/Rank.php @@ -103,11 +103,10 @@ class Rank // Get the rank database row $rankRow = DB::table('ranks') ->where('rank_id', $rankId) - ->get(); + ->first(); // Check if the rank actually exists if ($rankRow) { - $rankRow = $rankRow[0]; $this->id = $rankRow->rank_id; $this->name = $rankRow->rank_name; $this->hierarchy = $rankRow->rank_hierarchy; diff --git a/app/Session.php b/app/Session.php index 58aac5b..163cd7d 100644 --- a/app/Session.php +++ b/app/Session.php @@ -113,7 +113,7 @@ class Session $session = DB::table('sessions') ->where('user_id', $this->userId) ->where('session_key', $this->sessionId) - ->get(); + ->first(); // Check if we actually got something in return if (!$session) { @@ -121,7 +121,7 @@ class Session } // Check if the session expired - if ($session[0]->session_expire < time()) { + if ($session->session_expire < time()) { // ...and return false return 0; } @@ -130,13 +130,13 @@ class Session good thing is i can probably do CIDR based checking */ // If the remember flag is set extend the session time - if ($session[0]->session_remember) { + if ($session->session_remember) { DB::table('sessions') ->where('session_id', $session[0]->session_id) ->update(['session_expire' => time() + 604800]); } // Return 2 if the remember flag is set and return 1 if not - return $session[0]->session_remember ? 2 : 1; + return $session->session_remember ? 2 : 1; } } diff --git a/app/Template.php b/app/Template.php index d8ce7e4..b3f0ba4 100644 --- a/app/Template.php +++ b/app/Template.php @@ -88,7 +88,7 @@ class Template $key = basename($dir); if ($key === self::$name) { - $key = '__main__'; + $loader->addPath($dir, '__main__'); } $loader->addPath($dir, $key); diff --git a/app/User.php b/app/User.php index 7d343f9..add61bc 100644 --- a/app/User.php +++ b/app/User.php @@ -292,12 +292,11 @@ class User // Get the user database row $userRow = DB::table('users') ->where('user_id', $userId) - ->orWhere('username_clean', clean_string($userId, true, true)) - ->get(); + ->orWhere('username_clean', clean_string($userId, true)) + ->first(); // Populate the variables if ($userRow) { - $userRow = $userRow[0]; $this->id = intval($userRow->user_id); $this->username = $userRow->username; $this->usernameClean = $userRow->username_clean; @@ -500,7 +499,6 @@ class User ->where('poster_id', $this->id) ->distinct() ->groupBy('topic_id') - ->orderBy('post_time') ->count(); return [