From dbc09bf0d4d2537d0570f6de5958d5f2e38a864e Mon Sep 17 00:00:00 2001
From: brunoais <brunoaiss@gmail.com>
Date: Sat, 28 Feb 2015 20:34:25 +0000
Subject: [PATCH 1/9] [ticket/13661] Transform queries to get logs and log
 count into built queries

PHPBB3-13661
---
 phpBB/phpbb/log/log.php | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php
index 0c5205530b..6fb8dc79c6 100644
--- a/phpBB/phpbb/log/log.php
+++ b/phpBB/phpbb/log/log.php
@@ -521,15 +521,30 @@ class log implements \phpbb\log\log_interface
 			$sql_keywords = $this->generate_sql_keyword($keywords);
 		}
 
-		if ($count_logs)
-		{
-			$sql = 'SELECT COUNT(l.log_id) AS total_entries
-				FROM ' . $this->log_table . ' l, ' . USERS_TABLE . ' u
-				WHERE l.log_type = ' . (int) $log_type . '
-					AND l.user_id = u.user_id
+		$get_logs_sql_ary = array(
+			'SELECT' => 'l.*, u.username, u.username_clean, u.user_colour',
+			'FROM' => $this->log_table . ' l, ' . USERS_TABLE . ' u',
+			'WHERE' => 'l.user_id = u.user_id
 					AND l.log_time >= ' . (int) $log_time . "
 					$sql_keywords
-					$sql_additional";
+					$sql_additional",
+
+			'ORDER_BY' => $sort_by,
+		);
+
+		if($log_type){
+			$get_logs_sql_ary['WHERE'] = 'l.log_type = ' . (int) $log_type . '
+					AND ' . $get_logs_sql_ary['WHERE'];
+		}
+
+		if ($count_logs)
+		{
+			$count_logs_sql_ary = $get_logs_sql_ary;
+
+			$count_logs_sql_ary['SELECT'] = 'COUNT(l.log_id) AS total_entries';
+			unset($count_logs_sql_ary['ORDER_BY']);
+
+			$sql = $this->db->sql_build_array('SELECT', $count_logs_sql_ary);
 			$result = $this->db->sql_query($sql);
 			$this->entry_count = (int) $this->db->sql_fetchfield('total_entries');
 			$this->db->sql_freeresult($result);
@@ -548,14 +563,7 @@ class log implements \phpbb\log\log_interface
 			}
 		}
 
-		$sql = 'SELECT l.*, u.username, u.username_clean, u.user_colour
-			FROM ' . $this->log_table . ' l, ' . USERS_TABLE . ' u
-			WHERE l.log_type = ' . (int) $log_type . '
-				AND u.user_id = l.user_id
-				' . (($log_time) ? 'AND l.log_time >= ' . (int) $log_time : '') . "
-				$sql_keywords
-				$sql_additional
-			ORDER BY $sort_by";
+		$sql = $this->db->sql_build_array('SELECT', $get_logs_sql_ary);
 		$result = $this->db->sql_query_limit($sql, $limit, $this->last_page_offset);
 
 		$i = 0;

From d66a53a53127f9c50dd62c34bf08fdabacdea001 Mon Sep 17 00:00:00 2001
From: brunoais <brunoaiss@gmail.com>
Date: Sun, 1 Mar 2015 10:07:18 +0000
Subject: [PATCH 2/9] [ticket/13661] Add event to allow editing the queries
 used to get the logs

PHPBB3-13661
---
 phpBB/phpbb/log/log.php | 47 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)

diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php
index 6fb8dc79c6..1aa9e073f0 100644
--- a/phpBB/phpbb/log/log.php
+++ b/phpBB/phpbb/log/log.php
@@ -537,6 +537,49 @@ class log implements \phpbb\log\log_interface
 					AND ' . $get_logs_sql_ary['WHERE'];
 		}
 
