Skip to content

Commit 6f795f6

Browse files
xh3n1michield
authored andcommitted
Changed url and regex fields from varchar to text and check if indexes exist (#377)
* change data field from blob to mediumblob Signed-off-by: Xheni Myrtaj <[email protected]> * Create fuction to check if index exist Signed-off-by: Xheni Myrtaj <[email protected]> * Changed fields and indexes for fresh new installations and upgrade Signed-off-by: Xheni Myrtaj <[email protected]> * create doc for the function Signed-off-by: Xheni Myrtaj <[email protected]>
1 parent b0f28d0 commit 6f795f6

File tree

3 files changed

+92
-17
lines changed

3 files changed

+92
-17
lines changed

public_html/lists/admin/mysqli.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,22 @@ function Sql_Table_Column_Exists($table, $column)
349349
}
350350
}
351351
}
352+
/**
353+
* Check if a specific INDEX exists in a specific table
354+
* @param string $table The table name
355+
* @param string $index The key name
356+
* @return bool
357+
*/
358+
function table_index_exists($table, $index) {
359+
$e = false;
360+
if ($result = Sql_Query("SHOW INDEX FROM $table WHERE Key_name = '$index'")) {
361+
if($result->num_rows >= 1) {
362+
$e = true;
363+
}
364+
}
365+
mysqli_free_result($result);
366+
return $e;
367+
}
352368

353369
function Sql_Check_For_Table($table)
354370
{

public_html/lists/admin/structure.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,11 @@
359359
),
360360
'urlcache' => array(
361361
'id' => array('integer not null primary key auto_increment', 'ID'),
362-
'url' => array('varchar(255) not null', ''),
362+
'url' => array('text not null', ''),
363363
'lastmodified' => array('integer', ''),
364364
'added' => array('datetime', ''),
365365
'content' => array('longblob', ''),
366-
'index_1' => array('urlindex (url)', ''),
366+
'index_1' => array('urlindex (url(255))', ''),
367367
),
368368
//"gchartcache" => array(
369369
//"url" => array("text not null",""),
@@ -380,17 +380,17 @@
380380
'linkid' => array('integer not null primary key auto_increment', 'Link ID'),
381381
'messageid' => array('integer not null', 'Message ID'),
382382
'userid' => array('integer not null', 'subscriber ID'),
383-
'url' => array('varchar(255)', 'URL to log'),
383+
'url' => array('text', 'URL to log'),
384384
'forward' => array('text', 'URL to forward to'),
385385
'firstclick' => array('datetime', 'When first clicked'),
386386
'latestclick' => array('timestamp', 'When last clicked'),
387387
'clicked' => array('integer default 0', 'Number of clicks'),
388388
'index_1' => array('midindex (messageid)', ''),
389389
'index_2' => array('uidindex (userid)', ''),
390-
'index_3' => array('urlindex (url)', ''),
390+
'index_3' => array('urlindex (url(255))', ''),
391391
'index_4' => array('miduidindex (messageid,userid)', ''),
392392
// "index_5" => array("miduidurlindex (messageid,userid,url)",""),
393-
'unique_1' => array('miduidurlindex (messageid,userid,url)', ''),
393+
'unique_1' => array('miduidurlindex (messageid,userid,url(255))', ''),
394394
),
395395
'linktrack_ml' => array( // ml -> message link
396396
'messageid' => array('integer not null', 'Message ID'),
@@ -425,14 +425,14 @@
425425
),
426426
'linktrack_forward' => array(
427427
'id' => array('integer not null primary key auto_increment', 'forward ID'),
428-
'url' => array('varchar(255)', 'URL to log'),
428+
'url' => array('text', 'URL to log'),
429429
'uuid' => array('varchar(36) default ""', 'UUID'),
430430
// "forward" => array("text","URL to forward to"),
431431
'personalise' => array('tinyint default 0', 'Forward adding the UID?'),
432-
'index_1' => array('urlindex (url)', ''),
432+
'index_1' => array('urlindex (url(255))', ''),
433433
// "index_2" => array("urlforwardindex (url,forward(255))",""),
434434
// 'unique_1' => array('fwdunique (forward (500))','Forward should be unique'),
435-
'unique_1' => array('urlunique (url)', 'URL should be unique'),
435+
'unique_1' => array('urlunique (url(255))', 'URL should be unique'),
436436
'index_2' => array('uuididx (uuid)', 'sys:index'),
437437
),
438438
'linktrack_userclick' => array(
@@ -468,14 +468,14 @@
468468
// ),
469469
'bounceregex' => array(
470470
'id' => array('integer not null primary key auto_increment', 'ID'),
471-
'regex' => array('varchar(255)', 'Regex'),
471+
'regex' => array('text', 'Regex'),
472472
'action' => array('varchar(255)', 'Action on rule'),
473473
'listorder' => array('integer default 0', ''),
474474
'admin' => array('integer', ''),
475475
'comment' => array('text', ''),
476476
'status' => array('varchar(255)', ''),
477477
'count' => array('integer default 0', 'Count of matching bounces on this rule'),
478-
'unique_1' => array('regex (regex)', ''),
478+
'unique_1' => array('regex (regex(255))', ''),
479479
),
480480
'bounceregex_bounce' => array(
481481
'regex' => array('integer not null', 'Related regex'),
@@ -519,4 +519,4 @@
519519

520520
);
521521

522-
$DBstruct = $DBstructuser + $DBstructphplist; # order of tables is essential for smooth upgrade
522+
$DBstruct = $DBstructuser + $DBstructphplist; # order of tables is essential for smooth upgrade

public_html/lists/admin/upgrade.php

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function output($message)
3131
flush();
3232
}
3333

