3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
33 class CRM_Core_I18n_Schema
{
36 * Drop all views (for use by CRM_Core_DAO::dropAllTables() mostly).
38 public static function dropAllViews() {
39 $domain = new CRM_Core_DAO_Domain();
41 if (!$domain->locales
) {
45 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
46 $tables = CRM_Core_I18n_SchemaStructure
::tables();
48 foreach ($locales as $locale) {
49 foreach ($tables as $table) {
50 CRM_Core_DAO
::executeQuery("DROP VIEW IF EXISTS {$table}_{$locale}");
56 * Switch database from single-lang to multi (by adding
57 * the first language and dropping the original columns).
59 * @param string $locale
60 * the first locale to create (migrate to).
62 public static function makeMultilingual($locale) {
63 $domain = new CRM_Core_DAO_Domain();
66 // break early if the db is already multi-lang
67 if ($domain->locales
) {
71 $dao = new CRM_Core_DAO();
73 // build the column-adding SQL queries
74 $columns = CRM_Core_I18n_SchemaStructure
::columns();
75 $indices = CRM_Core_I18n_SchemaStructure
::indices();
77 foreach ($columns as $table => $hash) {
79 if (isset($indices[$table])) {
80 foreach ($indices[$table] as $index) {
81 if (CRM_Core_BAO_SchemaHandler
::checkIfIndexExists($table, $index['name'])) {
82 $queries[] = "DROP INDEX {$index['name']} ON {$table}";
87 foreach ($hash as $column => $type) {
88 $queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}";
89 if (CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($table, $column)) {
90 $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}";
91 $queries[] = "ALTER TABLE {$table} DROP {$column}";
96 $queries[] = self
::createViewQuery($locale, $table, $dao);
99 $queries = array_merge($queries, array_values(self
::createIndexQueries($locale, $table)));
102 // execute the queries without i18n rewriting
103 foreach ($queries as $query) {
104 $dao->query($query, FALSE);
107 // update civicrm_domain.locales
108 $domain->locales
= $locale;
113 * Switch database from multi-lang back to single (by dropping
114 * additional columns and views and retaining only the selected locale).
116 * @param string $retain
117 * the locale to retain.
119 public static function makeSinglelingual($retain) {
120 $domain = new CRM_Core_DAO_Domain();
122 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
124 // break early if the db is already single-lang
129 // lets drop all triggers first
130 $logging = new CRM_Logging_Schema();
131 $logging->dropTriggers();
133 // turn subsequent tables singlelingual
134 $tables = CRM_Core_I18n_SchemaStructure
::tables();
135 foreach ($tables as $table) {
136 self
::makeSinglelingualTable($retain, $table);
139 // update civicrm_domain.locales
140 $domain->locales
= 'NULL';
143 //CRM-6963 -fair assumption.
147 // now lets rebuild all triggers
148 CRM_Core_DAO
::triggerRebuild();
152 * Switch a given table from multi-lang to single (by retaining only the selected locale).
154 * @param string $retain
155 * the locale to retain.
156 * @param string $table
157 * the table containing the column.
158 * @param string $class
159 * schema structure class to use to recreate indices.
161 * @param array $triggers
163 public static function makeSinglelingualTable(
166 $class = 'CRM_Core_I18n_SchemaStructure',
169 $domain = new CRM_Core_DAO_Domain();
171 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
173 // break early if the db is already single-lang
178 $columns =& $class::columns();
179 $indices =& $class::indices();
181 $dropQueries = array();
183 if (isset($indices[$table])) {
184 foreach ($indices[$table] as $index) {
185 foreach ($locales as $loc) {
186 $queries[] = "DROP INDEX {$index['name']}_{$loc} ON {$table}";
192 foreach ($columns[$table] as $column => $type) {
193 $queries[] = "ALTER TABLE {$table} ADD {$column} {$type}";
194 $queries[] = "UPDATE {$table} SET {$column} = {$column}_{$retain}";
195 foreach ($locales as $loc) {
196 $dropQueries[] = "ALTER TABLE {$table} DROP {$column}_{$loc}";
201 foreach ($locales as $loc) {
202 $queries[] = "DROP VIEW {$table}_{$loc}";
205 // add original indices
206 $queries = array_merge($queries, self
::createIndexQueries(NULL, $table));
208 // execute the queries without i18n rewriting
209 $dao = new CRM_Core_DAO();
210 foreach ($queries as $query) {
211 $dao->query($query, FALSE);
214 foreach ($dropQueries as $query) {
215 $dao->query($query, FALSE);
218 if (!empty($triggers)) {
219 if (CRM_Core_Config
::isUpgradeMode()) {
220 foreach ($triggers as $triggerInfo) {
221 $when = $triggerInfo['when'];
222 $event = $triggerInfo['event'];
223 $triggerName = "{$table}_{$when}_{$event}";
224 CRM_Core_DAO
::executeQuery("DROP TRIGGER IF EXISTS {$triggerName}");
228 // invoke the meta trigger creation call
229 CRM_Core_DAO
::triggerRebuild($table);
234 * Add a new locale to a multi-lang db, setting
235 * its values to the current default locale.
237 * @param string $locale
238 * the new locale to add.
239 * @param string $source
240 * the locale to copy from.
242 public static function addLocale($locale, $source) {
243 // get the current supported locales
244 $domain = new CRM_Core_DAO_Domain();
246 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
248 // break early if the locale is already supported
249 if (in_array($locale, $locales)) {
253 $dao = new CRM_Core_DAO();
255 // build the required SQL queries
256 $columns = CRM_Core_I18n_SchemaStructure
::columns();
257 $indices = CRM_Core_I18n_SchemaStructure
::indices();
259 foreach ($columns as $table => $hash) {
261 foreach ($hash as $column => $type) {
262 // CRM-7854: skip existing columns
263 if (CRM_Core_DAO
::checkFieldExists($table, "{$column}_{$locale}", FALSE)) {
266 $queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}";
267 $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}_{$source}";
271 $queries[] = self
::createViewQuery($locale, $table, $dao);
274 $queries = array_merge($queries, array_values(self
::createIndexQueries($locale, $table)));
277 // execute the queries without i18n rewriting
278 foreach ($queries as $query) {
279 $dao->query($query, FALSE);
282 // update civicrm_domain.locales
283 $locales[] = $locale;
284 $domain->locales
= implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $locales);
287 // invoke the meta trigger creation call
288 CRM_Core_DAO
::triggerRebuild();
292 * Rebuild multilingual indices, views and triggers (useful for upgrades)
294 * @param array $locales
295 * locales to be rebuilt.
296 * @param string $version
297 * version of schema structure to use.
299 public static function rebuildMultilingualSchema($locales, $version = NULL) {
301 $latest = self
::getLatestSchema($version);
302 require_once "CRM/Core/I18n/SchemaStructure_{$latest}.php";
303 $class = "CRM_Core_I18n_SchemaStructure_{$latest}";
306 $class = 'CRM_Core_I18n_SchemaStructure';
308 $indices =& $class::indices();
309 $tables =& $class::tables();
311 $dao = new CRM_Core_DAO();
313 // get all of the already existing indices
315 foreach (array_keys($indices) as $table) {
316 $existing[$table] = array();
317 $dao->query("SHOW INDEX FROM $table", FALSE);
318 while ($dao->fetch()) {
319 if (preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $dao->Key_name
)) {
320 $existing[$table][] = $dao->Key_name
;
325 // from all of the CREATE INDEX queries fetch the ones creating missing indices
326 foreach ($locales as $locale) {
327 foreach (array_keys($indices) as $table) {
328 $allQueries = self
::createIndexQueries($locale, $table, $class);
329 foreach ($allQueries as $name => $query) {
330 if (!in_array("{$name}_{$locale}", $existing[$table])) {
338 foreach ($locales as $locale) {
339 foreach ($tables as $table) {
340 $queries[] = self
::createViewQuery($locale, $table, $dao, $class);
345 $last = array_pop($locales);
347 foreach ($queries as $query) {
348 $dao->query($query, FALSE);
351 // invoke the meta trigger creation call
352 CRM_Core_DAO
::triggerRebuild();
356 * Rewrite SQL query to use views to access tables with localized columns.
358 * @param string $query
359 * the query for rewrite.
362 * the rewritten query
364 public static function rewriteQuery($query) {
366 $tables = self
::schemaStructureTables();
367 foreach ($tables as $table) {
369 // should match the civicrm table name such as: civicrm_event
370 // but must not match the table name if it's a substring of another table: civicrm_events_in_cart
371 $query = preg_replace("/([^'\"])({$table})(\z|[^a-z_'\"])/", "\\1\\2{$dbLocale}\\3", $query);
373 // uncomment the below to rewrite the civicrm_value_* queries
374 // $query = preg_replace("/(civicrm_value_[a-z0-9_]+_\d+)([^_])/", "\\1{$dbLocale}\\2", $query);
379 * @param null $version
384 public static function schemaStructureTables($version = NULL, $force = FALSE) {
385 static $_tables = NULL;
386 if ($_tables === NULL ||
$force) {
388 $latest = self
::getLatestSchema($version);
389 // FIXME: Doing require_once is a must here because a call like CRM_Core_I18n_SchemaStructure_4_1_0 makes
390 // class loader look for file like - CRM/Core/I18n/SchemaStructure/4/1/0.php which is not what we want to be loaded
391 require_once "CRM/Core/I18n/SchemaStructure_{$latest}.php";
392 $class = "CRM_Core_I18n_SchemaStructure_{$latest}";
393 $tables =& $class::tables();
396 $tables = CRM_Core_I18n_SchemaStructure
::tables();
408 public static function getLatestSchema($version) {
409 // remove any .upgrade sub-str from version. Makes it easy to do version_compare & give right result
410 $version = str_ireplace(".upgrade", "", $version);
412 // fetch all the SchemaStructure versions we ship and sort by version
414 foreach (scandir(dirname(__FILE__
)) as $file) {
416 if (preg_match('/^SchemaStructure_([0-9a-z_]+)\.php$/', $file, $matches)) {
417 $schemas[] = str_replace('_', '.', $matches[1]);
420 usort($schemas, 'version_compare');
422 // find the latest schema structure older than (or equal to) $version
424 $latest = array_pop($schemas);
425 } while (version_compare($latest, $version, '>'));
427 return str_replace('.', '_', $latest);
431 * CREATE INDEX queries for a given locale and table.
433 * @param string $locale
434 * locale for which the queries should be created (null to create original indices).
435 * @param string $table
436 * table for which the queries should be created.
437 * @param string $class
438 * schema structure class to use.
441 * array of CREATE INDEX queries
443 private static function createIndexQueries($locale, $table, $class = 'CRM_Core_I18n_SchemaStructure') {
444 $indices =& $class::indices();
445 $columns =& $class::columns();
446 if (!isset($indices[$table])) {
451 foreach ($indices[$table] as $index) {
452 $unique = isset($index['unique']) && $index['unique'] ?
'UNIQUE' : '';
453 foreach ($index['field'] as $i => $col) {
454 // if a given column is localizable, extend its name with the locale
455 if ($locale and isset($columns[$table][$col])) {
456 $index['field'][$i] = "{$col}_{$locale}";
459 $cols = implode(', ', $index['field']);
460 $name = $index['name'];
462 $name .= '_' . $locale;
464 // CRM-7854: skip existing indices
465 if (CRM_Core_DAO
::checkConstraintExists($table, $name)) {
468 $queries[$index['name']] = "CREATE {$unique} INDEX {$name} ON {$table} ({$cols})";
474 * CREATE VIEW query for a given locale and table.
476 * @param string $locale
477 * locale of the view.
478 * @param string $table
480 * @param CRM_Core_DAO $dao
481 * A DAO object to run DESCRIBE queries.
482 * @param string $class
483 * schema structure class to use.
486 * array of CREATE INDEX queries
488 private static function createViewQuery($locale, $table, &$dao, $class = 'CRM_Core_I18n_SchemaStructure') {
489 $columns =& $class::columns();
491 $dao->query("DESCRIBE {$table}", FALSE);
492 while ($dao->fetch()) {
493 // view non-internationalized columns directly
494 if (!in_array($dao->Field
, array_keys($columns[$table])) and
495 !preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $dao->Field
)
497 $cols[] = $dao->Field
;
500 // view intrernationalized columns through an alias
501 foreach ($columns[$table] as $column => $_) {
502 $cols[] = "{$column}_{$locale} {$column}";
504 return "CREATE OR REPLACE VIEW {$table}_{$locale} AS SELECT " . implode(', ', $cols) . " FROM {$table}";
509 * @param null $tableName
511 public static function triggerInfo(&$info, $tableName = NULL) {
512 // get the current supported locales
513 $domain = new CRM_Core_DAO_Domain();
515 if (empty($domain->locales
)) {
519 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
520 $locale = array_pop($locales);
523 if (count($locales) == 0) {
527 $currentVer = CRM_Core_BAO_Domain
::version(TRUE);
529 if ($currentVer && CRM_Core_Config
::isUpgradeMode()) {
530 // take exact version so that proper schema structure file in invoked
531 $latest = self
::getLatestSchema($currentVer);
532 require_once "CRM/Core/I18n/SchemaStructure_{$latest}.php";
533 $class = "CRM_Core_I18n_SchemaStructure_{$latest}";
536 $class = 'CRM_Core_I18n_SchemaStructure';
539 $columns =& $class::columns();
541 foreach ($columns as $table => $hash) {
550 foreach ($hash as $column => $_) {
551 $trigger[] = "IF NEW.{$column}_{$locale} IS NOT NULL THEN";
552 foreach ($locales as $old) {
553 $trigger[] = "IF NEW.{$column}_{$old} IS NULL THEN SET NEW.{$column}_{$old} = NEW.{$column}_{$locale}; END IF;";
555 foreach ($locales as $old) {
556 $trigger[] = "ELSEIF NEW.{$column}_{$old} IS NOT NULL THEN";
557 foreach (array_merge($locales, array(
563 $trigger[] = "IF NEW.{$column}_{$loc} IS NULL THEN SET NEW.{$column}_{$loc} = NEW.{$column}_{$old}; END IF;";
566 $trigger[] = 'END IF;';
569 $sql = implode(' ', $trigger);
571 'table' => array($table),
573 'event' => array('UPDATE'),
578 // take care of the ON INSERT triggers
579 foreach ($columns as $table => $hash) {
581 foreach ($hash as $column => $_) {
582 $trigger[] = "IF NEW.{$column}_{$locale} IS NOT NULL THEN";
583 foreach ($locales as $old) {
584 $trigger[] = "SET NEW.{$column}_{$old} = NEW.{$column}_{$locale};";
586 foreach ($locales as $old) {
587 $trigger[] = "ELSEIF NEW.{$column}_{$old} IS NOT NULL THEN";
588 foreach (array_merge($locales, array(
594 $trigger[] = "SET NEW.{$column}_{$loc} = NEW.{$column}_{$old};";
597 $trigger[] = 'END IF;';
600 $sql = implode(' ', $trigger);
602 'table' => array($table),
604 'event' => array('INSERT'),