+		/**
+		* Modify the query to obtain the logs data
+		*
+		* @event core.get_logs_main_query_before
+		* @var	array	get_logs_sql_ary	The array in the format of the query builder with the query
+		*									to get the log count and the log list
+		* @var	string	mode				Mode of the entries we display
+		* @var	bool	count_logs			Do we count all matching entries?
+		* @var	int		limit				Limit the number of entries
+		* @var	int		offset				Offset when fetching the entries
+		* @var	mixed	forum_id			Limit entries to the forum_id,
+		*									can also be an array of forum_ids
+		* @var	int		topic_id			Limit entries to the topic_id
+		* @var	int		user_id				Limit entries to the user_id
+		* @var	int		log_time			Limit maximum age of log entries
+		* @var	string	sort_by				SQL order option
+		* @var	string	keywords			Will only return entries that have the
+		*									keywords in log_operation or log_data
+		* @var	string	profile_url			URL to the users profile
+		* @var	int		log_type			Limit logs to a certain type. If log_type
+		*									is false, no entries will be returned.
+		* @var	string	sql_additional		Additional conditions for the entries,
+		*									e.g.: 'AND l.forum_id = 1'
+		* @since 3.1.0-a1
+		*/
+		$vars = array(
+			'get_logs_sql_ary',
+			'mode',
+			'count_logs',
+			'limit',
+			'offset',
+			'forum_id',
+			'topic_id',
+			'user_id',
+			'log_time',
+			'sort_by',
+			'keywords',
+			'profile_url',
+			'log_type',
+			'sql_additional',
+		);
+		extract($this->dispatcher->trigger_event('core.get_logs_main_query_before', compact($vars)));
+			
 		if ($count_logs)
 		{
 			$count_logs_sql_ary = $get_logs_sql_ary;
@@ -544,7 +587,7 @@ class log implements \phpbb\log\log_interface
 			$count_logs_sql_ary['SELECT'] = 'COUNT(l.log_id) AS total_entries';
 			unset($count_logs_sql_ary['ORDER_BY']);
 
-			$sql = $this->db->sql_build_array('SELECT', $count_logs_sql_ary);
+			$sql = $this->db->sql_build_query('SELECT', $count_logs_sql_ary);
 			$result = $this->db->sql_query($sql);
 			$this->entry_count = (int) $this->db->sql_fetchfield('total_entries');
 			$this->db->sql_freeresult($result);
@@ -563,7 +606,7 @@ class log implements \phpbb\log\log_interface
 			}
 		}
 
-		$sql = $this->db->sql_build_array('SELECT', $get_logs_sql_ary);
+		$sql = $this->db->sql_build_query('SELECT', $get_logs_sql_ary);
 		$result = $this->db->sql_query_limit($sql, $limit, $this->last_page_offset);
 
 		$i = 0;

From c887eedaa5c9efeeec82cf29117325480905c9f3 Mon Sep 17 00:00:00 2001
From: brunoais <brunoaiss@gmail.com>
Date: Sun, 1 Mar 2015 14:01:57 +0000
Subject: [PATCH 3/9] [ticket/13661] Fixed the "FROM" in the built query.

I was doing it wrong by giving a string to the FROM clause in the built query.

PHPBB3-13661
---
 phpBB/phpbb/log/log.php | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php
index 1aa9e073f0..e2802ba3e1 100644
--- a/phpBB/phpbb/log/log.php
+++ b/phpBB/phpbb/log/log.php
@@ -523,7 +523,10 @@ class log implements \phpbb\log\log_interface
 
 		$get_logs_sql_ary = array(
 			'SELECT' => 'l.*, u.username, u.username_clean, u.user_colour',
-			'FROM' => $this->log_table . ' l, ' . USERS_TABLE . ' u',
+			'FROM' => array(
+						$this->log_table => 'l',
+						USERS_TABLE => 'u',
+					),
 			'WHERE' => 'l.user_id = u.user_id
 					AND l.log_time >= ' . (int) $log_time . "
 					$sql_keywords

From d0a1650a04fc74fb81c0123505854a09352d49c1 Mon Sep 17 00:00:00 2001
From: brunoais <brunoaiss@gmail.com>
Date: Sun, 1 Mar 2015 18:35:11 +0000
Subject: [PATCH 4/9] [ticket/13661] bugfix: The conditional is the log_time,
 not log_type

I mistakenly made the log_type the conditional instead of log_time.
Thankfully, the automated tests helped finding this mistake.

PHPBB3-13661
---
 phpBB/phpbb/log/log.php | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php
index e2802ba3e1..735d595bb5 100644
--- a/phpBB/phpbb/log/log.php
+++ b/phpBB/phpbb/log/log.php
@@ -527,8 +527,8 @@ class log implements \phpbb\log\log_interface
 						$this->log_table => 'l',
 						USERS_TABLE => 'u',
 					),
-			'WHERE' => 'l.user_id = u.user_id
-					AND l.log_time >= ' . (int) $log_time . "
+			'WHERE' => 'l.log_type = ' . (int) $log_type . "
+					AND l.user_id = u.user_id
 					$sql_keywords
 					$sql_additional",
 
@@ -536,7 +536,7 @@ class log implements \phpbb\log\log_interface
 		);
 
 		if($log_type){
-			$get_logs_sql_ary['WHERE'] = 'l.log_type = ' . (int) $log_type . '
+			$get_logs_sql_ary['WHERE'] = 'l.log_time >= ' . (int) $log_time . '
 					AND ' . $get_logs_sql_ary['WHERE'];
 		}
 

