[ticket/13645] Proper OOP for feeds

PHPBB3-13645
This commit is contained in:
Tristan Darricau 2015-08-06 14:20:06 +02:00
parent 8e5e954438
commit 5df9a45473
15 changed files with 506 additions and 320 deletions

View file

@ -229,6 +229,7 @@ $lang = array_merge($lang, array(
'FACEBOOK' => 'Facebook',
'FAQ' => 'FAQ',
'FAQ_EXPLAIN' => 'Frequently Asked Questions',
'FEATURE_NOT_AVAILABLE' => 'The requested feature is not available on this board.',
'FILENAME' => 'Filename',
'FILESIZE' => 'File size',
'FILEDATE' => 'File date',

View file

@ -16,7 +16,7 @@ namespace phpbb\feed;
/**
* Abstract class for feeds displaying attachments
*/
abstract class attachments_base extends \phpbb\feed\base
abstract class attachments_base extends base
{
/**
* Attachments that may be displayed

View file

@ -16,7 +16,7 @@ namespace phpbb\feed;
/**
* Base class with some generic functions and settings.
*/
abstract class base
abstract class base implements feed_interface
{
/**
* Feed helper object
@ -45,27 +45,27 @@ abstract class base
/**
* SQL Query to be executed to get feed items
*/
var $sql = array();
protected $sql = array();
/**
* Keys specified for retrieval of title, content, etc.
*/
var $keys = array();
protected $keys = array();
/**
* Number of items to fetch. Usually overwritten by $config['feed_something']
*/
var $num_items = 15;
protected $num_items = 15;
/**
* Separator for title elements to separate items (for example forum / topic)
*/
var $separator = "\xE2\x80\xA2"; // •
protected $separator = "\xE2\x80\xA2"; // •
/**
* Separator for the statistics row (Posted by, post date, replies, etc.)
*/
var $separator_stats = "\xE2\x80\x94"; // —
protected $separator_stats = "\xE2\x80\x94"; // —
/** @var mixed Query result handle */
protected $result;
@ -82,7 +82,7 @@ abstract class base
* @param \phpbb\content_visibility $content_visibility Auth object
* @param string $phpEx php file extension
*/
function __construct(\phpbb\feed\helper $helper, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\cache\driver\driver_interface $cache, \phpbb\user $user, \phpbb\auth\auth $auth, \phpbb\content_visibility $content_visibility, $phpEx)
public function __construct(\phpbb\feed\helper $helper, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\cache\driver\driver_interface $cache, \phpbb\user $user, \phpbb\auth\auth $auth, \phpbb\content_visibility $content_visibility, $phpEx)
{
$this->config = $config;
$this->helper = $helper;
@ -109,23 +109,23 @@ abstract class base
}
/**
* Set keys.
* {@inheritdoc}
*/
function set_keys()
public function set_keys()
{
}
/**
* Open feed
* {@inheritdoc}
*/
function open()
public function open()
{
}
/**
* Close feed
* {@inheritdoc}
*/
function close()
public function close()
{
if (!empty($this->result))
{
@ -134,28 +134,47 @@ abstract class base
}
/**
* Set key
*
* @param string $key Key
* @param mixed $value Value
* {@inheritdoc}
*/
function set($key, $value)
public function set($key, $value)
{
$this->keys[$key] = $value;
}
/**
* Get key
*
* @param string $key Key
* @return mixed
* {@inheritdoc}
*/
function get($key)
public function get($key)
{
return (isset($this->keys[$key])) ? $this->keys[$key] : null;
}
function get_readable_forums()
/**
* {@inheritdoc}
*/
public function get_item()
{
if (!isset($this->result))
{
if (!$this->get_sql())
{
return false;
}
// Query database
$sql = $this->db->sql_build_query('SELECT', $this->sql);
$this->result = $this->db->sql_query_limit($sql, $this->num_items);
}
return $this->db->sql_fetchrow($this->result);
}
/**
* Returns the ids of the forums readable by the current user.
*
* @return int[]
*/
protected function get_readable_forums()
{
static $forum_ids;
@ -167,7 +186,12 @@ abstract class base
return $forum_ids;
}
function get_moderator_approve_forums()
/**
* Returns the ids of the forum for which the current user can approve the post in the moderation queue.
*
* @return int[]
*/
protected function get_moderator_approve_forums()
{
static $forum_ids;
@ -179,7 +203,13 @@ abstract class base
return $forum_ids;
}
function is_moderator_approve_forum($forum_id)
/**
* Returns true if the current user can approve the post of the given forum
*
* @param int $forum_id Forum id to check
* @return bool
*/
protected function is_moderator_approve_forum($forum_id)
{
static $forum_ids;
@ -191,7 +221,12 @@ abstract class base
return (isset($forum_ids[$forum_id])) ? true : false;
}
function get_excluded_forums()
/**
* Returns the ids of the forum excluded from the feeds
*
* @return int[]
*/
protected function get_excluded_forums()
{
static $forum_ids;
@ -218,36 +253,35 @@ abstract class base
return $forum_ids;
}
function is_excluded_forum($forum_id)
/**
* Returns true if the given id is in the excluded forums list.
*
* @param int $forum_id Id to check
* @return bool
*/
protected function is_excluded_forum($forum_id)
{
$forum_ids = $this->get_excluded_forums();
return isset($forum_ids[$forum_id]) ? true : false;
}
function get_passworded_forums()
/**
* Returns all password protected forum ids the current user is currently NOT authenticated for.
*
* @return array Array of forum ids
*/
protected function get_passworded_forums()
{
return $this->user->get_passworded_forums();
}
function get_item()
{
if (!isset($this->result))
{
if (!$this->get_sql())
{
return false;
}
// Query database
$sql = $this->db->sql_build_query('SELECT', $this->sql);
$this->result = $this->db->sql_query_limit($sql, $this->num_items);
}
return $this->db->sql_fetchrow($this->result);
}
function user_viewprofile($row)
/**
* Returns the link to the user profile.
*
* @return string
*/
protected function user_viewprofile($row)
{
$author_id = (int) $row[$this->get('author_id')];
@ -260,4 +294,11 @@ abstract class base
return '<a href="' . $this->helper->append_sid('memberlist.' . $this->phpEx, 'mode=viewprofile&amp;u=' . $author_id) . '">' . $row[$this->get('creator')] . '</a>';
}
/**
* Returns the SQL query used to retrieve the posts of the feed.
*
* @return string SQL SELECT query
*/
protected abstract function get_sql();
}

View file

@ -17,7 +17,7 @@ use phpbb\auth\auth;
use phpbb\config\config;
use phpbb\db\driver\driver_interface;
use phpbb\exception\http_exception;
use phpbb\feed\base;
use phpbb\feed\feed_interface;
use phpbb\feed\exception\feed_unavailable_exception;
use phpbb\feed\exception\unauthorized_exception;
use phpbb\feed\helper as feed_helper;
@ -252,11 +252,11 @@ class feed
/**
* Display a given feed
*
* @param base $feed
* @param feed_interface $feed
*
* @return Response
*/
protected function send_feed(base $feed)
protected function send_feed(feed_interface $feed)
{
try
{
@ -275,13 +275,13 @@ class feed
/**
* Really send the feed
*
* @param base $feed
* @param feed_interface $feed
*
* @return Response
*
* @throw exception\feed_exception
*/
protected function send_feed_do(base $feed)
protected function send_feed_do(feed_interface $feed)
{
$feed_updated_time = 0;
$item_vars = array();

View file

@ -0,0 +1,67 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\feed;
/**
* Interface implemented by all feeds types
*/
interface feed_interface
{
/**
* Set keys.
*/
public function set_keys();
/**
* Open feed
*/
public function open();
/**
* Close feed
*/
public function close();
/**
* Set key
*
* @param string $key Key
* @param mixed $value Value
*/
public function set($key, $value);
/**
* Get key
*
* @param string $key Key
* @return mixed
*/
public function get($key);
/**
* Get the next post in the feed
*
* @return array
*/
public function get_item();
/**
* Adjust a feed entry
*
* @param $item_row
* @param $row
* @return array
*/
public function adjust_item(&$item_row, &$row);
}

View file

@ -23,10 +23,10 @@ use phpbb\feed\exception\unauthorized_forum_exception;
* This will give you the last {$this->num_items} posts made
* within a specific forum.
*/
class forum extends \phpbb\feed\post_base
class forum extends post_base
{
var $forum_id = 0;
var $forum_data = array();
protected $forum_id = 0;
protected $forum_data = array();
/**
* Set the Forum ID
@ -41,7 +41,10 @@ class forum extends \phpbb\feed\post_base
return $this;
}
function open()
/**
* {@inheritdoc}
*/
public function open()
{
// Check if forum exists
$sql = 'SELECT forum_id, forum_name, forum_password, forum_type, forum_options
@ -90,7 +93,10 @@ class forum extends \phpbb\feed\post_base
parent::open();
}
function get_sql()
/**
* {@inheritdoc}
*/
protected function get_sql()
{
// Determine topics with recent activity
$sql = 'SELECT topic_id, topic_last_post_time
@ -133,7 +139,10 @@ class forum extends \phpbb\feed\post_base
return true;
}
function adjust_item(&$item_row, &$row)
/**
* {@inheritdoc}
*/
public function adjust_item(&$item_row, &$row)
{
parent::adjust_item($item_row, $row);
@ -141,7 +150,10 @@ class forum extends \phpbb\feed\post_base
$item_row['forum_id'] = $this->forum_id;
}
function get_item()
/**
* {@inheritdoc}
*/
public function get_item()
{
return ($row = parent::get_item()) ? array_merge($this->forum_data, $row) : $row;
}

View file

@ -19,11 +19,14 @@ namespace phpbb\feed;
* This will give you a list of all postable forums where feeds are enabled
* including forum description, topic stats and post stats
*/
class forums extends \phpbb\feed\base
class forums extends base
{
var $num_items = 0;
protected $num_items = 0;
function set_keys()
/**
* {@inheritdoc}
*/
public function set_keys()
{
$this->set('title', 'forum_name');
$this->set('text', 'forum_desc');
@ -33,7 +36,10 @@ class forums extends \phpbb\feed\base
$this->set('options', 'forum_desc_options');
}
function get_sql()
/**
* {@inheritdoc}
*/
public function get_sql()
{
$in_fid_ary = array_diff($this->get_readable_forums(), $this->get_excluded_forums());
if (empty($in_fid_ary))
@ -55,7 +61,10 @@ class forums extends \phpbb\feed\base
return true;
}
function adjust_item(&$item_row, &$row)
/**
* {@inheritdoc}
*/
public function adjust_item(&$item_row, &$row)
{
$item_row['link'] = $this->helper->append_sid('viewforum.' . $this->phpEx, 'f=' . $row['forum_id']);

View file

@ -19,9 +19,13 @@ namespace phpbb\feed;
* This will give you {$this->num_items} first posts
* of all topics in the selected news forums.
*/
class news extends \phpbb\feed\topic_base
class news extends topic_base
{
function get_news_forums()
/**
* Returns the ids of the 'news forums'
* @return int[]
*/
private function get_news_forums()
{
static $forum_ids;
@ -48,7 +52,10 @@ class news extends \phpbb\feed\topic_base
return $forum_ids;
}
function get_sql()
/**
* {@inheritdoc}
*/
protected function get_sql()
{
// Determine forum ids
$in_fid_ary = array_intersect($this->get_news_forums(), $this->get_readable_forums());

View file

@ -19,9 +19,12 @@ namespace phpbb\feed;
* This will give you the newest {$this->num_items} posts
* from the whole board.
*/
class overall extends \phpbb\feed\post_base
class overall extends post_base
{
function get_sql()
/**
* {@inheritdoc}
*/
protected function get_sql()
{
$forum_ids = array_diff($this->get_readable_forums(), $this->get_excluded_forums(), $this->get_passworded_forums());
if (empty($forum_ids))
@ -77,7 +80,10 @@ class overall extends \phpbb\feed\post_base
return true;
}
function adjust_item(&$item_row, &$row)
/**
* {@inheritdoc}
*/
public function adjust_item(&$item_row, &$row)
{
parent::adjust_item($item_row, $row);

View file

@ -16,12 +16,14 @@ namespace phpbb\feed;
/**
* Abstract class for post based feeds
*/
abstract class post_base extends \phpbb\feed\attachments_base
abstract class post_base extends attachments_base
{
var $num_items = 'feed_limit_post';
var $attachments = array();
protected $num_items = 'feed_limit_post';
function set_keys()
/**
* {@inheritdoc}
*/
public function set_keys()
{
$this->set('title', 'post_subject');
$this->set('title2', 'topic_title');
@ -40,7 +42,10 @@ abstract class post_base extends \phpbb\feed\attachments_base
$this->set('enable_magic_url', 'enable_magic_url');
}
function adjust_item(&$item_row, &$row)
/**
* {@inheritdoc}
*/
public function adjust_item(&$item_row, &$row)
{
$item_row['link'] = $this->helper->append_sid('viewtopic.' . $this->phpEx, "t={$row['topic_id']}&amp;p={$row['post_id']}#p{$row['post_id']}");

View file

@ -23,11 +23,11 @@ use phpbb\feed\exception\unauthorized_topic_exception;
*
* This will give you the last {$this->num_items} posts made within this topic.
*/
class topic extends \phpbb\feed\post_base
class topic extends post_base
{
var $topic_id = 0;
var $forum_id = 0;
var $topic_data = array();
protected $topic_id = 0;
protected $forum_id = 0;
protected $topic_data = array();
/**
* Set the Topic ID
@ -42,7 +42,10 @@ class topic extends \phpbb\feed\post_base
return $this;
}
function open()
/**
* {@inheritdoc}
*/
public function open()
{
$sql = 'SELECT f.forum_options, f.forum_password, t.topic_id, t.forum_id, t.topic_visibility, t.topic_title, t.topic_time, t.topic_views, t.topic_posts_approved, t.topic_type
FROM ' . TOPICS_TABLE . ' t
@ -94,7 +97,10 @@ class topic extends \phpbb\feed\post_base
parent::open();
}
function get_sql()
/**
* {@inheritdoc}
*/
protected function get_sql()
{
$this->sql = array(
'SELECT' => 'p.post_id, p.post_time, p.post_edit_time, p.post_visibility, p.post_subject, p.post_text, p.bbcode_bitfield, p.bbcode_uid, p.enable_bbcode, p.enable_smilies, p.enable_magic_url, p.post_attachment, ' .
@ -112,14 +118,20 @@ class topic extends \phpbb\feed\post_base
return true;
}
function adjust_item(&$item_row, &$row)
/**
* {@inheritdoc}
*/
public function adjust_item(&$item_row, &$row)
{
parent::adjust_item($item_row, $row);
$item_row['forum_id'] = $this->forum_id;
}
function get_item()
/**
* {@inheritdoc}
*/
public function get_item()
{
return ($row = parent::get_item()) ? array_merge($this->topic_data, $row) : $row;
}

View file

@ -16,11 +16,14 @@ namespace phpbb\feed;
/**
* Abstract class for topic based feeds
*/
abstract class topic_base extends \phpbb\feed\attachments_base
abstract class topic_base extends attachments_base
{
var $num_items = 'feed_limit_topic';
protected $num_items = 'feed_limit_topic';
function set_keys()
/**
* {@inheritdoc}
*/
public function set_keys()
{
$this->set('title', 'topic_title');
$this->set('title2', 'forum_name');
@ -39,7 +42,10 @@ abstract class topic_base extends \phpbb\feed\attachments_base
$this->set('enable_magic_url', 'enable_magic_url');
}
function adjust_item(&$item_row, &$row)
/**
* {@inheritdoc}
*/
public function adjust_item(&$item_row, &$row)
{
$item_row['link'] = $this->helper->append_sid('viewtopic.' . $this->phpEx, 't=' . $row['topic_id'] . '&amp;p=' . $row['post_id'] . '#p' . $row['post_id']);

View file

@ -19,9 +19,12 @@ namespace phpbb\feed;
* This will give you the last {$this->num_items} created topics
* including the first post.
*/
class topics extends \phpbb\feed\topic_base
class topics extends topic_base
{
function get_sql()
/**
* {@inheritdoc}
*/
protected function get_sql()
{
$forum_ids_read = $this->get_readable_forums();
if (empty($forum_ids_read))
@ -77,7 +80,10 @@ class topics extends \phpbb\feed\topic_base
return true;
}
function adjust_item(&$item_row, &$row)
/**
* {@inheritdoc}
*/
public function adjust_item(&$item_row, &$row)
{
parent::adjust_item($item_row, $row);

View file

@ -20,11 +20,14 @@ namespace phpbb\feed;
* with replies made withing the last {$this->sort_days} days
* including the last post.
*/
class topics_active extends \phpbb\feed\topic_base
class topics_active extends topic_base
{
var $sort_days = 7;
protected $sort_days = 7;
function set_keys()
/**
* {@inheritdoc}
*/
public function set_keys()
{
parent::set_keys();
@ -32,7 +35,10 @@ class topics_active extends \phpbb\feed\topic_base
$this->set('creator', 'topic_last_poster_name');
}
function get_sql()
/**
* {@inheritdoc}
*/
protected function get_sql()
{
$forum_ids_read = $this->get_readable_forums();
if (empty($forum_ids_read))
@ -94,7 +100,12 @@ class topics_active extends \phpbb\feed\topic_base
return true;
}
function get_forum_ids()
/**
* Returns the ids of the forums not excluded from the active list
*
* @return int[]
*/
private function get_forum_ids()
{
static $forum_ids;
@ -122,7 +133,10 @@ class topics_active extends \phpbb\feed\topic_base
return $forum_ids;
}
function adjust_item(&$item_row, &$row)
/**
* {@inheritdoc}
*/
public function adjust_item(&$item_row, &$row)
{
parent::adjust_item($item_row, $row);