table('tiki_file_galleries'); $fileTable = TikiDb::get()->table('tiki_files'); $galleriesToDelete = []; $attachmentGalleries = $galleryTable->fetchAll( ['galleryId', 'name'], ['type' => 'attachments'] ); foreach ($attachmentGalleries as $gal) { $files = $fileTable->fetchCount(['galleryId' => $gal['galleryId']]); if (! $files) { $galleriesToDelete[] = $gal; echo "Attachment gallery: #{$gal['galleryId']} \"{$gal['name']}\" is empty, and will be removed\n"; ob_flush(); } } if ($galleriesToDelete) { $prompt = 'Are you sure you want to permanently remove all these (' . count($galleriesToDelete) . ') galleries? There is no undo... (y/n): '; if (readSTDIN($prompt, ['y', 'n']) == 'y') { echo "\n\n\nDeleting...\n\n"; foreach ($galleriesToDelete as $gal) { TikiLib::lib('filegal')->remove_file_gallery($gal['galleryId']); echo "Removed gallery: #{$gal['galleryId']} \"{$gal['name']}\"\n"; ob_flush(); } } } else { echo "No empty attachement galleries found\n"; ob_flush(); } $remaining = count($attachmentGalleries) - count($galleriesToDelete); echo "There are $remaining attachment galleries left that contain files.\n"; ob_flush(); } function readSTDIN($prompt, $valid_inputs, $default = '') { while (! isset($input) || (is_array($valid_inputs) && ! in_array($input, $valid_inputs)) || ($valid_inputs == 'is_file' && ! is_file($input))) { echo $prompt; ob_flush(); $input = strtolower(trim(fgets(STDIN))); if (empty($input) && ! empty($default)) { $input = $default; } } return $input; } removeEmptyAttachmentGals();