From 5be705ca2abbf2ad9a514de0e2dde748fadfa5f7 Mon Sep 17 00:00:00 2001 From: Miguel Dantas Date: Sun, 14 Jul 2019 00:05:01 +0100 Subject: [PATCH] [Embed] Updated fixup_files.php script to be able to fix broken oEmbedinfo using the --broken-oembed flag --- plugins/Embed/scripts/fixup_files.php | 166 ++++++++++++++++++++------ 1 file changed, 130 insertions(+), 36 deletions(-) diff --git a/plugins/Embed/scripts/fixup_files.php b/plugins/Embed/scripts/fixup_files.php index cee01ccb5c..0697714b94 100755 --- a/plugins/Embed/scripts/fixup_files.php +++ b/plugins/Embed/scripts/fixup_files.php @@ -16,7 +16,7 @@ // along with GNU social. If not, see . /** - * OembedPlugin implementation for GNU social + * EmbedPlugin implementation for GNU social * * @package GNUsocial * @author Mikael Nordfeldth @@ -25,60 +25,154 @@ * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -defined('GNUSOCIAL') || die(); +defined('GNUSOCIAL'); define('INSTALLDIR', realpath(__DIR__ . '/../../..')); -$longoptions = array('dry-run'); +$longoptions = ['dry-run', 'h-bug', 'broken-oembed', 'limit=']; $helptext = <<title = 'h'; -$f->mimetype = 'h'; -$f->size = 0; -$f->protected = 0; -$f->find(); -echo "Found $f->N bad items:\n"; +if ($h_bug) { + $query .= " file.title = 'h' + AND file.mimetype = 'h' + AND file.size = 0 + AND file.protected = 0"; +} elseif ($broken) { + $query .= " file.filename is NULL"; +} -while ($f->fetch()) { - echo "$f->id $f->url"; +$query .= empty($limit) ? "" : " AND notice.modified >= '{$limit}' ORDER BY notice.modified ASC"; - $data = File_redirection::lookupWhere($f->url); - if ($dry) { - if (is_array($data)) { - echo " (unchanged)\n"; - } else { - echo " (unchanged, but embedding lookup failed)\n"; +// echo $query; + +$fn = new DB_DataObject(); +$fn->query($query); + +if ($h_bug) { + echo "Found {$fn->N} bad items:\n"; +} else { + echo "Found {$fn->N} files.\n"; +} + +while ($fn->fetch()) { + $f = File::getByID($fn->file_id); + + try { + $data = File_embed::getByFile($f); + } catch (Exception $e) { + // Carry on + } + + if ($broken && $data instanceof File_embed) { + try { + $thumb = File_thumbnail::byFile($f, true /* not null url */); + $thumb->getPath(); // Check we have the file + } catch (Exception $e) { + $no_thumb = true; + // Doesn't exist, no problem } - } else { - // NULL out the mime/title/size/protected fields - $sql = sprintf( - "UPDATE file " . - "SET mimetype=null,title=null,size=null,protected=null " . - "WHERE id=%d", - $f->id - ); - $f->query($sql); - $f->decache(); + } - if (is_array($data)) { - Event::handle('EndFileSaveNew', array($f, $data, $f->url)); - echo " (ok)\n"; - } else { - echo " (ok, but embedding lookup failed)\n"; + + if ($h_bug) { + echo "ID: {$f->id}, URL {$f->url}"; + + if ($dry) { + if ($data instanceof File_embed) { + echo " (unchanged)\n"; + } else { + echo " (unchanged, but embedding lookup failed)\n"; + } + } elseif (!$dry) { + $sql = "UPDATE file " . + "SET mimetype=null, title=null,size=null,protected=null " . + "WHERE id={$f->id}"; + $f->query($sql); + $f->decache(); + if ($data instanceof File_embed) { + $fetch = true; + echo " (ok)\n"; + } else { + echo " (ok, but embedding lookup failed)\n"; + } } + + } elseif ($broken && + (!$data instanceof File_embed || + empty($data->title) || + empty($f->title) + || + ($thumb instanceof File_thumbnail && empty($thumb->filename)) + )) { + + // print_r($thumb); + + if (!$dry) { + echo "Will refetch for file with "; + } else { + echo "Found broken file with "; + } + + echo "ID: {$f->id}, URL {$f->url}\n"; + if (!$dry) { + $fetch = true; + $sql = "UPDATE file SET title=null, size=null, protected=null " . + "WHERE id={$f->id}"; + $f->query($sql); + $f->decache(); + + if ($data instanceof File_embed) { + $data->delete(); + $data->decache(); + } + + if ($thumb instanceof File_thumbnail) { + // Delete all thumbnails, not just this one + $f->query("DELETE from file_thumbnail WHERE file_id = {$f->id}"); + $thumb->decache(); + } + } + + } + + if ($fetch === true && !$dry) { + $fetch = false; + echo "Attempting to fetch Embed data\n"; + Event::handle('EndFileSaveNew', array($f)); } } -echo "done.\n"; +echo "Done.\n";