[Embed] Updated fixup_files.php script to be able to fix broken oEmbedinfo using the --broken-oembed flag

This commit is contained in:
Miguel Dantas 2019-07-14 00:05:01 +01:00 committed by Diogo Cordeiro
parent 07b4cfaeae
commit 5be705ca2a

View File

@ -16,7 +16,7 @@
// along with GNU social. If not, see <http://www.gnu.org/licenses/>. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/** /**
* OembedPlugin implementation for GNU social * EmbedPlugin implementation for GNU social
* *
* @package GNUsocial * @package GNUsocial
* @author Mikael Nordfeldth * @author Mikael Nordfeldth
@ -25,60 +25,154 @@
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
defined('GNUSOCIAL') || die(); defined('GNUSOCIAL');
define('INSTALLDIR', realpath(__DIR__ . '/../../..')); define('INSTALLDIR', realpath(__DIR__ . '/../../..'));
$longoptions = array('dry-run'); $longoptions = ['dry-run', 'h-bug', 'broken-oembed', 'limit='];
$helptext = <<<END_OF_USERROLE_HELP $helptext = <<<END_OF_USERROLE_HELP
fixup_files.php [options] fixup_files.php [options]
Patches up file entries with corrupted types and titles (the "h bug"). Patches attachments with broken oembed.
--dry-run look but don't touch --dry-run look but don't touch
--h-bug Patches up file entries with corrupted types and titles (the "h bug")
--broken-oembed Attempts refecting info for broken attachments
--limit [date] Only affect files from after this date. This is a timestamp, format is: yyyy-mm-dd (optional time hh:mm:ss may be provided)
END_OF_USERROLE_HELP; END_OF_USERROLE_HELP;
require_once INSTALLDIR.'/scripts/commandline.inc'; require_once INSTALLDIR.'/scripts/commandline.inc';
$dry = have_option('dry-run'); $dry = have_option('dry-run');
$h_bug = have_option('h-bug');
$broken = have_option('broken-oembed');
$limit = get_option_value('limit');
if (!($broken ^ $h_bug)) {
echo "Exactly one of --h-bug and --broken-oembed are required\n";
die();
}
$query = "
SELECT DISTINCT
file_to_post.file_id
FROM
file_to_post
INNER JOIN
file ON file.id = file_to_post.file_id
INNER JOIN
notice ON notice.id = file_to_post.post_id
WHERE";
$f = new File(); $f = new File();
$f->title = 'h'; if ($h_bug) {
$f->mimetype = 'h'; $query .= " file.title = 'h'
$f->size = 0; AND file.mimetype = 'h'
$f->protected = 0; AND file.size = 0
$f->find(); AND file.protected = 0";
echo "Found $f->N bad items:\n"; } elseif ($broken) {
$query .= " file.filename is NULL";
}
while ($f->fetch()) { $query .= empty($limit) ? "" : " AND notice.modified >= '{$limit}' ORDER BY notice.modified ASC";
echo "$f->id $f->url";
// 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
}
}
if ($h_bug) {
echo "ID: {$f->id}, URL {$f->url}";
$data = File_redirection::lookupWhere($f->url);
if ($dry) { if ($dry) {
if (is_array($data)) { if ($data instanceof File_embed) {
echo " (unchanged)\n"; echo " (unchanged)\n";
} else { } else {
echo " (unchanged, but embedding lookup failed)\n"; echo " (unchanged, but embedding lookup failed)\n";
} }
} else { } elseif (!$dry) {
// NULL out the mime/title/size/protected fields $sql = "UPDATE file " .
$sql = sprintf( "SET mimetype=null, title=null,size=null,protected=null " .
"UPDATE file " . "WHERE id={$f->id}";
"SET mimetype=null,title=null,size=null,protected=null " .
"WHERE id=%d",
$f->id
);
$f->query($sql); $f->query($sql);
$f->decache(); $f->decache();
if ($data instanceof File_embed) {
if (is_array($data)) { $fetch = true;
Event::handle('EndFileSaveNew', array($f, $data, $f->url));
echo " (ok)\n"; echo " (ok)\n";
} else { } else {
echo " (ok, but embedding lookup failed)\n"; 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";