34-
output("");
34+
output("");
3535
$dbversion = getConfig('version');
3636
$releaseDBversion = getConfig('releaseDBversion'); // release version check
3737
$inUpgrade = getConfig('in-upgrade-to');
@@ -110,13 +110,72 @@ function output($message)
110110
//# lock this process
111111
SaveConfig('in-upgrade-to', VERSION, 1);
112112

113+
if (version_compare($dbversion, '3.3.4','<')) {
114+
115+
Sql_Query("alter table {$GLOBALS['tables']['bounce']} modify data mediumblob ");
116+
117+
$indexesToRecreate = array(
118+
'urlcache' => array(
119+
'urlindex' => array('value' => 'url(255)', 'unique' => false),
120+
),
121+
'linktrack' => array(
122+
'urlindex' => array('value' => 'url(255)', 'unique' => false),
123+
'miduidurlindex' => array('value' => 'messageid,userid,url(255)', 'unique' => true),
124+
),
125+
'linktrack_forward' => array(
126+
'urlindex' => array('value' => 'url(255)', 'unique' => false),
127+
'urlunique' => array('value' => 'url(255)', 'unique' => true),
128+
),
129+
'bounceregex' => array(
130+
'regex' => array('value' => 'regex(255)', 'unique'=> true),
131+
),
132+
);
133+
134+
$tablesToAlter = array(
135+
'urlcache' => array('url'),
136+
'linktrack' => array('url', 'forward'),
137+
'linktrack_forward' => array('url'),
138+
'bounceregex' => array('regex'),
139+
);
140+
141+
foreach($indexesToRecreate as $table => $indexes) {
142+
143+
144+
foreach($indexes as $indexName => $settings) {
145+
146+
$exists = table_index_exists($GLOBALS['tables'][$table],$indexName);
147+
if ($exists) {
148+
Sql_Query("drop index $indexName on {$GLOBALS['tables'][$table]} ");
149+
}
150+
}
151+
152+
$alteringOperations = $tablesToAlter[$table];
153+
foreach($alteringOperations as $operation) {
154+
Sql_Query("alter table {$GLOBALS['tables'][$table]} modify $operation text ");
155+
}
156+
157+
foreach($indexes as $indexName => $settings) {
158+
$createStmt = '';
159+
if($settings['unique'] === true) {
160+
$createStmt = 'create unique index';
161+
} else {
162+
$createStmt = 'create index';
163+
}
164+
165+
Sql_Query("$createStmt $indexName on {$GLOBALS['tables'][$table]}({$settings['value']})");
166+
}
167+
168+
}
169+
}
170+
171+
113172
//# remember whether we've done this, to avoid doing it every time
114173
//# even thought that's not such a big deal
115174
$isUTF8 = getConfig('UTF8converted');
116175

117176
if (empty($isUTF8)) {
118177
$maxsize = 0;
119-
$req = Sql_Query('select (data_length+index_length) tablesize
178+
$req = Sql_Query('select (data_length+index_length) tablesize
120179
from information_schema.tables
121180
where table_schema="' .$GLOBALS['database_name'].'"');
122181

@@ -273,8 +332,8 @@ function output($message)
273332
// it is not strictly necessary to do this here, because processqueue does it as well.
274333
// that does mean that the first process queue may take a while.
275334

276-
// output(s('Giving a UUID to your subscribers and campaigns. If you have a lot of them, this may take a while.'));
277-
// output(s('If the page times out, you can reload. Or otherwise try to run the upgrade from commandline instead.').' '.resourceLink('https://resources.phplist.com/system/commandline', s('Documentation how to set up phpList commandline')));
335+
// output(s('Giving a UUID to your subscribers and campaigns. If you have a lot of them, this may take a while.'));
336+
// output(s('If the page times out, you can reload. Or otherwise try to run the upgrade from commandline instead.').' '.resourceLink('https://resources.phplist.com/system/commandline', s('Documentation how to set up phpList commandline')));
278337
} else {
279338
output(s('Giving a UUID to your subscribers and campaigns. If you have a lot of them, this may take a while.'));
280339
output(s('If the page times out, you can reload. Or otherwise try to run the upgrade from commandline instead.').' '.resourceLink('https://resources.phplist.com/system/commandline', s('Documentation how to set up phpList commandline')));
@@ -304,9 +363,9 @@ function output($message)
304363
}
305364

306365
if (version_compare($dbversion, '3.3.3','<')) {
307-
// add a draft campaign for invite plugin
366+
// add a draft campaign for invite plugin
308367
addInviteCampaign();
309-
368+
310369
}
311370

312371
if (version_compare($dbversion, '3.3.4','<')) {

0 commit comments

Comments
 (0)