-
Notifications
You must be signed in to change notification settings - Fork 126
/
class.ext_update.php
307 lines (276 loc) · 10.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
<?php
namespace DmitryDulepov\Realurl;
/***************************************************************
* Copyright notice
*
* (c) 2016 Dmitry Dulepov <dmitry.dulepov@gmail.com>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project 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 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script 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.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* This class updates realurl from version 1.x to 2.x.
*
* @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
*/
class ext_update {
/** Defines characters that cannot appear in the tx_realurl_data.spealing_url field */
const MYSQL_REGEXP_FOR_NON_URL_CHARACTERS = '[^a-zA-Z0-9\._%\-/\?=]';
/** @var \TYPO3\CMS\Core\Database\DatabaseConnection */
protected $databaseConnection;
/**
* Creates the instance of the class.
*/
public function __construct() {
$this->databaseConnection = $GLOBALS['TYPO3_DB'];
}
/**
* Runs the update.
*/
public function main() {
$locker = $this->getLocker();
try {
if ($locker) {
$locker->acquire();
}
}
catch (\Exception $e) {
// Nothing
}
$this->checkAndRenameTables();
$this->checkAndUpdatePathCachePrimaryKey();
$this->updateRealurlTableStructure();
$this->removeUrlDataEntriesWithIgnoredParameters();
$this->updateCJKSpeakingUrls();
$this->updateUrlHashes();
$this->updateZeroPids();
if ($locker && (method_exists($locker, 'isAcquired') && $locker->isAcquired() || method_exists($locker, 'getLockStatus') && $locker->getLockStatus())) {
$locker->release();
}
}
/**
* Checks if the script should execute. We check for everything except table
* structure.
*
* @return bool
*/
public function access() {
return $this->hasOldCacheTables() || $this->pathCacheNeedsUpdates() || $this->hasUnencodedCJKCharacters() ||
$this->hasEmptyUrlHashes() || $this->hasNonZeroPids()
;
}
/**
* Checks and renames *cache tables to *data tables.
*/
protected function checkAndRenameTables() {
$tableMap = array(
'tx_realurl_pathcache' => 'tx_realurl_pathdata',
'tx_realurl_urlcache' => 'tx_realurl_urldata',
);
$tables = $this->databaseConnection->admin_get_tables();
foreach ($tableMap as $oldTableName => $newTableName) {
if (isset($tables[$oldTableName])) {
if (!isset($tables[$newTableName])) {
$this->databaseConnection->sql_query('ALTER TABLE ' . $oldTableName . ' RENAME TO ' . $newTableName);
}
else {
if ((int)$tables[$newTableName]['Rows'] === 0) {
$this->databaseConnection->sql_query('DROP TABLE ' . $newTableName);
$this->databaseConnection->sql_query('CREATE TABLE ' . $newTableName . ' LIKE ' . $oldTableName);
$this->databaseConnection->sql_query('INSERT INTO ' . $newTableName . ' SELECT * FROM ' . $oldTableName);
}
$this->databaseConnection->sql_query('DROP TABLE ' . $oldTableName);
}
}
}
}
/**
* Checks if the primary key needs updates (this is something that TYPO3
* sql parser fails to do for years) and does necessary changes.
*
* @return void
*/
protected function checkAndUpdatePathCachePrimaryKey() {
if ($this->pathCacheNeedsUpdates()) {
$this->databaseConnection->sql_query('ALTER TABLE tx_realurl_pathdata CHANGE cache_id uid int(11) NOT NULL');
$this->databaseConnection->sql_query('ALTER TABLE tx_realurl_pathdata DROP PRIMARY KEY');
$this->databaseConnection->sql_query('ALTER TABLE tx_realurl_pathdata MODIFY uid int(11) NOT NULL auto_increment PRIMARY KEY');
}
}
/**
* Obtains the locker depending on the TYPO3 version.
*
* @return \TYPO3\CMS\Core\Locking\Locker|\TYPO3\CMS\Core\Locking\LockingStrategyInterface
*/
protected function getLocker() {
if (class_exists('\\TYPO3\\CMS\\Core\\Locking\\LockFactory')) {
$locker = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Locking\\LockFactory')->createLocker('tx_realurl_update');
}
elseif (class_exists('\\TYPO3\\CMS\\Core\\Locking\\Locker')) {
$locker = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Locking\\Locker', 'tx_realurl_update');
}
else {
$locker = null;
}
return $locker;
}
/**
* Checks if the database has empty url hashes.
*
* @return bool
*/
protected function hasEmptyUrlHashes() {
$count = $this->databaseConnection->exec_SELECTcountRows(
'*',
'tx_realurl_urldata',
'original_url_hash=0 OR speaking_url_hash=0'
);
// If $count is false, it means that fields are missing. So we force
// the update because table structure will be also fixed by the updater.
return $count === false || $count > 0;
}
/**
* Checks if the system has old cache tables.
*
* @return bool
*/
protected function hasOldCacheTables() {
$tables = $this->databaseConnection->admin_get_tables();
return isset($tables['tx_realurl_pathcache']) || isset($tables['tx_realurl_urlcache']);
}
/**
* Checks if tx_realurl_urldata has unencoded CJK characters.
*
* @return bool
*/
protected function hasUnencodedCJKCharacters() {
if (!ExtensionManagementUtility::isLoaded('dbal')) {
$count = $this->databaseConnection->exec_SELECTcountRows('*', 'tx_realurl_urldata', 'speaking_url RLIKE \'' . self::MYSQL_REGEXP_FOR_NON_URL_CHARACTERS . '\'');
}
else {
$count = 0;
}
return ($count > 0);
}
/**
* Checks if the system has zer pid values in realurl tables.
*
* @return bool
*/
protected function hasNonZeroPids()
{
$count = $this->databaseConnection->exec_SELECTcountRows('*', 'tx_realurl_urldata', 'pid<>0');
$count += $this->databaseConnection->exec_SELECTcountRows('*', 'tx_realurl_pathdata', 'pid<>0');
return $count > 0;
}
/**
* Checks if path cache table is ok.
*
* @return bool
*/
protected function pathCacheNeedsUpdates() {
$fields = $this->databaseConnection->admin_get_fields('tx_realurl_pathdata');
return isset($fields['cache_id']) || !isset($fields['uid']) || stripos($fields['uid']['Extra'], 'auto_increment') === false;
}
/**
* Removes entries with parameters that should be ignored.
*/
protected function removeUrlDataEntriesWithIgnoredParameters() {
$this->databaseConnection->exec_DELETEquery('tx_realurl_urldata', 'original_url RLIKE \'(^|&)(utm_[a-z]+|pk_campaign|pk_kwd)=\'');
}
/**
* Converts CJK characters to url-encoded form.
*
* @see https://github.com/dmitryd/typo3-realurl/issue/378
* @see http://www.regular-expressions.info/unicode.html#script
* @see http://php.net/manual/en/regexp.reference.unicode.php
*/
protected function updateCJKSpeakingUrls() {
if (!ExtensionManagementUtility::isLoaded('dbal')) {
$resource = $this->databaseConnection->exec_SELECTquery('uid, speaking_url', 'tx_realurl_urldata', 'speaking_url RLIKE \'' . self::MYSQL_REGEXP_FOR_NON_URL_CHARACTERS . '\'');
while (false !== ($data = $this->databaseConnection->sql_fetch_assoc($resource))) {
if (preg_match('/[\p{Han}\p{Hangul}\p{Kannada}\p{Katakana}\p{Hiragana}\p{Tai_Tham}\p{Thai}]+/u', $data['speaking_url'])) {
// Note: cannot use parse_url() here because it corrupts CJK characters!
list($path, $query) = explode('?', $data['speaking_url']);
$segments = explode('/', $path);
array_walk($segments, function(&$segment) {
$segment = rawurlencode($segment);
});
$url = implode('/', $segments);
if (!empty($query)) {
$url .= '?' . $query;
}
$this->databaseConnection->exec_UPDATEquery('tx_realurl_urldata', 'uid=' . (int)$data['uid'], array(
'speaking_url' => $url
));
}
}
$this->databaseConnection->sql_free_result($resource);
}
}
/**
* Updates realurl table structure. The code is copied almost 1:1 from
* ExtensionManagerTables class.
*
* We ignore any errors because nothing can be done about those really. The
* client will have to do database update anyway, so he will see all failed
* queries.
*
* @return void
*/
protected function updateRealurlTableStructure() {
$updateStatements = array();
// Get all necessary statements for ext_tables.sql file
$rawDefinitions = file_get_contents(ExtensionManagementUtility::extPath('realurl', 'ext_tables.sql'));
$sqlParser = GeneralUtility::makeInstance('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
$fieldDefinitionsFromFile = $sqlParser->getFieldDefinitions_fileContent($rawDefinitions);
if (count($fieldDefinitionsFromFile)) {
$fieldDefinitionsFromCurrentDatabase = $sqlParser->getFieldDefinitions_database();
$diff = $sqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
$updateStatements = $sqlParser->getUpdateSuggestions($diff);
}
foreach ((array)$updateStatements['add'] as $string) {
$this->databaseConnection->admin_query($string);
}
foreach ((array)$updateStatements['change'] as $string) {
$this->databaseConnection->admin_query($string);
}
foreach ((array)$updateStatements['create_table'] as $string) {
$this->databaseConnection->admin_query($string);
}
}
/**
* Updates URL hashes.
*/
protected function updateUrlHashes() {
$this->databaseConnection->sql_query('UPDATE tx_realurl_urldata SET
original_url_hash=CRC32(original_url),
speaking_url_hash=CRC32(speaking_url)
WHERE original_url_hash=0 OR speaking_url_hash=0
');
}
/**
* Updates zero pid values.
*/
protected function updateZeroPids()
{
$this->databaseConnection->sql_query('UPDATE tx_realurl_urldata SET pid=0');
$this->databaseConnection->sql_query('UPDATE tx_realurl_pathdata SET pid=0');
}
}