Differential D10603 Diff 25476 src/infrastructure/storage/management/PhabricatorStorageManagementAPI.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/storage/management/PhabricatorStorageManagementAPI.php
Show First 20 Lines • Show All 176 Lines • ▼ Show 20 Lines | final class PhabricatorStorageManagementAPI { | ||||
public function applyPatchSQL($sql) { | public function applyPatchSQL($sql) { | ||||
$sql = Filesystem::readFile($sql); | $sql = Filesystem::readFile($sql); | ||||
$queries = preg_split('/;\s+/', $sql); | $queries = preg_split('/;\s+/', $sql); | ||||
$queries = array_filter($queries); | $queries = array_filter($queries); | ||||
$conn = $this->getConn(null); | $conn = $this->getConn(null); | ||||
$charset_info = $this->getCharsetInfo(); | |||||
list($charset, $collate_text, $collate_sort) = $charset_info; | |||||
foreach ($queries as $query) { | foreach ($queries as $query) { | ||||
$query = str_replace('{$NAMESPACE}', $this->namespace, $query); | $query = str_replace('{$NAMESPACE}', $this->namespace, $query); | ||||
$query = str_replace('{$CHARSET}', $charset, $query); | |||||
$query = str_replace('{$COLLATE_TEXT}', $collate_text, $query); | |||||
$query = str_replace('{$COLLATE_SORT}', $collate_sort, $query); | |||||
queryfx( | queryfx( | ||||
$conn, | $conn, | ||||
'%Q', | '%Q', | ||||
$query); | $query); | ||||
} | } | ||||
} | } | ||||
public function applyPatchPHP($script) { | public function applyPatchPHP($script) { | ||||
$schema_conn = $this->getConn(null); | $schema_conn = $this->getConn(null); | ||||
require_once $script; | require_once $script; | ||||
} | } | ||||
public function isCharacterSetAvailable($character_set) { | public function isCharacterSetAvailable($character_set) { | ||||
$conn = $this->getConn(null); | $conn = $this->getConn(null); | ||||
$result = queryfx_one( | $result = queryfx_one( | ||||
$conn, | $conn, | ||||
'SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.CHARACTER_SETS | 'SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.CHARACTER_SETS | ||||
WHERE CHARACTER_SET_NAME = %s', | WHERE CHARACTER_SET_NAME = %s', | ||||
$character_set); | $character_set); | ||||
return (bool)$result; | return (bool)$result; | ||||
} | } | ||||
public function getCharsetInfo() { | |||||
if ($this->isCharacterSetAvailable('utf8mb4')) { | |||||
// If utf8mb4 is available, we use it with the utf8mb4_unicode_ci | |||||
// collation. This is most correct, and will sort properly. | |||||
$charset = 'utf8mb4'; | |||||
$collate_text = 'utf8mb4_bin'; | |||||
$collate_sort = 'utf8mb4_unicode_ci'; | |||||
} else { | |||||
// If utf8mb4 is not available, we use binary. This allows us to store | |||||
// 4-byte unicode characters. This has some tradeoffs: | |||||
// | |||||
// Unicode characters won't sort correctly. There's nothing we can do | |||||
// about this while still supporting 4-byte characters. | |||||
// | |||||
// It's possible that strings will be truncated in the middle of a | |||||
// character on insert. We encourage users to set STRICT_ALL_TABLES | |||||
// to prevent this. | |||||
$charset = 'binary'; | |||||
$collate_text = 'binary'; | |||||
$collate_sort = 'binary'; | |||||
} | |||||
return array($charset, $collate_text, $collate_sort); | |||||
} | |||||
} | } |