#!/usr/bin/env php . /** * EmbedPlugin implementation for GNU social * * @package GNUsocial * @author Mikael Nordfeldth * @author Diogo Cordeiro * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ define('INSTALLDIR', dirname(__DIR__, 3)); define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public'); $longoptions = ['dry-run', 'h-bug', 'broken-oembed', 'limit=']; $helptext = <<= '{$limit}' ORDER BY notice.modified ASC"; // 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}"; if ($dry) { if ($data instanceof File_embed) { echo " (unchanged)\n"; } else { echo " (unchanged, but embedding lookup failed)\n"; } } elseif (!$dry) { $f->query(sprintf( <<<'END' UPDATE file SET mimetype = NULL, title = NULL, size = NULL, protected = NULL, modified = CURRENT_TIMESTAMP WHERE id = %d END, $f->getID() )); $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->getID()}, URL {$f->url}\n"; if (!$dry) { $fetch = true; $f->query(sprintf( <<<'END' UPDATE file SET title = NULL, size = NULL, protected = NULL, modified = CURRENT_TIMESTAMP WHERE id = %d, END, $f->getID() )); $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->getID()}"); $thumb->decache(); } } } if (isset($fetch) && $fetch === true && !$dry) { $fetch = false; echo "Attempting to fetch Embed data\n"; Event::handle('EndFileSaveNew', array($f)); } } echo "Done.\n";