diff --git a/potx.inc b/potx.inc index 6ee0900..a32ce0f 100644 --- a/potx.inc +++ b/potx.inc @@ -1985,34 +1985,51 @@ function _potx_load_yaml_translation_patterns($path) { function _potx_parse_yaml_file($code, $file_name, $file_path, $save_callback) { global $yaml_translation_patterns; global $_potx_config_set; + global $_potx_schema_set; + global $_potx_module_set; + if (!is_array($_potx_schema_set)) { + $_potx_schema_set = array(); + } if (!is_array($yaml_translation_patterns)) { _potx_init_yaml_translation_patterns(); } + try { + $yaml = Yaml::parse($code); + } catch (ParseException $e) { + watchdog('potx', "YAML parseing error on file @path: @error", array( + '@path' => $file_path, + '@error' => $e->getMessage(), + )); + + return; + } + foreach ($yaml_translation_patterns as $pattern => $trans_list) { if (fnmatch($pattern, $file_name) || fnmatch('*/' . $pattern, $file_name)) { - try { - $yaml = Yaml::parse($code); - _potx_find_yaml_translatables($yaml, $trans_list, $file_name, $save_callback); - } catch (ParseException $e) { - watchdog('potx', "YAML parseing error on file @path: @error", array( - '@path' => $file_path, - '@error' => $e->getMessage(), - )); - } + _potx_find_yaml_translatables($yaml, $trans_list, $file_name, $save_callback); } } if (preg_match('~config/schema/[^/]+\.yml$~', $file_name)) { - $schema = Yaml::parse($code); - foreach ($schema as $key => $element) { - _potx_process_config_schema($key, $element); - } + + $module_name = basename(dirname(dirname(dirname($file_name)))); + $_potx_schema_set[$module_name][] = $file_name; } elseif (preg_match('~config/(install|optional)/[^/]+\.yml$~', $file_name)) { $_potx_config_set[] = $file_path; } + elseif (preg_match('~[^/]+.info.yml~', $file_name)) { + if (!is_array($module_set)) { + $module_set = array(); + } + $module_name = substr(basename($file_name), 0, -9); + $_potx_module_set[$module_name] = array( + 'path' => dirname(realpath($file_name)), + 'deps' => isset($yaml['dependencies']) ? $yaml['dependencies'] : array() + ); + } } /** @@ -2132,18 +2149,6 @@ function _potx_find_yaml_translatables($yaml, $trans_list, $file_name, $save_cal function _potx_process_config_schema($schema_prefix, $schema_data) { global $_potx_processed_schema; - if (!isset($_potx_processed_schema)) { - // The initial values for 'translatables' allows a limited support for - // extracting translatable strings from contrib projects, until - // https://www.drupal.org/node/1933988 gets in. - $_potx_processed_schema = array( - 'translatables' => array('label', 'text', 'date_format'), - 'types' => array(), - 'mappings' => array(), - 'contexts' => array('date_format' => 'PHP date format'), - ); - } - // Elements can opt out of translation with a 'translatable: false' key. if (isset($schema_data['translatable']) && $schema_data['translatable'] === FALSE) { return; @@ -2357,11 +2362,37 @@ function _potx_replace_variable($value, $data) { */ function _potx_parse_shipped_configuration($save_callback = '_potx_save_string', $api_version = POTX_API_CURRENT) { global $_potx_config_set; + global $_potx_processed_schema; + global $_potx_processed_modules; + global $_potx_cached_schema; + if (!is_array($_potx_config_set)) { return; } foreach ($_potx_config_set as $config_path) { + + $module_path = dirname(dirname(dirname($config_path))); + $module_name = basename($module_path); + + $_potx_processed_modules = array(); + + + if (isset($_potx_cached_schema[$module_name])) { + $_potx_processed_schema = $_potx_cached_schema[$module_name]; + } + else { + $_potx_processed_schema = array( + 'translatables' => array('label', 'text', 'date_format'), + 'types' => array(), + 'mappings' => array(), + 'contexts' => array('date_format' => 'PHP date format'), + ); + + _potx_build_schema('core'); + _potx_build_schema($module_name); + } + // Find the schema that matches the config file. $path_info = pathinfo($config_path); $config_name = $path_info['filename']; @@ -2380,6 +2411,53 @@ function _potx_parse_shipped_configuration($save_callback = '_potx_save_string', } } +function _potx_build_schema($module_name) { + global $_potx_cached_schema; + global $_potx_module_set; + global $_potx_schema_set; + global $_potx_processed_schema; + global $_potx_processed_modules; + + if (in_array($module_name, $_potx_processed_modules)) { + return; + } + + if (isset($_potx_cached_schema[$module_name])) { + $module_schema = $_potx_cached_schema[$module_name]; + $_potx_processed_schema['translatables'] = array_merge($_potx_processed_schema['translatables'], $module_schema['translatables']); + $_potx_processed_schema['types'] = array_merge($_potx_processed_schema['types'], $module_schema['types']); + $_potx_processed_schema['mappings'] = array_merge($_potx_processed_schema['mappings'], $module_schema['mappings']); + $_potx_processed_schema['contexts'] = array_merge($_potx_processed_schema['contexts'], $module_schema['contexts']); + + $_potx_processed_modules[] = $module_name; + return; + } + + if (is_array($_potx_module_set[$module_name]['deps'])) { + + $deps = $_potx_module_set[$module_name]['deps']; + + foreach ($deps as $dep) { + _potx_build_schema($dep); + } + } + + if (!is_array($_potx_schema_set[$module_name])) { + return; + } + + foreach ($_potx_schema_set[$module_name] as $file_path) { + $code = file_get_contents($file_path); + $yaml = Yaml::parse($code); + foreach ($yaml as $key => $element) { + _potx_process_config_schema($key, $element); + } + } + + $_potx_processed_modules[] = $module_name; + $_potx_cached_schema[$module_name] = $_potx_processed_schema; +} + /** * Recursively check elements in shipped configuration with the processed schema. *