Bookmark plugin enhancement: display Bookmark's list. Integration of @chimo's work (http://http://sn.chromic.org/) from https://github.com/chimo/BookmarkList into official plugin.

This commit is contained in:
Jean Baptiste Favre 2012-09-14 17:37:42 +02:00
parent 7a5bd495c5
commit d36f443666
5 changed files with 780 additions and 2 deletions

View File

@ -148,6 +148,9 @@ class BookmarkPlugin extends MicroAppPlugin
switch ($cls) switch ($cls)
{ {
case 'BookmarksAction':
case 'BookmarksrssAction':
case 'ApiTimelineBookmarksAction':
case 'ShowbookmarkAction': case 'ShowbookmarkAction':
case 'NewbookmarkAction': case 'NewbookmarkAction':
case 'BookmarkpopupAction': case 'BookmarkpopupAction':
@ -180,6 +183,26 @@ class BookmarkPlugin extends MicroAppPlugin
*/ */
function onRouterInitialized($m) function onRouterInitialized($m)
{ {
if (common_config('singleuser', 'enabled')) {
$nickname = User::singleUserNickname();
$m->connect('bookmarks',
array('action' => 'bookmarks', 'nickname' => $nickname));
$m->connect('bookmarks/rss',
array('action' => 'bookmarksrss', 'nickname' => $nickname));
} else {
$m->connect(':nickname/bookmarks',
array('action' => 'bookmarks'),
array('nickname' => Nickname::DISPLAY_FMT));
$m->connect(':nickname/bookmarks/rss',
array('action' => 'bookmarksrss'),
array('nickname' => Nickname::DISPLAY_FMT));
}
$m->connect('api/bookmarks/:id.:format',
array('action' => 'ApiTimelineBookmarks',
'id' => Nickname::INPUT_FMT,
'format' => '(xml|json|rss|atom|as)'));
$m->connect('main/bookmark/new', $m->connect('main/bookmark/new',
array('action' => 'newbookmark'), array('action' => 'newbookmark'),
array('id' => '[0-9]+')); array('id' => '[0-9]+'));
@ -230,11 +253,13 @@ class BookmarkPlugin extends MicroAppPlugin
{ {
$versions[] = array('name' => 'Bookmark', $versions[] = array('name' => 'Bookmark',
'version' => self::VERSION, 'version' => self::VERSION,
'author' => 'Evan Prodromou', 'author' => 'Evan Prodromou, Stephane Berube, Jean Baptiste Favre',
'homepage' => 'http://status.net/wiki/Plugin:Bookmark', 'homepage' => 'http://status.net/wiki/Plugin:Bookmark',
'description' => 'description' =>
// TRANS: Plugin description. // TRANS: Plugin description.
_m('Simple extension for supporting bookmarks.')); _m('Simple extension for supporting bookmarks. ') .
'BookmarkList feature has been developped by Stephane Berube. ' .
'Integration has been done by Jean Baptiste Favre.');
return true; return true;
} }
@ -315,6 +340,41 @@ class BookmarkPlugin extends MicroAppPlugin
return false; return false;
} }
/**
* Modify the default menu to link to our custom action
*
* Using event handlers, it's possible to modify the default UI for pages
* almost without limit. In this method, we add a menu item to the default
* primary menu for the interface to link to our action.
*
* The Action class provides a rich set of events to hook, as well as output
* methods.
*
* @param Action $action The current action handler. Use this to
* do any output.
*
* @return boolean hook value; true means continue processing, false means stop.
*
* @see Action
*/
function onEndPersonalGroupNav($action)
{
$this->user = common_current_user();
if (!$this->user) {
// TRANS: Client error displayed when trying to display bookmarks for a non-existing user.
$this->clientError(_('No such user.'));
return false;
}
$action->menuItem(common_local_url('bookmarks', array('nickname' => $this->user->nickname)),
// TRANS: Menu item in sample plugin.
_m('Bookmarks'),
// TRANS: Menu item title in sample plugin.
_m('A list of your bookmarks'), false, 'nav_timeline_bookmarks');
return true;
}
/** /**
* Save a remote bookmark (from Salmon or PuSH) * Save a remote bookmark (from Salmon or PuSH)
* *

View File

@ -0,0 +1,268 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Show a user's favorite notices
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category API
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @copyright 2009-2010 StatusNet, Inc.
* @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
require_once INSTALLDIR.'/lib/apibareauth.php';
require_once 'bookmarksnoticestream.php';
/**
* Returns the 20 most recent favorite notices for the authenticating user or user
* specified by the ID parameter in the requested format.
*
* @category API
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @author Evan Prodromou <evan@status.net>
* @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class ApiTimelineBookmarksAction extends ApiBareAuthAction
{
var $notices = null;
/**
* Take arguments for running
*
* @param array $args $_REQUEST args
*
* @return boolean success flag
*/
function prepare($args)
{
parent::prepare($args);
$this->user = $this->getTargetUser($this->arg('id'));
if (empty($this->user)) {
// TRANS: Client error displayed when requesting most recent favourite notices by a user for a non-existing user.
$this->clientError(_('No such user.'), 404, $this->format);
return;
}
$this->notices = $this->getNotices();
return true;
}
/**
* Handle the request
*
* Just show the notices
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
{
parent::handle($args);
$this->showTimeline();
}
/**
* Show the timeline of notices
*
* @return void
*/
function showTimeline()
{
$profile = $this->user->getProfile();
$avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
$sitename = common_config('site', 'name');
$title = sprintf(
// TRANS: Title for timeline of most recent favourite notices by a user.
// TRANS: %1$s is the StatusNet sitename, %2$s is a user nickname.
_('%1$s / Bookmarks from %2$s'),
$sitename,
$this->user->nickname
);
$taguribase = TagURI::base();
$id = "tag:$taguribase:Bookmarks:" . $this->user->id;
$subtitle = sprintf(
// TRANS: Subtitle for timeline of most recent favourite notices by a user.
// TRANS: %1$s is the StatusNet sitename, %2$s is a user's full name,
// TRANS: %3$s is a user nickname.
_('%1$s updates bookmarked by %2$s / %3$s.'),
$sitename,
$profile->getBestName(),
$this->user->nickname
);
$logo = !empty($avatar)
? $avatar->displayUrl()
: Avatar::defaultImage(AVATAR_PROFILE_SIZE);
$link = common_local_url(
'bookmarks',
array('nickname' => $this->user->nickname)
);
$self = $this->getSelfUri();
switch($this->format) {
case 'xml':
$this->showXmlTimeline($this->notices);
break;
case 'rss':
$this->showRssTimeline(
$this->notices,
$title,
$link,
$subtitle,
null,
$logo,
$self
);
break;
case 'atom':
header('Content-Type: application/atom+xml; charset=utf-8');
$atom = new AtomNoticeFeed($this->auth_user);
$atom->setId($id);
$atom->setTitle($title);
$atom->setSubtitle($subtitle);
$atom->setLogo($logo);
$atom->setUpdated('now');
$atom->addLink($link);
$atom->setSelfLink($self);
$atom->addEntryFromNotices($this->notices);
$this->raw($atom->getString());
break;
case 'json':
$this->showJsonTimeline($this->notices);
break;
case 'as':
header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
$doc = new ActivityStreamJSONDocument($this->auth_user);
$doc->setTitle($title);
$doc->addLink($link,'alternate', 'text/html');
$doc->addItemsFromNotices($this->notices);
$this->raw($doc->asString());
break;
default:
// TRANS: Client error displayed when coming across a non-supported API method.
$this->clientError(_('API method not found.'), $code = 404);
break;
}
}
/**
* Get notices
*
* @return array notices
*/
function getNotices()
{
$notices = array();
common_debug("since id = " . $this->since_id . " max id = " . $this->max_id);
$notice = new BookmarksNoticeStream($this->user->id, true);
$notice = $notice->getNotices(
($this->page-1) * $this->count,
$this->count,
$this->since_id,
$this->max_id
);
while ($notice->fetch()) {
$notices[] = clone($notice);
}
return $notices;
}
/**
* Is this action read only?
*
* @param array $args other arguments
*
* @return boolean true
*/
function isReadOnly($args)
{
return true;
}
/**
* When was this feed last modified?
*
* @return string datestamp of the latest notice in the stream
*/
function lastModified()
{
if (!empty($this->notices) && (count($this->notices) > 0)) {
return strtotime($this->notices[0]->created);
}
return null;
}
/**
* An entity tag for this stream
*
* Returns an Etag based on the action name, language, user ID, and
* timestamps of the first and last notice in the timeline
*
* @return string etag
*/
function etag()
{
if (!empty($this->notices) && (count($this->notices) > 0)) {
$last = count($this->notices) - 1;
return '"' . implode(
':',
array($this->arg('action'),
common_user_cache_hash($this->auth_user),
common_language(),
$this->user->id,
strtotime($this->notices[0]->created),
strtotime($this->notices[$last]->created))
)
. '"';
}
return null;
}
}

View File

@ -0,0 +1,233 @@
<?php
/**
* Give a warm greeting to our friendly user
*
* PHP version 5
*
* @category Bookmark
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2009, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET')) {
exit(1);
}
require_once 'bookmarksnoticestream.php';
/**
* List currently logged-in user's bookmakrs
*
* @category Bookmark
* @package StatusNet
* @author Stephane Berube <chimo@chromic.org>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link https://github.com/chimo/BookmarkList
*/
class BookmarksAction extends Action
{
var $user = null;
var $gc = null;
/**
* Take arguments for running
*
* This method is called first, and it lets the action class get
* all its arguments and validate them. It's also the time
* to fetch any relevant data from the database.
*
* Action classes should run parent::prepare($args) as the first
* line of this method to make sure the default argument-processing
* happens.
*
* @param array $args $_REQUEST args
*
* @return boolean success flag
*/
function prepare($args)
{
parent::prepare($args);
if (common_config('singleuser', 'enabled')) {
$nickname = User::singleUserNickname();
} else {
// PHP 5.4
// $nickname = $this->returnToArgs()[1]['nickname'];
// PHP < 5.4
$nickname = $this->returnToArgs();
$nickname = $nickname[1]['nickname'];
}
$this->user = User::staticGet('nickname', $nickname);
if (!$this->user) {
// TRANS: Client error displayed when trying to display bookmarks for a non-existing user.
$this->clientError(_('No such user.'));
return false;
}
$this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
$stream = new BookmarksNoticeStream($this->user->id, true);
$this->notices = $stream->getNotices(($this->page-1)*NOTICES_PER_PAGE,
NOTICES_PER_PAGE + 1);
if($this->page > 1 && $this->notices->N == 0) {
throw new ClientException(_('No such page.'), 404);
}
return true;
}
/**
* Handle request
*
* This is the main method for handling a request. Note that
* most preparation should be done in the prepare() method;
* by the time handle() is called the action should be
* more or less ready to go.
*
* @param array $args $_REQUEST args; handled in prepare()
*
* @return void
*/
function handle($args)
{
parent::handle($args);
$this->showPage();
}
/**
* Title of this page
*
* Override this method to show a custom title.
*
* @return string Title of the page
*/
function title()
{
if (empty($this->user)) {
// TRANS: Page title for sample plugin.
return _m('Log in');
} else {
// TRANS: Page title for sample plugin. %s is a user nickname.
return sprintf(_m('%s\'s bookmarks'), $this->user->nickname);
}
}
/**
* Feeds for the <head> section
*
* @return array Feed objects to show
*/
function getFeeds()
{
return array(new Feed(Feed::JSON,
common_local_url('ApiTimelineBookmarks',
array(
'id' => $this->user->nickname,
'format' => 'as')),
// TRANS: Feed link text. %s is a username.
sprintf(_('Feed for favorites of %s (Activity Streams JSON)'),
$this->user->nickname)),
new Feed(Feed::RSS1,
common_local_url('bookmarksrss',
array('nickname' => $this->user->nickname)),
// TRANS: Feed link text. %s is a username.
sprintf(_('Feed for favorites of %s (RSS 1.0)'),
$this->user->nickname)),
new Feed(Feed::RSS2,
common_local_url('ApiTimelineBookmarks',
array(
'id' => $this->user->nickname,
'format' => 'rss')),
// TRANS: Feed link text. %s is a username.
sprintf(_('Feed for favorites of %s (RSS 2.0)'),
$this->user->nickname)),
new Feed(Feed::ATOM,
common_local_url('ApiTimelineBookmarks',
array(
'id' => $this->user->nickname,
'format' => 'atom')),
// TRANS: Feed link text. %s is a username.
sprintf(_('Feed for favorites of %s (Atom)'),
$this->user->nickname)));
}
/**
* Show content in the content area
*
* The default StatusNet page has a lot of decorations: menus,
* logos, tabs, all that jazz. This method is used to show
* content in the content area of the page; it's the main
* thing you want to overload.
*
* This method also demonstrates use of a plural localized string.
*
* @return void
*/
function showContent()
{
$nl = new NoticeList($this->notices, $this);
$cnt = $nl->show();
if ($cnt == 0) {
$this->showEmptyList();
}
$this->pagination($this->page > 1,
$cnt > NOTICES_PER_PAGE,
$this->page, 'bookmarks',
array('nickname' => $this->user->nickname));
}
function showEmptyList() {
$message = sprintf(_('This is %1$s\'s bookmark stream, but %1$s hasn\'t bookmarked anything yet.'), $this->user->nickname) . ' ';
$this->elementStart('div', 'guide');
$this->raw(common_markup_to_html($message));
$this->elementEnd('div');
}
/**
* Return true if read only.
*
* Some actions only read from the database; others read and write.
* The simple database load-balancer built into StatusNet will
* direct read-only actions to database mirrors (if they are configured),
* and read-write actions to the master database.
*
* This defaults to false to avoid data integrity issues, but you
* should make sure to overload it for performance gains.
*
* @param array $args other arguments, if RO/RW status depends on them.
*
* @return boolean is read only action?
*/
function isReadOnly($args)
{
return true;
}
}

View File

@ -0,0 +1,80 @@
<?php
class RawBookmarksNoticeStream extends NoticeStream
{
protected $user_id;
protected $own;
function __construct($user_id, $own)
{
$this->user_id = $user_id;
$this->own = $own;
}
function getNoticeIds($offset, $limit, $since_id, $max_id)
{
$notice = new Notice();
$qry = null;
$qry = 'SELECT notice.* FROM notice ';
$qry .= 'INNER JOIN bookmark ON bookmark.uri = notice.uri ';
$qry .= 'WHERE bookmark.profile_id = ' . $this->user_id . ' ';
$qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
if ($since_id != 0) {
$qry .= 'AND notice.id > ' . $since_id . ' ';
}
if ($max_id != 0) {
$qry .= 'AND notice.id <= ' . $max_id . ' ';
}
// NOTE: we sort by bookmark time, not by notice time!
$qry .= 'ORDER BY created DESC ';
if (!is_null($offset)) {
$qry .= "LIMIT $limit OFFSET $offset";
}
$notice->query($qry);
$ids = array();
while ($notice->fetch()) {
$ids[] = $notice->id;
}
$notice->free();
unset($notice);
return $ids;
}
}
/**
* Notice stream for bookmarks
*
* @category Stream
* @package StatusNet
* @author Stephane Berube <chimo@chromic.org>
* @copyright 2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
class BookmarksNoticeStream extends ScopingNoticeStream
{
function __construct($user_id, $own, $profile = -1)
{
$stream = new RawBookmarksNoticeStream($user_id, $own);
if ($own) {
$key = 'bookmark:ids_by_user_own:'.$user_id;
} else {
$key = 'bookmark:ids_by_user:'.$user_id;
}
if (is_int($profile) && $profile == -1) {
$profile = Profile::current();
}
parent::__construct(new CachingNoticeStream($stream, $key),
$profile);
}
}

View File

@ -0,0 +1,137 @@
<?php
/**
* RSS feed for user bookmarks action class.
*
* PHP version 5
*
* @category Action
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Robin Millette <millette@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2008, 2009, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
require_once INSTALLDIR.'/lib/rssaction.php';
require_once 'bookmarksnoticestream.php';
/**
* RSS feed for user bookmarks action class.
*
* Formatting of RSS handled by Rss10Action
*
* @category Action
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Robin Millette <millette@status.net>
* @author Zach Copley <zach@status.net>
* @author Stephane Berube <chimo@chromic.org> (modified 'favoritesrss.php' to show bookmarks instead)
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*/
class BookmarksrssAction extends Rss10Action
{
/** 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::staticGet('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.'));
return false;
} 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)
{
$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;
}
/**
* Get channel.
*
* @return array associative array on channel information
*/
function getChannel()
{
$user = $this->user;
$c = array('url' => common_local_url('bookmarksrss',
array('nickname' =>
$user->nickname)),
// TRANS: Title of RSS feed with bookmarks of a user.
// TRANS: %s is a user's nickname.
'title' => sprintf(_("%s's bookmarks"), $user->nickname),
'link' => common_local_url('bookmarks',
array('nickname' =>
$user->nickname)),
// 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')));
return $c;
}
/**
* Get image.
*
* @return void
*/
function getImage()
{
return null;
}
}