*
* @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');
}
//
// 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);
}
}
}
- if (!empty($notice_id)) {
- File_to_post::processNew($file->id, $notice_id);
+ if ($notice instanceof Notice) {
+ File_to_post::processNew($file, $notice);
}
return $file;
}
);
}
- 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;
}
}
*/
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);
}
}
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);
}
}
}
/**
* @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?
}
public function attachToNotice(Notice $notice)
{
- File_to_post::processNew($this->fileRecord->id, $notice->id);
+ File_to_post::processNew($this->fileRecord, $notice);
}
public function getPath()
$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) {
* @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
}