1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class DataIntegrityTestUTF8 extends BuildTask |
5
|
|
|
{ |
6
|
|
|
private static $replacement_array = array( |
7
|
|
|
'Â' => '', |
8
|
|
|
'Â' => '', |
9
|
|
|
'Â' => '', |
10
|
|
|
'’' => ''', |
11
|
|
|
'–' => '—', |
12
|
|
|
'
' => '', |
13
|
|
|
'“' => '"', |
14
|
|
|
'â€^Ý' => '"', |
15
|
|
|
'<br>' => '<br />', |
16
|
|
|
'•' => '•', |
17
|
|
|
'Ý' => '- ' |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* standard SS variable |
22
|
|
|
* @var String |
23
|
|
|
*/ |
24
|
|
|
protected $title = "Convert tables to utf-8 and replace funny characters."; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* standard SS variable |
28
|
|
|
* @var String |
29
|
|
|
*/ |
30
|
|
|
protected $description = "Converts table to utf-8 by replacing a bunch of characters that show up in the Silverstripe Conversion. CAREFUL: replaces all tables in Database to utf-8!"; |
31
|
|
|
|
32
|
|
|
public function run($request) |
33
|
|
|
{ |
34
|
|
|
ini_set('max_execution_time', 3000); |
35
|
|
|
$tables = DB::query('SHOW tables'); |
36
|
|
|
$unique = array(); |
|
|
|
|
37
|
|
|
$arrayOfReplacements = Config::inst()->get("DataIntegrityTestUTF8", "replacement_array"); |
38
|
|
|
foreach ($tables as $table) { |
39
|
|
|
$table = array_pop($table); |
40
|
|
|
DB::query("ALTER TABLE \"$table\" CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci"); |
41
|
|
|
DB::alteration_message("<h2>Resetting $table to utf8</h2>"); |
42
|
|
|
$this->flushNow(); |
43
|
|
|
$originatingClass = str_replace("_Live", "", $table); |
44
|
|
|
if (class_exists($originatingClass) && !class_exists($table)) { |
45
|
|
|
} else { |
46
|
|
|
$originatingClass = $table; |
47
|
|
|
} |
48
|
|
|
if (class_exists($originatingClass)) { |
49
|
|
|
$fields = Config::inst()->get($originatingClass, "db", $uninherited = 1); |
50
|
|
|
if ($fields && count($fields)) { |
51
|
|
|
$unusedFields = array(); |
52
|
|
|
foreach ($fields as $fieldName => $type) { |
|
|
|
|
53
|
|
|
$usedFieldsChanged = array("CHECKING $table.$fieldName : "); |
54
|
|
|
if (substr($type, 0, 4) == "HTML") { |
55
|
|
|
foreach ($arrayOfReplacements as $from => $to) { |
|
|
|
|
56
|
|
|
DB::query("UPDATE \"$table\" SET \"$fieldName\" = REPLACE(\"$fieldName\", '$from', '$to');"); |
57
|
|
|
$count = DB::getConn()->affectedRows(); |
|
|
|
|
58
|
|
|
$toWord = $to; |
59
|
|
|
if ($to == '') { |
60
|
|
|
$toWord = '[NOTHING]'; |
61
|
|
|
} |
62
|
|
|
$usedFieldsChanged[] = "$count Replacements <strong>$from</strong> with <strong>$toWord</strong>"; |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
$unusedFields[] = $fieldName; |
66
|
|
|
} |
67
|
|
|
if (count($usedFieldsChanged)) { |
68
|
|
|
DB::alteration_message(implode("<br /> - ", $usedFieldsChanged)); |
69
|
|
|
$this->flushNow(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
if (count($unusedFields)) { |
73
|
|
|
DB::alteration_message("Skipped the following fields: ".implode(",", $unusedFields)); |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
DB::alteration_message("No fields for $originatingClass"); |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
DB::alteration_message("Skipping $originatingClass - class can not be found"); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
DB::alteration_message("<hr /><hr /><hr /><hr /><hr /><hr /><hr />COMPLETED<hr /><hr /><hr /><hr /><hr /><hr /><hr />"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function flushNow() |
86
|
|
|
{ |
87
|
|
|
// check that buffer is actually set before flushing |
88
|
|
|
if (ob_get_length()) { |
89
|
|
|
@ob_flush(); |
|
|
|
|
90
|
|
|
@flush(); |
|
|
|
|
91
|
|
|
@ob_end_flush(); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
@ob_start(); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.