From e46b2803a728104adcd17b19912f8f7a9a6428fd Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Fri, 10 Jul 2015 00:28:36 +0200 Subject: [PATCH] Introducing TargetedRss10Action for simplifying RSS 1.0 --- actions/allrss.php | 72 +++----------------- actions/grouprss.php | 56 ++++------------ actions/noticesearchrss.php | 17 +---- actions/publicrss.php | 34 +--------- actions/repliesrss.php | 50 +++----------- actions/tagrss.php | 24 +++---- actions/userrss.php | 82 +++++------------------ classes/Local_group.php | 2 +- classes/Profile.php | 5 ++ classes/Reply.php | 3 +- lib/rss10action.php | 15 ++++- plugins/Bookmark/actions/bookmarksrss.php | 70 +++---------------- plugins/Favorite/actions/favoritesrss.php | 58 ++-------------- 13 files changed, 93 insertions(+), 395 deletions(-) diff --git a/actions/allrss.php b/actions/allrss.php index d311701a57..4b6df25048 100644 --- a/actions/allrss.php +++ b/actions/allrss.php @@ -42,52 +42,12 @@ if (!defined('GNUSOCIAL')) { exit(1); } * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 * @link http://status.net/ */ -class AllrssAction extends Rss10Action +class AllrssAction extends TargetedRss10Action { - var $user = null; - - /** - * Initialization. - * - * @param array $args Web and URL arguments - * - * @return boolean false if user doesn't exist - * - */ - function prepare($args) + protected function getNotices() { - parent::prepare($args); - $nickname = $this->trimmed('nickname'); - $this->user = User::getKV('nickname', $nickname); - - if (!$this->user) { - // TRANS: Client error when user not found for an rss related action. - $this->clientError(_('No such user.')); - } else { - $this->notices = $this->getNotices($this->limit); - return true; - } - } - - /** - * Get notices - * - * @param integer $limit max number of notices to return - * - * @return array notices - */ - function getNotices($limit=0) - { - $stream = new InboxNoticeStream($this->user->getProfile()); - $notice = $stream->getNotices(0, $limit, null, null); - - $notices = array(); - - while ($notice->fetch()) { - $notices[] = clone($notice); - } - - return $notices; + $stream = new InboxNoticeStream($this->target); + return $stream->getNotices(0, $this->limit)->fetchAll(); } /** @@ -97,33 +57,17 @@ class AllrssAction extends Rss10Action */ function getChannel() { - $user = $this->user; $c = array('url' => common_local_url('allrss', array('nickname' => - $user->nickname)), + $this->target->getNickname())), // TRANS: Message is used as link title. %s is a user nickname. - 'title' => sprintf(_('%s and friends'), $user->nickname), + 'title' => sprintf(_('%s and friends'), $this->target->getNickname()), 'link' => common_local_url('all', array('nickname' => - $user->nickname)), + $this->target->getNickname())), // TRANS: Message is used as link description. %1$s is a username, %2$s is a site name. 'description' => sprintf(_('Updates from %1$s and friends on %2$s!'), - $user->nickname, common_config('site', 'name'))); + $this->target->getNickname(), common_config('site', 'name'))); return $c; } - - /** - * Get image. - * - * @return string user avatar URL or null - */ - function getImage() - { - $user = $this->user; - $profile = $user->getProfile(); - if (!$profile) { - return null; - } - return $profile->avatarUrl(AVATAR_PROFILE_SIZE); - } } diff --git a/actions/grouprss.php b/actions/grouprss.php index dca323ad73..14d85d89cd 100644 --- a/actions/grouprss.php +++ b/actions/grouprss.php @@ -41,10 +41,10 @@ define('MEMBERS_PER_SECTION', 27); * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ */ -class groupRssAction extends Rss10Action +class GroupRssAction extends TargetedRss10Action { /** group we're viewing. */ - var $group = null; + protected $group = null; /** * Is this page read-only? @@ -56,18 +56,8 @@ class groupRssAction extends Rss10Action return true; } - /** - * Prepare the action - * - * Reads and validates arguments and instantiates the attributes. - * - * @param array $args $_REQUEST args - * - * @return boolean success flag - */ - function prepare($args) + protected function doStreamPreparation() { - parent::prepare($args); $nickname_arg = $this->arg('nickname'); $nickname = common_canonical_nickname($nickname_arg); @@ -86,52 +76,32 @@ class groupRssAction extends Rss10Action $local = Local_group::getKV('nickname', $nickname); - if (!$local) { + if (!$local instanceof Local_group) { // TRANS: Client error displayed when requesting a group RSS feed for group that does not exist. $this->clientError(_('No such group.'), 404); } - $this->group = User_group::getKV('id', $local->group_id); - - if (!$this->group) { - // TRANS: Client error displayed when requesting a group RSS feed for an object that is not a group. - $this->clientError(_('No such group.'), 404); - } - - $this->notices = $this->getNotices($this->limit); - return true; + $this->group = $local->getGroup(); + $this->target = $this->group->getProfile(); } - function getNotices($limit=0) + protected function getNotices() { - $group = $this->group; - - if (is_null($group)) { - return null; - } - - $notices = array(); - $notice = $group->getNotices(0, ($limit == 0) ? NOTICES_PER_PAGE : $limit); - - while ($notice->fetch()) { - $notices[] = clone($notice); - } - - return $notices; + $stream = $this->group->getNotices(0, $this->limit); + return $stream->fetchAll(); } function getChannel() { - $group = $this->group; $c = array('url' => common_local_url('grouprss', array('nickname' => - $group->nickname)), + $this->target->getNickname())), // TRANS: Message is used as link title. %s is a user nickname. - 'title' => sprintf(_('%s timeline'), $group->nickname), - 'link' => common_local_url('showgroup', array('nickname' => $group->nickname)), + 'title' => sprintf(_('%s timeline'), $this->target->getNickname()), + 'link' => common_local_url('showgroup', array('nickname' => $this->target->getNickname())), // TRANS: Message is used as link description. %1$s is a group name, %2$s is a site name. 'description' => sprintf(_('Updates from members of %1$s on %2$s!'), - $group->nickname, common_config('site', 'name'))); + $this->target->getNickname(), common_config('site', 'name'))); return $c; } diff --git a/actions/noticesearchrss.php b/actions/noticesearchrss.php index 2ecdd547fb..2a5187b885 100644 --- a/actions/noticesearchrss.php +++ b/actions/noticesearchrss.php @@ -44,19 +44,7 @@ if (!defined('GNUSOCIAL')) { exit(1); } */ class NoticesearchrssAction extends Rss10Action { - function init() - { - return true; - } - - function prepare($args) - { - parent::prepare($args); - $this->notices = $this->getNotices(); - return true; - } - - function getNotices($limit=0) + protected function getNotices() { $q = $this->trimmed('q'); $notices = array(); @@ -66,8 +54,7 @@ class NoticesearchrssAction extends Rss10Action $search_engine = $notice->getSearchEngine('notice'); $search_engine->set_sort_mode('chron'); - if (!$limit) $limit = 20; - $search_engine->limit(0, $limit, true); + $search_engine->limit(0, $this->limit, true); if (false === $search_engine->query($q)) { $cnt = 0; } else { diff --git a/actions/publicrss.php b/actions/publicrss.php index 7ad665e282..5dcff3ba3d 100644 --- a/actions/publicrss.php +++ b/actions/publicrss.php @@ -44,29 +44,6 @@ if (!defined('GNUSOCIAL')) { exit(1); } */ class PublicrssAction extends Rss10Action { - /** - * Read arguments and initialize members - * - * @param array $args Arguments from $_REQUEST - * @return boolean success - */ - function prepare($args) - { - parent::prepare($args); - $this->notices = $this->getNotices($this->limit); - return true; - } - - /** - * Initialization. - * - * @return boolean true - */ - function init() - { - return true; - } - /** * Get notices * @@ -74,15 +51,10 @@ class PublicrssAction extends Rss10Action * * @return array notices */ - function getNotices($limit=0) + protected function getNotices() { - $notices = array(); - $notice = Notice::publicStream(0, ($limit == 0) ? 48 : $limit); - while ($notice->fetch()) { - $notices[] = clone($notice); - } - - return $notices; + $stream = Notice::publicStream(0, $this->limit); + return $stream->fetchAll(); } /** diff --git a/actions/repliesrss.php b/actions/repliesrss.php index 4e9af1e9ff..54f83592c0 100644 --- a/actions/repliesrss.php +++ b/actions/repliesrss.php @@ -21,64 +21,30 @@ if (!defined('GNUSOCIAL')) { exit(1); } // Formatting of RSS handled by Rss10Action -class RepliesrssAction extends Rss10Action +class RepliesrssAction extends TargetedRss10Action { - var $user = null; - - function prepare($args) + protected function getNotices() { - parent::prepare($args); - $nickname = $this->trimmed('nickname'); - $this->user = User::getKV('nickname', $nickname); - - if (!$this->user) { - // TRANS: Client error displayed when providing a non-existing nickname in a RSS 1.0 action. - $this->clientError(_('No such user.')); - } else { - $this->notices = $this->getNotices($this->limit); - return true; - } - } - - function getNotices($limit=0) - { - $user = $this->user; - - $notice = $user->getReplies(0, ($limit == 0) ? 48 : $limit); - - $notices = array(); - - while ($notice->fetch()) { - $notices[] = clone($notice); - } - - return $notices; + $stream = $this->target->getReplies(0, $this->limit); + return $stream->fetchAll(); } function getChannel() { - $user = $this->user; $c = array('url' => common_local_url('repliesrss', array('nickname' => - $user->nickname)), + $this->target->getNickname())), // TRANS: RSS reply feed title. %s is a user nickname. - 'title' => sprintf(_("Replies to %s"), $user->nickname), + 'title' => sprintf(_("Replies to %s"), $this->target->getNickname()), 'link' => common_local_url('replies', - array('nickname' => - $user->nickname)), + array('nickname' => $this->target->getNickname())), // TRANS: RSS reply feed description. // TRANS: %1$s is a user nickname, %2$s is the StatusNet site name. 'description' => sprintf(_('Replies to %1$s on %2$s.'), - $user->nickname, common_config('site', 'name'))); + $this->target->getNickname(), common_config('site', 'name'))); return $c; } - function getImage() - { - $profile = $this->user->getProfile(); - return $profile->avatarUrl(AVATAR_PROFILE_SIZE); - } - function isReadOnly($args) { return true; diff --git a/actions/tagrss.php b/actions/tagrss.php index 66eb77c271..0d4d68ffba 100644 --- a/actions/tagrss.php +++ b/actions/tagrss.php @@ -20,33 +20,25 @@ if (!defined('GNUSOCIAL')) { exit(1); } // Formatting of RSS handled by Rss10Action + class TagrssAction extends Rss10Action { - var $tag; + protected $tag; - function prepare($args) { - parent::prepare($args); + protected function doStreamPreparation() + { $tag = common_canonical_tag($this->trimmed('tag')); $this->tag = Notice_tag::getKV('tag', $tag); - if (!$this->tag) { + if (!$this->tag instanceof Notice_tag) { // TRANS: Client error when requesting a tag feed for a non-existing tag. $this->clientError(_('No such tag.')); - } else { - $this->notices = $this->getNotices($this->limit); - return true; } } - function getNotices($limit=0) + protected function getNotices() { - $tag = $this->tag; - - if (is_null($tag)) { - return null; - } - - $notice = Notice_tag::getStream($tag->tag)->getNotices(0, ($limit == 0) ? NOTICES_PER_PAGE : $limit); - return $notice->fetchAll(); + $stream = Notice_tag::getStream($this->tag->tag)->getNotices(0, $this->limit); + return $stream->fetchAll(); } function getChannel() diff --git a/actions/userrss.php b/actions/userrss.php index aae41ee504..fd49a0e899 100644 --- a/actions/userrss.php +++ b/actions/userrss.php @@ -21,94 +21,48 @@ if (!defined('GNUSOCIAL')) { exit(1); } // Formatting of RSS handled by Rss10Action -class UserrssAction extends Rss10Action +class UserrssAction extends TargetedRss10Action { - var $tag = null; + protected $tag = null; - protected function prepare(array $args=array()) + protected function doStreamPreparation() { - parent::prepare($args); - $nickname = $this->trimmed('nickname'); - $this->user = User::getKV('nickname', $nickname); $this->tag = $this->trimmed('tag'); + } - if (!$this->user) { - // TRANS: Client error displayed when user not found for an action. - $this->clientError(_('No such user.')); - } - + protected function getNotices() + { if (!empty($this->tag)) { - $this->notices = $this->getTaggedNotices(); - } else { - $this->notices = $this->getNotices(); + $stream = $this->target->getTaggedNotices($this->tag, 0, $this->limit); + return $stream->fetchAll(); } + // otherwise we fetch a normal user stream - return true; - } - - function getTaggedNotices() - { - $notice = $this->user->getTaggedNotices( - $this->tag, - 0, - ($this->limit == 0) ? NOTICES_PER_PAGE : $this->limit, - 0, - 0 - ); - - $notices = array(); - while ($notice->fetch()) { - $notices[] = clone($notice); - } - - return $notices; - } - - - function getNotices() - { - $notice = $this->user->getNotices( - 0, - ($this->limit == 0) ? NOTICES_PER_PAGE : $this->limit - ); - - $notices = array(); - while ($notice->fetch()) { - $notices[] = clone($notice); - } - - return $notices; + $stream = $this->target->getNotices(0, $this->limit); + return $stream->fetchAll(); } function getChannel() { - $user = $this->user; - $profile = $user->getProfile(); $c = array('url' => common_local_url('userrss', array('nickname' => - $user->nickname)), + $this->target->getNickname())), // TRANS: Message is used as link title. %s is a user nickname. - 'title' => sprintf(_('%s timeline'), $user->nickname), - 'link' => $profile->profileurl, + 'title' => sprintf(_('%s timeline'), $this->target->getNickname()), + 'link' => $this->target->getUrl(), // TRANS: Message is used as link description. %1$s is a username, %2$s is a site name. 'description' => sprintf(_('Updates from %1$s on %2$s!'), - $user->nickname, common_config('site', 'name'))); + $this->target->getNickname(), common_config('site', 'name'))); return $c; } - function getImage() - { - $profile = $this->user->getProfile(); - return $profile->avatarUrl(AVATAR_PROFILE_SIZE); - } - // override parent to add X-SUP-ID URL - function initRss($limit=0) + function initRss() { - $url = common_local_url('sup', null, null, $this->user->id); + $url = common_local_url('sup', null, null, $this->target->getID()); header('X-SUP-ID: '.$url); - parent::initRss($limit); + parent::initRss(); } function isReadOnly($args) diff --git a/classes/Local_group.php b/classes/Local_group.php index 9e95102d85..1cebd4c40c 100644 --- a/classes/Local_group.php +++ b/classes/Local_group.php @@ -50,7 +50,7 @@ class Local_group extends Managed_DataObject $group->find(true); if (!$group instanceof User_group) { common_log(LOG_ERR, 'User_group does not exist for Local_group: '.$this->group_id); - throw new NoResultException($group); + throw new NoSuchGroupException(array('id' => $this->group_id)); } return $group; } diff --git a/classes/Profile.php b/classes/Profile.php index b5ba00caa9..f78cd34e36 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -242,6 +242,11 @@ class Profile extends Managed_DataObject return null; } + function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) + { + return Reply::stream($this->getID(), $offset, $limit, $since_id, $before_id); + } + function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0) { $stream = new TaggedProfileNoticeStream($this, $tag); diff --git a/classes/Reply.php b/classes/Reply.php index 36686d0c76..d3405e6581 100644 --- a/classes/Reply.php +++ b/classes/Reply.php @@ -55,10 +55,9 @@ class Reply extends Managed_DataObject return $result; } - function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0) + static function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0) { $stream = new ReplyNoticeStream($user_id); - return $stream->getNotices($offset, $limit, $since_id, $max_id); } } diff --git a/lib/rss10action.php b/lib/rss10action.php index 8d73bec1d9..7b6eed9c96 100644 --- a/lib/rss10action.php +++ b/lib/rss10action.php @@ -50,7 +50,7 @@ class Rss10Action extends ManagedAction { $this->limit = $this->int('limit'); - if ($this->limit == 0) { + if (empty($this->limit)) { $this->limit = DEFAULT_RSS_LIMIT; } @@ -73,10 +73,19 @@ class Rss10Action extends ManagedAction common_log(LOG_WARNING, "Failed RSS auth attempt, nickname = $nickname, proxy = $proxy, ip = $ip."); $this->show_basic_auth_error(); - return; + // the above calls 'exit' } } } + + $this->doStreamPreparation(); + + $this->notices = $this->getNotices($this->limit); + } + + protected function doStreamPreparation() + { + // for example if we need to set $this->target or something } function show_basic_auth_error() @@ -98,7 +107,7 @@ class Rss10Action extends ManagedAction * @return array an array of Notice objects sorted in reverse chron */ - function getNotices() + protected function getNotices($limit=0) { return array(); } diff --git a/plugins/Bookmark/actions/bookmarksrss.php b/plugins/Bookmark/actions/bookmarksrss.php index 1f2baa17d4..955b782666 100644 --- a/plugins/Bookmark/actions/bookmarksrss.php +++ b/plugins/Bookmark/actions/bookmarksrss.php @@ -44,54 +44,12 @@ if (!defined('GNUSOCIAL')) { exit(1); } * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 * @link http://status.net/ */ -class BookmarksrssAction extends Rss10Action +class BookmarksrssAction extends TargetedRss10Action { - /** The user whose bookmarks to display */ - - var $user = null; - - /** - * Find the user to display by supplied nickname - * - * @param array $args Arguments from $_REQUEST - * - * @return boolean success - */ - function prepare($args) - { - parent::prepare($args); - - $nickname = $this->trimmed('nickname'); - $this->user = User::getKV('nickname', $nickname); - - if (!$this->user) { - // TRANS: Client error displayed when trying to get the RSS feed with bookmarks of a user that does not exist. - $this->clientError(_('No such user.')); - } else { - $this->notices = $this->getNotices($this->limit); - return true; - } - } - - /** - * Get notices - * - * @param integer $limit max number of notices to return - * - * @return array notices - */ - function getNotices($limit=0) + protected function getNotices() { - $user = $this->user; - - $notice = new BookmarksNoticeStream($this->user->id, true); - $notice = $notice->getNotices(0, NOTICES_PER_PAGE); - - $notices = array(); - while ($notice->fetch()) { - $notices[] = clone($notice); - } - return $notices; + $stream = new BookmarksNoticeStream($this->target->getID(), true); + return $stream->getNotices(0, $this->limit)->fetchAll(); } /** @@ -101,31 +59,19 @@ class BookmarksrssAction extends Rss10Action */ function getChannel() { - $user = $this->user; $c = array('url' => common_local_url('bookmarksrss', array('nickname' => - $user->nickname)), + $this->target->getNickname())), // TRANS: Title of RSS feed with bookmarks of a user. // TRANS: %s is a user's nickname. - 'title' => sprintf(_("%s's bookmarks"), $user->nickname), + 'title' => sprintf(_("%s's bookmarks"), $this->target->getNickname()), 'link' => common_local_url('bookmarks', array('nickname' => - $user->nickname)), + $this->target->getNickname())), // TRANS: Desciption of RSS feed with bookmarks of a user. // TRANS: %1$s is a user's nickname, %2$s is the name of the StatusNet site. 'description' => sprintf(_('Bookmarks posted by %1$s on %2$s!'), - $user->nickname, common_config('site', 'name'))); + $this->target->getNickname(), common_config('site', 'name'))); return $c; } - - /** - * Get image. - * - * @return void - */ - function getImage() - { - return null; - } - } diff --git a/plugins/Favorite/actions/favoritesrss.php b/plugins/Favorite/actions/favoritesrss.php index b532e0bf31..ca5602d5ec 100644 --- a/plugins/Favorite/actions/favoritesrss.php +++ b/plugins/Favorite/actions/favoritesrss.php @@ -43,50 +43,15 @@ if (!defined('GNUSOCIAL')) { exit(1); } * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 * @link http://status.net/ */ -class FavoritesrssAction extends Rss10Action +class FavoritesrssAction extends TargetedRss10Action { - /** The user whose favorites to display */ - - var $user = null; - - /** - * Find the user to display by supplied nickname - * - * @param array $args Arguments from $_REQUEST - * - * @return boolean success - */ - function prepare($args) + protected function getNotices() { - parent::prepare($args); + // is this our own stream? + $own = $this->scoped instanceof Profile ? $this->target->getID() === $this->scoped->getID() : false; - $nickname = $this->trimmed('nickname'); - $this->user = User::getKV('nickname', $nickname); - - if (!$this->user) { - // TRANS: Client error displayed when trying to get the RSS feed with favorites of a user that does not exist. - $this->clientError(_('No such user.')); - } else { - $this->notices = $this->getNotices($this->limit); - return true; - } - } - - /** - * Get notices - * - * @param integer $limit max number of notices to return - * - * @return array notices - */ - function getNotices($limit=0) - { - $notice = Fave::stream($this->user->id, 0, $limit, $false); - $notices = array(); - while ($notice->fetch()) { - $notices[] = clone($notice); - } - return $notices; + $stream = Fave::stream($this->target->getID(), 0, $this->limit, $own); + return $stream->fetchAll(); } /** @@ -112,15 +77,4 @@ class FavoritesrssAction extends Rss10Action $user->nickname, common_config('site', 'name'))); return $c; } - - /** - * Get image. - * - * @return void - */ - function getImage() - { - return null; - } - }