Advertisement
Guest User

CiviCRM to take advantage of SQL translations

a guest
Jun 29th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 5.80 KB | None | 0 0
  1. --- civicrm/CRM/Core/I18n/Schema-4.1.2-wordpress.php    2011-11-30 13:39:50.000000000 +0100
  2. +++ civicrm/CRM/Core/I18n/Schema.php    2012-06-29 12:34:12.009290721 +0200
  3. @@ -212,6 +212,8 @@
  4.       */
  5.      static function addLocale($locale, $source)
  6.      {
  7. +        global $civicrm_root;
  8. +        
  9.          // get the current supported locales
  10.          $domain = new CRM_Core_DAO_Domain();
  11.          $domain->find(true);
  12. @@ -225,16 +227,107 @@
  13.          // build the required SQL queries
  14.          $columns = CRM_Core_I18n_SchemaStructure::columns();
  15.          $indices = CRM_Core_I18n_SchemaStructure::indices();
  16. +    
  17. +        ////// HACK: try to use the translated strings from the sql file
  18. +        $sqlPath = $civicrm_root . DIRECTORY_SEPARATOR . 'sql';
  19. +        $fileName = ($locale == 'en_US') ?
  20. +            $sqlPath . DIRECTORY_SEPARATOR . 'civicrm_data.mysql' :
  21. +            $sqlPath . DIRECTORY_SEPARATOR . "civicrm_data.{$locale}.mysql";
  22. +        
  23. +        $translations = array();
  24. +    
  25. +        if ( file_exists($fileName)) {
  26. +            
  27. +            $string = file_get_contents($fileName);
  28. +            
  29. +            // change \r\n to fix windows issues
  30. +            $string = str_replace("\r\n", "\n", $string);
  31. +            
  32. +            //get rid of comments starting with # and --
  33. +            $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
  34. +            $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
  35. +            
  36. +            $queries = preg_split('/;\s*$/m', $string);
  37. +            
  38. +            $Trim = function( &$e ) { $e = trim($e, "`\x20\x09\x0A\x0D\x00\x0B"); };
  39. +            
  40. +            foreach ($queries as $query) {
  41. +                
  42. +                $query = trim($query);
  43. +                
  44. +                // we are just working with "INSERT INTO" queries that also contain a 'name' field
  45. +                if (!empty($query) && preg_match('/^INSERT INTO[^(]*\([^)]*\bname\b[^)]*\)/', $query) ) {
  46. +                    
  47. +                    // match the <table name> <field names> <list of values>
  48. +                    preg_match('/^INSERT\sINTO\s*`?(?<table>[a-zA-Z_-]+)`?\s*\((?<fields>[^)]*)\)\s*VALUES\s*(?<values>.*)$/ms', $query, $insert);
  49. +                    $table  = $insert['table'];
  50. +                    $fields = $insert['fields'];
  51. +                    $values = $insert['values'];
  52. +                    
  53. +                    // doesn't make sense if the table is not localizable
  54. +                    if ( ! array_key_exists($table, $columns) ) {
  55. +                        continue;
  56. +                    }
  57. +                    
  58. +                    // obtain and clean field names
  59. +                    $fields = explode( ',', $fields);
  60. +                    array_walk( $fields, $Trim);
  61. +                    
  62. +                    // localizable fields of this table
  63. +                    $l10n_fields = array_keys( $columns[ $table ] );
  64. +                    
  65. +                    // match each row of values
  66. +                    preg_match_all("/\(
  67. +                        ((?:
  68. +                            [^()'] |
  69. +                            \" (?: [^\"] | \\\" )* \" |
  70. +                            '  (?: [^']  |  \\' )*  ' |
  71. +                            \w[\w\d_-]*\( [^()]* \) # not accepting any parenthesis inside here, sorry
  72. +                        )*)
  73. +                        \) \s* (?: , | $ ) /Umx", $values, $values_rows, PREG_SET_ORDER);
  74. +                    
  75. +                    foreach ($values_rows as $values_row) {
  76. +                        
  77. +                        // for each row get the actual values
  78. +                        preg_match_all("/ (
  79. +                                [^()']* |
  80. +                                \" (?: [^\"] | \\\" )* \" |
  81. +                                 ' (?:  [^'] |  \\' )*  ' |
  82. +                                \w[\w\d_-]*\( [^()]* \)
  83. +                            ) \s* (?: , | $ ) /Umx", $values_row[1], $values, PREG_SET_ORDER);
  84. +                        
  85. +                        $row = array();
  86. +                        for($i = 0; $i < count($fields); $i++) {
  87. +                            if ( $fields[$i] == 'name' || in_array( $fields[$i], $l10n_fields)) {
  88. +                                $row[ $fields[$i] ] = $values[$i][1];
  89. +                            }
  90. +                        }
  91. +                        $translations[ $table ][] = $row;
  92. +                    }
  93. +                }
  94. +            }
  95. +        }
  96. +        
  97.          $queries = array();
  98.          foreach ($columns as $table => $hash) {
  99.              // add new columns
  100.              foreach ($hash as $column => $type) {
  101.                  // CRM-7854: skip existing columns
  102. -                if (CRM_Core_DAO::checkFieldExists($table, "{$column}_{$locale}", false)) continue;
  103. +                if (CRM_Core_DAO::checkFieldExists($table, "{$column}_{$locale}", FALSE)) {
  104. +                    continue;
  105. +                }
  106.                  $queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}";
  107. -                $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}_{$source}";
  108. +                // Update to the translated values, if they exist
  109. +                if ($translations && array_key_exists( $table, $translations) ) {
  110. +                    foreach ($translations[$table] as $row) {
  111. +                        $insert_value = ( $row[$column] ) ? $row[$column] : $column . '_' . $source;
  112. +                        $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$insert_value} WHERE name = {$row['name']}";
  113. +                    }
  114. +                } else {
  115. +                    $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}_{$source}";
  116. +                }
  117.              }
  118. -
  119. +            
  120.              // add view
  121.              $queries[] = self::createViewQuery($locale, $table, $dao);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement