summaryrefslogtreecommitdiffstats
path: root/application/controllers/tools.php
blob: 7c8538cfb93d554b32ee265cc8fd8e1cf07b0f46 (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
94
95
96
97
98
99
<?php
/*
 * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

class Tools extends MY_Controller {

	function __construct()
	{
		parent::__construct();

		$this->load->model('mfile');
		if (!$this->input->is_cli_request()) {
			throw new \exceptions\ApiException("api/cli-only", "This can only be called via CLI");
		}
	}

	function index()
	{
		echo "php index.php <controller> <function> [arguments]\n";
		echo "\n";
		echo "Functions:\n";
		echo "  file cron               Cronjob\n";
		echo "  file nuke_id <ID>       Nukes all IDs sharing the same hash\n";
		echo "  user cron               Cronjob\n";
		echo "  tools update_database   Update/Initialise the database\n";
		echo "\n";
		echo "Functions that shouldn't have to be run:\n";
		echo "  file clean_stale_files     Remove files without database entries\n";
		echo "  file update_file_metadata  Update filesize and mimetype in database\n";
		exit;
	}

	function update_database()
	{
		$this->load->library('migration');
		if ( ! $this->migration->current()) {
			throw new \exceptions\ApiException("tools/update_database/migration-error", $this->migration->error_string());
		}
	}

	function drop_all_tables_using_prefix()
	{
		$tables = $this->db->list_tables();
		$prefix = $this->db->dbprefix;
		$tables_to_drop = array();

		foreach ($tables as $table) {
			if (strpos($table, $prefix) === 0) {
				$tables_to_drop[] = $this->db->protect_identifiers($table);
			}
		}

		if (empty($tables_to_drop)) {
			return;
		}

		$this->db->query('SET FOREIGN_KEY_CHECKS = 0');
		$this->db->query('DROP TABLE '.implode(", ", $tables_to_drop));
		$this->db->query('SET FOREIGN_KEY_CHECKS = 1');
	}

	function test()
	{
		global $argv;
		$url = $argv[3];
		$testcase = $argv[4];

		$testclass = '\tests\\'.$testcase;
		$test = new $testclass();
		$test->setServer($url);

		$exitcode = 0;

		$refl = new ReflectionClass($test);
		foreach ($refl->getMethods() as $method) {
			if (strpos($method->name, "test_") === 0) {
				try {
					$test->init();
					$test->{$method->name}();
					$test->cleanup();
				} catch (\Exception $e) {
					echo "not ok - uncaught exception in $testcase->$method->name\n";
					_actual_exception_handler($e);
					$exitcode = 255;
				}
			}
		}
		if ($exitcode == 0) {
			$test->done_testing();
		} else {
			exit($exitcode);
		}
	}
}