summaryrefslogtreecommitdiff
path: root/classes
diff options
context:
space:
mode:
Diffstat (limited to 'classes')
-rwxr-xr-xclasses/Conversation.php3
-rw-r--r--classes/Fave.php14
-rw-r--r--classes/File.php69
-rw-r--r--classes/File_oembed.php6
-rw-r--r--classes/File_redirection.php111
-rw-r--r--classes/Foreign_link.php17
-rw-r--r--classes/Foreign_user.php1
-rw-r--r--classes/Group_alias.php2
-rw-r--r--classes/Inbox.php48
-rw-r--r--classes/Memcached_DataObject.php105
-rw-r--r--classes/Notice.php272
-rw-r--r--classes/Profile.php152
-rw-r--r--classes/Profile_role.php18
-rw-r--r--classes/Queue_item.php13
-rw-r--r--classes/Reply.php14
-rw-r--r--classes/Safe_DataObject.php43
-rw-r--r--classes/Status_network.php58
-rw-r--r--classes/Subscription.php72
-rw-r--r--classes/User.php108
-rw-r--r--classes/User_group.php53
-rw-r--r--classes/User_username.php2
21 files changed, 950 insertions, 231 deletions
diff --git a/classes/Conversation.php b/classes/Conversation.php
index ea8bd87b5..f540004ef 100755
--- a/classes/Conversation.php
+++ b/classes/Conversation.php
@@ -63,7 +63,8 @@ class Conversation extends Memcached_DataObject
}
$orig = clone($conv);
- $orig->uri = common_local_url('conversation', array('id' => $id));
+ $orig->uri = common_local_url('conversation', array('id' => $id),
+ null, null, false);
$result = $orig->update($conv);
if (empty($result)) {
diff --git a/classes/Fave.php b/classes/Fave.php
index a04f15e9c..ed4f56aee 100644
--- a/classes/Fave.php
+++ b/classes/Fave.php
@@ -21,7 +21,15 @@ class Fave extends Memcached_DataObject
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
- static function addNew($profile, $notice) {
+ /**
+ * Save a favorite record.
+ * @fixme post-author notification should be moved here
+ *
+ * @param Profile $profile the local or remote user who likes
+ * @param Notice $notice the notice that is liked
+ * @return mixed false on failure, or Fave record on success
+ */
+ static function addNew(Profile $profile, Notice $notice) {
$fave = null;
@@ -67,13 +75,13 @@ class Fave extends Memcached_DataObject
return Memcached_DataObject::pkeyGet('Fave', $kv);
}
- function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false)
+ function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0)
{
$ids = Notice::stream(array('Fave', '_streamDirect'),
array($user_id, $own),
($own) ? 'fave:ids_by_user_own:'.$user_id :
'fave:ids_by_user:'.$user_id,
- $offset, $limit);
+ $offset, $limit, $since_id, $max_id);
return $ids;
}
diff --git a/classes/File.php b/classes/File.php
index 79a7d6681..0f230a6ee 100644
--- a/classes/File.php
+++ b/classes/File.php
@@ -67,7 +67,14 @@ class File extends Memcached_DataObject
return $att;
}
- function saveNew($redir_data, $given_url) {
+ /**
+ * Save a new file record.
+ *
+ * @param array $redir_data lookup data eg from File_redirection::where()
+ * @param string $given_url
+ * @return File
+ */
+ function saveNew(array $redir_data, $given_url) {
$x = new File;
$x->url = $given_url;
if (!empty($redir_data['protected'])) $x->protected = $redir_data['protected'];
@@ -77,22 +84,43 @@ class File extends Memcached_DataObject
if (isset($redir_data['time']) && $redir_data['time'] > 0) $x->date = intval($redir_data['time']);
$file_id = $x->insert();
+ $x->saveOembed($redir_data, $given_url);
+ return $x;
+ }
+
+ /**
+ * Save embedding information for this file, if applicable.
+ *
+ * Normally this won't need to be called manually, as File::saveNew()
+ * takes care of it.
+ *
+ * @param array $redir_data lookup data eg from File_redirection::where()
+ * @param string $given_url
+ * @return boolean success
+ */
+ public function saveOembed($redir_data, $given_url)
+ {
if (isset($redir_data['type'])
&& (('text/html' === substr($redir_data['type'], 0, 9) || 'application/xhtml+xml' === substr($redir_data['type'], 0, 21)))
&& ($oembed_data = File_oembed::_getOembed($given_url))) {
- $fo = File_oembed::staticGet('file_id', $file_id);
+ $fo = File_oembed::staticGet('file_id', $this->id);
if (empty($fo)) {
- File_oembed::saveNew($oembed_data, $file_id);
+ File_oembed::saveNew($oembed_data, $this->id);
+ return true;
} else {
common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file $file_id", __FILE__);
}
}
- return $x;
+ return false;
}
- function processNew($given_url, $notice_id=null) {
+ /**
+ * @fixme refactor this mess, it's gotten pretty scary.
+ * @param bool $followRedirects
+ */
+ function processNew($given_url, $notice_id=null, $followRedirects=true) {
if (empty($given_url)) return -1; // error, no url to process
$given_url = File_redirection::_canonUrl($given_url);
if (empty($given_url)) return -1; // error, no url to process
@@ -100,20 +128,33 @@ class File extends Memcached_DataObject
if (empty($file)) {
$file_redir = File_redirection::staticGet('url', $given_url);
if (empty($file_redir)) {
+ // @fixme for new URLs this also looks up non-redirect data
+ // such as target content type, size, etc, which we need
+ // for File::saveNew(); so we call it even if not following
+ // new redirects.
$redir_data = File_redirection::where($given_url);
if (is_array($redir_data)) {
$redir_url = $redir_data['url'];
} elseif (is_string($redir_data)) {
$redir_url = $redir_data;
+ $redir_data = array();
} else {
throw new ServerException("Can't process url '$given_url'");
}
// TODO: max field length
- if ($redir_url === $given_url || strlen($redir_url) > 255) {
+ if ($redir_url === $given_url || strlen($redir_url) > 255 || !$followRedirects) {
$x = File::saveNew($redir_data, $given_url);
$file_id = $x->id;
} else {
- $x = File::processNew($redir_url, $notice_id);
+ // This seems kind of messed up... for now skipping this part
+ // if we're already under a redirect, so we don't go into
+ // horrible infinite loops if we've been given an unstable
+ // redirect (where the final destination of the first request
+ // doesn't match what we get when we ask for it again).
+ //
+ // Seen in the wild with clojure.org, which redirects through
+ // wikispaces for auth and appends session data in the URL params.
+ $x = File::processNew($redir_url, $notice_id, /*followRedirects*/false);
$file_id = $x->id;
File_redirection::saveNew($redir_data, $file_id, $given_url);
}
@@ -260,8 +301,11 @@ class File extends Memcached_DataObject
$enclosure->mimetype=$this->mimetype;
if(! isset($this->filename)){
- $notEnclosureMimeTypes = array('text/html','application/xhtml+xml');
- $mimetype = strtolower($this->mimetype);
+ $notEnclosureMimeTypes = array(null,'text/html','application/xhtml+xml');
+ $mimetype = $this->mimetype;
+ if($mimetype != null){
+ $mimetype = strtolower($this->mimetype);
+ }
$semicolon = strpos($mimetype,';');
if($semicolon){
$mimetype = substr($mimetype,0,$semicolon);
@@ -290,5 +334,12 @@ class File extends Memcached_DataObject
}
return $enclosure;
}
+
+ // quick back-compat hack, since there's still code using this
+ function isEnclosure()
+ {
+ $enclosure = $this->getEnclosure();
+ return !empty($enclosure);
+ }
}
diff --git a/classes/File_oembed.php b/classes/File_oembed.php
index 11f160718..041b44740 100644
--- a/classes/File_oembed.php
+++ b/classes/File_oembed.php
@@ -81,6 +81,12 @@ class File_oembed extends Memcached_DataObject
}
}
+ /**
+ * Save embedding info for a new file.
+ *
+ * @param object $data Services_oEmbed_Object_*
+ * @param int $file_id
+ */
function saveNew($data, $file_id) {
$file_oembed = new File_oembed;
$file_oembed->file_id = $file_id;
diff --git a/classes/File_redirection.php b/classes/File_redirection.php
index 08a6e8d8b..f128b3e07 100644
--- a/classes/File_redirection.php
+++ b/classes/File_redirection.php
@@ -58,24 +58,30 @@ class File_redirection extends Memcached_DataObject
return $request;
}
- function _redirectWhere_imp($short_url, $redirs = 10, $protected = false) {
+ /**
+ * Check if this URL is a redirect and return redir info.
+ *
+ * Most code should call File_redirection::where instead, to check if we
+ * already know that redirection and avoid extra hits to the web.
+ *
+ * The URL is hit and any redirects are followed, up to 10 levels or until
+ * a protected URL is reached.
+ *
+ * @param string $in_url
+ * @return mixed one of:
+ * string - target URL, if this is a direct link or can't be followed
+ * array - redirect info if this is an *unknown* redirect:
+ * associative array with the following elements:
+ * code: HTTP status code
+ * redirects: count of redirects followed
+ * url: URL string of final target
+ * type (optional): MIME type from Content-Type header
+ * size (optional): byte size from Content-Length header
+ * time (optional): timestamp from Last-Modified header
+ */
+ public function lookupWhere($short_url, $redirs = 10, $protected = false) {
if ($redirs < 0) return false;
- // let's see if we know this...
- $a = File::staticGet('url', $short_url);
-
- if (!empty($a)) {
- // this is a direct link to $a->url
- return $a->url;
- } else {
- $b = File_redirection::staticGet('url', $short_url);
- if (!empty($b)) {
- // this is a redirect to $b->file_id
- $a = File::staticGet('id', $b->file_id);
- return $a->url;
- }
- }
-
if(strpos($short_url,'://') === false){
return $short_url;
}
@@ -93,12 +99,13 @@ class File_redirection extends Memcached_DataObject
}
} catch (Exception $e) {
// Invalid URL or failure to reach server
+ common_log(LOG_ERR, "Error while following redirects for $short_url: " . $e->getMessage());
return $short_url;
}
if ($response->getRedirectCount() && File::isProtected($response->getUrl())) {
// Bump back up the redirect chain until we find a non-protected URL
- return self::_redirectWhere_imp($short_url, $response->getRedirectCount() - 1, true);
+ return self::lookupWhere($short_url, $response->getRedirectCount() - 1, true);
}
$ret = array('code' => $response->getStatus()
@@ -115,11 +122,60 @@ class File_redirection extends Memcached_DataObject
return $ret;
}
- function where($in_url) {
- $ret = File_redirection::_redirectWhere_imp($in_url);
+ /**
+ * Check if this URL is a redirect and return redir info.
+ * If a File record is present for this URL, it is not considered a redirect.
+ * If a File_redirection record is present for this URL, the recorded target is returned.
+ *
+ * If no File or File_redirect record is present, the URL is hit and any
+ * redirects are followed, up to 10 levels or until a protected URL is
+ * reached.
+ *
+ * @param string $in_url
+ * @return mixed one of:
+ * string - target URL, if this is a direct link or a known redirect
+ * array - redirect info if this is an *unknown* redirect:
+ * associative array with the following elements:
+ * code: HTTP status code
+ * redirects: count of redirects followed
+ * url: URL string of final target
+ * type (optional): MIME type from Content-Type header
+ * size (optional): byte size from Content-Length header
+ * time (optional): timestamp from Last-Modified header
+ */
+ public function where($in_url) {
+ // let's see if we know this...
+ $a = File::staticGet('url', $in_url);
+
+ if (!empty($a)) {
+ // this is a direct link to $a->url
+ return $a->url;
+ } else {
+ $b = File_redirection::staticGet('url', $in_url);
+ if (!empty($b)) {
+ // this is a redirect to $b->file_id
+ $a = File::staticGet('id', $b->file_id);
+ return $a->url;
+ }
+ }
+
+ $ret = File_redirection::lookupWhere($in_url);
return $ret;
}
+ /**
+ * Shorten a URL with the current user's configured shortening
+ * options, if applicable.
+ *
+ * If it cannot be shortened or the "short" URL is longer than the
+ * original, the original is returned.
+ *
+ * If the referenced item has not been seen before, embedding data
+ * may be saved.
+ *
+ * @param string $long_url
+ * @return string
+ */
function makeShort($long_url) {
$canon = File_redirection::_canonUrl($long_url);
@@ -141,11 +197,20 @@ class File_redirection extends Memcached_DataObject
// store it
$file = File::staticGet('url', $long_url);
if (empty($file)) {
+ // Check if the target URL is itself a redirect...
$redir_data = File_redirection::where($long_url);
- $file = File::saveNew($redir_data, $long_url);
- $file_id = $file->id;
- if (!empty($redir_data['oembed']['json'])) {
- File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
+ if (is_array($redir_data)) {
+ // We haven't seen the target URL before.
+ // Save file and embedding data about it!
+ $file = File::saveNew($redir_data, $long_url);
+ $file_id = $file->id;
+ if (!empty($redir_data['oembed']['json'])) {
+ File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
+ }
+ } else if (is_string($redir_data)) {
+ // The file is a known redirect target.
+ $file = File::staticGet('url', $redir_data);
+ $file_id = $file->id;
}
} else {
$file_id = $file->id;
diff --git a/classes/Foreign_link.php b/classes/Foreign_link.php
index ae8c22fd8..e47b2e309 100644
--- a/classes/Foreign_link.php
+++ b/classes/Foreign_link.php
@@ -113,4 +113,21 @@ class Foreign_link extends Memcached_DataObject
return User::staticGet($this->user_id);
}
+ // Make sure we only ever delete one record at a time
+ function safeDelete()
+ {
+ if (!empty($this->user_id)
+ && !empty($this->foreign_id)
+ && !empty($this->service))
+ {
+ return $this->delete();
+ } else {
+ common_debug(LOG_WARNING,
+ 'Foreign_link::safeDelete() tried to delete a '
+ . 'Foreign_link without a fully specified compound key: '
+ . var_export($this, true));
+ return false;
+ }
+ }
+
}
diff --git a/classes/Foreign_user.php b/classes/Foreign_user.php
index 8b3e03dfb..0dd94ffb9 100644
--- a/classes/Foreign_user.php
+++ b/classes/Foreign_user.php
@@ -41,6 +41,7 @@ class Foreign_user extends Memcached_DataObject
function updateKeys(&$orig)
{
+ $this->_connect();
$parts = array();
foreach (array('id', 'service', 'uri', 'nickname') as $k) {
if (strcmp($this->$k, $orig->$k) != 0) {
diff --git a/classes/Group_alias.php b/classes/Group_alias.php
index be3d0a6c6..c5a1895a1 100644
--- a/classes/Group_alias.php
+++ b/classes/Group_alias.php
@@ -34,7 +34,7 @@ class Group_alias extends Memcached_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
- function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Group_alias',$k,$v); }
+ function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Group_alias',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
diff --git a/classes/Inbox.php b/classes/Inbox.php
index 014ba3d82..430419ba5 100644
--- a/classes/Inbox.php
+++ b/classes/Inbox.php
@@ -96,17 +96,31 @@ class Inbox extends Memcached_DataObject
$inbox = new Inbox();
$inbox->user_id = $user_id;
- $inbox->notice_ids = call_user_func_array('pack', array_merge(array('N*'), $ids));
+ $inbox->pack($ids);
$inbox->fake = true;
return $inbox;
}
+ /**
+ * Append the given notice to the given user's inbox.
+ * Caching updates are managed for the inbox itself.
+ *
+ * If the notice is already in this inbox, the second
+ * add will be silently dropped.
+ *
+ * @param int @user_id
+ * @param int $notice_id
+ * @return boolean success
+ */
static function insertNotice($user_id, $notice_id)
{
- $inbox = DB_DataObject::staticGet('inbox', 'user_id', $user_id);
-
- if (empty($inbox)) {
+ // Going straight to the DB rather than trusting our caching
+ // during an update. Note: not using DB_DataObject::staticGet,
+ // which is unsafe to use directly (in-process caching causes
+ // memory leaks, which accumulate in queue processes).
+ $inbox = new Inbox();
+ if (!$inbox->get('user_id', $user_id)) {
$inbox = Inbox::initialize($user_id);
}
@@ -114,6 +128,13 @@ class Inbox extends Memcached_DataObject
return false;
}
+ $ids = $inbox->unpack();
+ if (in_array(intval($notice_id), $ids)) {
+ // Already in there, we probably re-ran some inbox adds
+ // due to an error. Skip the dupe silently.
+ return true;
+ }
+
$result = $inbox->query(sprintf('UPDATE inbox '.
'set notice_ids = concat(cast(0x%08x as binary(4)), '.
'substr(notice_ids, 1, %d)) '.
@@ -150,7 +171,7 @@ class Inbox extends Memcached_DataObject
}
}
- $ids = unpack('N*', $inbox->notice_ids);
+ $ids = $inbox->unpack();
if (!empty($since_id)) {
$newids = array();
@@ -229,4 +250,21 @@ class Inbox extends Memcached_DataObject
}
return new ArrayWrapper($items);
}
+
+ /**
+ * Saves a list of integer notice_ids into a packed blob in this object.
+ * @param array $ids list of integer notice_ids
+ */
+ protected function pack(array $ids)
+ {
+ $this->notice_ids = call_user_func_array('pack', array_merge(array('N*'), $ids));
+ }
+
+ /**
+ * @return array of integer notice_ids
+ */
+ protected function unpack()
+ {
+ return unpack('N*', $this->notice_ids);
+ }
}
diff --git a/classes/Memcached_DataObject.php b/classes/Memcached_DataObject.php
index bc4c3a000..4579f64df 100644
--- a/classes/Memcached_DataObject.php
+++ b/classes/Memcached_DataObject.php
@@ -128,12 +128,13 @@ class Memcached_DataObject extends Safe_DataObject
}
static function cacheKey($cls, $k, $v) {
- if (is_object($cls) || is_object($k) || is_object($v)) {
+ if (is_object($cls) || is_object($k) || (is_object($v) && !($v instanceof DB_DataObject_Cast))) {
$e = new Exception();
common_log(LOG_ERR, __METHOD__ . ' object in param: ' .
str_replace("\n", " ", $e->getTraceAsString()));
}
- return common_cache_key(strtolower($cls).':'.$k.':'.$v);
+ $vstr = self::valueString($v);
+ return common_cache_key(strtolower($cls).':'.$k.':'.$vstr);
}
static function getcached($cls, $k, $v) {
@@ -229,10 +230,10 @@ class Memcached_DataObject extends Safe_DataObject
if (empty($this->$key)) {
continue;
}
- $ckeys[] = $this->cacheKey($this->tableName(), $key, $this->$key);
+ $ckeys[] = $this->cacheKey($this->tableName(), $key, self::valueString($this->$key));
} else if ($type == 'K' || $type == 'N') {
$pkey[] = $key;
- $pval[] = $this->$key;
+ $pval[] = self::valueString($this->$key);
} else {
throw new Exception("Unknown key type $key => $type for " . $this->tableName());
}
@@ -330,6 +331,10 @@ class Memcached_DataObject extends Safe_DataObject
*/
function _query($string)
{
+ if (common_config('db', 'annotate_queries')) {
+ $string = $this->annotateQuery($string);
+ }
+
$start = microtime(true);
$result = parent::_query($string);
$delta = microtime(true) - $start;
@@ -342,6 +347,70 @@ class Memcached_DataObject extends Safe_DataObject
return $result;
}
+ /**
+ * Find the first caller in the stack trace that's not a
+ * low-level database function and add a comment to the
+ * query string. This should then be visible in process lists
+ * and slow query logs, to help identify problem areas.
+ *
+ * Also marks whether this was a web GET/POST or which daemon
+ * was running it.
+ *
+ * @param string $string SQL query string
+ * @return string SQL query string, with a comment in it
+ */
+ function annotateQuery($string)
+ {
+ $ignore = array('annotateQuery',
+ '_query',
+ 'query',
+ 'get',
+ 'insert',
+ 'delete',
+ 'update',
+ 'find');
+ $ignoreStatic = array('staticGet',
+ 'pkeyGet',
+ 'cachedQuery');
+ $here = get_class($this); // if we get confused
+ $bt = debug_backtrace();
+
+ // Find the first caller that's not us?
+ foreach ($bt as $frame) {
+ $func = $frame['function'];
+ if (isset($frame['type']) && $frame['type'] == '::') {
+ if (in_array($func, $ignoreStatic)) {
+ continue;
+ }
+ $here = $frame['class'] . '::' . $func;
+ break;
+ } else if (isset($frame['type']) && $frame['type'] == '->') {
+ if ($frame['object'] === $this && in_array($func, $ignore)) {
+ continue;
+ }
+ if (in_array($func, $ignoreStatic)) {
+ continue; // @fixme this shouldn't be needed?
+ }
+ $here = get_class($frame['object']) . '->' . $func;
+ break;
+ }
+ $here = $func;
+ break;
+ }
+
+ if (php_sapi_name() == 'cli') {
+ $context = basename($_SERVER['PHP_SELF']);
+ } else {
+ $context = $_SERVER['REQUEST_METHOD'];
+ }
+
+ // Slip the comment in after the first command,
+ // or DB_DataObject gets confused about handling inserts and such.
+ $parts = explode(' ', $string, 2);
+ $parts[0] .= " /* $context $here */";
+ return implode(' ', $parts);
+ }
+
// Sanitize a query for logging
// @fixme don't trim spaces in string literals
function sanitizeQuery($string)
@@ -505,6 +574,9 @@ class Memcached_DataObject extends Safe_DataObject
if ($this->id) {
$id .= ':' . $this->id;
}
+ if ($message instanceof PEAR_Error) {
+ $message = $message->getMessage();
+ }
throw new ServerException("[$id] DB_DataObject error [$type]: $message");
}
@@ -533,5 +605,30 @@ class Memcached_DataObject extends Safe_DataObject
return $c->set($cacheKey, $value);
}
+
+ static function valueString($v)
+ {
+ $vstr = null;
+ if (is_object($v) && $v instanceof DB_DataObject_Cast) {
+ switch ($v->type) {
+ case 'date':
+ $vstr = $v->year . '-' . $v->month . '-' . $v->day;
+ break;
+ case 'blob':
+ case 'string':
+ case 'sql':
+ case 'datetime':
+ case 'time':
+ throw new ServerException("Unhandled DB_DataObject_Cast type passed as cacheKey value: '$v->type'");
+ break;
+ default:
+ throw new ServerException("Unknown DB_DataObject_Cast type passed as cacheKey value: '$v->type'");
+ break;
+ }
+ } else {
+ $vstr = strval($v);
+ }
+ return $vstr;
+ }
}
diff --git a/classes/Notice.php b/classes/Notice.php
index 63dc96897..482bc550b 100644
--- a/classes/Notice.php
+++ b/classes/Notice.php
@@ -29,6 +29,7 @@
* @author Robin Millette <millette@controlyourself.ca>
* @author Sarven Capadisli <csarven@controlyourself.ca>
* @author Tom Adams <tom@holizz.com>
+ * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
* @license GNU Affero General Public License http://www.gnu.org/licenses/
*/
@@ -97,15 +98,20 @@ class Notice extends Memcached_DataObject
// For auditing purposes, save a record that the notice
// was deleted.
- $deleted = new Deleted_notice();
+ // @fixme we have some cases where things get re-run and so the
+ // insert fails.
+ $deleted = Deleted_notice::staticGet('id', $this->id);
+ if (!$deleted) {
+ $deleted = new Deleted_notice();
- $deleted->id = $this->id;
- $deleted->profile_id = $this->profile_id;
- $deleted->uri = $this->uri;
- $deleted->created = $this->created;
- $deleted->deleted = common_sql_now();
+ $deleted->id = $this->id;
+ $deleted->profile_id = $this->profile_id;
+ $deleted->uri = $this->uri;
+ $deleted->created = $this->created;
+ $deleted->deleted = common_sql_now();
- $deleted->insert();
+ $deleted->insert();
+ }
// Clear related records
@@ -119,6 +125,9 @@ class Notice extends Memcached_DataObject
// NOTE: we don't clear queue items
$result = parent::delete();
+
+ $this->blowOnDelete();
+ return $result;
}
/**
@@ -145,11 +154,11 @@ class Notice extends Memcached_DataObject
//turn each into their canonical tag
//this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
for($i=0; $i<count($hashtags); $i++) {
+ /* elide characters we don't want in the tag */
$hashtags[$i] = common_canonical_tag($hashtags[$i]);
}
foreach(array_unique($hashtags) as $hashtag) {
- /* elide characters we don't want in the tag */
$this->saveTag($hashtag);
self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, $hashtag);
}
@@ -169,7 +178,8 @@ class Notice extends Memcached_DataObject
$id = $tag->insert();
if (!$id) {
- throw new ServerException(sprintf(_('DB error inserting hashtag: %s'),
+ // TRANS: Server exception. %s are the error details.
+ throw new ServerException(sprintf(_('Database error inserting hashtag: %s'),
$last_error->message));
return;
}
@@ -211,6 +221,8 @@ class Notice extends Memcached_DataObject
* extracting ! tags from content
* array 'tags' list of hashtag strings to save with the notice
* in place of extracting # tags from content
+ * array 'urls' list of attached/referred URLs to save with the
+ * notice in place of extracting links from content
* @fixme tag override
*
* @return Notice
@@ -368,21 +380,26 @@ class Notice extends Memcached_DataObject
$notice->saveReplies();
}
+ if (isset($tags)) {
+ $notice->saveKnownTags($tags);
+ } else {
+ $notice->saveTags();
+ }
+
+ // Note: groups may save tags, so must be run after tags are saved
+ // to avoid errors on duplicates.
if (isset($groups)) {
$notice->saveKnownGroups($groups);
} else {
$notice->saveGroups();
}
- if (isset($tags)) {
- $notice->saveKnownTags($tags);
+ if (isset($urls)) {
+ $notice->saveKnownUrls($urls);
} else {
- $notice->saveTags();
+ $notice->saveUrls();
}
- // @fixme pass in data for URLs too?
- $notice->saveUrls();
-
// Prepare inbox delivery, may be queued to background.
$notice->distribute();
@@ -413,7 +430,21 @@ class Notice extends Memcached_DataObject
}
$profile = Profile::staticGet($this->profile_id);
- $profile->blowNoticeCount();
+ if (!empty($profile)) {
+ $profile->blowNoticeCount();
+ }
+ }
+
+ /**
+ * Clear cache entries related to this notice at delete time.
+ * Necessary to avoid breaking paging on public, profile timelines.
+ */
+ function blowOnDelete()
+ {
+ $this->blowOnInsert();
+
+ self::blow('profile:notice_ids:%d;last', $this->profile_id);
+ self::blow('public;last');
}
/** save all urls in the notice to the db
@@ -427,6 +458,25 @@ class Notice extends Memcached_DataObject
common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
}
+ /**
+ * Save the given URLs as related links/attachments to the db
+ *
+ * follow redirects and save all available file information
+ * (mimetype, date, size, oembed, etc.)
+ *
+ * @return void
+ */
+ function saveKnownUrls($urls)
+ {
+ // @fixme validation?
+ foreach ($urls as $url) {
+ File::processNew($url, $this->id);
+ }
+ }
+
+ /**
+ * @private callback
+ */
function saveUrl($data) {
list($url, $notice_id) = $data;
File::processNew($url, $notice_id);
@@ -565,7 +615,6 @@ class Notice extends Memcached_DataObject
array(),
'public',
$offset, $limit, $since_id, $max_id);
-
return Notice::getStreamByIds($ids);
}
@@ -660,6 +709,27 @@ class Notice extends Memcached_DataObject
}
/**
+ * Is this notice part of an active conversation?
+ *
+ * @return boolean true if other messages exist in the same
+ * conversation, false if this is the only one
+ */
+ function hasConversation()
+ {
+ if (!empty($this->conversation)) {
+ $conversation = Notice::conversationStream(
+ $this->conversation,
+ 1,
+ 1
+ );
+ if ($conversation->N > 0) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
* @param $groups array of Group *objects*
* @param $recipients array of profile *ids*
*/
@@ -853,7 +923,7 @@ class Notice extends Memcached_DataObject
foreach (array_unique($match[1]) as $nickname) {
/* XXX: remote groups. */
- $group = User_group::getForNickname($nickname);
+ $group = User_group::getForNickname($nickname, $profile);
if (empty($group)) {
continue;
@@ -917,18 +987,25 @@ class Notice extends Memcached_DataObject
* messages, we won't deliver to any remote targets as that's the
* source service's responsibility.
*
- * @fixme Unlike saveReplies() there's no mail notification here.
- * Move that to distrib queue handler?
+ * Mail notifications etc will be handled later.
*
* @param array of unique identifier URIs for recipients
*/
function saveKnownReplies($uris)
{
+ if (empty($uris)) {
+ return;
+ }
+ $sender = Profile::staticGet($this->profile_id);
+
foreach ($uris as $uri) {
$user = User::staticGet('uri', $uri);
if (!empty($user)) {
+ if ($user->hasBlocked($sender)) {
+ continue;
+ }
$reply = new Reply();
@@ -936,8 +1013,6 @@ class Notice extends Memcached_DataObject
$reply->profile_id = $user->id;
$id = $reply->insert();
-
- self::blow('reply:stream:%d', $user->id);
}
}
@@ -949,8 +1024,7 @@ class Notice extends Memcached_DataObject
* and save reply records indicating that this message needs to be
* delivered to those users.
*
- * Side effect: local recipients get e-mail notifications here.
- * @fixme move mail notifications to distrib?
+ * Mail notifications to local profiles will be sent later.
*
* @return array of integer profile IDs
*/
@@ -1004,23 +1078,21 @@ class Notice extends Memcached_DataObject
throw new ServerException("Couldn't save reply for {$this->id}, {$mentioned->id}");
} else {
$replied[$mentioned->id] = 1;
+ self::blow('reply:stream:%d', $mentioned->id);
}
}
}
$recipientIds = array_keys($replied);
- foreach ($recipientIds as $recipientId) {
- $user = User::staticGet('id', $recipientId);
- if (!empty($user)) {
- self::blow('reply:stream:%d', $reply->profile_id);
- mail_notify_attn($user, $this);
- }
- }
-
return $recipientIds;
}
+ /**
+ * Pull the complete list of @-reply targets for this notice.
+ *
+ * @return array of integer profile ids
+ */
function getReplies()
{
// XXX: cache me
@@ -1044,6 +1116,30 @@ class Notice extends Memcached_DataObject
}
/**
+ * Send e-mail notifications to local @-reply targets.
+ *
+ * Replies must already have been saved; this is expected to be run
+ * from the distrib queue handler.
+ */
+ function sendReplyNotifications()
+ {
+ // Don't send reply notifications for repeats
+
+ if (!empty($this->repeat_of)) {
+ return array();
+ }
+
+ $recipientIds = $this->getReplies();
+
+ foreach ($recipientIds as $recipientId) {
+ $user = User::staticGet('id', $recipientId);
+ if (!empty($user)) {
+ mail_notify_attn($user, $this);
+ }
+ }
+ }
+
+ /**
* Pull list of groups this notice needs to be delivered to,
* as previously recorded by saveGroups() or saveKnownGroups().
*
@@ -1082,7 +1178,7 @@ class Notice extends Memcached_DataObject
return $groups;
}
- function asAtomEntry($namespace=false, $source=false)
+ function asAtomEntry($namespace=false, $source=false, $author=true, $cur=null)
{
$profile = $this->getProfile();
@@ -1095,7 +1191,8 @@ class Notice extends Memcached_DataObject
'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
'xmlns:media' => 'http://purl.org/syndication/atommedia',
'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
- 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0');
+ 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
+ 'xmlns:statusnet' => 'http://status.net/schema/api/1/');
} else {
$attrs = array();
}
@@ -1104,6 +1201,7 @@ class Notice extends Memcached_DataObject
if ($source) {
$xs->elementStart('source');
+ $xs->element('id', null, $profile->profileurl);
$xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
$xs->element('link', array('href' => $profile->profileurl));
$user = User::staticGet('id', $profile->id);
@@ -1119,16 +1217,19 @@ class Notice extends Memcached_DataObject
}
$xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
+ $xs->element('updated', null, common_date_w3dtf($this->created));
}
if ($source) {
$xs->elementEnd('source');
}
- $xs->element('title', null, $this->content);
+ $xs->element('title', null, common_xml_safe_str($this->content));
- $xs->raw($profile->asAtomAuthor());
- $xs->raw($profile->asActivityActor());
+ if ($author) {
+ $xs->raw($profile->asAtomAuthor($cur));
+ $xs->raw($profile->asActivityActor());
+ }
$xs->element('link', array('rel' => 'alternate',
'type' => 'text/html',
@@ -1139,6 +1240,46 @@ class Notice extends Memcached_DataObject
$xs->element('published', null, common_date_w3dtf($this->created));
$xs->element('updated', null, common_date_w3dtf($this->created));
+ $source = null;
+
+ $ns = $this->getSource();
+
+ if ($ns) {
+ if (!empty($ns->name) && !empty($ns->url)) {
+ $source = '<a href="'
+ . htmlspecialchars($ns->url)
+ . '" rel="nofollow">'
+ . htmlspecialchars($ns->name)
+ . '</a>';
+ } else {
+ $source = $ns->code;
+ }
+ }
+
+ $noticeInfoAttr = array(
+ 'local_id' => $this->id, // local notice ID (useful to clients for ordering)
+ 'source' => $source, // the client name (source attribution)
+ );
+
+ $ns = $this->getSource();
+ if ($ns) {
+ if (!empty($ns->url)) {
+ $noticeInfoAttr['source_link'] = $ns->url;
+ }
+ }
+
+ if (!empty($cur)) {
+ $noticeInfoAttr['favorite'] = ($cur->hasFave($this)) ? "true" : "false";
+ $profile = $cur->getProfile();
+ $noticeInfoAttr['repeated'] = ($profile->hasRepeated($this->id)) ? "true" : "false";
+ }
+
+ if (!empty($this->repeat_of)) {
+ $noticeInfoAttr['repeat_of'] = $this->repeat_of;
+ }
+
+ $xs->element('statusnet:notice_info', $noticeInfoAttr, null);
+
if ($this->reply_to) {
$reply_notice = Notice::staticGet('id', $this->reply_to);
if (!empty($reply_notice)) {
@@ -1199,7 +1340,11 @@ class Notice extends Memcached_DataObject
}
}
- $xs->element('content', array('type' => 'html'), $this->rendered);
+ $xs->element(
+ 'content',
+ array('type' => 'html'),
+ common_xml_safe_str($this->rendered)
+ );
$tag = new Notice_tag();
$tag->notice_id = $this->id;
@@ -1430,6 +1575,8 @@ class Notice extends Memcached_DataObject
{
$author = Profile::staticGet('id', $this->profile_id);
+ // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
+ // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
$content = sprintf(_('RT @%1$s %2$s'),
$author->nickname,
$this->content);
@@ -1699,4 +1846,53 @@ class Notice extends Memcached_DataObject
return $result;
}
+
+ /**
+ * Get the source of the notice
+ *
+ * @return Notice_source $ns A notice source object. 'code' is the only attribute
+ * guaranteed to be populated.
+ */
+ function getSource()
+ {
+ $ns = new Notice_source();
+ if (!empty($this->source)) {
+ switch ($this->source) {
+ case 'web':
+ case 'xmpp':
+ case 'mail':
+ case 'omb':
+ case 'system':
+ case 'api':
+ $ns->code = $this->source;
+ break;
+ default:
+ $ns = Notice_source::staticGet($this->source);
+ if (!$ns) {
+ $ns = new Notice_source();
+ $ns->code = $this->source;
+ $app = Oauth_application::staticGet('name', $this->source);
+ if ($app) {
+ $ns->name = $app->name;
+ $ns->url = $app->source_url;
+ }
+ }
+ break;
+ }
+ }
+ return $ns;
+ }
+
+ /**
+ * Determine whether the notice was locally created
+ *
+ * @return boolean locality
+ */
+
+ public function isLocal()
+ {
+ return ($this->is_local == Notice::LOCAL_PUBLIC ||
+ $this->is_local == Notice::LOCAL_NONPUBLIC);
+ }
+
}
diff --git a/classes/Profile.php b/classes/Profile.php
index 470ef3320..a303469e9 100644
--- a/classes/Profile.php
+++ b/classes/Profile.php
@@ -147,14 +147,16 @@ class Profile extends Memcached_DataObject
return ($this->fullname) ? $this->fullname : $this->nickname;
}
- # Get latest notice on or before date; default now
- function getCurrentNotice($dt=null)
+ /**
+ * Get the most recent notice posted by this user, if any.
+ *
+ * @return mixed Notice or null
+ */
+ function getCurrentNotice()
{
$notice = new Notice();
$notice->profile_id = $this->id;
- if ($dt) {
- $notice->whereAdd('created < "' . $dt . '"');
- }
+ // @fixme change this to sort on notice.id only when indexes are updated
$notice->orderBy('created DESC, notice.id DESC');
$notice->limit(1);
if ($notice->find(true)) {
@@ -223,31 +225,62 @@ class Profile extends Memcached_DataObject
{
$notice = new Notice();
- $notice->profile_id = $this->id;
+ // Temporary hack until notice_profile_id_idx is updated
+ // to (profile_id, id) instead of (profile_id, created, id).
+ // It's been falling back to PRIMARY instead, which is really
+ // very inefficient for a profile that hasn't posted in a few
+ // months. Even though forcing the index will cause a filesort,
+ // it's usually going to be better.
+ if (common_config('db', 'type') == 'mysql') {
+ $index = '';
+ $query =
+ "select id from notice force index (notice_profile_id_idx) ".
+ "where profile_id=" . $notice->escape($this->id);
+
+ if ($since_id != 0) {
+ $query .= " and id > $since_id";
+ }
- $notice->selectAdd();
- $notice->selectAdd('id');
+ if ($max_id != 0) {
+ $query .= " and id < $max_id";
+ }
- if ($since_id != 0) {
- $notice->whereAdd('id > ' . $since_id);
- }
+ $query .= ' order by id DESC';
- if ($max_id != 0) {
- $notice->whereAdd('id <= ' . $max_id);
- }
+ if (!is_null($offset)) {
+ $query .= " LIMIT $limit OFFSET $offset";
+ }
- $notice->orderBy('id DESC');
+ $notice->query($query);
+ } else {
+ $index = '';
- if (!is_null($offset)) {
- $notice->limit($offset, $limit);
+ $notice->profile_id = $this->id;
+
+ $notice->selectAdd();
+ $notice->selectAdd('id');
+
+ if ($since_id != 0) {
+ $notice->whereAdd('id > ' . $since_id);
+ }
+
+ if ($max_id != 0) {
+ $notice->whereAdd('id <= ' . $max_id);
+ }
+
+ $notice->orderBy('id DESC');
+
+ if (!is_null($offset)) {
+ $notice->limit($offset, $limit);
+ }
+
+ $notice->find();
}
$ids = array();
- if ($notice->find()) {
- while ($notice->fetch()) {
- $ids[] = $notice->id;
- }
+ while ($notice->fetch()) {
+ $ids[] = $notice->id;
}
return $ids;
@@ -282,6 +315,32 @@ class Profile extends Memcached_DataObject
}
}
+ function getGroups($offset=0, $limit=null)
+ {
+ $qry =
+ 'SELECT user_group.* ' .
+ 'FROM user_group JOIN group_member '.
+ 'ON user_group.id = group_member.group_id ' .
+ 'WHERE group_member.profile_id = %d ' .
+ 'ORDER BY group_member.created DESC ';
+
+ if ($offset>0 && !is_null($limit)) {
+ if ($offset) {
+ if (common_config('db','type') == 'pgsql') {
+ $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+ } else {
+ $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+ }
+ }
+ }
+
+ $groups = new User_group();
+
+ $cnt = $groups->query(sprintf($qry, $this->id));
+
+ return $groups;
+ }
+
function avatarUrl($size=AVATAR_PROFILE_SIZE)
{
$avatar = $this->getAvatar($size);
@@ -549,11 +608,41 @@ class Profile extends Memcached_DataObject
{
$sub = new Subscription();
$sub->subscriber = $this->id;
- $sub->delete();
+
+ $sub->find();
+
+ while ($sub->fetch()) {
+ $other = Profile::staticGet('id', $sub->subscribed);
+ if (empty($other)) {
+ continue;
+ }
+ if ($other->id == $this->id) {
+ continue;
+ }
+ Subscription::cancel($this, $other);
+ }
$subd = new Subscription();
$subd->subscribed = $this->id;
- $subd->delete();
+ $subd->find();
+
+ while ($subd->fetch()) {
+ $other = Profile::staticGet('id', $subd->subscriber);
+ if (empty($other)) {
+ continue;
+ }
+ if ($other->id == $this->id) {
+ continue;
+ }
+ Subscription::cancel($other, $this);
+ }
+
+ $self = new Subscription();
+
+ $self->subscriber = $this->id;
+ $self->subscribed = $this->id;
+
+ $self->delete();
}
function _deleteMessages()
@@ -704,6 +793,9 @@ class Profile extends Memcached_DataObject
function hasRight($right)
{
$result = false;
+ if ($this->hasRole(Profile_role::DELETED)) {
+ return false;
+ }
if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
switch ($right)
{
@@ -717,6 +809,10 @@ class Profile extends Memcached_DataObject
case Right::CONFIGURESITE:
$result = $this->hasRole(Profile_role::ADMINISTRATOR);
break;
+ case Right::GRANTROLE:
+ case Right::REVOKEROLE:
+ $result = $this->hasRole(Profile_role::OWNER);
+ break;
case Right::NEWNOTICE:
case Right::NEWMESSAGE:
case Right::SUBSCRIBE:
@@ -753,15 +849,23 @@ class Profile extends Memcached_DataObject
*
* Assumes that Atom has been previously set up as the base namespace.
*
+ * @param Profile $cur the current authenticated user
+ *
* @return string
*/
- function asAtomAuthor()
+ function asAtomAuthor($cur = null)
{
$xs = new XMLStringer(true);
$xs->elementStart('author');
$xs->element('name', null, $this->nickname);
$xs->element('uri', null, $this->getUri());
+ if ($cur != null) {
+ $attrs = Array();
+ $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
+ $attrs['blocking'] = $cur->hasBlocked($this) ? 'true' : 'false';
+ $xs->element('statusnet:profile_info', $attrs, null);
+ }
$xs->elementEnd('author');
return $xs->getString();
diff --git a/classes/Profile_role.php b/classes/Profile_role.php
index bf2c453ed..e7aa1f0f0 100644
--- a/classes/Profile_role.php
+++ b/classes/Profile_role.php
@@ -53,4 +53,22 @@ class Profile_role extends Memcached_DataObject
const ADMINISTRATOR = 'administrator';
const SANDBOXED = 'sandboxed';
const SILENCED = 'silenced';
+ const DELETED = 'deleted'; // Pending final deletion of notices...
+
+ public static function isValid($role)
+ {
+ // @fixme could probably pull this from class constants
+ $known = array(self::OWNER,
+ self::MODERATOR,
+ self::ADMINISTRATOR,
+ self::SANDBOXED,
+ self::SILENCED);
+ return in_array($role, $known);
+ }
+
+ public static function isSettable($role)
+ {
+ $allowedRoles = array('administrator', 'moderator');
+ return self::isValid($role) && in_array($role, $allowedRoles);
+ }
}
diff --git a/classes/Queue_item.php b/classes/Queue_item.php
index f83c2cef1..c7e17be6e 100644
--- a/classes/Queue_item.php
+++ b/classes/Queue_item.php
@@ -64,4 +64,17 @@ class Queue_item extends Memcached_DataObject
$qi = null;
return null;
}
+
+ /**
+ * Release a claimed item.
+ */
+ function releaseCLaim()
+ {
+ // DB_DataObject doesn't let us save nulls right now
+ $sql = sprintf("UPDATE queue_item SET claimed=NULL WHERE id=%d", $this->id);
+ $this->query($sql);
+
+ $this->claimed = null;
+ $this->encache();
+ }
}
diff --git a/classes/Reply.php b/classes/Reply.php
index 659e04c92..dc6296bda 100644
--- a/classes/Reply.php
+++ b/classes/Reply.php
@@ -22,6 +22,20 @@ class Reply extends Memcached_DataObject
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
+ /**
+ * Wrapper for record insertion to update related caches
+ */
+ function insert()
+ {
+ $result = parent::insert();
+
+ if ($result) {
+ self::blow('reply:stream:%d', $this->profile_id);
+ }
+
+ return $result;
+ }
+
function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
{
$ids = Notice::stream(array('Reply', '_streamDirect'),
diff --git a/classes/Safe_DataObject.php b/classes/Safe_DataObject.php
index 021f7b506..e926cb0d5 100644
--- a/classes/Safe_DataObject.php
+++ b/classes/Safe_DataObject.php
@@ -43,6 +43,25 @@ class Safe_DataObject extends DB_DataObject
}
/**
+ * Magic function called at clone() time.
+ *
+ * We use this to drop connection with some global resources.
+ * This supports the fairly common pattern where individual
+ * items being read in a loop via a single object are cloned
+ * for individual processing, then fall out of scope when the
+ * loop comes around again.
+ *
+ * As that triggers the destructor, we want to make sure that
+ * the original object doesn't have its database result killed.
+ * It will still be freed properly when the original object
+ * gets destroyed.
+ */
+ function __clone()
+ {
+ $this->_DB_resultid = false;
+ }
+
+ /**
* Magic function called at serialize() time.
*
* We use this to drop a couple process-specific references
@@ -77,6 +96,30 @@ class Safe_DataObject extends DB_DataObject
$this->_link_loaded = false;
}
+ /**
+ * Magic function called when someone attempts to call a method
+ * that doesn't exist. DB_DataObject uses this to implement
+ * setters and getters for fields, but neglects to throw an error
+ * when you just misspell an actual method name. This leads to
+ * silent failures which can cause all kinds of havoc.
+ *
+ * @param string $method
+ * @param array $params
+ * @return mixed
+ * @throws Exception
+ */
+ function __call($method, $params)
+ {
+ $return = null;
+ // Yes, that's _call with one underscore, which does the
+ // actual implementation.
+ if ($this->_call($method, $params, $return)) {
+ return $return;
+ } else {
+ throw new Exception('Call to undefined method ' .
+ get_class($this) . '::' . $method);
+ }
+ }
/**
* Work around memory-leak bugs...
diff --git a/classes/Status_network.php b/classes/Status_network.php
index a452c32ce..64016dd79 100644
--- a/classes/Status_network.php
+++ b/classes/Status_network.php
@@ -144,26 +144,49 @@ class Status_network extends Safe_DataObject
return parent::update($orig);
}
+ /**
+ * DB_DataObject doesn't allow updating keys (even non-primary)
+ */
+ function updateKeys(&$orig)
+ {
+ $this->_connect();
+ foreach (array('hostname', 'pathname') as $k) {
+ if (strcmp($this->$k, $orig->$k) != 0) {
+ $parts[] = $k . ' = ' . $this->_quote($this->$k);
+ }
+ }
+ if (count($parts) == 0) {
+ // No changes
+ return true;
+ }
+
+ $toupdate = implode(', ', $parts);
+
+ $table = common_database_tablename($this->tableName());
+ $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
+ ' WHERE nickname = ' . $this->_quote($this->nickname);
+ $orig->decache();
+ $result = $this->query($qry);
+ if ($result) {
+ $this->encache();
+ }
+ return $result;
+ }
+
function delete()
{
$this->decache(); # while we still have the values!
return parent::delete();
}
-
+
/**
* @param string $servername hostname
- * @param string $pathname URL base path
* @param string $wildcard hostname suffix to match wildcard config
+ * @return mixed Status_network or null
*/
- static function setupSite($servername, $pathname, $wildcard)
+ static function getFromHostname($servername, $wildcard)
{
- global $config;
-
$sn = null;
-
- // XXX I18N, probably not crucial for hostnames
- // XXX This probably needs a tune up
-
if (0 == strncasecmp(strrev($wildcard), strrev($servername), strlen($wildcard))) {
// special case for exact match
if (0 == strcasecmp($servername, $wildcard)) {
@@ -182,6 +205,23 @@ class Status_network extends Safe_DataObject
}
}
}
+ return $sn;
+ }
+
+ /**
+ * @param string $servername hostname
+ * @param string $pathname URL base path
+ * @param string $wildcard hostname suffix to match wildcard config
+ */
+ static function setupSite($servername, $pathname, $wildcard)
+ {
+ global $config;
+
+ $sn = null;
+
+ // XXX I18N, probably not crucial for hostnames
+ // XXX This probably needs a tune up
+ $sn = self::getFromHostname($servername, $wildcard);
if (!empty($sn)) {
diff --git a/classes/Subscription.php b/classes/Subscription.php
index 9cef2df1a..0679c0925 100644
--- a/classes/Subscription.php
+++ b/classes/Subscription.php
@@ -62,6 +62,14 @@ class Subscription extends Memcached_DataObject
static function start($subscriber, $other)
{
+ // @fixme should we enforce this as profiles in callers instead?
+ if ($subscriber instanceof User) {
+ $subscriber = $subscriber->getProfile();
+ }
+ if ($other instanceof User) {
+ $other = $other->getProfile();
+ }
+
if (!$subscriber->hasRight(Right::SUBSCRIBE)) {
throw new Exception(_('You have been banned from subscribing.'));
}
@@ -75,26 +83,13 @@ class Subscription extends Memcached_DataObject
}
if (Event::handle('StartSubscribe', array($subscriber, $other))) {
-
- $sub = new Subscription();
-
- $sub->subscriber = $subscriber->id;
- $sub->subscribed = $other->id;
- $sub->created = common_sql_now();
-
- $result = $sub->insert();
-
- if (!$result) {
- common_log_db_error($sub, 'INSERT', __FILE__);
- throw new Exception(_('Could not save subscription.'));
- }
-
+ $sub = self::saveNew($subscriber->id, $other->id);
$sub->notify();
self::blow('user:notices_with_friends:%d', $subscriber->id);
- $subscriber->blowSubscriptionsCount();
- $other->blowSubscribersCount();
+ $subscriber->blowSubscriptionCount();
+ $other->blowSubscriberCount();
$otherUser = User::staticGet('id', $other->id);
@@ -103,20 +98,11 @@ class Subscription extends Memcached_DataObject
!self::exists($other, $subscriber) &&
!$subscriber->hasBlocked($other)) {
- $auto = new Subscription();
-
- $auto->subscriber = $subscriber->id;
- $auto->subscribed = $other->id;
- $auto->created = common_sql_now();
-
- $result = $auto->insert();
-
- if (!$result) {
- common_log_db_error($auto, 'INSERT', __FILE__);
- throw new Exception(_('Could not save subscription.'));
+ try {
+ self::start($other, $subscriber);
+ } catch (Exception $e) {
+ common_log(LOG_ERR, "Exception during autosubscribe of {$other->nickname} to profile {$subscriber->id}: {$e->getMessage()}");
}
-
- $auto->notify();
}
Event::handle('EndSubscribe', array($subscriber, $other));
@@ -125,6 +111,30 @@ class Subscription extends Memcached_DataObject
return true;
}
+ /**
+ * Low-level subscription save.
+ * Outside callers should use Subscription::start()
+ */
+ protected function saveNew($subscriber_id, $other_id)
+ {
+ $sub = new Subscription();
+
+ $sub->subscriber = $subscriber_id;
+ $sub->subscribed = $other_id;
+ $sub->jabber = 1;
+ $sub->sms = 1;
+ $sub->created = common_sql_now();
+
+ $result = $sub->insert();
+
+ if (!$result) {
+ common_log_db_error($sub, 'INSERT', __FILE__);
+ throw new Exception(_('Could not save subscription.'));
+ }
+
+ return $sub;
+ }
+
function notify()
{
# XXX: add other notifications (Jabber, SMS) here
@@ -203,8 +213,8 @@ class Subscription extends Memcached_DataObject
self::blow('user:notices_with_friends:%d', $subscriber->id);
- $subscriber->blowSubscriptionsCount();
- $other->blowSubscribersCount();
+ $subscriber->blowSubscriptionCount();
+ $other->blowSubscriberCount();
Event::handle('EndUnsubscribe', array($subscriber, $other));
}
diff --git a/classes/User.php b/classes/User.php
index 357c1e501..2abb7eeb6 100644
--- a/classes/User.php
+++ b/classes/User.php
@@ -75,7 +75,11 @@ class User extends Memcached_DataObject
function getProfile()
{
- return Profile::staticGet('id', $this->id);
+ $profile = Profile::staticGet('id', $this->id);
+ if (empty($profile)) {
+ throw new UserNoProfileException($this);
+ }
+ return $profile;
}
function isSubscribed($other)
@@ -87,6 +91,7 @@ class User extends Memcached_DataObject
function updateKeys(&$orig)
{
+ $this->_connect();
$parts = array();
foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
if (strcmp($this->$k, $orig->$k) != 0) {
@@ -132,13 +137,15 @@ class User extends Memcached_DataObject
return !in_array($nickname, $blacklist);
}
- function getCurrentNotice($dt=null)
+ /**
+ * Get the most recent notice posted by this user, if any.
+ *
+ * @return mixed Notice or null
+ */
+ function getCurrentNotice()
{
$profile = $this->getProfile();
- if (!$profile) {
- return null;
- }
- return $profile->getCurrentNotice($dt);
+ return $profile->getCurrentNotice();
}
function getCarrier()
@@ -146,19 +153,12 @@ class User extends Memcached_DataObject
return Sms_carrier::staticGet('id', $this->carrier);
}
+ /**
+ * @deprecated use Subscription::start($sub, $other);
+ */
function subscribeTo($other)
{
- $sub = new Subscription();
- $sub->subscriber = $this->id;
- $sub->subscribed = $other->id;
-
- $sub->created = common_sql_now(); // current time
-
- if (!$sub->insert()) {
- return false;
- }
-
- return true;
+ return Subscription::start($this->getProfile(), $other);
}
function hasBlocked($other)
@@ -339,17 +339,7 @@ class User extends Memcached_DataObject
common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
__FILE__);
} else {
- $defsub = new Subscription();
- $defsub->subscriber = $user->id;
- $defsub->subscribed = $defuser->id;
- $defsub->created = $user->created;
-
- $result = $defsub->insert();
-
- if (!$result) {
- common_log_db_error($defsub, 'INSERT', __FILE__);
- return false;
- }
+ Subscription::start($user, $defuser);
}
}
@@ -465,26 +455,18 @@ class User extends Memcached_DataObject
function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
$profile = $this->getProfile();
- if (!$profile) {
- return null;
- } else {
- return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
- }
+ return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
}
function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
{
$profile = $this->getProfile();
- if (!$profile) {
- return null;
- } else {
- return $profile->getNotices($offset, $limit, $since_id, $before_id);
- }
+ return $profile->getNotices($offset, $limit, $since_id, $before_id);
}
- function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE, $own=false)
+ function favoriteNotices($own=false, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
{
- $ids = Fave::stream($this->id, $offset, $limit, $own);
+ $ids = Fave::stream($this->id, $offset, $limit, $own, $since_id, $max_id);
return Notice::getStreamByIds($ids);
}
@@ -543,8 +525,8 @@ class User extends Memcached_DataObject
common_log(LOG_WARNING,
sprintf(
"Profile ID %d (%s) tried to block his or herself.",
- $profile->id,
- $profile->nickname
+ $this->id,
+ $this->nickname
)
);
return false;
@@ -566,12 +548,9 @@ class User extends Memcached_DataObject
return false;
}
- // Cancel their subscription, if it exists
-
- $otherUser = User::staticGet('id', $other->id);
-
- if (!empty($otherUser)) {
- subs_unsubscribe_to($otherUser, $this->getProfile());
+ $self = $this->getProfile();
+ if (Subscription::exists($other, $self)) {
+ Subscription::cancel($other, $self);
}
$block->query('COMMIT');
@@ -613,41 +592,19 @@ class User extends Memcached_DataObject
function getGroups($offset=0, $limit=null)
{
- $qry =
- 'SELECT user_group.* ' .
- 'FROM user_group JOIN group_member '.
- 'ON user_group.id = group_member.group_id ' .
- 'WHERE group_member.profile_id = %d ' .
- 'ORDER BY group_member.created DESC ';
-
- if ($offset>0 && !is_null($limit)) {
- if ($offset) {
- if (common_config('db','type') == 'pgsql') {
- $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
- } else {
- $qry .= ' LIMIT ' . $offset . ', ' . $limit;
- }
- }
- }
-
- $groups = new User_group();
-
- $cnt = $groups->query(sprintf($qry, $this->id));
-
- return $groups;
+ $profile = $this->getProfile();
+ return $profile->getGroups($offset, $limit);
}
function getSubscriptions($offset=0, $limit=null)
{
$profile = $this->getProfile();
- assert(!empty($profile));
return $profile->getSubscriptions($offset, $limit);
}
function getSubscribers($offset=0, $limit=null)
{
$profile = $this->getProfile();
- assert(!empty($profile));
return $profile->getSubscribers($offset, $limit);
}
@@ -710,9 +667,11 @@ class User extends Memcached_DataObject
function delete()
{
- $profile = $this->getProfile();
- if ($profile) {
+ try {
+ $profile = $this->getProfile();
$profile->delete();
+ } catch (UserNoProfileException $unp) {
+ common_log(LOG_INFO, "User {$this->nickname} has no profile; continuing deletion.");
}
$related = array('Fave',
@@ -721,6 +680,7 @@ class User extends Memcached_DataObject
'Foreign_link',
'Invitation',
);
+
Event::handle('UserDeleteRelated', array($this, &$related));
foreach ($related as $cls) {
diff --git a/classes/User_group.php b/classes/User_group.php
index e92887474..e04c46626 100644
--- a/classes/User_group.php
+++ b/classes/User_group.php
@@ -154,6 +154,21 @@ class User_group extends Memcached_DataObject
return $members;
}
+ function getMemberCount()
+ {
+ // XXX: WORM cache this
+
+ $members = $this->getMembers();
+ $member_count = 0;
+
+ /** $member->count() doesn't work. */
+ while ($members->fetch()) {
+ $member_count++;
+ }
+
+ return $member_count;
+ }
+
function getAdmins($offset=0, $limit=null)
{
$qry =
@@ -279,12 +294,26 @@ class User_group extends Memcached_DataObject
return true;
}
- static function getForNickname($nickname)
+ static function getForNickname($nickname, $profile=null)
{
$nickname = common_canonical_nickname($nickname);
- $group = User_group::staticGet('nickname', $nickname);
+
+ // Are there any matching remote groups this profile's in?
+ if ($profile) {
+ $group = $profile->getGroups();
+ while ($group->fetch()) {
+ if ($group->nickname == $nickname) {
+ // @fixme is this the best way?
+ return clone($group);
+ }
+ }
+ }
+
+ // If not, check local groups.
+
+ $group = Local_group::staticGet('nickname', $nickname);
if (!empty($group)) {
- return $group;
+ return User_group::staticGet('id', $group->group_id);
}
$alias = Group_alias::staticGet('alias', $nickname);
if (!empty($alias)) {
@@ -357,16 +386,15 @@ class User_group extends Memcached_DataObject
if ($source) {
$xs->elementStart('source');
+ $xs->element('id', null, $this->permalink());
$xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
$xs->element('link', array('href' => $this->permalink()));
- }
-
- if ($source) {
+ $xs->element('updated', null, $this->modified);
$xs->elementEnd('source');
}
$xs->element('title', null, $this->nickname);
- $xs->element('summary', null, $this->description);
+ $xs->element('summary', null, common_xml_safe_str($this->description));
$xs->element('link', array('rel' => 'alternate',
'href' => $this->permalink()));
@@ -376,7 +404,11 @@ class User_group extends Memcached_DataObject
$xs->element('published', null, common_date_w3dtf($this->created));
$xs->element('updated', null, common_date_w3dtf($this->modified));
- $xs->element('content', array('type' => 'html'), $this->description);
+ $xs->element(
+ 'content',
+ array('type' => 'html'),
+ common_xml_safe_str($this->description)
+ );
$xs->elementEnd('entry');
@@ -442,6 +474,11 @@ class User_group extends Memcached_DataObject
$group->query('BEGIN');
+ if (empty($uri)) {
+ // fill in later...
+ $uri = null;
+ }
+
$group->nickname = $nickname;
$group->fullname = $fullname;
$group->homepage = $homepage;
diff --git a/classes/User_username.php b/classes/User_username.php
index 853fd5cb8..8d99cddd3 100644
--- a/classes/User_username.php
+++ b/classes/User_username.php
@@ -55,7 +55,7 @@ class User_username extends Memcached_DataObject
// now define the keys.
function keys() {
- return array('provider_name', 'username');
+ return array('provider_name' => 'K', 'username' => 'K');
}
}