. * */ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Core\Worker; use Friendica\Database\DBA; use Friendica\Model\Photo; /** * Removes cached avatars from public contacts that aren't in use */ class RemoveUnusedAvatars { public static function execute() { $condition = ["`uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?) AND `id` IN (SELECT `contact-id` FROM `photo`) AND NOT `id` IN (SELECT `author-id` FROM `post-user`) AND NOT `id` IN (SELECT `owner-id` FROM `post-user`) AND NOT `id` IN (SELECT `causer-id` FROM `post-user`) AND NOT `id` IN (SELECT `cid` FROM `post-tag`) AND NOT `id` IN (SELECT `contact-id` FROM `post-user`)", 0, 0]; $total = DBA::count('contact', $condition); Logger::notice('Starting removal', ['total' => $total]); $count = 0; $contacts = DBA::select('contact', ['id'], $condition); while ($contact = DBA::fetch($contacts)) { DBA::update('contact', ['photo' => '', 'thumb' => '', 'micro' => ''], ['id' => $contact['id']]); Photo::delete(['contact-id' => $contact['id'], 'album' => Photo::CONTACT_PHOTOS]); if ((++$count % 1000) == 0) { if (!Worker::isInMaintenanceWindow()) { Logger::notice('We are outside of the maintenance window, quitting'); return; } Logger::notice('In removal', ['count' => $count, 'total' => $total]); } } DBA::close($contacts); Logger::notice('Removal done', ['count' => $count, 'total' => $total]); } }