summaryrefslogtreecommitdiff
path: root/tools/rebuild.php
blob: 9099ffebe90e382ee44580e8cd6f0afefd6eec8d (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
#!/usr/bin/php
<?php
	require 'inc/functions.php';
	require 'inc/display.php';
	require 'inc/template.php';
	require 'inc/database.php';
	require 'inc/user.php';
	require 'inc/mod.php';
	
	require dirname(__FILE__) . '/inc/cli.php';
	
	$mod = Array(
		'id' => -1,
		'type' => ADMIN,
		'username' => '?',
		'boards' => Array('*')
	);
	
	$start = microtime(true);
	
	echo "== Tinyboard {$config['version']} ==\n";	
	
	if(!is_writable($config['file_script'])) {
		echo "Dropping priviledges... (I can't operate as user; I need PHP's rights.)\n";
		
		$filename = '.' . md5(rand()) . '.php';
		
		echo "Copying rebuilder to web directory...\n";
		copy(__FILE__, $filename);
		chmod($filename, 0666);
		
		if(preg_match('/^https?:\/\//', $config['root'])) {
			$url = $config['root'] . $filename;
		} else {
			// assume localhost
			$url = 'http://localhost' . $config['root'] . $filename;
		}
		
		echo "Downloading $url\n";
		
		passthru('curl -s -N ' . escapeshellarg($url));

		echo "\n".'Cleaning up afterwards...'."\n";
		
		unlink($filename);
		
		echo "Bye!\n";
		exit;
	}
	
	echo "Clearing template cache...\n";
	$twig = new Twig_Environment($loader, Array(
		'cache' => "{$config['dir']['template']}/cache"
	));
	$twig->clearCacheFiles();
	
	echo "Regenerating theme files...\n";
	rebuildThemes('all');
	
	echo "Generating Javascript file...\n";
	buildJavascript();
	
	$boards = listBoards();
	
	foreach($boards as &$board) {
		echo "Opening board /{$board['uri']}/...\n";
		openBoard($board['uri']);
		
		echo "Creating index pages...\n";
		buildIndex();
		
		$query = query(sprintf("SELECT `id` FROM `posts_%s` WHERE `thread` IS NULL", $board['uri'])) or error(db_error());
		while($post = $query->fetch()) {
			echo "Rebuilding #{$post['id']}...\n";
			buildThread($post['id']);
		}
	}
	
	printf("Complete! Took %g seconds\n", microtime(true) - $start);
	
	modLog('Rebuilt everything using tools/rebuild.php');