Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / locale / src / LocaleLookup.php
1 <?php
2
3 namespace Drupal\locale;
4
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Cache\CacheCollector;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Language\LanguageManagerInterface;
9 use Drupal\Core\Lock\LockBackendInterface;
10 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
11 use Symfony\Component\HttpFoundation\RequestStack;
12
13 /**
14  * A cache collector to allow for dynamic building of the locale cache.
15  */
16 class LocaleLookup extends CacheCollector {
17
18   /**
19    * A language code.
20    *
21    * @var string
22    */
23   protected $langcode;
24
25   /**
26    * The msgctxt context.
27    *
28    * @var string
29    */
30   protected $context;
31
32   /**
33    * The locale storage.
34    *
35    * @var \Drupal\locale\StringStorageInterface
36    */
37   protected $stringStorage;
38
39   /**
40    * The cache backend that should be used.
41    *
42    * @var \Drupal\Core\Cache\CacheBackendInterface
43    */
44   protected $cache;
45
46   /**
47    * The lock backend that should be used.
48    *
49    * @var \Drupal\Core\Lock\LockBackendInterface
50    */
51   protected $lock;
52
53   /**
54    * The configuration factory.
55    *
56    * @var \Drupal\Core\Config\ConfigFactoryInterface
57    */
58   protected $configFactory;
59
60   /**
61    * The language manager.
62    *
63    * @var \Drupal\Core\Language\LanguageManagerInterface
64    */
65   protected $languageManager;
66
67   /**
68    * The request stack.
69    *
70    * @var \Symfony\Component\HttpFoundation\RequestStack
71    */
72   protected $requestStack;
73
74   /**
75    * Constructs a LocaleLookup object.
76    *
77    * @param string $langcode
78    *   The language code.
79    * @param string $context
80    *   The string context.
81    * @param \Drupal\locale\StringStorageInterface $string_storage
82    *   The string storage.
83    * @param \Drupal\Core\Cache\CacheBackendInterface $cache
84    *   The cache backend.
85    * @param \Drupal\Core\Lock\LockBackendInterface $lock
86    *   The lock backend.
87    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
88    *   The config factory.
89    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
90    *   The language manager.
91    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
92    *   The request stack.
93    */
94   public function __construct($langcode, $context, StringStorageInterface $string_storage, CacheBackendInterface $cache, LockBackendInterface $lock, ConfigFactoryInterface $config_factory, LanguageManagerInterface $language_manager, RequestStack $request_stack) {
95     $this->langcode = $langcode;
96     $this->context = (string) $context;
97     $this->stringStorage = $string_storage;
98     $this->configFactory = $config_factory;
99     $this->languageManager = $language_manager;
100
101     $this->cache = $cache;
102     $this->lock = $lock;
103     $this->tags = ['locale'];
104     $this->requestStack = $request_stack;
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   protected function getCid() {
111     if (!isset($this->cid)) {
112       // Add the current user's role IDs to the cache key, this ensures that,
113       // for example, strings for admin menu items and settings forms are not
114       // cached for anonymous users.
115       $user = \Drupal::currentUser();
116       $rids = $user ? implode(':', $user->getRoles()) : '';
117       $this->cid = "locale:{$this->langcode}:{$this->context}:$rids";
118
119       // Getting the roles from the current user might have resulted in t()
120       // calls that attempted to get translations from the locale cache. In that
121       // case they would not go into this method again as
122       // CacheCollector::lazyLoadCache() already set the loaded flag. They would
123       // however call resolveCacheMiss() and add that string to the list of
124       // cache misses that need to be written into the cache. Prevent that by
125       // resetting that list. All that happens in such a case are a few uncached
126       // translation lookups.
127       $this->keysToPersist = [];
128     }
129     return $this->cid;
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   protected function resolveCacheMiss($offset) {
136     $translation = $this->stringStorage->findTranslation([
137       'language' => $this->langcode,
138       'source' => $offset,
139       'context' => $this->context,
140     ]);
141
142     if ($translation) {
143       $value = !empty($translation->translation) ? $translation->translation : TRUE;
144     }
145     else {
146       // We don't have the source string, update the {locales_source} table to
147       // indicate the string is not translated.
148       $this->stringStorage->createString([
149         'source' => $offset,
150         'context' => $this->context,
151         'version' => \Drupal::VERSION,
152       ])->addLocation('path', $this->requestStack->getCurrentRequest()->getRequestUri())->save();
153       $value = TRUE;
154     }
155
156     // If there is no translation available for the current language then use
157     // language fallback to try other translations.
158     if ($value === TRUE) {
159       $fallbacks = $this->languageManager->getFallbackCandidates(['langcode' => $this->langcode, 'operation' => 'locale_lookup', 'data' => $offset]);
160       if (!empty($fallbacks)) {
161         foreach ($fallbacks as $langcode) {
162           $translation = $this->stringStorage->findTranslation([
163             'language' => $langcode,
164             'source' => $offset,
165             'context' => $this->context,
166           ]);
167
168           if ($translation && !empty($translation->translation)) {
169             $value = $translation->translation;
170             break;
171           }
172         }
173       }
174     }
175
176     if (is_string($value) && strpos($value, PluralTranslatableMarkup::DELIMITER) !== FALSE) {
177       // Community translations imported from localize.drupal.org as well as
178       // migrated translations may contain @count[number].
179       $value = preg_replace('!@count\[\d+\]!', '@count', $value);
180     }
181
182     $this->storage[$offset] = $value;
183     // Disabling the usage of string caching allows a module to watch for
184     // the exact list of strings used on a page. From a performance
185     // perspective that is a really bad idea, so we have no user
186     // interface for this. Be careful when turning this option off!
187     if ($this->configFactory->get('locale.settings')->get('cache_strings')) {
188       $this->persist($offset);
189     }
190     return $value;
191   }
192
193 }