3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 class CRM_Core_I18n_Schema
{
38 * Drop all views (for use by CRM_Core_DAO::dropAllTables() mostly).
42 public static function dropAllViews() {
43 $domain = new CRM_Core_DAO_Domain();
45 if (!$domain->locales
) {
49 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
50 $tables = CRM_Core_I18n_SchemaStructure
::tables();
52 foreach ($locales as $locale) {
53 foreach ($tables as $table) {
54 CRM_Core_DAO
::executeQuery("DROP VIEW IF EXISTS {$table}_{$locale}");
60 * Switch database from single-lang to multi (by adding
61 * the first language and dropping the original columns).
63 * @param string $locale
64 * the first locale to create (migrate to).
68 public static function makeMultilingual($locale) {
69 $domain = new CRM_Core_DAO_Domain();
72 // break early if the db is already multi-lang
73 if ($domain->locales
) {
77 $dao = new CRM_Core_DAO();
79 // build the column-adding SQL queries
80 $columns = CRM_Core_I18n_SchemaStructure
::columns();
81 $indices = CRM_Core_I18n_SchemaStructure
::indices();
83 foreach ($columns as $table => $hash) {
85 if (isset($indices[$table])) {
86 foreach ($indices[$table] as $index) {
87 $queries[] = "DROP INDEX {$index['name']} ON {$table}";
91 foreach ($hash as $column => $type) {
92 $queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}";
93 $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}";
94 $queries[] = "ALTER TABLE {$table} DROP {$column}";
98 $queries[] = self
::createViewQuery($locale, $table, $dao);
101 $queries = array_merge($queries, array_values(self
::createIndexQueries($locale, $table)));
104 // execute the queries without i18n rewriting
105 foreach ($queries as $query) {
106 $dao->query($query, FALSE);
109 // update civicrm_domain.locales
110 $domain->locales
= $locale;
115 * Switch database from multi-lang back to single (by dropping
116 * additional columns and views and retaining only the selected locale).
118 * @param string $retain
119 * the locale to retain.
123 public static function makeSinglelingual($retain) {
124 $domain = new CRM_Core_DAO_Domain();
126 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
128 // break early if the db is already single-lang
133 // lets drop all triggers first
134 $logging = new CRM_Logging_Schema();
135 $logging->dropTriggers();
137 // turn subsequent tables singlelingual
138 $tables = CRM_Core_I18n_SchemaStructure
::tables();
139 foreach ($tables as $table) {
140 self
::makeSinglelingualTable($retain, $table);
143 // update civicrm_domain.locales
144 $domain->locales
= 'NULL';
147 //CRM-6963 -fair assumption.
151 // now lets rebuild all triggers
152 CRM_Core_DAO
::triggerRebuild();
156 * Switch a given table from multi-lang to single (by retaining only the selected locale).
158 * @param string $retain
159 * the locale to retain.
160 * @param string $table
161 * the table containing the column.
162 * @param string $class
163 * schema structure class to use to recreate indices.
165 * @param array $triggers
169 public static function makeSinglelingualTable(
172 $class = 'CRM_Core_I18n_SchemaStructure',
175 $domain = new CRM_Core_DAO_Domain();
177 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
179 // break early if the db is already single-lang
184 $columns =& $class::columns();
185 $indices =& $class::indices();
187 $dropQueries = array();
189 if (isset($indices[$table])) {
190 foreach ($indices[$table] as $index) {
191 foreach ($locales as $loc) {
192 $queries[] = "DROP INDEX {$index['name']}_{$loc} ON {$table}";
198 foreach ($columns[$table] as $column => $type) {
199 $queries[] = "ALTER TABLE {$table} ADD {$column} {$type}";
200 $queries[] = "UPDATE {$table} SET {$column} = {$column}_{$retain}";
201 foreach ($locales as $loc) {
202 $dropQueries[] = "ALTER TABLE {$table} DROP {$column}_{$loc}";
207 foreach ($locales as $loc) {
208 $queries[] = "DROP VIEW {$table}_{$loc}";
211 // add original indices
212 $queries = array_merge($queries, self
::createIndexQueries(NULL, $table));
214 // execute the queries without i18n rewriting
215 $dao = new CRM_Core_DAO();
216 foreach ($queries as $query) {
217 $dao->query($query, FALSE);
220 foreach ($dropQueries as $query) {
221 $dao->query($query, FALSE);
224 if (!empty($triggers)) {
225 if (CRM_Core_Config
::isUpgradeMode()) {
226 foreach ($triggers as $triggerInfo) {
227 $when = $triggerInfo['when'];
228 $event = $triggerInfo['event'];
229 $triggerName = "{$table}_{$when}_{$event}";
230 CRM_Core_DAO
::executeQuery("DROP TRIGGER IF EXISTS {$triggerName}");
234 // invoke the meta trigger creation call
235 CRM_Core_DAO
::triggerRebuild($table);
240 * Add a new locale to a multi-lang db, setting
241 * its values to the current default locale.
243 * @param string $locale
244 * the new locale to add.
245 * @param string $source
246 * the locale to copy from.
250 public static function addLocale($locale, $source) {
251 // get the current supported locales
252 $domain = new CRM_Core_DAO_Domain();
254 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
256 // break early if the locale is already supported
257 if (in_array($locale, $locales)) {
261 $dao = new CRM_Core_DAO();
263 // build the required SQL queries
264 $columns = CRM_Core_I18n_SchemaStructure
::columns();
265 $indices = CRM_Core_I18n_SchemaStructure
::indices();
267 foreach ($columns as $table => $hash) {
269 foreach ($hash as $column => $type) {
270 // CRM-7854: skip existing columns
271 if (CRM_Core_DAO
::checkFieldExists($table, "{$column}_{$locale}", FALSE)) {
274 $queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}";
275 $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}_{$source}";
279 $queries[] = self
::createViewQuery($locale, $table, $dao);
282 $queries = array_merge($queries, array_values(self
::createIndexQueries($locale, $table)));
285 // execute the queries without i18n rewriting
286 foreach ($queries as $query) {
287 $dao->query($query, FALSE);
290 // update civicrm_domain.locales
291 $locales[] = $locale;
292 $domain->locales
= implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $locales);
295 // invoke the meta trigger creation call
296 CRM_Core_DAO
::triggerRebuild();
300 * Rebuild multilingual indices, views and triggers (useful for upgrades)
302 * @param array $locales
303 * locales to be rebuilt.
304 * @param string $version
305 * version of schema structure to use.
309 public static function rebuildMultilingualSchema($locales, $version = NULL) {
311 $latest = self
::getLatestSchema($version);
312 require_once "CRM/Core/I18n/SchemaStructure_{$latest}.php";
313 $class = "CRM_Core_I18n_SchemaStructure_{$latest}";
316 $class = 'CRM_Core_I18n_SchemaStructure';
318 $indices =& $class::indices();
319 $tables =& $class::tables();
321 $dao = new CRM_Core_DAO();
323 // get all of the already existing indices
325 foreach (array_keys($indices) as $table) {
326 $existing[$table] = array();
327 $dao->query("SHOW INDEX FROM $table", FALSE);
328 while ($dao->fetch()) {
329 if (preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $dao->Key_name
)) {
330 $existing[$table][] = $dao->Key_name
;
335 // from all of the CREATE INDEX queries fetch the ones creating missing indices
336 foreach ($locales as $locale) {
337 foreach (array_keys($indices) as $table) {
338 $allQueries = self
::createIndexQueries($locale, $table, $class);
339 foreach ($allQueries as $name => $query) {
340 if (!in_array("{$name}_{$locale}", $existing[$table])) {
348 foreach ($locales as $locale) {
349 foreach ($tables as $table) {
350 $queries[] = self
::createViewQuery($locale, $table, $dao, $class);
355 $last = array_pop($locales);
357 foreach ($queries as $query) {
358 $dao->query($query, FALSE);
361 // invoke the meta trigger creation call
362 CRM_Core_DAO
::triggerRebuild();
366 * Rewrite SQL query to use views to access tables with localized columns.
368 * @param string $query
369 * the query for rewrite.
372 * the rewritten query
374 public static function rewriteQuery($query) {
376 $tables = self
::schemaStructureTables();
377 foreach ($tables as $table) {
378 $query = preg_replace("/([^'\"])({$table})([^_'\"])/", "\\1\\2{$dbLocale}\\3", $query);
380 // uncomment the below to rewrite the civicrm_value_* queries
381 // $query = preg_replace("/(civicrm_value_[a-z0-9_]+_\d+)([^_])/", "\\1{$dbLocale}\\2", $query);
386 * @param null $version
391 public static function schemaStructureTables($version = NULL, $force = FALSE) {
392 static $_tables = NULL;
393 if ($_tables === NULL ||
$force) {
395 $latest = self
::getLatestSchema($version);
396 // FIXME: Doing require_once is a must here because a call like CRM_Core_I18n_SchemaStructure_4_1_0 makes
397 // class loader look for file like - CRM/Core/I18n/SchemaStructure/4/1/0.php which is not what we want to be loaded
398 require_once "CRM/Core/I18n/SchemaStructure_{$latest}.php";
399 $class = "CRM_Core_I18n_SchemaStructure_{$latest}";
400 $tables =& $class::tables();
403 $tables = CRM_Core_I18n_SchemaStructure
::tables();
415 public static function getLatestSchema($version) {
416 // remove any .upgrade sub-str from version. Makes it easy to do version_compare & give right result
417 $version = str_ireplace(".upgrade", "", $version);
419 // fetch all the SchemaStructure versions we ship and sort by version
421 foreach (scandir(dirname(__FILE__
)) as $file) {
423 if (preg_match('/^SchemaStructure_([0-9a-z_]+)\.php$/', $file, $matches)) {
424 $schemas[] = str_replace('_', '.', $matches[1]);
427 usort($schemas, 'version_compare');
429 // find the latest schema structure older than (or equal to) $version
431 $latest = array_pop($schemas);
432 } while (version_compare($latest, $version, '>'));
434 return str_replace('.', '_', $latest);
438 * CREATE INDEX queries for a given locale and table.
440 * @param string $locale
441 * locale for which the queries should be created (null to create original indices).
442 * @param string $table
443 * table for which the queries should be created.
444 * @param string $class
445 * schema structure class to use.
448 * array of CREATE INDEX queries
450 private static function createIndexQueries($locale, $table, $class = 'CRM_Core_I18n_SchemaStructure') {
451 $indices =& $class::indices();
452 $columns =& $class::columns();
453 if (!isset($indices[$table])) {
458 foreach ($indices[$table] as $index) {
459 $unique = isset($index['unique']) && $index['unique'] ?
'UNIQUE' : '';
460 foreach ($index['field'] as $i => $col) {
461 // if a given column is localizable, extend its name with the locale
462 if ($locale and isset($columns[$table][$col])) {
463 $index['field'][$i] = "{$col}_{$locale}";
466 $cols = implode(', ', $index['field']);
467 $name = $index['name'];
469 $name .= '_' . $locale;
471 // CRM-7854: skip existing indices
472 if (CRM_Core_DAO
::checkConstraintExists($table, $name)) {
475 $queries[$index['name']] = "CREATE {$unique} INDEX {$name} ON {$table} ({$cols})";
481 * CREATE VIEW query for a given locale and table.
483 * @param string $locale
484 * locale of the view.
485 * @param string $table
487 * @param CRM_Core_DAO $dao
488 * A DAO object to run DESCRIBE queries.
489 * @param string $class
490 * schema structure class to use.
493 * array of CREATE INDEX queries
495 private static function createViewQuery($locale, $table, &$dao, $class = 'CRM_Core_I18n_SchemaStructure') {
496 $columns =& $class::columns();
498 $dao->query("DESCRIBE {$table}", FALSE);
499 while ($dao->fetch()) {
500 // view non-internationalized columns directly
501 if (!in_array($dao->Field
, array_keys($columns[$table])) and
502 !preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $dao->Field
)
504 $cols[] = $dao->Field
;
507 // view intrernationalized columns through an alias
508 foreach ($columns[$table] as $column => $_) {
509 $cols[] = "{$column}_{$locale} {$column}";
511 return "CREATE OR REPLACE VIEW {$table}_{$locale} AS SELECT " . implode(', ', $cols) . " FROM {$table}";
516 * @param null $tableName
518 public static function triggerInfo(&$info, $tableName = NULL) {
519 // get the current supported locales
520 $domain = new CRM_Core_DAO_Domain();
522 if (empty($domain->locales
)) {
526 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
527 $locale = array_pop($locales);
530 if (count($locales) == 0) {
534 $currentVer = CRM_Core_BAO_Domain
::version(TRUE);
536 if ($currentVer && CRM_Core_Config
::isUpgradeMode()) {
537 // take exact version so that proper schema structure file in invoked
538 $latest = self
::getLatestSchema($currentVer);
539 require_once "CRM/Core/I18n/SchemaStructure_{$latest}.php";
540 $class = "CRM_Core_I18n_SchemaStructure_{$latest}";
543 $class = 'CRM_Core_I18n_SchemaStructure';
546 $columns =& $class::columns();
548 foreach ($columns as $table => $hash) {
557 foreach ($hash as $column => $_) {
558 $trigger[] = "IF NEW.{$column}_{$locale} IS NOT NULL THEN";
559 foreach ($locales as $old) {
560 $trigger[] = "IF NEW.{$column}_{$old} IS NULL THEN SET NEW.{$column}_{$old} = NEW.{$column}_{$locale}; END IF;";
562 foreach ($locales as $old) {
563 $trigger[] = "ELSEIF NEW.{$column}_{$old} IS NOT NULL THEN";
564 foreach (array_merge($locales, array(
570 $trigger[] = "IF NEW.{$column}_{$loc} IS NULL THEN SET NEW.{$column}_{$loc} = NEW.{$column}_{$old}; END IF;";
573 $trigger[] = 'END IF;';
576 $sql = implode(' ', $trigger);
578 'table' => array($table),
580 'event' => array('UPDATE'),
585 // take care of the ON INSERT triggers
586 foreach ($columns as $table => $hash) {
588 foreach ($hash as $column => $_) {
589 $trigger[] = "IF NEW.{$column}_{$locale} IS NOT NULL THEN";
590 foreach ($locales as $old) {
591 $trigger[] = "SET NEW.{$column}_{$old} = NEW.{$column}_{$locale};";
593 foreach ($locales as $old) {
594 $trigger[] = "ELSEIF NEW.{$column}_{$old} IS NOT NULL THEN";
595 foreach (array_merge($locales, array(
601 $trigger[] = "SET NEW.{$column}_{$loc} = NEW.{$column}_{$old};";
604 $trigger[] = 'END IF;';
607 $sql = implode(' ', $trigger);
609 'table' => array($table),
611 'event' => array('INSERT'),