From 2916ddc38c964992b44fd5be50963521f59c1858 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 6 Jun 2013 17:03:10 +0200 Subject: [ticket/11481] Move feed base to own file PHPBB3-11481 --- phpBB/includes/feed/base.php | 230 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 phpBB/includes/feed/base.php (limited to 'phpBB/includes/feed/base.php') diff --git a/phpBB/includes/feed/base.php b/phpBB/includes/feed/base.php new file mode 100644 index 0000000000..300ccf9b1e --- /dev/null +++ b/phpBB/includes/feed/base.php @@ -0,0 +1,230 @@ +set_keys(); + + // Allow num_items to be string + if (is_string($this->num_items)) + { + $this->num_items = (int) $config[$this->num_items]; + + // A precaution + if (!$this->num_items) + { + $this->num_items = 10; + } + } + } + + /** + * Set keys. + */ + function set_keys() + { + } + + /** + * Open feed + */ + function open() + { + } + + /** + * Close feed + */ + function close() + { + global $db; + + if (!empty($this->result)) + { + $db->sql_freeresult($this->result); + } + } + + /** + * Set key + */ + function set($key, $value) + { + $this->keys[$key] = $value; + } + + /** + * Get key + */ + function get($key) + { + return (isset($this->keys[$key])) ? $this->keys[$key] : NULL; + } + + function get_readable_forums() + { + global $auth; + static $forum_ids; + + if (!isset($forum_ids)) + { + $forum_ids = array_keys($auth->acl_getf('f_read', true)); + } + + return $forum_ids; + } + + function get_moderator_approve_forums() + { + global $auth; + static $forum_ids; + + if (!isset($forum_ids)) + { + $forum_ids = array_keys($auth->acl_getf('m_approve', true)); + } + + return $forum_ids; + } + + function is_moderator_approve_forum($forum_id) + { + static $forum_ids; + + if (!isset($forum_ids)) + { + $forum_ids = array_flip($this->get_moderator_approve_forums()); + } + + return (isset($forum_ids[$forum_id])) ? true : false; + } + + function get_excluded_forums() + { + global $db, $cache; + static $forum_ids; + + // Matches acp/acp_board.php + $cache_name = 'feed_excluded_forum_ids'; + + if (!isset($forum_ids) && ($forum_ids = $cache->get('_' . $cache_name)) === false) + { + $sql = 'SELECT forum_id + FROM ' . FORUMS_TABLE . ' + WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '<> 0'); + $result = $db->sql_query($sql); + + $forum_ids = array(); + while ($forum_id = (int) $db->sql_fetchfield('forum_id')) + { + $forum_ids[$forum_id] = $forum_id; + } + $db->sql_freeresult($result); + + $cache->put('_' . $cache_name, $forum_ids); + } + + return $forum_ids; + } + + function is_excluded_forum($forum_id) + { + $forum_ids = $this->get_excluded_forums(); + + return isset($forum_ids[$forum_id]) ? true : false; + } + + function get_passworded_forums() + { + global $user; + + return $user->get_passworded_forums(); + } + + function get_item() + { + global $db, $cache; + static $result; + + if (!isset($result)) + { + if (!$this->get_sql()) + { + return false; + } + + // Query database + $sql = $db->sql_build_query('SELECT', $this->sql); + $result = $db->sql_query_limit($sql, $this->num_items); + } + + return $db->sql_fetchrow($result); + } + + function user_viewprofile($row) + { + global $phpEx, $user; + + $author_id = (int) $row[$this->get('author_id')]; + + if ($author_id == ANONYMOUS) + { + // Since we cannot link to a profile, we just return GUEST + // instead of $row['username'] + return $user->lang['GUEST']; + } + + return '' . $row[$this->get('creator')] . ''; + } +} -- cgit v1.2.1 From 3efe0eb24687a0e36e316b60887eff3ed45ae9b4 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 6 Jun 2013 20:04:23 +0200 Subject: [ticket/11481] Use container for all classes and inject dependencies PHPBB3-11481 --- phpBB/includes/feed/base.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/feed/base.php') diff --git a/phpBB/includes/feed/base.php b/phpBB/includes/feed/base.php index 300ccf9b1e..2ffd20d31a 100644 --- a/phpBB/includes/feed/base.php +++ b/phpBB/includes/feed/base.php @@ -22,6 +22,12 @@ if (!defined('IN_PHPBB')) */ abstract class phpbb_feed_base { + /** + * Feed helper object + * @var phpbb_feed_helper + */ + protected $helper; + /** * SQL Query to be executed to get feed items */ @@ -49,8 +55,11 @@ abstract class phpbb_feed_base /** * Constructor + * + * @param phpbb_feed_helper $helper Feed helper + * @return null */ - function __construct() + function __construct(phpbb_feed_helper $helper) { global $config; @@ -67,6 +76,8 @@ abstract class phpbb_feed_base $this->num_items = 10; } } + + $this->helper = $helper; } /** @@ -225,6 +236,6 @@ abstract class phpbb_feed_base return $user->lang['GUEST']; } - return '' . $row[$this->get('creator')] . ''; + return '' . $row[$this->get('creator')] . ''; } } -- cgit v1.2.1 From 63334514555acea665186f20046a49a5ed41c334 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 6 Jun 2013 20:32:47 +0200 Subject: [ticket/11481] Remove globals and use dependency injection instead PHPBB3-11481 --- phpBB/includes/feed/base.php | 78 +++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 30 deletions(-) (limited to 'phpBB/includes/feed/base.php') diff --git a/phpBB/includes/feed/base.php b/phpBB/includes/feed/base.php index 2ffd20d31a..c8015ab916 100644 --- a/phpBB/includes/feed/base.php +++ b/phpBB/includes/feed/base.php @@ -28,6 +28,24 @@ abstract class phpbb_feed_base */ protected $helper; + /** @var phpbb_config */ + protected $config; + + /** @var phpbb_db_driver */ + protected $db; + + /** @var phpbb_cache_driver_interface */ + protected $cache; + + /** @var phpbb_user */ + protected $user; + + /** @var phpbb_auth */ + protected $auth; + + /** @var string */ + protected $phpEx; + /** * SQL Query to be executed to get feed items */ @@ -57,18 +75,30 @@ abstract class phpbb_feed_base * Constructor * * @param phpbb_feed_helper $helper Feed helper + * @param phpbb_config $config Config object + * @param phpbb_db_driver $db Database connection + * @param phpbb_cache_driver_interface $cache Cache object + * @param phpbb_user $user User object + * @param phpbb_auth $auth Auth object + * @param string $phpEx php file extension * @return null */ - function __construct(phpbb_feed_helper $helper) + function __construct(phpbb_feed_helper $helper, phpbb_config $config, phpbb_db_driver $db, phpbb_cache_driver_interface $cache, phpbb_user $user, phpbb_auth $auth, $phpEx) { - global $config; + $this->config = $config; + $this->helper = $helper; + $this->db = $db; + $this->cache = $cache; + $this->user = $user; + $this->auth = $auth; + $this->phpEx = $phpEx; $this->set_keys(); // Allow num_items to be string if (is_string($this->num_items)) { - $this->num_items = (int) $config[$this->num_items]; + $this->num_items = (int) $this->config[$this->num_items]; // A precaution if (!$this->num_items) @@ -76,8 +106,6 @@ abstract class phpbb_feed_base $this->num_items = 10; } } - - $this->helper = $helper; } /** @@ -99,11 +127,9 @@ abstract class phpbb_feed_base */ function close() { - global $db; - if (!empty($this->result)) { - $db->sql_freeresult($this->result); + $this->db->sql_freeresult($this->result); } } @@ -125,12 +151,11 @@ abstract class phpbb_feed_base function get_readable_forums() { - global $auth; static $forum_ids; if (!isset($forum_ids)) { - $forum_ids = array_keys($auth->acl_getf('f_read', true)); + $forum_ids = array_keys($this->auth->acl_getf('f_read', true)); } return $forum_ids; @@ -138,12 +163,11 @@ abstract class phpbb_feed_base function get_moderator_approve_forums() { - global $auth; static $forum_ids; if (!isset($forum_ids)) { - $forum_ids = array_keys($auth->acl_getf('m_approve', true)); + $forum_ids = array_keys($this->auth->acl_getf('m_approve', true)); } return $forum_ids; @@ -163,27 +187,26 @@ abstract class phpbb_feed_base function get_excluded_forums() { - global $db, $cache; static $forum_ids; // Matches acp/acp_board.php $cache_name = 'feed_excluded_forum_ids'; - if (!isset($forum_ids) && ($forum_ids = $cache->get('_' . $cache_name)) === false) + if (!isset($forum_ids) && ($forum_ids = $this->cache->get('_' . $cache_name)) === false) { $sql = 'SELECT forum_id FROM ' . FORUMS_TABLE . ' - WHERE ' . $db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '<> 0'); - $result = $db->sql_query($sql); + WHERE ' . $this->db->sql_bit_and('forum_options', FORUM_OPTION_FEED_EXCLUDE, '<> 0'); + $result = $this->db->sql_query($sql); $forum_ids = array(); - while ($forum_id = (int) $db->sql_fetchfield('forum_id')) + while ($forum_id = (int) $this->db->sql_fetchfield('forum_id')) { $forum_ids[$forum_id] = $forum_id; } - $db->sql_freeresult($result); + $this->db->sql_freeresult($result); - $cache->put('_' . $cache_name, $forum_ids); + $this->cache->put('_' . $cache_name, $forum_ids); } return $forum_ids; @@ -198,14 +221,11 @@ abstract class phpbb_feed_base function get_passworded_forums() { - global $user; - - return $user->get_passworded_forums(); + return $this->user->get_passworded_forums(); } function get_item() { - global $db, $cache; static $result; if (!isset($result)) @@ -216,26 +236,24 @@ abstract class phpbb_feed_base } // Query database - $sql = $db->sql_build_query('SELECT', $this->sql); - $result = $db->sql_query_limit($sql, $this->num_items); + $sql = $this->db->sql_build_query('SELECT', $this->sql); + $result = $this->db->sql_query_limit($sql, $this->num_items); } - return $db->sql_fetchrow($result); + return $this->db->sql_fetchrow($result); } function user_viewprofile($row) { - global $phpEx, $user; - $author_id = (int) $row[$this->get('author_id')]; if ($author_id == ANONYMOUS) { // Since we cannot link to a profile, we just return GUEST // instead of $row['username'] - return $user->lang['GUEST']; + return $this->user->lang['GUEST']; } - return '' . $row[$this->get('creator')] . ''; + return '' . $row[$this->get('creator')] . ''; } } -- cgit v1.2.1 From e36deed24f62f4fca0a3e82b2d58d97499ff7764 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 6 Jun 2013 20:35:38 +0200 Subject: [ticket/11481] Move prepended slash from calls into function PHPBB3-11481 --- phpBB/includes/feed/base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/feed/base.php') diff --git a/phpBB/includes/feed/base.php b/phpBB/includes/feed/base.php index c8015ab916..af28ee8dc8 100644 --- a/phpBB/includes/feed/base.php +++ b/phpBB/includes/feed/base.php @@ -254,6 +254,6 @@ abstract class phpbb_feed_base return $this->user->lang['GUEST']; } - return '' . $row[$this->get('creator')] . ''; + return '' . $row[$this->get('creator')] . ''; } } -- cgit v1.2.1