Add --delete-all option to administration script for mass paste deletion

This commit is contained in:
root 2025-04-22 04:12:46 -05:00
parent d220ba7c2b
commit 302921a811

View file

@ -72,6 +72,35 @@ class Administration
exit("paste $pasteId successfully deleted" . PHP_EOL); exit("paste $pasteId successfully deleted" . PHP_EOL);
} }
/**
* lists all stored paste IDs
*
* @access private
*/
private function _list_ids()
{
$ids = $this->_store->getAllPastes();
foreach ($ids as $pasteid) {
echo $pasteid, PHP_EOL;
}
exit;
}
/**
* deletes all stored pastes (regardless of expiration)
*
* @access private
*/
private function _delete_all()
{
$ids = $this->_store->getAllPastes();
foreach ($ids as $pasteid) {
echo "Deleting paste ID: $pasteid" . PHP_EOL;
$this->_store->delete($pasteid);
}
exit("All pastes successfully deleted" . PHP_EOL);
}
/** /**
* removes empty directories, if current storage model uses Filesystem * removes empty directories, if current storage model uses Filesystem
* *
@ -124,7 +153,7 @@ class Administration
{ {
echo <<<'EOT' echo <<<'EOT'
Usage: Usage:
administration [--delete <paste id> | --empty-dirs | --help | --purge | --statistics] administration [--delete <paste id> | --empty-dirs | --help | --purge | --statistics | --list-ids]
Options: Options:
-d, --delete deletes the requested paste ID -d, --delete deletes the requested paste ID
@ -133,6 +162,7 @@ Options:
-h, --help displays this help message -h, --help displays this help message
-p, --purge purge all expired pastes -p, --purge purge all expired pastes
-s, --statistics reads all stored pastes and comments and reports statistics -s, --statistics reads all stored pastes and comments and reports statistics
-l, --list-ids lists all paste IDs
EOT, PHP_EOL; EOT, PHP_EOL;
exit($code); exit($code);
} }
@ -177,7 +207,8 @@ EOT, PHP_EOL;
self::_help(2); self::_help(2);
} }
$this->_opts = getopt('hd:eps', array('help', 'delete:', 'empty-dirs', 'purge', 'statistics')); $this->_opts = getopt('hd:epsl', array('help', 'delete:', 'empty-dirs', 'purge', 'statistics', 'list-ids', 'delete-all'));
if (!$this->_opts) { if (!$this->_opts) {
self::_error_echo('unsupported arguments given'); self::_error_echo('unsupported arguments given');
echo PHP_EOL; echo PHP_EOL;
@ -307,7 +338,13 @@ EOT, PHP_EOL;
$class = 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model'); $class = 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model');
$this->_store = new $class($this->_conf->getSection('model_options')); $this->_store = new $class($this->_conf->getSection('model_options'));
if ($this->_option('l', 'list-ids') !== null) {
$this->_list_ids();
}
if ($this->_option(null, 'delete-all') !== null) {
$this->_delete_all();
}
if (($pasteId = $this->_option('d', 'delete')) !== null) { if (($pasteId = $this->_option('d', 'delete')) !== null) {
$this->_delete($pasteId); $this->_delete($pasteId);
} }
@ -328,4 +365,4 @@ EOT, PHP_EOL;
} }
} }
new Administration(); new Administration();