From 772bbdfeae879b17aa2b62f10469bc8b63230093 Mon Sep 17 00:00:00 2001
From: brunoais <brunoaiss@gmail.com>
Date: Sun, 1 Mar 2015 19:58:57 +0000
Subject: [PATCH 5/9] [ticket/13661] Removed superfluous whitespace

PHPBB3-13661
---
 phpBB/phpbb/log/log.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php
index 735d595bb5..0fd33b4618 100644
--- a/phpBB/phpbb/log/log.php
+++ b/phpBB/phpbb/log/log.php
@@ -582,7 +582,7 @@ class log implements \phpbb\log\log_interface
 			'sql_additional',
 		);
 		extract($this->dispatcher->trigger_event('core.get_logs_main_query_before', compact($vars)));
-			
+
 		if ($count_logs)
 		{
 			$count_logs_sql_ary = $get_logs_sql_ary;

From d53b584c9a5ac247bfb88aa230dc064b7a73e72c Mon Sep 17 00:00:00 2001
From: brunoais <brunoaiss@gmail.com>
Date: Sun, 1 Mar 2015 20:12:37 +0000
Subject: [PATCH 6/9] [ticket/13661] Wrong event @since version

PHPBB3-13661
---
 phpBB/phpbb/log/log.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php
index 0fd33b4618..8e5f75807c 100644
--- a/phpBB/phpbb/log/log.php
+++ b/phpBB/phpbb/log/log.php
@@ -563,7 +563,7 @@ class log implements \phpbb\log\log_interface
 		*									is false, no entries will be returned.
 		* @var	string	sql_additional		Additional conditions for the entries,
 		*									e.g.: 'AND l.forum_id = 1'
-		* @since 3.1.0-a1
+		* @since 3.1.4-RC1
 		*/
 		$vars = array(
 			'get_logs_sql_ary',

From c7cc8a098e2a38bbe822468cead4ee9df1745104 Mon Sep 17 00:00:00 2001
From: brunoais <brunoaiss@gmail.com>
Date: Sun, 1 Mar 2015 21:17:06 +0000
Subject: [PATCH 7/9] [ticket/13661] Re-Fixed  $log_type -> $log_time

Thanks to goof for pointing it out...

PHPBB3-13661
---
 phpBB/phpbb/log/log.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php
index 8e5f75807c..3d89e7403f 100644
--- a/phpBB/phpbb/log/log.php
+++ b/phpBB/phpbb/log/log.php
@@ -535,7 +535,7 @@ class log implements \phpbb\log\log_interface
 			'ORDER_BY' => $sort_by,
 		);
 
-		if($log_type){
+		if($log_time){
 			$get_logs_sql_ary['WHERE'] = 'l.log_time >= ' . (int) $log_time . '
 					AND ' . $get_logs_sql_ary['WHERE'];
 		}

From 134a5e0391aa4d05d347a77c0218b29ab9b60a1b Mon Sep 17 00:00:00 2001
From: brunoais <brunoaiss@gmail.com>
Date: Mon, 2 Mar 2015 08:34:52 +0000
Subject: [PATCH 8/9] [ticket/13661] Brackets in their own line

I didn't even notice that I used my own coding guidelines there...
Thanks to MGaetan89 for pointing it out.

PHPBB3-13661
---
 phpBB/phpbb/log/log.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php
index 3d89e7403f..1aba5851a5 100644
--- a/phpBB/phpbb/log/log.php
+++ b/phpBB/phpbb/log/log.php
@@ -535,7 +535,8 @@ class log implements \phpbb\log\log_interface
 			'ORDER_BY' => $sort_by,
 		);
 
-		if($log_time){
+		if($log_time)
+		{
 			$get_logs_sql_ary['WHERE'] = 'l.log_time >= ' . (int) $log_time . '
 					AND ' . $get_logs_sql_ary['WHERE'];
 		}

From 684a050916c55570ef6b21ad00b3aee81279439d Mon Sep 17 00:00:00 2001
From: brunoais <brunoaiss@gmail.com>
Date: Wed, 6 May 2015 22:53:43 +0100
Subject: [PATCH 9/9] [ticket/13661] BUMP version to 3.1.5-dev

PHPBB3-13661
---
 phpBB/phpbb/log/log.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php
index 1aba5851a5..f4ba76ff0c 100644
--- a/phpBB/phpbb/log/log.php
+++ b/phpBB/phpbb/log/log.php
@@ -564,7 +564,7 @@ class log implements \phpbb\log\log_interface
 		*									is false, no entries will be returned.
 		* @var	string	sql_additional		Additional conditions for the entries,
 		*									e.g.: 'AND l.forum_id = 1'
-		* @since 3.1.4-RC1
+		* @since 3.1.5-RC1
 		*/
 		$vars = array(
 			'get_logs_sql_ary',