From: Mikael Nordfeldth Date: Thu, 4 Jun 2015 15:36:11 +0000 (+0200) Subject: Send objects instead of integers to File_to_post::processNew X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=fe6498e7c875f5b386a9c7d2cc1fba5677daae09;p=quix0rs-gnu-social.git Send objects instead of integers to File_to_post::processNew --- diff --git a/classes/File.php b/classes/File.php index 76c00dc1f8..d4abbfddee 100644 --- a/classes/File.php +++ b/classes/File.php @@ -116,14 +116,14 @@ class File extends Managed_DataObject * * @fixme refactor this mess, it's gotten pretty scary. * @param string $given_url the URL we're looking at - * @param int $notice_id (optional) + * @param Notice $notice (optional) * @param bool $followRedirects defaults to true * * @return mixed File on success, -1 on some errors * * @throws ServerException on failure */ - public static function processNew($given_url, $notice_id=null, $followRedirects=true) { + public static function processNew($given_url, Notice $notice=null, $followRedirects=true) { if (empty($given_url)) { throw new ServerException('No given URL to process'); } @@ -181,7 +181,7 @@ class File extends Managed_DataObject // // Seen in the wild with clojure.org, which redirects through // wikispaces for auth and appends session data in the URL params. - $file = self::processNew($redir_url, $notice_id, /*followRedirects*/false); + $file = self::processNew($redir_url, $notice, /*followRedirects*/false); File_redirection::saveNew($redir_data, $file->id, $given_url); } @@ -193,8 +193,8 @@ class File extends Managed_DataObject } } - if (!empty($notice_id)) { - File_to_post::processNew($file->id, $notice_id); + if ($notice instanceof Notice) { + File_to_post::processNew($file, $notice); } return $file; } diff --git a/classes/File_to_post.php b/classes/File_to_post.php index b3c44d4a22..7f2aca3362 100644 --- a/classes/File_to_post.php +++ b/classes/File_to_post.php @@ -58,30 +58,29 @@ class File_to_post extends Managed_DataObject ); } - function processNew($file_id, $notice_id) { + function processNew(File $file, Notice $notice) { static $seen = array(); + + $file_id = $file->getID(); + $notice_id = $notice->getID(); + if (!array_key_exists($notice_id, $seen)) { + $seen[$notice_id] = array(); + } + if (empty($seen[$notice_id]) || !in_array($file_id, $seen[$notice_id])) { $f2p = File_to_post::pkeyGet(array('post_id' => $notice_id, 'file_id' => $file_id)); - if (empty($f2p)) { + if (!$f2p instanceof File_to_post) { $f2p = new File_to_post; $f2p->file_id = $file_id; $f2p->post_id = $notice_id; $f2p->insert(); - $f = File::getKV($file_id); - - if (!empty($f)) { - $f->blowCache(); - } + $file->blowCache(); } - if (empty($seen[$notice_id])) { - $seen[$notice_id] = array($file_id); - } else { - $seen[$notice_id][] = $file_id; - } + $seen[$notice_id][] = $file_id; } } diff --git a/classes/Notice.php b/classes/Notice.php index b8af0fac6d..f9d80c1289 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1109,7 +1109,7 @@ class Notice extends Managed_DataObject */ function saveUrls() { if (common_config('attachments', 'process_links')) { - common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id); + common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this); } } @@ -1126,11 +1126,7 @@ class Notice extends Managed_DataObject if (common_config('attachments', 'process_links')) { // @fixme validation? foreach (array_unique($urls) as $url) { - try { - File::processNew($url, $this->id); - } catch (ServerException $e) { - // Could not save URL. Log it? - } + $this->saveUrl($url, $this); } } } @@ -1138,9 +1134,9 @@ class Notice extends Managed_DataObject /** * @private callback */ - function saveUrl($url, $notice_id) { + function saveUrl($url, Notice $notice) { try { - File::processNew($url, $notice_id); + File::processNew($url, $notice); } catch (ServerException $e) { // Could not save URL. Log it? } diff --git a/lib/mediafile.php b/lib/mediafile.php index 546239ed7d..2b8f324df2 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -61,7 +61,7 @@ class MediaFile public function attachToNotice(Notice $notice) { - File_to_post::processNew($this->fileRecord->id, $notice->id); + File_to_post::processNew($this->fileRecord, $notice); } public function getPath() diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php index 07c9d1c182..4d1b95e2b7 100644 --- a/plugins/OStatus/classes/Ostatus_profile.php +++ b/plugins/OStatus/classes/Ostatus_profile.php @@ -691,8 +691,8 @@ class Ostatus_profile extends Managed_DataObject $options); if ($saved instanceof Notice) { Ostatus_source::saveNew($saved, $this, $method); - if (!empty($attachment)) { - File_to_post::processNew($attachment->id, $saved->id); + if ($attachment instanceof File) { + File_to_post::processNew($attachment, $saved); } } } catch (Exception $e) { diff --git a/plugins/TwitterBridge/lib/twitterimport.php b/plugins/TwitterBridge/lib/twitterimport.php index 5258bfc2c9..45b7547ce2 100644 --- a/plugins/TwitterBridge/lib/twitterimport.php +++ b/plugins/TwitterBridge/lib/twitterimport.php @@ -564,13 +564,13 @@ class TwitterImport * @param Notice $notice * @param object $status */ - function saveStatusAttachments($notice, $status) + function saveStatusAttachments(Notice $notice, $status) { if (common_config('attachments', 'process_links')) { if (!empty($status->entities) && !empty($status->entities->urls)) { foreach ($status->entities->urls as $url) { try { - File::processNew($url->url, $notice->id); + File::processNew($url->url, $notice); } catch (ServerException $e) { // Could not process attached URL }