From 73fe8a81b7def91113332a3ec55567d67b25f2d5 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sun, 10 Nov 2013 14:33:45 +0100 Subject: [PATCH] Slight cleanup in typing and syntax of File and File_* classes --- classes/File.php | 26 ++++++++++++-------------- classes/File_oembed.php | 11 +++++++---- classes/File_redirection.php | 14 +++++--------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/classes/File.php b/classes/File.php index e3300e7e68..a5914b506e 100644 --- a/classes/File.php +++ b/classes/File.php @@ -17,14 +17,7 @@ * along with this program. If not, see . */ -if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } - -require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; -require_once INSTALLDIR.'/classes/File_redirection.php'; -require_once INSTALLDIR.'/classes/File_oembed.php'; -require_once INSTALLDIR.'/classes/File_thumbnail.php'; -require_once INSTALLDIR.'/classes/File_to_post.php'; -//require_once INSTALLDIR.'/classes/File_redirection.php'; +if (!defined('GNUSOCIAL')) { exit(1); } /** * Table Definition for file @@ -88,7 +81,7 @@ class File extends Managed_DataObject $x = File::getKV('url', $given_url); - if (empty($x)) { + if (!$x instanceof File) { $x = new File; $x->url = $given_url; if (!empty($redir_data['protected'])) $x->protected = $redir_data['protected']; @@ -116,16 +109,21 @@ class File extends Managed_DataObject public function saveOembed(array $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))) { + && (('text/html' === substr($redir_data['type'], 0, 9) + || 'application/xhtml+xml' === substr($redir_data['type'], 0, 21)))) { + try { + $oembed_data = File_oembed::_getOembed($given_url); + } catch (Exception $e) { + return false; + } $fo = File_oembed::getKV('file_id', $this->id); - if (empty($fo)) { + if ($fo instanceof File_oembed) { + common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file $file_id", __FILE__); + } else { 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 false; diff --git a/classes/File_oembed.php b/classes/File_oembed.php index a250a1cc3b..460722c799 100644 --- a/classes/File_oembed.php +++ b/classes/File_oembed.php @@ -98,6 +98,9 @@ class File_oembed extends Managed_DataObject function saveNew($data, $file_id) { $file_oembed = new File_oembed; $file_oembed->file_id = $file_id; + if (!isset($data->version)) { + common_debug('DEBUGGING oEmbed: data->version undefined in variable $data: '.var_export($data, true)); + } $file_oembed->version = $data->version; $file_oembed->type = $data->type; if (!empty($data->provider_name)) $file_oembed->provider = $data->provider_name; @@ -114,7 +117,9 @@ class File_oembed extends Managed_DataObject $given_url = File_redirection::_canonUrl($file_oembed->url); if (! empty($given_url)){ $file = File::getKV('url', $given_url); - if (empty($file)) { + if ($file instanceof File) { + $file_oembed->mimetype = $file->mimetype; + } else { $file_redir = File_redirection::getKV('url', $given_url); if (empty($file_redir)) { $redir_data = File_redirection::where($given_url); @@ -122,15 +127,13 @@ class File_oembed extends Managed_DataObject } else { $file_id = $file_redir->file_id; } - } else { - $file_oembed->mimetype=$file->mimetype; } } } $file_oembed->insert(); if (!empty($data->thumbnail_url) || ($data->type == 'photo')) { $ft = File_thumbnail::getKV('file_id', $file_id); - if (!empty($ft)) { + if ($ft instanceof File_thumbnail) { common_log(LOG_WARNING, "Strangely, a File_thumbnail object exists for new file $file_id", __FILE__); } else { diff --git a/classes/File_redirection.php b/classes/File_redirection.php index 00a5216505..42021668d9 100644 --- a/classes/File_redirection.php +++ b/classes/File_redirection.php @@ -17,11 +17,7 @@ * along with this program. If not, see . */ -if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } - -require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; -require_once INSTALLDIR.'/classes/File.php'; -require_once INSTALLDIR.'/classes/File_oembed.php'; +if (!defined('GNUSOCIAL')) { exit(1); } /** * Table Definition for file_redirection @@ -251,7 +247,9 @@ class File_redirection extends Managed_DataObject $short_url = (string)$short_url; // store it $file = File::getKV('url', $long_url); - if (empty($file)) { + if ($file instanceof File) { + $file_id = $file->id; + } else { // Check if the target URL is itself a redirect... $redir_data = File_redirection::where($long_url); if (is_array($redir_data)) { @@ -275,11 +273,9 @@ class File_redirection extends Managed_DataObject } $file_id = $file->id; } - } else { - $file_id = $file->id; } $file_redir = File_redirection::getKV('url', $short_url); - if (empty($file_redir)) { + if (!$file_redir instanceof File_redirection) { $file_redir = new File_redirection; $file_redir->url = $short_url; $file_redir->file_id = $file_id; -- 2.39.5