tools/: move cli utils from tinyboard-tools to tinyboard repo

This commit is contained in:
czaks 2013-07-02 22:45:52 -04:00
parent fe1e07f1bf
commit 47640a0ce8
3 changed files with 257 additions and 0 deletions

52
tools/benchmark.php Normal file
View File

@ -0,0 +1,52 @@
#!/usr/bin/php
<?php
/*
* benchmark.php - benchmarks thumbnailing methods
*
*/
require dirname(__FILE__) . '/inc/cli.php';
require 'inc/image.php';
// move back to this directory
chdir(dirname(__FILE__));
if(count($argv) != 2)
die("Usage: {$argv[0]} [file]\n");
$file = $argv[1];
$extension = strtolower(substr($file, strrpos($file, '.') + 1));
$out = tempnam($config['tmp'], 'thumb');
$count = 300;
function benchmark($method) {
global $config, $file, $extension, $out, $count;
$config['thumb_method'] = $method;
printf("Method: %s\nThumbnailing %d times... ", $method, $count);
$start = microtime(true);
for($i = 0; $i < $count; $i++) {
$image = new Image($file, $extension);
$thumb = $image->resize(
$config['thumb_ext'] ? $config['thumb_ext'] : $extension,
$config['thumb_width'],
$config['thumb_height']
);
$thumb->to($out);
$thumb->_destroy();
$image->destroy();
}
$end = microtime(true);
printf("Took %.2f seconds (%.2f/second; %.2f ms)\n", $end - $start, $rate = ($count / ($end - $start)), 1000 / $rate);
unlink($out);
}
benchmark('gd');
benchmark('imagick');
benchmark('convert');

100
tools/inc/cli.php Normal file
View File

@ -0,0 +1,100 @@
<?php
/*
* This script will look for Tinyboard in the following places (in order):
* - $TINYBOARD_PATH environment varaible
* - ./
* - ./Tinyboard/
* - ../
*/
set_time_limit(0);
$shell_path = getcwd();
if(getenv('TINYBOARD_PATH') !== false)
$dir = getenv('TINYBOARD_PATH');
elseif(file_exists('inc/functions.php'))
$dir = false;
elseif(file_exists('Tinyboard') && is_dir('Tinyboard') && file_exists('Tinyboard/inc/functions.php'))
$dir = 'Tinyboard';
elseif(file_exists('../inc/functions.php'))
$dir = '..';
else
die("Could not locate Tinyboard directory!\n");
if($dir && !chdir($dir))
die("Could not change directory to {$dir}\n");
if(!getenv('TINYBOARD_PATH')) {
// follow symlink
chdir(realpath('inc') . '/..');
}
putenv('TINYBOARD_PATH=' . getcwd());
require 'inc/functions.php';
require 'inc/mod.php';
$mod = Array(
'id' => -1,
'type' => ADMIN,
'username' => '?',
'boards' => Array('*')
);
function get_httpd_privileges() {
global $config, $shell_path, $argv;
if(php_sapi_name() != 'cli')
die("get_httpd_privileges(): invoked from HTTP client.\n");
echo "Dropping priviledges...\n";
if(!is_writable('.'))
die("get_httpd_privileges(): web directory is not writable\n");
$filename = '.' . md5(rand()) . '.php';
$inc_filename = '.' . md5(rand()) . '.php';
echo "Copying rebuilder to web directory...\n";
// replace "/inc/cli.php" with its new filename
passthru("cat " . escapeshellarg($shell_path . '/' . $_SERVER['PHP_SELF']) . " | sed \"s/'\/inc\/cli\.php'/'\/{$inc_filename}'/\" > {$filename}");
$inc_header = "<?php\n";
// copy environment
$env = explode("\n", shell_exec('printenv | grep ^TINYBOARD'));
foreach($env as $line) {
if(!empty($line))
$inc_header .= "putenv('" . addslashes($line) . "');\n";
}
// copy command line arguments
$inc_header .= "\$argv = " . var_export($argv, true) . ";\n";
// copy this file
file_put_contents($inc_filename, $inc_header . substr($inc = file_get_contents(__FILE__), strpos($inc, "\n")));
chmod($filename, 0666);
chmod($inc_filename, 0666);
if(preg_match('/^https?:\/\//', $config['root'])) {
$url = $config['root'] . $filename;
} elseif($host = getenv('TINYBOARD_HOST')) {
$url = 'http://' . $host . $config['root'] . $filename;
} else {
// assume localhost
$url = 'http://localhost' . $config['root'] . $filename;
}
echo "Downloading $url\n";
passthru('curl -s -N ' . escapeshellarg($url));
unlink($filename);
unlink($inc_filename);
exit(0);
}

105
tools/rebuild.php Executable file
View File

@ -0,0 +1,105 @@
#!/usr/bin/php
<?php
/*
* rebuild.php - rebuilds all static files
*
* Command line arguments:
* -q, --quiet
* Suppress output.
*
* --quick
* Do not rebuild posts.
*
* -b, --board <string>
* Rebuild only the specified board.
*
* -f, --full
* Rebuild replies as well as threads (re-markup).
*
*/
require dirname(__FILE__) . '/inc/cli.php';
if(!is_writable($config['file_script'])) {
get_httpd_privileges();
}
$start = microtime(true);
// parse command line
$opts = getopt('qfb:', Array('board:', 'quick', 'full', 'quiet'));
$options = Array();
$options['board'] = isset($opts['board']) ? $opts['board'] : (isset($opts['b']) ? $opts['b'] : false);
$options['quiet'] = isset($opts['q']) || isset($opts['quiet']);
$options['quick'] = isset($opts['quick']);
$options['full'] = isset($opts['full']) || isset($opts['f']);
if(!$options['quiet'])
echo "== Tinyboard {$config['version']} ==\n";
if(!$options['quiet'])
echo "Clearing template cache...\n";
load_twig();
$twig->clearCacheFiles();
if(!$options['quiet'])
echo "Regenerating theme files...\n";
rebuildThemes('all');
if(!$options['quiet'])
echo "Generating Javascript file...\n";
buildJavascript();
$main_js = $config['file_script'];
$boards = listBoards();
foreach($boards as &$board) {
if($options['board'] && $board['uri'] != $options['board'])
continue;
if(!$options['quiet'])
echo "Opening board /{$board['uri']}/...\n";
openBoard($board['uri']);
if($config['file_script'] != $main_js) {
// different javascript file
if(!$options['quiet'])
echo "Generating Javascript file...\n";
buildJavascript();
}
if(!$options['quiet'])
echo "Creating index pages...\n";
buildIndex();
if($options['quick'])
continue; // do no more
if($options['full']) {
$query = query(sprintf("SELECT `id` FROM `posts_%s`", $board['uri'])) or error(db_error());
while($post = $query->fetch()) {
if(!$options['quiet'])
echo "Rebuilding #{$post['id']}...\n";
rebuildPost($post['id']);
}
}
$query = query(sprintf("SELECT `id` FROM `posts_%s` WHERE `thread` IS NULL", $board['uri'])) or error(db_error());
while($post = $query->fetch()) {
if(!$options['quiet'])
echo "Rebuilding #{$post['id']}...\n";
buildThread($post['id']);
}
}
if(!$options['quiet'])
printf("Complete! Took %g seconds\n", microtime(true) - $start);
unset($board);
modLog('Rebuilt everything using tools/rebuild.php');