summaryrefslogtreecommitdiff
path: root/thumbnailer.php
blob: cf39fcbb606564e6c3eeb97ab7230357a9581e5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
 * Thumbnailer
 *
 * usage is simple:
 *		php -q thumbnailer.php [# of thumbnails]
 * example:
 *		php -q thumbnailer.php 20
 * suggested crontab entry runs the thumbnailer every minute:
 *		* * * * * apache php -q /path/to/bitweaver/fisheye/thumbnailer.php 20 >> /var/log/httpd/thumbnail_log
 *
 * @version $Header$
 * @package fisheye
 * @subpackage functions
 */

namespace Bitweaver\Fisheye;

use Bitweaver\KernelTools;

	global $gBitSystem, $gBitDb, $_SERVER;

	$_SERVER['SCRIPT_URL'] = '';
	$_SERVER['HTTP_HOST'] = '';
	$_SERVER['HTTP_HOST'] = '';
	$_SERVER['HTTP_HOST'] = '';
	$_SERVER['SERVER_NAME'] = '';

/**
 * required setup
 */
	if( !empty( $argc ) ) {
		// reduce feedback for command line to keep log noise way down
		define( 'BIT_PHP_ERROR_REPORTING', E_ERROR | E_PARSE );
	}

	// running from cron can cause us not to be in the right dir.
	chdir( dirname( __FILE__ ) );
	require_once '../kernel/includes/setup_inc.php';

	// add some protection for arbitrary thumbail execution.
	// if argc is present, we will trust it was exec'ed command line.
	if( empty( $argc ) && !$gBitUser->isAdmin() ) {
		$gBitSystem->fatalError( KernelTools::tra( 'You cannot run the thumbnailer' ));
	}

	$thumbCount = ( !empty( $argv[1] ) ) ? $argv[1] : ( !empty( $_REQUEST['thumbnails'] ) ? $_REQUEST['thumbnails'] : 10);

	$gBitDb->StartTrans();

	$sql = "SELECT pq.content_id AS hash_key, pq.*
			FROM `".BIT_DB_PREFIX."liberty_process_queue` pq
			WHERE pq.begin_date IS null
			ORDER BY pq.queue_date";
	$rows = $gBitSystem->mDb->getAssoc( $sql, false, $thumbCount );

	$processContent = [];
	foreach( $rows as $row ) {
		$processContent[$row['content_id']] = $row;
		$processContent[$row['content_id']]['parameters'] = unserialize( $row['processor_parameters'] );
		$sql2 = "UPDATE `".BIT_DB_PREFIX."liberty_process_queue` SET `begin_date`=? WHERE `content_id`=?";
		$rs2 = $gBitSystem->mDb->getOne( $sql2, [ date( 'U' ), $row['content_id'] ] );
	}
	$gBitDb->CompleteTrans();

	$log = [];
	$total = date( 'U' );
	foreach( array_keys( $processContent ) as $contentId ) {
		$image = new FisheyeImage( null, $contentId );
		$begin = date( 'U' );
		if( !empty( $processContent[$contentId]['parameters']['resize_original'] ) ) {
			$image->resizeOriginal( $processContent[$contentId]['parameters']['resize_original'] );
		}
		if( $image->renderThumbnails() ) {
			$log[$contentId]['message'] = 'SUCCESS: Thumbnails created';
			$sql3 = "UPDATE `".BIT_DB_PREFIX."liberty_process_queue` SET `begin_date`=?, `end_date`=? WHERE `content_id`=?";
			$rs3 = $gBitSystem->mDb->getOne( $sql3, [ $begin, $gBitSystem->getUTCTime(), $contentId ] );
		} else {
			$log[$contentId]['message'] = ' ERROR: '.$image->mErrors['thumbnail'];
		}
		$log[$contentId]['time'] = date( 'd/M/Y:H:i:s O' );
		$log[$contentId]['duration'] = date( 'U' ) - $begin;
		$log[$contentId]['delay'] = date( 'U' ) - $total;
	}

	foreach( array_keys( $log ) as $contentId ) {
		// generate something that kinda looks like apache common log format
		print $contentId.' - - ['.$log[$contentId]['time'].'] "'.$log[$contentId]['message'].'" '.$log[$contentId]['duration']."seconds <br/>\n";
	}

	if( count($processContent) ) {
		print '# '.count($processContent)." images processed in ".(date( 'U' ) - $total)." seconds<br/>\n";
	}