Send objects instead of integers to File_to_post::processNew

This commit is contained in:
Mikael Nordfeldth 2015-06-04 17:36:11 +02:00
parent 5358fb3cce
commit fe6498e7c8
6 changed files with 25 additions and 30 deletions

View File

@ -116,14 +116,14 @@ class File extends Managed_DataObject
* *
* @fixme refactor this mess, it's gotten pretty scary. * @fixme refactor this mess, it's gotten pretty scary.
* @param string $given_url the URL we're looking at * @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 * @param bool $followRedirects defaults to true
* *
* @return mixed File on success, -1 on some errors * @return mixed File on success, -1 on some errors
* *
* @throws ServerException on failure * @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)) { if (empty($given_url)) {
throw new ServerException('No given URL to process'); 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 // Seen in the wild with clojure.org, which redirects through
// wikispaces for auth and appends session data in the URL params. // 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); File_redirection::saveNew($redir_data, $file->id, $given_url);
} }
@ -193,8 +193,8 @@ class File extends Managed_DataObject
} }
} }
if (!empty($notice_id)) { if ($notice instanceof Notice) {
File_to_post::processNew($file->id, $notice_id); File_to_post::processNew($file, $notice);
} }
return $file; return $file;
} }

View File

@ -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(); 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])) { if (empty($seen[$notice_id]) || !in_array($file_id, $seen[$notice_id])) {
$f2p = File_to_post::pkeyGet(array('post_id' => $notice_id, $f2p = File_to_post::pkeyGet(array('post_id' => $notice_id,
'file_id' => $file_id)); 'file_id' => $file_id));
if (empty($f2p)) { if (!$f2p instanceof File_to_post) {
$f2p = new File_to_post; $f2p = new File_to_post;
$f2p->file_id = $file_id; $f2p->file_id = $file_id;
$f2p->post_id = $notice_id; $f2p->post_id = $notice_id;
$f2p->insert(); $f2p->insert();
$f = File::getKV($file_id); $file->blowCache();
if (!empty($f)) {
$f->blowCache();
}
} }
if (empty($seen[$notice_id])) { $seen[$notice_id][] = $file_id;
$seen[$notice_id] = array($file_id);
} else {
$seen[$notice_id][] = $file_id;
}
} }
} }

View File

@ -1109,7 +1109,7 @@ class Notice extends Managed_DataObject
*/ */
function saveUrls() { function saveUrls() {
if (common_config('attachments', 'process_links')) { 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')) { if (common_config('attachments', 'process_links')) {
// @fixme validation? // @fixme validation?
foreach (array_unique($urls) as $url) { foreach (array_unique($urls) as $url) {
try { $this->saveUrl($url, $this);
File::processNew($url, $this->id);
} catch (ServerException $e) {
// Could not save URL. Log it?
}
} }
} }
} }
@ -1138,9 +1134,9 @@ class Notice extends Managed_DataObject
/** /**
* @private callback * @private callback
*/ */
function saveUrl($url, $notice_id) { function saveUrl($url, Notice $notice) {
try { try {
File::processNew($url, $notice_id); File::processNew($url, $notice);
} catch (ServerException $e) { } catch (ServerException $e) {
// Could not save URL. Log it? // Could not save URL. Log it?
} }

View File

@ -61,7 +61,7 @@ class MediaFile
public function attachToNotice(Notice $notice) public function attachToNotice(Notice $notice)
{ {
File_to_post::processNew($this->fileRecord->id, $notice->id); File_to_post::processNew($this->fileRecord, $notice);
} }
public function getPath() public function getPath()

View File

@ -691,8 +691,8 @@ class Ostatus_profile extends Managed_DataObject
$options); $options);
if ($saved instanceof Notice) { if ($saved instanceof Notice) {
Ostatus_source::saveNew($saved, $this, $method); Ostatus_source::saveNew($saved, $this, $method);
if (!empty($attachment)) { if ($attachment instanceof File) {
File_to_post::processNew($attachment->id, $saved->id); File_to_post::processNew($attachment, $saved);
} }
} }
} catch (Exception $e) { } catch (Exception $e) {

View File

@ -564,13 +564,13 @@ class TwitterImport
* @param Notice $notice * @param Notice $notice
* @param object $status * @param object $status
*/ */
function saveStatusAttachments($notice, $status) function saveStatusAttachments(Notice $notice, $status)
{ {
if (common_config('attachments', 'process_links')) { if (common_config('attachments', 'process_links')) {
if (!empty($status->entities) && !empty($status->entities->urls)) { if (!empty($status->entities) && !empty($status->entities->urls)) {
foreach ($status->entities->urls as $url) { foreach ($status->entities->urls as $url) {
try { try {
File::processNew($url->url, $notice->id); File::processNew($url->url, $notice);
} catch (ServerException $e) { } catch (ServerException $e) {
// Could not process attached URL // Could not process attached URL
} }