From 64288a62f816ed76114b456e07d3e515daddee85 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 11 Nov 2012 21:05:13 -0500 Subject: [PATCH 1/6] Adding a search notice stream object --- actions/apisearchjson.php | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/actions/apisearchjson.php b/actions/apisearchjson.php index 79b2666554..7ee353440f 100644 --- a/actions/apisearchjson.php +++ b/actions/apisearchjson.php @@ -89,6 +89,12 @@ class ApiSearchJSONAction extends ApiPrivateAuthAction $this->since_id = $this->trimmed('since_id'); $this->geocode = $this->trimmed('geocode'); + if (!empty($this->auth_user)) { + $this->auth_profile = $this->auth_user->getProfile(); + } else { + $this->auth_profile = null; + } + return true; } @@ -112,21 +118,13 @@ class ApiSearchJSONAction extends ApiPrivateAuthAction */ function showResults() { - // TODO: Support search operators like from: and to:, boolean, etc. - - $notice = new Notice(); - - // lcase it for comparison $q = strtolower($this->query); - $search_engine = $notice->getSearchEngine('notice'); - $search_engine->set_sort_mode('chron'); - $search_engine->limit(($this->page - 1) * $this->rpp, $this->rpp + 1, true); - if (false === $search_engine->query($q)) { - $cnt = 0; - } else { - $cnt = $notice->find(); - } + // TODO: Support search operators like from: and to:, boolean, etc. + + $stream = new SearchNoticeStream($q, $this->auth_profile); + + $notice = $stream->getNotices(($this->page - 1) * $this->rpp, $this->rpp + 1); // TODO: max_id, lang, geocode From edf28790211d707df05e65d158119b345c8b76d9 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 11 Nov 2012 21:27:11 -0500 Subject: [PATCH 2/6] Divert tag and url searches --- actions/apisearchjson.php | 60 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/actions/apisearchjson.php b/actions/apisearchjson.php index 7ee353440f..dcdf055c6f 100644 --- a/actions/apisearchjson.php +++ b/actions/apisearchjson.php @@ -122,9 +122,24 @@ class ApiSearchJSONAction extends ApiPrivateAuthAction // TODO: Support search operators like from: and to:, boolean, etc. - $stream = new SearchNoticeStream($q, $this->auth_profile); + if (preg_match('/^#([\pL\pN_\-\.]{1,64})/ue', $this->q)) { + $stream = new TagNoticeStream(substr($q, 1), $this->auth_profile); + } else if ($this->isAnURL($q)) { + $canon = File_redirection::_canonUrl($q); + $file = File::staticGet('url', $canon); + if (!empty($file)) { + $stream = new FileNoticeStream($file, $this->auth_profile); + } + } else { + $stream = new SearchNoticeStream($q, $this->auth_profile); + } - $notice = $stream->getNotices(($this->page - 1) * $this->rpp, $this->rpp + 1); + if (empty($stream)) { + // XXX: This is hackish, but need some simple way to say "There's no results" + $notice = new ArrayWrapper(array()); + } else { + $notice = $stream->getNotices(($this->page - 1) * $this->rpp, $this->rpp + 1); + } // TODO: max_id, lang, geocode @@ -135,6 +150,47 @@ class ApiSearchJSONAction extends ApiPrivateAuthAction $this->endDocument('json'); } + function isAnURL($q) { + $regex = '#^'. + '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'. + '('. + '(?:'. + '(?:'. //Known protocols + '(?:'. + '(?:(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|irc)://)'. + '|'. + '(?:(?:mailto|aim|tel|xmpp):)'. + ')'. + '(?:[\pN\pL\-\_\+\%\~]+(?::[\pN\pL\-\_\+\%\~]+)?\@)?'. //user:pass@ + '(?:'. + '(?:'. + '\[[\pN\pL\-\_\:\.]+(? Date: Sun, 11 Nov 2012 21:34:40 -0500 Subject: [PATCH 3/6] Bad caching parameter --- lib/filenoticestream.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filenoticestream.php b/lib/filenoticestream.php index fba6183fc2..6dd5a84ff0 100644 --- a/lib/filenoticestream.php +++ b/lib/filenoticestream.php @@ -42,7 +42,7 @@ class FileNoticeStream extends ScopingNoticeStream $profile = Profile::current(); } parent::__construct(new CachingNoticeStream(new RawFileNoticeStream($file), - 'file:notice-ids:'.$this->url), + 'file:notice-ids:'.$this->file->id), $profile); } } From d06965603d413cd2bad6b0a5e1cc281e3cf3bb9d Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 11 Nov 2012 21:36:42 -0500 Subject: [PATCH 4/6] Still bad --- lib/filenoticestream.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/filenoticestream.php b/lib/filenoticestream.php index 6dd5a84ff0..f7bca1ed68 100644 --- a/lib/filenoticestream.php +++ b/lib/filenoticestream.php @@ -42,7 +42,7 @@ class FileNoticeStream extends ScopingNoticeStream $profile = Profile::current(); } parent::__construct(new CachingNoticeStream(new RawFileNoticeStream($file), - 'file:notice-ids:'.$this->file->id), + 'file:notice-ids:'.$file->id), $profile); } } From 6aaee4e5fe8e934db9770858b8fbf9e8581cd1ee Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 11 Nov 2012 22:55:22 -0500 Subject: [PATCH 5/6] $this->q => $q --- actions/apisearchjson.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actions/apisearchjson.php b/actions/apisearchjson.php index dcdf055c6f..612dbdda5c 100644 --- a/actions/apisearchjson.php +++ b/actions/apisearchjson.php @@ -122,7 +122,7 @@ class ApiSearchJSONAction extends ApiPrivateAuthAction // TODO: Support search operators like from: and to:, boolean, etc. - if (preg_match('/^#([\pL\pN_\-\.]{1,64})/ue', $this->q)) { + if (preg_match('/^#([\pL\pN_\-\.]{1,64})$/ue', $q)) { $stream = new TagNoticeStream(substr($q, 1), $this->auth_profile); } else if ($this->isAnURL($q)) { $canon = File_redirection::_canonUrl($q); From 6f424eb80f49d1e7f3c8128126fbe9a63cb502ad Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 25 Nov 2012 10:39:49 -0500 Subject: [PATCH 6/6] If there's an exception in notice distribution, continue --- classes/Notice.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index 2c404e2550..bab9a0dbfd 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1031,9 +1031,15 @@ class Notice extends Managed_DataObject } foreach ($ni as $id => $source) { - $user = User::staticGet('id', $id); - if (empty($user) || $user->hasBlocked($profile) || - ($originalProfile && $user->hasBlocked($originalProfile))) { + try { + $user = User::staticGet('id', $id); + if (empty($user) || + $user->hasBlocked($profile) || + ($originalProfile && $user->hasBlocked($originalProfile))) { + unset($ni[$id]); + } + } catch (UserNoProfileException $e) { + // User doesn't have a profile; invalid; skip them. unset($ni[$id]); } }