Completed
Push — master ( b4956d...e33aad )
by Matt
03:29 queued 02:00
created

dbtool_module::is_valid_engine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 *
4
 * Database Optimize & Repair Tool
5
 *
6
 * @copyright (c) 2013 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\dbtool\acp;
12
13
/**
14
 * @package acp
15
 */
16
class dbtool_module
17
{
18
	/** @var \phpbb\db\driver\driver_interface */
19
	protected $db;
20
21
	/** @var \phpbb\language\language */
22
	protected $language;
23
24
	/** @var \phpbb\request\request */
25
	protected $request;
26
27
	/** @var \phpbb\template\template */
28
	protected $template;
29
30
	/** @var \vse\dbtool\tool\tool_interface */
31
	protected $db_tool;
32
33
	/** @var string */
34
	public $page_title;
35
36
	/** @var string */
37
	public $tpl_name;
38
39
	/** @var string */
40
	public $u_action;
41
42
	/**
43
	 * Constructor
44
	 */
45
	public function __construct()
46
	{
47
		global $phpbb_container;
48
49
		try
50
		{
51
			$this->db       = $phpbb_container->get('dbal.conn');
52
			$this->language = $phpbb_container->get('language');
53
			$this->request  = $phpbb_container->get('request');
54
			$this->template = $phpbb_container->get('template');
55
			$this->db_tool  = $phpbb_container->get('vse.dbtool.tool');
56
		}
57
		catch (\Exception $e)
58
		{
59
			trigger_error($e->getMessage(), E_USER_WARNING);
60
		}
61
62
		$this->language->add_lang('dbtool_acp', 'vse/dbtool');
63
	}
64
65
	/**
66
	 * Main ACP module
67
	 *
68
	 * @access public
69
	 */
70
	public function main()
71
	{
72
		$this->tpl_name = 'acp_dbtool';
73
		$this->page_title = 'ACP_OPTIMIZE_REPAIR';
74
75
		if (!$this->db_tool->is_mysql())
76
		{
77
			trigger_error($this->language->lang('WARNING_MYSQL'), E_USER_WARNING);
78
		}
79
80
		if ($this->request->is_set_post('submit'))
81
		{
82
			$this->run_tool();
83
		}
84
85
		$this->display_tables();
86
	}
87
88
	/**
89
	 * Run database tool
90
	 *
91
	 * @access protected
92
	 */
93
	protected function run_tool()
94
	{
95
		$operation = strtoupper($this->request->variable('operation', ''));
96
		$tables = $this->request->variable('mark', ['']);
97
		$disable_board = $this->request->variable('disable_board', 0);
98
99
		if (confirm_box(true))
100
		{
101
			if (!count($tables))
102
			{
103
				trigger_error($this->language->lang('TABLE_ERROR') . adm_back_link($this->u_action), E_USER_WARNING);
104
			}
105
106
			if ($this->db_tool->is_valid_operation($operation))
107
			{
108
				$results = $this->db_tool->run($operation, $tables, $disable_board);
109
				$results = '<br />' . implode('<br />', $results);
110
				trigger_error($this->language->lang($operation . '_SUCCESS') . $results . adm_back_link($this->u_action));
111
			}
112
		}
113
		else
114
		{
115
			confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields([
116
				'submit'		=> 1,
117
				'operation'		=> $operation,
118
				'mark'			=> $tables,
119
				'disable_board'	=> $disable_board,
120
			]), 'confirm_dbtool.html');
121
		}
122
	}
123
124
	/**
125
	 * Generate Show Table Data
126
	 *
127
	 * @access protected
128
	 */
129
	protected function display_tables()
130
	{
131
		$table_data = [];
132
		$total_data_size = $total_data_free = 0;
133
134
		$tables = $this->db->sql_query('SHOW TABLE STATUS');
135
136
		while ($table = $this->db->sql_fetchrow($tables))
137
		{
138
			$table['Engine'] = (!empty($table['Type']) ? $table['Type'] : $table['Engine']);
139
			if ($this->db_tool->is_valid_engine($table['Engine']))
140
			{
141
				// Data_free should always be 0 for InnoDB tables
142
				if ($this->db_tool->is_innodb($table['Engine']))
143
				{
144
					$table['Data_free'] = 0;
145
				}
146
147
				$data_size = $table['Data_length'] + $table['Index_length'];
148
				$total_data_size += $data_size;
149
				$total_data_free += $table['Data_free'];
150
151
				$table_data[] = [
152
					'TABLE_NAME'	=> $table['Name'],
153
					'TABLE_TYPE'	=> $table['Engine'],
154
					'DATA_SIZE'		=> get_formatted_filesize($data_size),
155
					'DATA_FREE'		=> get_formatted_filesize($table['Data_free']),
156
					'S_OVERHEAD'	=> (bool) $table['Data_free'],
157
				];
158
			}
159
		}
160
		$this->db->sql_freeresult($tables);
161
162
		$this->template->assign_vars([
163
			'TABLE_DATA'		=> $table_data,
164
			'TOTAL_DATA_SIZE'	=> get_formatted_filesize($total_data_size),
165
			'TOTAL_DATA_FREE'	=> get_formatted_filesize($total_data_free),
166
			'U_ACTION'			=> $this->u_action,
167
		]);
168
	}
169
}
170