Last active
December 13, 2015 17:08
-
-
Save mrvdb/4945701 to your computer and use it in GitHub Desktop.
Statusnet script to clean up db and filesystem avatars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/* | |
Check avatars in the database with the filesystem, clean as necessary | |
*/ | |
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); | |
$longoptions = array('dry-run','skip-db','skip-fs'); | |
$helptext = <<<END_OF_USERROLE_HELP | |
fixup_avatars.php [options] | |
Check and fixes avatars in the database vs the ones on file | |
--dry-run look but dont touch | |
--skip-db skip removing db entries | |
--skip-fs skip removing filesystem entries | |
END_OF_USERROLE_HELP; | |
require_once INSTALLDIR.'/scripts/commandline.inc'; | |
function main() | |
{ | |
$dry = have_option('dry-run'); | |
if (!have_option('skip-db')) | |
fixup_Avatars_DB($dry); | |
if (!have_option('skip-fs')) | |
fixup_Avatars_FS($dry); | |
printfnq("DONE\n"); | |
} | |
// Loop over all db avatar registrations, check for corresponding file | |
// If the files are missing, remove the db entries, so the avatar gets | |
// recreated on syncing. | |
function fixup_Avatars_DB($dry=TRUE) { | |
printfnq("Checking avatar files with db entries as reference." . ($dry ? "(dry-run)\n":"\n")); | |
$avatar = new Avatar(); | |
if($avatar->find()) { | |
while($avatar->fetch()) { | |
// Check if the avatar is really on the filesystem | |
$path = $avatar->path($avatar->filename); | |
if (!file_exists($path)) { | |
printfnq("Avatar: ".$path." is missing."); | |
// Remove this avatar from the database, it will be put back on sync | |
if(!$dry) { | |
// This removes db entry and file | |
$avatar->delete(); | |
printfnq(" Removed db entry."); | |
} else { | |
printfnq(" No action (dry-run)"); | |
} | |
printfnq("DONE\n"); | |
} | |
} | |
} else { | |
printfnq("No result from avatar->find\n"); | |
} | |
} | |
// Loop over all avatar files, check for a corresponding db entry If | |
// there is none, the file is redundant and can be removed | |
function fixup_Avatars_FS($dry=TRUE) { | |
printfnq("Checking avatar db entries with files as reference." . ($dry ? "(dry-run)\n" : "\n")); | |
// Fill an array with all group avatars, so we do not delete them | |
// Unfortunately, too late for my instance, grrr... | |
$group_avatars = array(); | |
$group = new User_group(); | |
$group->find(); | |
while($group->fetch()) { | |
$group_avatars[basename($group->original_logo)] = TRUE; | |
$group_avatars[basename($group->homepage_logo)] = TRUE; | |
$group_avatars[basename($group->stream_logo)] = TRUE; | |
$group_avatars[basename($group->mini_logo)] = TRUE; | |
} | |
$avatar = new Avatar(); | |
$files = new DirectoryIterator($avatar->path('')); | |
foreach ($files as $info) { | |
if($info->isDot()) continue; | |
// Construct url as in db, that is an indexed unique field | |
$file = $info->getFilename(); | |
$avatar->url = $avatar->url($file); | |
// Do not use find() here, it consumes waay to much memory (remember: > 300k avatars!) | |
if(!$avatar->count()) { | |
printfnq("Avatar: ".$file. " has no db entry."); | |
// Is the file a group avatar? | |
if (!isset($group_avatars[$file])) { | |
if(!$dry) { | |
unlink($avatar->path($file)); | |
printfnq(" (deleted file)\n"); | |
} else { | |
printfnq(" (dry-run)\n"); | |
} | |
} else { | |
printfnq(" (group avatar, no action)\n"); | |
} | |
} | |
} | |
} | |
main(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment