-
Notifications
You must be signed in to change notification settings - Fork 6
/
smf_innodb_converter.php
719 lines (633 loc) · 16.9 KB
/
smf_innodb_converter.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<?php
/**
*
* A utility to convert all tables that match your smf db prefix to InnoDB.
*
* *** SMF 2.0 & 2.1 ***
* *** MySQL v5.5+ only ***
*
* Usage guidelines:
* (1) Use at your own risk.
* (2) ALWAYS run in your test environment first.
* (3) ALWAYS backup your system first - expect the unexpected.
* (4) Copy this file to your base SMF directory - (the one with Settings.php in it).
* (5) Execute it from your browser.
* (6) Delete this file when you're done.
* by sbulen
*
*/
$site_title = 'SMF InnoDB Converter';
$db_needed = true;
$ui = new SimpleSmfUI($site_title, $db_needed);
$ui->addChunk('Settings', function() use ($ui)
{
$dumpvars = array('mbname', 'boardurl', 'db_server', 'db_name', 'db_prefix', 'language', 'db_type', 'db_character_set', 'db_mb4');
$settings = array();
$settings[0] = array('Variable','Value');
foreach($dumpvars AS $var)
{
if (!isset($ui->getSettingsFile()[$var]))
$value = '<strong>NOT SET</strong>';
elseif (is_null($ui->getSettingsFile()[$var]))
$value = '<em>null</em>';
elseif ($ui->getSettingsFile()[$var] === false)
$value = '<em>false</em>';
elseif ($ui->getSettingsFile()[$var] === true)
$value = '<em>true</em>';
else
$value = $ui->getSettingsFile()[$var];
$settings[] = array($var, $value);
}
$ui->dumpTable($settings);
});
$ui->addChunk('Check Tables', function() use ($ui)
{
global $db_connection, $db_prefix, $smcFunc;
// Get all the tables that aren't InnoDB yet...
$table_engines = array();
$table_engines[] = array('Table', 'Engine');
$sql = "SHOW TABLE STATUS WHERE Name LIKE '$db_prefix%'";
$result = $smcFunc['db_query']('', $sql, array());
while($row = $smcFunc['db_fetch_assoc']($result))
{
if (strcasecmp($row['Engine'], 'InnoDB'))
$table_engines[] = array('Name' => $row['Name'], 'Engine' => $row['Engine']);
}
if (count($table_engines) == 1)
echo 'All of your tables are InnoDB!';
else
{
$ui->dumpTable($table_engines);
echo 'Would you like to convert these to InnoDB?<br>';
echo '<br><form><input type="submit" class="button" formmethod="post" name="proceed" value="Proceed?"></form><br>';
// Don't go any further unless they hit that button
if (empty($_SESSION['proceed']))
return;
// Only do it once per button press...
unset($_SESSION['proceed']);
// Get rid of the header
unset($table_engines[0]);
// Do the deed...
foreach ($table_engines AS $table_info)
{
echo "Converting table {$table_info['Name']}...";
$sql = "ALTER TABLE {$table_info['Name']} ENGINE=InnoDB;";
$smcFunc['db_query']('', $sql, array());
echo 'done<br>';
}
}
});
$ui->addChunk('All Tables', function() use ($ui)
{
global $db_connection, $db_prefix, $smcFunc;
// Show 'em all...
$table_engines = array();
$table_engines[] = array('Table', 'Engine');
$sql = "SHOW TABLE STATUS WHERE Name LIKE '$db_prefix%'";
$result = $smcFunc['db_query']('', $sql, array());
while($row = $smcFunc['db_fetch_assoc']($result))
$table_engines[] = array($row['Name'], $row['Engine']);
$ui->dumpTable($table_engines);
});
$ui->go();
/**
* SimpleSmfUI
*
* A simple basic abstracted UI for utilities.
*
* Copyright 2021-2023 Shawn Bulen
*
* This file is part of the sjrbTools library.
*
* SimpleSmfUI is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SimpleSmfUI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SimpleSmfUI. If not, see <https://www.gnu.org/licenses/>.
*
*/
// This oughtta hold us off until php 9.0...
#[AllowDynamicProperties]
class SimpleSmfUI
{
/*
* Properties
*/
protected $site_title = 'Simple UI';
protected $max_width = 1200;
protected $db_needed;
protected $txt = array(
'err_no_title' => 'Site title is required and must be a string!',
'err_width' => 'Funky width specified!',
'err_no_settings' => 'Could not find Settings.php! Place this file in the same folder as Settings.php.',
'err_no_db' => 'Could not establish connection with the database!',
'err_no_chunk_title' => 'Invalid chunk title!',
'err_no_chunk_func' => 'Invalid chunk function!',
'errors' => 'Errors',
);
protected $chunks = array();
protected $errors = array();
/*
* SMF Properties
*/
protected $settings_file;
/**
* Constructor
*
* Builds a SimpleSmfUI object
*
* @param string title
* @param bool db_needed
* @return void
*/
function __construct($title, $db_needed = null, $max_width = 800)
{
// Might as well try...
@set_time_limit(6000);
@ini_set('memory_limit', '512M');
// Title...
if (is_string($title))
$this->site_title = $title;
else
$this->addError('err_no_title');
// db_needed...
if (empty($db_needed))
$this->db_needed = false;
else
$this->db_needed = true;
// Width...
if (is_numeric($max_width))
$this->max_width = $max_width;
else
$this->addError('err_width');
// Error handler
// Note that php error suppression - @ - still calls the error handler. It will return 0 as it does so (pre php8).
// Note error handling in php8+ no longer fails silently on many errors, but error_reporting()
// will return 4437 (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE)
// as it does so.
set_error_handler(
function($errno, $errstr, $errfile, $errline)
{
if ((error_reporting() != 0) && (error_reporting() != (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE)))
$this->addError($errstr . ' (' . $errno . ')');
// Always try & report errors gracefully...
return true;
}
);
// DB...
define('SMF', 1);
define('SMF_VERSION', '2.x');
define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION);
define('SMF_SOFTWARE_YEAR', '2021');
define('POSTGRE_TITLE', 'PostgreSQL');
define('MYSQL_TITLE', 'MySQL');
define('SMF_USER_AGENT', 'Mozilla/5.0 (' . php_uname('s') . ' ' . php_uname('m') . ') AppleWebKit/605.1.15 (KHTML, like Gecko) SMF/' . strtr(SMF_VERSION, ' ', '.'));
// These must remain globals when calling SMF funcs...
global $smcFunc, $db_connection, $db_prefix, $db_name, $db_type, $sourcedir, $cachedir, $db_character_set, $db_port;
$smcFunc = array();
$this->settings_file = array();
if ($this->db_needed)
{
// Load & save off settings file contents
if (file_exists('Settings.php'))
{
include_once('Settings.php');
$dumpvars = array('mbname', 'db_server', 'db_name', 'db_prefix', 'db_type', 'db_character_set', 'db_mb4', 'language',
'boardurl', 'boarddir', 'sourcedir', 'packagesdir', 'tasksdir', 'cachedir',
'maintenance', 'mtitle', 'mmessage',
'cookiename', 'db_persist', 'db_error_send',
'cache_accelerator', 'cache_enable', 'cache_memcached',
'image_proxy_enabled', 'image_proxy_secret', 'image_proxy_maxsize');
foreach($dumpvars as $setting)
$this->settings_file[$setting] = (isset(${$setting}) ? ${$setting} : '<strong>NOT SET</strong>');
}
else
$this->addError('err_no_settings');
if (!empty($sourcedir))
{
// Get the database going!
if (empty($db_type) || $db_type == 'mysqli')
$db_type = 'mysql';
// Add in the port if needed
$db_options = array();
if (!empty($db_port))
$db_options['port'] = $db_port;
// Make the connection...
require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
$db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options);
if (empty($db_connection))
$this->addError('err_no_db');
// Set names...
if (!empty($db_character_set))
$smcFunc['db_query']('', '
SET NAMES {string:db_character_set}',
array(
'db_character_set' => $db_character_set,
)
);
}
}
}
/**
* Render chunk
*
* Display one portion of the form
*
* @return void
*/
protected function doChunk($ix, $chunk)
{
echo '<section>'; // sections needed to narrow scope of expand/collapse action
echo '<input type="checkbox" name="collapse" checked id="chunk' . $ix . '">
<div class="chunkhdr">
<label for="chunk' . $ix . '">' . $chunk['title'] . '</label>
</div>
<div class="content_nopad">
<div class="content">';
$chunk['function']();
echo '</div></div>';
echo '</section>';
}
/**
* Display errors
*
* Display errors in current display area
*
* @return void
*/
protected function renderErrors()
{
echo '<div id="errhdr">' . $this->txt['errors'] . '</div>
<div class="content_nopad">
<div class="content">';
foreach ($this->errors AS $error)
echo $error . '<br>';
echo '</div></div>';
}
/**
* Render header
*
* Spits out the head, title, style & starts the body
*
* @return void
*/
protected function renderHeader()
{
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8">
<title>' . $this->site_title . '</title>
<style type="text/css">
body
{
padding: 12px 0px 0px 0px;
font-family: "Roboto", sans-serif;
background-color: rgb(242,242,242);
max-width: ' . $this->max_width . 'px;
margin: 0 auto;
box-shadow: 2px 2px 2px 2px rgb(220,220,220);
border-bottom: 1px solid rgb(170,170,170);
}
#header
{
padding: 4px 4% 4px 4%;
background-color: rgb(54,126,189);
font-family: "Roboto", sans-serif;
font-size: x-large;
height: 30px;
vertical-align: middle;
text-align:center;
border-bottom: 1px solid rgb(58,123,155);
}
.content_nopad
{
background: linear-gradient(180deg, rgb(190,190,190) 0%, rgb(252,252,252) 5px);
}
.content
{
padding: 10px 10px 10px 10px;
font-size: small;
}
#nametag
{
font-family: "Roboto", sans-serif;
font-size: smaller;
color: rgb(122,132,134);
background-color: rgb(252,252,252);
text-align: right;
display: inline-block;
width: 50%;
}
#warningtag
{
font-family: "Roboto", sans-serif;
font-size: smaller;
color: rgb(122,132,134);
background-color: rgb(252,252,252);
text-align: left;
display: inline-block;
width: 50%;
}
.chunkhdr
{
padding: 4px 4% 4px 4%;
background-color: rgb(0,153,204);
font-family: "Roboto", sans-serif;
font-size: large;
height: 25px;
vertical-align: middle;
text-align:center;
border-bottom: 1px solid rgb(89,154,187);
}
.chunkhdr label
{
display: inline-block;
width: 100%;
}
.chunkhdr:hover,
.chunkhdr:focus
{
background: rgb(76,175,208);
}
input[name="collapse"]:not(:checked) ~ .content_nopad
{
display: none;
}
input[name="collapse"]
{
display: none;
}
.chunkhdr label:before {
content: "▲";
display: inline-block;
float: left;
vertical-align: middle;
color: rgb(54,126,189);
}
input[name="collapse"]:not(:checked) ~ .chunkhdr label:before
{
transform: rotate(180deg);
transform-origin: center;
}
#errhdr
{
padding: 4px 4% 4px 4%;
background-color: rgb(230,130,130);
font-family: "Roboto", sans-serif;
font-size: large;
height: 25px;
vertical-align: middle;
text-align:center;
}
input[type=text], select
{
width: 85%;
height: 25px;
margin: 4px 0px;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.button
{
padding: 1px 1px;
text-align: center;
width: 100px;
box-shadow: 1px 1px 1px 1px rgb(210,210,210);
}
.sui_table
{
display: table;
table-layout:fixed;
border-collapse: collapse;
border-spacing: 0;
padding: 0px;
border: 1px solid #ccc;
text-align: left;
vertical-align: top;
white-space: normal;
word-break: break-word;
font-weight: normal;
font-variant: normal;
color: inherit;
}
.sui_row
{
display: table-row;
}
.sui_row_red
{
display: table-row;
background-color: rgb(230,130,130);
}
.sui_row_green
{
display: table-row;
background-color: #42ddcf;
}
.sui_row_header
{
display: table-header-group;
font-weight: bold;
color: rgb(46,96,140);
}
.sui_cell
{
border: 1px solid #ccc;
display: table-cell;
padding: 2px;
min-width: 80px;
max-width: 750px;
}
.sui_cell_yellow
{
border: 1px solid #ccc;
display: table-cell;
padding: 2px;
min-width: 80px;
max-width: 750px;
background-color: #f7f793;
}
</style>
</head>
<body>
<div id="header">' . $this->site_title . '
</div>';
}
/**
* Render header
*
* Closes out the body & html tags
*
* @return void
*/
protected function renderFooter()
{
// Close out body & html tags
echo '<div id="warningtag">Remove when not in use</div>';
echo '<div id="nametag">sbulen/sjrbTools</div>';
echo '</body>
</html>';
}
/**
* Cleanse text
*
* Some basic hygiene for user-entered input
*
* @param string input
* @param bool gtlt - whether to leave > and < alone (e.g., for queries)
* @return string cleansed
*/
public function cleanseText($input, $gtlt = false)
{
$input = trim($input);
$input = htmlspecialchars($input);
if ($gtlt)
{
$input = str_replace('>', '>', $input);
$input = str_replace('&gt;', '>', $input);
$input = str_replace('<', '<', $input);
$input = str_replace('&lt;', '<', $input);
}
return $input;
}
/**
* Dump table
*
* Render a simple 2-d array in table form
*
* @param array passed_array
* @return void
*/
public function dumpTable($passed_array)
{
static $special_cells = array('<strong>NOT SET</strong>', '<em>null</em>', '<em>true</em>', '<em>false</em>');
$header = true;
echo '<br><div class="sui_table">';
foreach($passed_array as $row)
{
// Some cleansing...
foreach ($row AS $ix => $cell)
{
// Treat NOT SET, null, true, & false special...
if (in_array($cell, $special_cells))
$row[$ix] = $cell;
else
{
$row[$ix] = htmlspecialchars($cell);
// Undo any line breaks you just broke...
$row[$ix] = str_replace('<br>', '<br>', $row[$ix]);
$row[$ix] = str_replace('<br />', '<br>', $row[$ix]);
}
}
if ($header)
echo '<div class="sui_row_header">';
else
echo '<div class="sui_row">';
echo '<div class="sui_cell">';
echo implode('</div><div class="sui_cell">', $row);
echo '</div></div>';
$header = false;
}
echo '</div><br>';
}
/**
* Add Chunk
*
* Adds an entry to the internal chunk array.
* Each chunk will display a header, do some logic, & display some content.
* If errors are encountered, ideally they should be added to the errors display and displayed at the end.
*
* @param string title - title to display above this chunk
* @param function logic - what to execute, passed as an anonymous function
* @return void
*/
public function addChunk($title, $func)
{
if (!is_string($title))
{
$title = '';
$this->addError('err_no_chunk_title');
}
if (!is_callable($func))
{
$func = function() {};
$this->addError('err_no_chunk_func');
}
$this->chunks[] = array('title' => $title, 'function' => $func);
}
/**
* Get Settings File contents
*
* @return array
*/
public function getSettingsFile()
{
return $this->settings_file;
}
/**
* Add Error
*
* Add error to internal log
*
* @param string key - is key to $txt array
* @param string more - is additional info to be added to output string if needed
* @return void
*/
public function addError($key, $more = '')
{
if (!is_string($key))
$key = '';
if (!is_string($more))
$more = '';
if (!empty($this->txt[$key]))
$key = $this->txt[$key];
$this->errors[] = $key . ' ' . $more;
}
/**
* Go
*
* Got everything, now do it...
*
* @return void
*/
public function go()
{
global $db_connection;
// Responding to a POST? Cleanse info, put in session and redirect
session_start();
if ($_POST)
{
$_SESSION = array();
foreach($_POST as $var => $val)
$_SESSION[$this->cleanseText($var)] = $this->cleanseText($val);
// Redirect to this page
header("Location: {$_SERVER['REQUEST_URI']}", true, 302);
exit();
}
// OK, display stuff...
$this->renderHeader();
// Execute the chunks...
// Note if db_needed & no connection, do not process chunks, just display the errors
if (!$this->db_needed || ($this->db_needed && !empty($db_connection)))
{
foreach($this->chunks AS $ix => $chunk)
$this->doChunk($ix, $chunk);
}
// Display any errors...
if (!empty($this->errors))
$this->renderErrors();
$this->renderFooter();
// Ensure refreshes actually refresh!
$_SESSION = array();
}
}