/
ShashinUpgradeWp.php
executable file
·206 lines (167 loc) · 9.13 KB
/
ShashinUpgradeWp.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
<?php
class Admin_ShashinUpgradeWp {
private $dbFacade;
private $album;
private $photo;
private $functionsFacade;
private $adminContainer;
private $albumTable;
private $albumTableBackup;
private $photoTable;
private $photoTableBackup;
private $picasaServer;
public function __construct() {
}
public function setDbFacade(ToppaDatabaseFacade $dbFacade) {
$this->dbFacade = $dbFacade;
return $this->dbFacade;
}
public function setAlbum(Lib_ShashinAlbum $album) {
$this->album = $album;
return $this->album;
}
public function setPhoto(Lib_ShashinPhoto $photo) {
$this->photo = $photo;
return $this->photo;
}
public function setFunctionsFacade(ToppaFunctionsFacade $functionsFacade) {
$this->functionsFacade = $functionsFacade;
return $this->functionsFacade;
}
public function setAdminContainer(Admin_ShashinContainer $adminContainer) {
$this->adminContainer = $adminContainer;
return $this->adminContainer;
}
public function run() {
$this->setTableProperties();
if (!$this->isUpgradeNeeded()) {
return true;
}
$this->picasaServer = $this->setPicasaServer();
if (!$this->picasaServer) {
throw New Exception(__('Shashin upgrade is needed, but is not possible because the Shashin 2 Picasa server settings are missing. Please re-install Shashin 2 before upgrading to Shashin 3', 'shashin'));
}
$this->upgradePhotoTableIfNeeded();
return $this->upgradeAlbumTableIfNeeded();
}
public function setTableProperties() {
$this->albumTable = $this->album->getTableName();
$this->albumTableBackup = $this->albumTable . '_shashin2_backup';
$this->photoTable = $this->photo->getTableName();
$this->photoTableBackup = $this->photoTable . '_shashin2_backup';
return true;
}
public function isUpgradeNeeded() {
$photoTableNeedsUpgrade = $this->checkTableNeedsUpgrade($this->photoTable, 'photo_key');
$albumTableNeedsUpgrade = $this->checkTableNeedsUpgrade($this->albumTable, 'album_key');
if ($photoTableNeedsUpgrade || $albumTableNeedsUpgrade) {
return true;
}
return false;
}
public function setPicasaServer() {
$oldSettings = $this->functionsFacade->getSetting('shashin_options');
if (!$oldSettings) {
return false;
}
elseif (is_string($oldSettings)) {
$oldSettings = unserialize($oldSettings);
}
if (is_array($oldSettings) && $oldSettings['picasa_server']) {
$this->picasaServer = $oldSettings['picasa_server'];
return $this->picasaServer;
}
return false;
}
public function upgradePhotoTableIfNeeded() {
if (!$this->checkTableNeedsUpgrade($this->photoTable, 'photo_key')) {
return false;
}
if (!$this->checkTableNeedsUpgrade($this->albumTable, 'album_key')) {
throw New Exception(__('Shashin album table not in correct state for upgrade', 'shashin'));
}
$this->dbFacade->executeQuery("create table if not exists {$this->photoTableBackup} select * from {$this->photoTable}");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change photo_key id int unsigned auto_increment");
$this->dbFacade->executeQuery("alter table {$this->photoTable} add sourceId varchar(255)");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change title filename varchar(255)");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change link_url linkUrl text");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change content_url contentUrl text");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change taken_timestamp takenTimestamp int unsigned");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change uploaded_timestamp uploadedTimestamp int unsigned");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change include_in_random includeInRandom char(1)");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change enclosure_type contentType varchar(255)");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change picasa_order sourceOrder int unsigned");
$this->dbFacade->executeQuery("alter table {$this->photoTable} change focal_length focalLength varchar(10)");
$this->dbFacade->executeQuery("delete from {$this->photoTable} where deleted = 'Y'");
$this->dbFacade->executeQuery("alter table {$this->photoTable} drop column deleted");
$this->dbFacade->executeQuery("alter table {$this->photoTable} drop column enclosure_url");
$this->dbFacade->executeQuery("alter table {$this->photoTable} add albumId smallint unsigned");
$this->dbFacade->executeQuery("alter table {$this->photoTable} add albumType varchar(20)");
// this works better than alter table, given the data type change
$this->dbFacade->executeQuery("update {$this->photoTable} set sourceId = photo_id");
$albumData = $this->dbFacade->executeQuery('select album_key, album_id from ' . $this->album->getTableName(), 'get_results');
foreach ($albumData as $data) {
$this->dbFacade->executeQuery(
"update {$this->photoTable} set albumId = {$data['album_key']} where album_id = {$data['album_id']}"
);
}
$this->dbFacade->executeQuery("alter table {$this->photoTable} drop column album_id");
$this->dbFacade->executeQuery("alter table {$this->photoTable} drop column photo_id");
$this->dbFacade->executeQuery("update {$this->photoTable} set albumType = 'picasa' where albumType is null");
return true;
}
public function upgradeAlbumTableIfNeeded() {
if (!$this->checkTableNeedsUpgrade($this->albumTable, 'album_key')) {
return false;
}
$this->dbFacade->executeQuery("create table if not exists {$this->albumTableBackup} select * from {$this->albumTable}");
$this->dbFacade->executeQuery("alter table {$this->albumTable} change album_key id smallint unsigned auto_increment");
$this->dbFacade->executeQuery("alter table {$this->albumTable} add sourceId varchar(255)");
$this->dbFacade->executeQuery("alter table {$this->albumTable} add albumType varchar(20)");
$this->dbFacade->executeQuery("alter table {$this->albumTable} add dataUrl varchar(255)");
$this->dbFacade->executeQuery("alter table {$this->albumTable} change link_url linkUrl varchar(255)");
$this->dbFacade->executeQuery("alter table {$this->albumTable} change cover_photo_url coverPhotoUrl varchar(255)");
$this->dbFacade->executeQuery("alter table {$this->albumTable} change last_updated lastSync int unsigned");
$this->dbFacade->executeQuery("alter table {$this->albumTable} change photo_count photoCount smallint unsigned");
$this->dbFacade->executeQuery("alter table {$this->albumTable} change pub_date pubDate int unsigned");
$this->dbFacade->executeQuery("alter table {$this->albumTable} change geo_pos geoPos varchar(25)");
$this->dbFacade->executeQuery("alter table {$this->albumTable} change include_in_random includeInRandom char(1)");
// this is better than alter table, given the data type change
$this->dbFacade->executeQuery("update {$this->albumTable} set sourceId = album_id");
$albumData = $this->dbFacade->executeQuery("select id, sourceId, user from {$this->albumTable}", 'get_results');
foreach ($albumData as $data) {
$albumJsonUrl = $this->picasaServer
. '/data/feed/api/user/'
. $data['user']
. '/albumid/'
. $data['sourceId']
. '?kind=photo&alt=json';
$this->dbFacade->executeQuery(
"update {$this->albumTable} set dataUrl = '$albumJsonUrl' where id = {$data['id']}"
);
}
$this->dbFacade->executeQuery("alter table {$this->albumTable} drop column album_id");
$this->dbFacade->executeQuery("update {$this->albumTable} set albumType = 'picasa' where albumType is null");
return true;
}
public function checkTableNeedsUpgrade($tableName, $fieldToCheck) {
if ($this->dbFacade->executeQuery("show tables like '$tableName'", 'get_var')) {
$tableDescription = $this->dbFacade->executeQuery("describe $tableName", 'get_results');
}
else {
return false;
}
foreach ($tableDescription as $row) {
if ($row['Field'] == $fieldToCheck) {
return true;
}
}
return false;
}
public function cleanup() {
$this->setTableProperties();
$this->functionsFacade->deleteSetting('shashin_options');
$this->dbFacade->executeQuery("drop table {$this->albumTableBackup}");
$this->dbFacade->executeQuery("drop table {$this->photoTableBackup}");
}
}