*
* @see DB_DataObject
*/
-
-class Bookmark extends Memcached_DataObject
+class Bookmark extends Managed_DataObject
{
public $__table = 'bookmark'; // table name
public $id; // char(36) primary_key not_null
public $uri; // varchar(255)
public $created; // datetime
- /**
- * Get an instance by key
- *
- * This is a utility method to get a single instance with a given key value.
- *
- * @param string $k Key to use to lookup (usually 'user_id' for this class)
- * @param mixed $v Value to lookup
- *
- * @return User_greeting_count object found, or null for no hits
- *
- */
-
- function staticGet($k, $v=null)
- {
- return Memcached_DataObject::staticGet('Bookmark', $k, $v);
- }
-
- /**
- * Get an instance by compound key
- *
- * This is a utility method to get a single instance with a given set of
- * key-value pairs. Usually used for the primary key for a compound key; thus
- * the name.
- *
- * @param array $kv array of key-value mappings
- *
- * @return Bookmark object found, or null for no hits
- *
- */
-
- function pkeyGet($kv)
- {
- return Memcached_DataObject::pkeyGet('Bookmark', $kv);
- }
-
- /**
- * return table definition for DB_DataObject
- *
- * DB_DataObject needs to know something about the table to manipulate
- * instances. This method provides all the DB_DataObject needs to know.
- *
- * @return array array of column definitions
- */
-
- function table()
- {
- return array('id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
- 'profile_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
- 'url' => DB_DATAOBJECT_STR,
- 'title' => DB_DATAOBJECT_STR,
- 'description' => DB_DATAOBJECT_STR,
- 'uri' => DB_DATAOBJECT_STR,
- 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE +
- DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
- }
-
- /**
- * return key definitions for DB_DataObject
- *
- * @return array list of key field names
- */
-
- function keys()
- {
- return array_keys($this->keyTypes());
- }
-
- /**
- * return key definitions for Memcached_DataObject
- *
- * @return array associative array of key definitions
- */
-
- function keyTypes()
- {
- return array('id' => 'K',
- 'uri' => 'U');
- }
-
- /**
- * Magic formula for non-autoincrementing integer primary keys
- *
- * @return array magic three-false array that stops auto-incrementing.
- */
-
- function sequenceKey()
+ public static function schemaDef()
{
- return array(false, false, false);
+ return array(
+ 'fields' => array(
+ 'id' => array('type' => 'char',
+ 'length' => 36,
+ 'not null' => true),
+ 'profile_id' => array('type' => 'int', 'not null' => true),
+ 'uri' => array('type' => 'varchar',
+ 'length' => 255,
+ 'not null' => true),
+ 'url' => array('type' => 'varchar',
+ 'length' => 255,
+ 'not null' => true),
+ 'title' => array('type' => 'varchar', 'length' => 255),
+ 'description' => array('type' => 'text'),
+ 'created' => array('type' => 'datetime', 'not null' => true),
+ ),
+ 'primary key' => array('id'),
+ 'unique keys' => array(
+ 'bookmark_uri_key' => array('uri'),
+ ),
+ 'foreign keys' => array(
+ 'bookmark_profile_id_fkey' => array('profile', array('profile_id' => 'id'))
+ ),
+ 'indexes' => array('bookmark_created_idx' => array('created'),
+ 'bookmark_url_idx' => array('url'),
+ 'bookmark_profile_id_idx' => array('profile_id'),
+ ),
+ );
}
/**
* Get a bookmark based on a notice
- *
+ *
* @param Notice $notice Notice to check for
*
* @return Bookmark found bookmark or null
*/
-
- function getByNotice($notice)
+ static function getByNotice($notice)
{
- return self::staticGet('uri', $notice->uri);
+ return self::getKV('uri', $notice->uri);
}
/**
*
* @return Bookmark bookmark found or null
*/
-
static function getByURL($profile, $url)
{
$nb = new Bookmark();
-
+
$nb->profile_id = $profile->id;
$nb->url = $url;
*
* @return Notice saved notice
*/
-
static function saveNew($profile, $title, $url, $rawtags, $description,
$options=null)
{
$nb = self::getByURL($profile, $url);
if (!empty($nb)) {
- throw new ClientException(_('Bookmark already exists.'));
+ // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
+ throw new ClientException(_m('Bookmark already exists.'));
}
if (empty($options)) {
}
if (array_key_exists('uri', $options)) {
- $other = Bookmark::staticGet('uri', $options['uri']);
+ $other = Bookmark::getKV('uri', $options['uri']);
if (!empty($other)) {
- throw new ClientException(_('Bookmark already exists.'));
+ // TRANS: Client exception thrown when trying to save a new bookmark that already exists.
+ throw new ClientException(_m('Bookmark already exists.'));
}
}
if (array_key_exists('uri', $options)) {
$nb->uri = $options['uri'];
} else {
- $nb->uri = common_local_url('showbookmark',
- array('id' => $nb->id));
+ // FIXME: hacks to work around router bugs in
+ // queue daemons
+
+ $r = Router::get();
+
+ $path = $r->build('showbookmark',
+ array('id' => $nb->id));
+
+ if (empty($path)) {
+ $nb->uri = common_path('bookmark/'.$nb->id, false, false);
+ } else {
+ $nb->uri = common_local_url('showbookmark',
+ array('id' => $nb->id),
+ null,
+ null,
+ false);
+ }
}
$nb->insert();
// Use user's preferences for short URLs, if possible
try {
- $user = User::staticGet('id', $profile->id);
+ $user = User::getKV('id', $profile->id);
- $shortUrl = File_redirection::makeShort($url,
+ $shortUrl = File_redirection::makeShort($url,
empty($user) ? null : $user);
} catch (Exception $e) {
// Don't let this stop us.
$shortUrl = $url;
}
- $content = sprintf(_('"%s" %s %s %s'),
+ // TRANS: Bookmark content.
+ // TRANS: %1$s is a title, %2$s is a short URL, %3$s is the bookmark description,
+ // TRANS: %4$s is space separated list of hash tags.
+ $content = sprintf(_m('"%1$s" %2$s %3$s %4$s'),
$title,
$shortUrl,
$description,
implode(' ', $hashtags));
- $rendered = sprintf(_('<span class="xfolkentry">'.
- '<a class="taggedlink" href="%s">%s</a> '.
- '<span class="description">%s</span> '.
- '<span class="meta">%s</span>'.
+ // TRANS: Rendered bookmark content.
+ // TRANS: %1$s is a URL, %2$s the bookmark title, %3$s is the bookmark description,
+ // TRANS: %4$s is space separated list of hash tags.
+ $rendered = sprintf(_m('<span class="xfolkentry">'.
+ '<a class="taggedlink" href="%1$s">%2$s</a> '.
+ '<span class="description">%3$s</span> '.
+ '<span class="meta">%4$s</span>'.
'</span>'),
htmlspecialchars($url),
htmlspecialchars($title),
$options = array_merge(array('urls' => array($url),
'rendered' => $rendered,
'tags' => $tags,
- 'replies' => $replies),
+ 'replies' => $replies,
+ 'object_type' => ActivityObject::BOOKMARK),
$options);
if (!array_key_exists('uri', $options)) {
$options['uri'] = $nb->uri;
}
- $saved = Notice::saveNew($profile->id,
- $content,
- array_key_exists('source', $options) ?
- $options['source'] : 'web',
- $options);
+ try {
+ $saved = Notice::saveNew($profile->id,
+ $content,
+ array_key_exists('source', $options) ?
+ $options['source'] : 'web',
+ $options);
+ } catch (Exception $e) {
+ $nb->delete();
+ throw $e;
+ }
+
+ if (empty($saved)) {
+ $nb->delete();
+ }
return $saved;
}