From: Mikael Nordfeldth Date: Sat, 30 May 2015 13:41:04 +0000 (+0200) Subject: scripts/nukefile.php for blasting crap from the server X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=3294d704a44203eb891d4b6485452fd16976ec2e;p=quix0rs-gnu-social.git scripts/nukefile.php for blasting crap from the server Deletes notices and the locally stored file based on File id, as you may want to just get rid of shit sometimes. --- diff --git a/classes/File.php b/classes/File.php index 594506449a..1cb7eab6b8 100644 --- a/classes/File.php +++ b/classes/File.php @@ -249,6 +249,15 @@ class File extends Managed_DataObject return true; } + public function getFilename() + { + if (!self::validFilename($this->filename)) { + // TRANS: Client exception thrown if a file upload does not have a valid name. + throw new ClientException(_("Invalid filename.")); + } + return $this->filename; + } + // where should the file go? static function filename(Profile $profile, $origname, $mimetype) diff --git a/classes/File_to_post.php b/classes/File_to_post.php index 4c751ae4f3..b3c44d4a22 100644 --- a/classes/File_to_post.php +++ b/classes/File_to_post.php @@ -85,6 +85,24 @@ class File_to_post extends Managed_DataObject } } + static function getNoticeIDsByFile(File $file) + { + $f2p = new File_to_post(); + + $f2p->selectAdd(); + $f2p->selectAdd('post_id'); + + $f2p->file_id = $file->id; + + $ids = array(); + + if (!$f2p->find()) { + throw new NoResultException($f2p); + } + + return $f2p->fetchAll('post_id'); + } + function delete($useWhere=false) { $f = File::getKV('id', $this->file_id); diff --git a/scripts/nukefile.php b/scripts/nukefile.php new file mode 100755 index 0000000000..1381676483 --- /dev/null +++ b/scripts/nukefile.php @@ -0,0 +1,78 @@ +#!/usr/bin/env php +. + */ + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); + +$shortoptions = 'i::yv'; +$longoptions = array('id=', 'yes', 'verbose'); + +$helptext = <<getFilename(); + } catch (Exception $e) { + $filename = '(remote file or no filename)'; + } + print "About to PERMANENTLY delete file ($filename) ({$file->id}). Are you sure? [y/N] "; + $response = fgets(STDIN); + if (strtolower(trim($response)) != 'y') { + print "Aborting.\n"; + exit(0); + } +} + +print "Finding notices...\n"; +try { + $ids = File_to_post::getNoticeIDsByFile($file); + $notice = Notice::multiGet('id', $ids); + while ($notice->fetch()) { + print "Deleting notice {$notice->id}".($verbose ? ": $notice->content\n" : "\n"); + $notice->delete(); + } +} catch (NoResultException $e) { + print "No notices found with this File attached.\n"; +} +print "Deleting File object together with possibly locally stored copy.\n"; +$file->delete(); +print "DONE.\n";