diff options
| author | Michael Foster <savetheinternet@tinyboard.org> | 2013-08-19 01:33:30 +1000 |
|---|---|---|
| committer | Michael Foster <savetheinternet@tinyboard.org> | 2013-08-19 01:33:30 +1000 |
| commit | bb300c5d38285ea54a0d898023f24ca5d180ea9b (patch) | |
| tree | 9a0b9fd5cd01733d59f7144fb59359229ffd0352 /tools | |
| parent | d6526d27da4b5687749bdc3ce0b8c0611e72517d (diff) | |
tools/delete-stray-images.php
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/delete-stray-images.php | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tools/delete-stray-images.php b/tools/delete-stray-images.php new file mode 100644 index 00000000..cf0dff46 --- /dev/null +++ b/tools/delete-stray-images.php @@ -0,0 +1,61 @@ +#!/usr/bin/php +<?php +/* + * delete-stray-images.php - there was a period when undoImage() was not working at all. This meant that + * if an error occured while uploading an image, the uploaded images might not have been deleted. + * + * This script iterates through every board and deletes any stray files in src/ or thumb/ that don't + * exist in the database. + * + */ + +require dirname(__FILE__) . '/inc/cli.php'; + + +$boards = listBoards(); + +foreach ($boards as $board) { + echo "/{$board['uri']}/... "; + + openBoard($board['uri']); + + $query = query(sprintf("SELECT `file`, `thumb` FROM ``posts_%s`` WHERE `file` IS NOT NULL", $board['uri'])); + $valid_src = array(); + $valid_thumb = array(); + + while ($post = $query->fetch(PDO::FETCH_ASSOC)) { + $valid_src[] = $post['file']; + $valid_thumb[] = $post['thumb']; + } + + $files_src = array_map('basename', glob($board['dir'] . $config['dir']['img'] . '*')); + $files_thumb = array_map('basename', glob($board['dir'] . $config['dir']['thumb'] . '*')); + + $stray_src = array_diff($files_src, $valid_src); + $stray_thumb = array_diff($files_thumb, $valid_thumb); + + $stats = array( + 'deleted' => 0, + 'size' => 0 + ); + + foreach ($stray_src as $src) { + $stats['deleted']++; + $stats['size'] = filesize($board['dir'] . $config['dir']['img'] . $src); + if (!file_unlink($board['dir'] . $config['dir']['img'] . $src)) { + $er = error_get_last(); + die("error: " . $er['message'] . "\n"); + } + } + + foreach ($stray_src as $thumb) { + $stats['deleted']++; + $stats['size'] = filesize($board['dir'] . $config['dir']['thumb'] . $thumb); + if (!file_unlink($board['dir'] . $config['dir']['thumb'] . $src)) { + $er = error_get_last(); + die("error: " . $er['message'] . "\n"); + } + } + + echo sprintf("deleted %s files (%s)\n", $stats['deleted'], format_bytes($stats['size'])); +} |
