dbc71e7aee1a9494e02e16807de27a9cbbcb3845
[yaffs-website] / vendor / drush / drush / src / Drupal / Commands / core / LocaleCommands.php
1 <?php
2
3 namespace Drush\Drupal\Commands\core;
4
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\State\StateInterface;
7 use Drush\Commands\DrushCommands;
8
9 class LocaleCommands extends DrushCommands
10 {
11
12     protected $moduleHandler;
13
14     protected $state;
15
16     /**
17      * @return \Drupal\Core\Extension\ModuleHandlerInterface
18      */
19     public function getModuleHandler()
20     {
21         return $this->moduleHandler;
22     }
23
24     /**
25      * @return mixed
26      */
27     public function getState()
28     {
29         return $this->state;
30     }
31
32     public function __construct(ModuleHandlerInterface $moduleHandler, StateInterface $state)
33     {
34         $this->moduleHandler = $moduleHandler;
35         $this->state = $state;
36     }
37
38     /**
39      * Checks for available translation updates.
40      *
41      * @command locale:check
42      * @aliases locale-check
43      * @validate-module-enabled locale
44      */
45     public function check()
46     {
47         $this->getModuleHandler()->loadInclude('locale', 'inc', 'locale.compare');
48
49         // Check translation status of all translatable project in all languages.
50         // First we clear the cached list of projects. Although not strictly
51         // necessary, this is helpful in case the project list is out of sync.
52         locale_translation_flush_projects();
53         locale_translation_check_projects();
54
55         // Execute a batch if required. A batch is only used when remote files
56         // are checked.
57         if (batch_get()) {
58             drush_backend_batch_process();
59         }
60     }
61
62     /**
63      * Imports the available translation updates.
64      *
65      * @see TranslationStatusForm::buildForm()
66      * @see TranslationStatusForm::prepareUpdateData()
67      * @see TranslationStatusForm::submitForm()
68      *
69      * @todo This can be simplified once https://www.drupal.org/node/2631584 lands
70      *   in Drupal core.
71      *
72      * @command locale:update
73      * @aliases locale-update
74      * @option langcodes A comma-separated list of language codes to update. If omitted, all translations will be updated.
75      * @validate-module-enabled locale
76      */
77     public function update($options = ['langcodes' => self::REQ])
78     {
79         $module_handler = $this->getModuleHandler();
80         $module_handler->loadInclude('locale', 'fetch.inc');
81         $module_handler->loadInclude('locale', 'bulk.inc');
82
83         $langcodes = [];
84         foreach (locale_translation_get_status() as $project_id => $project) {
85             foreach ($project as $langcode => $project_info) {
86                 if (!empty($project_info->type) && !in_array($langcode, $langcodes)) {
87                     $langcodes[] = $langcode;
88                 }
89             }
90         }
91
92         if ($passed_langcodes = $translationOptions['langcodes']) {
93             $langcodes = array_intersect($langcodes, explode(',', $passed_langcodes));
94             // @todo Not selecting any language code in the user interface results in
95             //   all translations being updated, so we mimick that behavior here.
96         }
97
98         // Deduplicate the list of langcodes since each project may have added the
99         // same language several times.
100         $langcodes = array_unique($langcodes);
101
102         // @todo Restricting by projects is not possible in the user interface and is
103         //   broken when attempting to do it in a hook_form_alter() implementation so
104         //   we do not allow for it here either.
105         $projects = [];
106
107         // Set the translation import options. This determines if existing
108         // translations will be overwritten by imported strings.
109         $translationOptions = _locale_translation_default_update_options();
110
111         // If the status was updated recently we can immediately start fetching the
112         // translation updates. If the status is expired we clear it an run a batch to
113         // update the status and then fetch the translation updates.
114         $last_checked = $this->getState()->get('locale.translation_last_checked');
115         if ($last_checked < REQUEST_TIME - LOCALE_TRANSLATION_STATUS_TTL) {
116             locale_translation_clear_status();
117             $batch = locale_translation_batch_update_build([], $langcodes, $translationOptions);
118             batch_set($batch);
119         } else {
120             // Set a batch to download and import translations.
121             $batch = locale_translation_batch_fetch_build($projects, $langcodes, $translationOptions);
122             batch_set($batch);
123             // Set a batch to update configuration as well.
124             if ($batch = locale_config_batch_update_components($translationOptions, $langcodes)) {
125                 batch_set($batch);
126             }
127         }
128
129         drush_backend_batch_process();
130     }
131 }