-
Notifications
You must be signed in to change notification settings - Fork 3
/
class.ext_update.php
423 lines (375 loc) · 12.3 KB
/
class.ext_update.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
<?php
use TYPO3\CMS\Core\Resource\DuplicationBehavior;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Resource\ResourceStorage;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* This class offers functions to update the database from the previous major extension version to the current one.
*
* @author Oliver Klee <[email protected]>
*/
class ext_update
{
/**
* the folder where uploaded images get stored.
*
* @var string
*/
const UPLOAD_FOLDER = 'uploads/tx_realty/';
/**
* @var string[]
*/
private $requiredExtensions = ['oelib', 'realty'];
/**
* @var string[][]
*/
private $requiredTables = [
'attachments' => ['tx_realty_objects', 'tx_realty_images', 'tx_realty_documents'],
'districts' => ['tx_realty_objects', 'tx_realty_cities', 'tx_realty_districts'],
];
/**
* @var string[][][]
*/
private $requiredColumns = [
'attachments' => [
'tx_realty_objects' => ['attachments', 'images', 'documents'],
],
];
/**
* @var int[]
*/
private $attachmentSorting = [];
/**
* Checks whether the update module needs to to anything
*
* @return bool
*/
public function access()
{
return $this->extensionsAreInstalled()
&& ($this->needsToUpdateAttachments() || $this->needsToUpdateDistricts());
}
/**
* Returns the update module content.
*
* @return string
* the update module content, will be empty if nothing was updated
*/
public function main()
{
$output = '';
if ($this->needsToUpdateAttachments()) {
$output = $this->updateAttachments();
}
if ($this->needsToUpdateDistricts()) {
$output .= $this->updateDistricts();
}
return $output;
}
/**
* @return bool
*/
private function needsToUpdateAttachments()
{
return $this->tablesExist('attachments') && $this->columnsExist('attachments') && $this->oldAttachmentsExist();
}
/**
* @return bool
*/
private function extensionsAreInstalled()
{
$result = true;
foreach ($this->requiredExtensions as $key) {
$result = $result && ExtensionManagementUtility::isLoaded($key);
}
return $result;
}
/**
* @param string $migrationKey
*
* @return bool
*/
private function tablesExist($migrationKey)
{
$result = true;
foreach ($this->requiredTables[$migrationKey] as $table) {
$result = $result && \Tx_Oelib_Db::existsTable($table);
}
return $result;
}
/**
* @param string $migrationKey
*
* @return bool
*/
private function columnsExist($migrationKey)
{
$result = true;
foreach ($this->requiredColumns[$migrationKey] as $table => $columns) {
foreach ($columns as $column) {
$result = $result && \Tx_Oelib_Db::tableHasColumn($table, $column);
}
}
return $result;
}
/**
* @return bool
*/
private function oldAttachmentsExist()
{
$numberOfAffectedObjects = \Tx_Oelib_Db::count(
'tx_realty_objects',
'deleted = 0 AND attachments = 0 AND (images > 0 OR documents > 0)'
);
return $numberOfAffectedObjects > 0;
}
/**
* @return string
*/
private function updateAttachments()
{
$output = '<h3>Converting the legacy images and documents to FAL attachments.</h3>';
$realtyObjects = \Tx_Oelib_Db::selectMultiple(
'*',
'tx_realty_objects',
'deleted = 0 AND attachments = 0 AND (images > 0 OR documents > 0)'
);
$output .= '<p>' . \count($realtyObjects) . ' objects are affected.</p>';
/** @var array $realtyObject */
foreach ($realtyObjects as $realtyObject) {
$output .= $this->updateAttachmentForSingleObject($realtyObject);
}
return $output;
}
/**
* @param array $realtyObject
*
* @return string
*/
private function updateAttachmentForSingleObject(array $realtyObject)
{
$objectUid = (int)$realtyObject['uid'];
$this->attachmentSorting[$objectUid] = 1;
$output = '<h4>Processing object #' . $objectUid . '</h4>';
$output .= $this->updateImagesForSingleObject($objectUid);
$output .= $this->updateDocumentsForSingleObject($objectUid);
\Tx_Oelib_Db::delete('tx_realty_images', 'object = ' . $objectUid);
\Tx_Oelib_Db::delete('tx_realty_documents', 'object = ' . $objectUid);
$numberOfImages = (int)$realtyObject['images'];
$numberOfDocuments = (int)$realtyObject['documents'];
\Tx_Oelib_Db::update(
'tx_realty_objects',
'uid = ' . $objectUid,
['attachments' => $numberOfImages + $numberOfDocuments, 'images' => 0, 'documents' => 0]
);
return $output;
}
/**
* @param int $objectUid
*
* @return string
*/
private function updateImagesForSingleObject($objectUid)
{
$images = \Tx_Oelib_Db::selectMultiple(
'*',
'tx_realty_images',
'deleted = 0 AND hidden = 0 AND object = ' . $objectUid,
'',
'sorting ASC'
);
if (empty($images)) {
return '';
}
$output = '<h5>Converting ' . \count($images) . ' image reference(s) to FAL …</h5>';
foreach ($images as $image) {
$file = $this->copyFile($image['image'], $objectUid, $image['caption']);
if ($file === null) {
continue;
}
$output .= '<p>Creating file record for path "' .
\htmlspecialchars($file->getIdentifier(), ENT_COMPAT | ENT_HTML5) .
'", file UID: ' . $file->getProperty('uid') . '</p>';
}
return $output;
}
/**
* @param int $objectUid
*
* @return string
*/
private function updateDocumentsForSingleObject($objectUid)
{
$documents = \Tx_Oelib_Db::selectMultiple(
'*',
'tx_realty_documents',
'deleted = 0 AND object = ' . $objectUid,
'',
'sorting ASC'
);
if (empty($documents)) {
return '';
}
$output = '<h5>Converting ' . \count($documents) . ' document reference(s) to FAL …</h5>';
foreach ($documents as $document) {
$file = $this->copyFile($document['filename'], $objectUid, $document['title']);
if ($file === null) {
continue;
}
$output .= '<p>Creating file record for path "' .
\htmlspecialchars($file->getIdentifier(), ENT_COMPAT | ENT_HTML5) .
'", file UID: ' . $file->getProperty('uid') . '</p>';
}
return $output;
}
/**
* @return ResourceStorage
*/
private function getDefaultStorage()
{
return $this->getResourceFactory()->getDefaultStorage();
}
/**
* @return ResourceFactory
*/
private function getResourceFactory()
{
return ResourceFactory::getInstance();
}
/**
* @param string $fileName
* @param int $objectUid
* @param string $title
*
* @return File|null
*/
private function copyFile($fileName, $objectUid, $title)
{
$absoluteFileName = $this->getAbsoluteRealtyUploadsFolder() . $fileName;
if (!\is_file($absoluteFileName)) {
return null;
}
$storage = $this->getDefaultStorage();
$folderPath = 'realty_attachments/' . $objectUid . '/';
if ($storage->hasFolder($folderPath)) {
$folder = $storage->getFolder($folderPath);
} else {
$folder = $storage->createFolder($folderPath);
}
if ($folder->hasFile($fileName)) {
$fileIdentifier = $folderPath . $fileName;
/** @var File $file */
$file = $this->getResourceFactory()
->getFileObjectByStorageAndIdentifier($storage->getUid(), $fileIdentifier);
} else {
/** @var File $file */
$file = $storage->addFile(
$absoluteFileName,
$folder,
$fileName,
DuplicationBehavior::RENAME,
false
);
}
$fileUid = (int)$file->getProperty('uid');
\Tx_Oelib_Db::update('sys_file_metadata', 'file = ' . $fileUid, ['title' => $title]);
$where = 'deleted = 0 AND uid_local = ' . $fileUid . ' AND uid_foreign = ' . $objectUid .
' AND tablenames = "tx_realty_objects" AND fieldname = "attachments"';
if (!\Tx_Oelib_Db::existsRecord('sys_file_reference', $where)) {
$timestamp = (int)$GLOBALS['SIM_EXEC_TIME'];
$referenceData = [
'uid_local' => $fileUid,
'uid_foreign' => $objectUid,
'tablenames' => 'tx_realty_objects',
'fieldname' => 'attachments',
'table_local' => 'sys_file',
'crdate' => $timestamp,
'tstamp' => $timestamp,
'sorting_foreign' => $this->attachmentSorting[$objectUid],
'l10n_diffsource' => '',
];
\Tx_Oelib_Db::insert('sys_file_reference', $referenceData);
$this->attachmentSorting[$objectUid]++;
}
return $file;
}
/**
* @return string including the trailing slash
*/
private function getAbsoluteRealtyUploadsFolder()
{
return GeneralUtility::getFileAbsFileName(self::UPLOAD_FOLDER);
}
/**
* @return bool
*/
private function needsToUpdateDistricts()
{
return $this->tablesExist('districts') && $this->oldDistrictsExist();
}
/**
* @return bool
*/
private function oldDistrictsExist()
{
$numberOfAffectedObjects = \Tx_Oelib_Db::count(
'tx_realty_districts',
'deleted = 0 AND city = 0 AND EXISTS(' .
'SELECT * FROM tx_realty_objects WHERE deleted = 0 AND city != 0 AND district = tx_realty_districts.uid' .
')'
);
return $numberOfAffectedObjects > 0;
}
/**
* @return string
*/
private function updateDistricts()
{
$output = '<h3>Assigning the cities to the districts</h3>';
$districts = \Tx_Oelib_Db::selectMultiple(
'*',
'tx_realty_districts',
'deleted = 0 AND city = 0 AND EXISTS(' .
'SELECT * FROM tx_realty_objects WHERE deleted = 0 AND city != 0 AND district = tx_realty_districts.uid' .
')'
);
$output .= '<p>' . \count($districts) . ' districts are affected.</p>';
/** @var array $district */
foreach ($districts as $district) {
$output .= $this->updateSingleDistrict($district);
}
return $output;
}
/**
* @param array $district
*
* @return string
*/
private function updateSingleDistrict(array $district)
{
$districtUid = (int)$district['uid'];
$districtTitle = $district['title'];
$output = '<p>Updating district "' . \htmlspecialchars($districtTitle, ENT_QUOTES | ENT_HTML5) .
'" (#' . $districtUid . ')<br/>';
$objectWithDistrict = \Tx_Oelib_Db::selectSingle(
'*',
'tx_realty_objects',
'deleted = 0 AND city != 0 AND district = ' . $districtUid
);
$cityUid = $objectWithDistrict['city'];
try {
$city = \Tx_Oelib_Db::selectSingle('*', 'tx_realty_cities', 'uid = ' . $cityUid . ' AND deleted = 0');
$cityUid = (int)$city['uid'];
$cityTitle = $city['title'];
\Tx_Oelib_Db::update('tx_realty_districts', 'uid = ' . $districtUid, ['city' => $cityUid]);
$output .= 'Assigning it to city "' . \htmlspecialchars($cityTitle, ENT_QUOTES | ENT_HTML5) .
'" (#' . $cityUid . ').';
} catch (\Tx_Oelib_Exception_EmptyQueryResult $exception) {
$output .= 'The city record seems to be missing (probably deleted).';
}
$output .= '</p>';
return $output;
}
}