1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Migrations; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database; |
6
|
|
|
use GeminiLabs\SiteReviews\Database\CountManager; |
7
|
|
|
use GeminiLabs\SiteReviews\Database\Query; |
8
|
|
|
use GeminiLabs\SiteReviews\Database\SqlSchema; |
9
|
|
|
use GeminiLabs\SiteReviews\Install; |
10
|
|
|
|
11
|
|
|
class Migrate_5_9_0 |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
7 |
|
public function migrateDatabase() |
17
|
|
|
{ |
18
|
7 |
|
$table = glsr(SqlSchema::class)->table('ratings'); |
19
|
7 |
|
if ($this->isDatabaseVersionUpdated()) { |
20
|
6 |
|
return true; |
21
|
|
|
} |
22
|
1 |
|
glsr(Database::class)->dbQuery(glsr(Query::class)->sql(" |
23
|
1 |
|
ALTER TABLE {$table} |
24
|
|
|
ADD terms tinyint(1) NOT NULL DEFAULT '1' |
25
|
|
|
AFTER url |
26
|
|
|
")); |
27
|
1 |
|
if ($this->isDatabaseVersionUpdated()) { |
28
|
1 |
|
return true; |
29
|
|
|
} |
30
|
|
|
glsr_log()->error(sprintf('Database table [%s] could not be altered, column [terms] not added.', $table)); |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
7 |
|
public function repairDatabase() |
38
|
|
|
{ |
39
|
7 |
|
require_once ABSPATH.'/wp-admin/includes/plugin.php'; |
40
|
7 |
|
if (is_plugin_active_for_network(plugin_basename(glsr()->file))) { |
41
|
|
|
foreach (glsr(Install::class)->sites() as $siteId) { |
42
|
|
|
switch_to_blog($siteId); |
43
|
|
|
$this->install(); |
44
|
|
|
restore_current_blog(); |
45
|
|
|
} |
46
|
|
|
return; |
47
|
|
|
} |
48
|
7 |
|
$this->install(); |
49
|
7 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
7 |
|
public function run() |
55
|
|
|
{ |
56
|
7 |
|
$this->repairDatabase(); // fix orphaned rows and foreign indexes |
57
|
7 |
|
glsr(CountManager::class)->recalculate(); |
58
|
7 |
|
return $this->migrateDatabase(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
7 |
|
protected function install() |
65
|
|
|
{ |
66
|
7 |
|
glsr(SqlSchema::class)->createTables(); |
67
|
7 |
|
glsr(SqlSchema::class)->addForeignConstraints(); |
68
|
7 |
|
glsr(Database::class)->deleteInvalidPostAssignments(); |
69
|
7 |
|
glsr(Database::class)->deleteInvalidTermAssignments(); |
70
|
7 |
|
glsr(Database::class)->deleteInvalidUserAssignments(); |
71
|
7 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
7 |
|
protected function isDatabaseVersionUpdated() |
77
|
|
|
{ |
78
|
7 |
|
$table = glsr(SqlSchema::class)->table('ratings'); |
79
|
7 |
|
if (glsr(SqlSchema::class)->columnExists($table, 'terms')) { |
80
|
7 |
|
if (!glsr(Database::class)->version('1.1')) { |
81
|
|
|
update_option(glsr()->prefix.'db_version', '1.1'); |
82
|
|
|
} |
83
|
7 |
|
return true; |
84
|
|
|
} |
85
|
1 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|