Add some PHP-CLI scripts to be run as cronjobs

This commit is contained in:
Kijin Sung 2017-01-26 21:29:22 +09:00
parent 30cdf63b77
commit 054191a69d
5 changed files with 203 additions and 0 deletions

View file

@ -0,0 +1,58 @@
<?php
/**
* This script deletes empty directories under the 'files' directory.
*
* It may be useful when your web host imposes a hard limit on the number of
* inodes, or when your backups take too long due to the large number of
* unused directories.
*
* This script only works on Unix-like operating systems where the 'find'
* command is available.
*/
require_once __DIR__ . '/common.php';
// Initialize the exit status.
$exit_status = 0;
// Delete empty directories in the attachment directory.
passthru(sprintf('find %s -type d -empty -delete', escapeshellarg(RX_BASEDIR . 'files/attach')), $result);
if ($result == 0)
{
echo "Successfully deleted all empty directories under files/attach.\n";
}
else
{
echo "Error while deleting empty directories under files/attach.\n";
$exit_status = $result;
}
// Delete empty directories in the member extra info directory.
passthru(sprintf('find %s -type d -empty -delete', escapeshellarg(RX_BASEDIR . 'files/member_extra_info')), $result);
if ($result == 0)
{
echo "Successfully deleted all empty directories under files/member_extra_info.\n";
}
else
{
echo "Error while deleting empty directories under files/member_extra_info.\n";
$exit_status = $result;
}
// Delete empty directories in the thumbnails directory.
passthru(sprintf('find %s -type d -empty -delete', escapeshellarg(RX_BASEDIR . 'files/thumbnails')), $result);
if ($result == 0)
{
echo "Successfully deleted all empty directories under files/thumbnails.\n";
}
else
{
echo "Error while deleting empty directories under files/thumbnails.\n";
$exit_status = $result;
}
// Set the exit status if there were any errors.
if ($exit_status != 0)
{
exit($exit_status);
}

View file

@ -0,0 +1,25 @@
<?php
/**
* This script deletes files that were not properly uploaded.
*
* Files can remain in an invalid status for two reasons: 1) a user abandons
* a document or comment after uploading files; or 2) a chunked upload is
* aborted without the server having any opportunity to clean it up.
* These files can obviously take up a lot of disk space. In order to prevent
* them from accumulating too much, you should run this script at least once
* every few days.
*/
require_once __DIR__ . '/common.php';
// Delete garbage files older than this number of days.
$days = 10;
// Initialize the exit status.
$exit_status = 0;
// Set the exit status if there were any errors.
if ($exit_status != 0)
{
exit($exit_status);
}

View file

@ -0,0 +1,37 @@
<?php
/**
* This script deletes old notifications.
*
* Notifications must be dismissed as quickly as possible in order to prevent
* the ncenterlite_notify table from becoming too large. For best performance,
* you should run this script at least once every few days.
*/
require_once __DIR__ . '/common.php';
// Delete notifications older than this number of days.
$days = 30;
// Initialize the exit status.
$exit_status = 0;
// Execute the query.
$args = new stdClass;
$args->old_date = date('YmdHis', time() - ($days * 86400));
$output = executeQuery('ncenterlite.deleteNotifyAll', $args);
if ($output->toBool())
{
echo "Successfully deleted all notifications older than $days days.\n";
}
else
{
echo "Error while deleting notifications older than $days days.\n";
echo $output->getMessage() . "\n";
$exit_status = abs($output->getError());
}
// Set the exit status if there were any errors.
if ($exit_status != 0)
{
exit($exit_status);
}

View file

@ -0,0 +1,41 @@
<?php
/**
* This script deletes old thumbnails.
*
* Thumbnails can take up a large amount of disk space and inodes if they are
* allowed to accumulate. Since most websites only need thumbnails for recent
* posts, it is okay to delete old thumbnails.
*
* Do not run this script if you have a gallery-style module where visitors
* regularly view old posts. This will force thumbnails to be regenerated,
* increasing the server load and making your pages load slower.
*
* This script only works on Unix-like operating systems where the 'find'
* command is available.
*/
require_once __DIR__ . '/common.php';
// Delete thumbnails older than this number of days.
$days = 90;
// Initialize the exit status.
$exit_status = 0;
// Delete old thumbnails.
passthru(sprintf('find %s -type f -mtime +%d -delete', escapeshellarg(RX_BASEDIR . 'files/thumbnails'), abs($days)), $result);
if ($result == 0)
{
echo "Successfully deleted all thumbnails older than $days days.\n";
}
else
{
echo "Error while deleting thumbnails older than $days days.\n";
$exit_status = $result;
}
// Set the exit status if there were any errors.
if ($exit_status != 0)
{
exit($exit_status);
}

42
common/scripts/common.php Normal file
View file

@ -0,0 +1,42 @@
<?php
/**
* This file must be included at the top of all shell scripts (cron jobs).
*
* HERE BE DRAGONS.
*
* Failure to perform the checks listed in this file at the top of a cron job,
* or any attempt to work around the limitations deliberately placed in this
* file, may result in errors or degradation of service.
*
* Please be warned that errors may not show up immediately, especially if you
* screw up the permissions inside deeply nested directory trees. You may find
* it difficult and/or costly to undo the damages when errors begin to show up
* several months later.
*/
// Abort if not CLI.
if (PHP_SAPI !== 'cli')
{
echo "This script must be executed on the command line interface.\n";
exit(1);
}
// Load Rhymix.
chdir(dirname(dirname(__DIR__)));
require_once dirname(__DIR__) . '/autoload.php';
// Abort if the UID does not match.
$uid = Rhymix\Framework\Storage::getServerUID();
if ($uid === 0)
{
echo "This script must not be executed by the root user.\n";
exit(2);
}
$web_server_uid = fileowner(RX_BASEDIR . 'files/config/config.php');
if ($uid !== $web_server_uid)
{
$web_server_uid = posix_getpwuid($web_server_uid);
echo "This script must be executed by the same user as the usual web server process ({$web_server_uid['name']}).\n";
exit(3);
}