Révision 6331c987
Ajouté par Assos Assos il y a environ 10 ans
drupal7/sites/all/modules/i18n/i18n_string/i18n_string.inc | ||
---|---|---|
28 | 28 |
// Properties from metadata |
29 | 29 |
public $title; |
30 | 30 |
// Array of translations to multiple languages |
31 |
public $translations; |
|
31 |
public $translations = array();
|
|
32 | 32 |
// Textgroup object |
33 | 33 |
protected $_textgroup; |
34 | 34 |
|
... | ... | |
39 | 39 |
if ($data) { |
40 | 40 |
$this->set_properties($data); |
41 | 41 |
} |
42 |
// Attempt to re-build the data from the persistent cache. |
|
43 |
$this->rebuild_from_cache($data); |
|
42 | 44 |
} |
45 |
|
|
46 |
/** |
|
47 |
* Rebuild the object data based on the persistent cache. |
|
48 |
* |
|
49 |
* Since the textgroup defines if a string is cacheable or not the caching |
|
50 |
* of the string objects happens in the textgroup handler itself. |
|
51 |
* |
|
52 |
* @see i18n_string_textgroup_cached::__destruct() |
|
53 |
*/ |
|
54 |
protected function rebuild_from_cache($data = NULL) { |
|
55 |
// Check if we've the required information to repopulate the cache and do so |
|
56 |
// if possible. |
|
57 |
$meta_data_exist = isset($this->textgroup) && isset($this->type) && isset($this->objectid) && isset($this->property); |
|
58 |
if ($meta_data_exist && ($cache = cache_get($this->get_cid())) && !empty($cache->data)) { |
|
59 |
// Re-spawn the cached data. |
|
60 |
// @TODO do we need a array_diff to ensure we don't overwrite the data |
|
61 |
// provided by the $data parameter? |
|
62 |
$this->set_properties($cache->data); |
|
63 |
} |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* Reset cache, needed for tests. |
|
68 |
*/ |
|
69 |
public function cache_reset() { |
|
70 |
$this->translations = array(); |
|
71 |
// Ensure a possible persistent cache of this object is cleared too. |
|
72 |
cache_clear_all($this->get_cid(), 'cache', TRUE); |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* Returns the caching id for this object. |
|
77 |
* |
|
78 |
* @return string |
|
79 |
* The caching id. |
|
80 |
*/ |
|
81 |
public function get_cid() { |
|
82 |
return 'i18n:string:obj:' . $this->get_name(); |
|
83 |
} |
|
84 |
|
|
43 | 85 |
/** |
44 | 86 |
* Get message parameters from context and string. |
45 | 87 |
*/ |
... | ... | |
63 | 105 |
$this->objectkey = (int)$this->objectid; |
64 | 106 |
// Remaining elements glued again with ':' |
65 | 107 |
$this->property = $parts ? implode(':', $parts) : ''; |
108 |
|
|
109 |
// Attempt to re-build the other data from the persistent cache. |
|
110 |
$this->rebuild_from_cache(); |
|
111 |
|
|
66 | 112 |
return $this; |
67 | 113 |
} |
68 | 114 |
/** |
... | ... | |
100 | 146 |
if (isset($string['format'])) { |
101 | 147 |
$this->format = $string['format']; |
102 | 148 |
} |
149 |
if (isset($string['title'])) { |
|
150 |
$this->title = $string['title']; |
|
151 |
} |
|
103 | 152 |
} |
104 | 153 |
else { |
105 | 154 |
$this->string = $string; |
106 | 155 |
} |
107 |
if (isset($string['title'])) { |
|
108 |
$this->title = $string['title']; |
|
109 |
} |
|
156 |
|
|
110 | 157 |
return $this; |
111 | 158 |
} |
112 | 159 |
/** |
... | ... | |
119 | 166 |
* Get translation to language from string object |
120 | 167 |
*/ |
121 | 168 |
public function get_translation($langcode) { |
122 |
if (!isset($this->translations[$langcode])) {
|
|
169 |
if (empty($this->translations[$langcode])) {
|
|
123 | 170 |
$translation = $this->textgroup()->load_translation($this, $langcode); |
124 | 171 |
if ($translation && isset($translation->translation)) { |
125 | 172 |
$this->set_translation($translation, $langcode); |
... | ... | |
279 | 326 |
// Debug flag, set to true to print out more information. |
280 | 327 |
public $debug; |
281 | 328 |
// Cached or preloaded string objects |
282 |
public $strings; |
|
329 |
public $strings = array();
|
|
283 | 330 |
// Multiple translations search map |
284 |
protected $cache_multiple; |
|
331 |
protected $cache_multiple = array();
|
|
285 | 332 |
|
286 | 333 |
/** |
287 | 334 |
* Class constructor. |
... | ... | |
400 | 447 |
/** |
401 | 448 |
* Filter array of strings |
402 | 449 |
* |
403 |
* @param $filter |
|
450 |
* @param array $string_list |
|
451 |
* Array of strings to be filtered. |
|
452 |
* @param array $filter |
|
404 | 453 |
* Array of name value conditions. |
454 |
* |
|
455 |
* @return array |
|
456 |
* Strings from $string_list that match the filter conditions. |
|
405 | 457 |
*/ |
406 | 458 |
protected static function string_filter($string_list, $filter) { |
407 | 459 |
// Remove 'language' and '*' conditions. |
... | ... | |
566 | 618 |
public function cache_reset() { |
567 | 619 |
$this->strings = array(); |
568 | 620 |
$this->string_format = array(); |
569 |
$this->translations = array(); |
|
621 |
|
|
622 |
// Reset the persistent caches. |
|
623 |
cache_clear_all('i18n:string:tgroup:' . $this->textgroup , 'cache', TRUE); |
|
624 |
// Reset the complete string object cache too. |
|
625 |
cache_clear_all('i18n:string:obj:', 'cache', TRUE); |
|
570 | 626 |
} |
571 | 627 |
|
572 | 628 |
/** |
... | ... | |
613 | 669 |
public static function load_translation($i18nstring, $langcode) { |
614 | 670 |
// Search the database using lid if we've got it or textgroup, context otherwise |
615 | 671 |
if (!empty($i18nstring->lid)) { |
616 |
// We've alreay got lid, we just need translation data |
|
672 |
// We've already got lid, we just need translation data
|
|
617 | 673 |
$query = db_select('locales_target', 't'); |
618 | 674 |
$query->condition('t.lid', $i18nstring->lid); |
619 | 675 |
} |
... | ... | |
1178 | 1234 |
*/ |
1179 | 1235 |
public function translate($langcode, $options = array()) { |
1180 | 1236 |
// We may have it already translated. As objects are statically cached, translations are too. |
1181 |
if (!isset($this->translations[$langcode])) {
|
|
1237 |
if (empty($this->translations[$langcode])) {
|
|
1182 | 1238 |
$this->translations[$langcode] = $this->translate_object($langcode, $options); |
1183 | 1239 |
} |
1184 | 1240 |
return $this->translations[$langcode]; |
... | ... | |
1284 | 1340 |
return $this->textgroup()->load_strings(array('type' => $type, 'objectid' => $id)); |
1285 | 1341 |
} |
1286 | 1342 |
} |
1343 |
|
|
1344 |
|
|
1345 |
/** |
|
1346 |
* Textgroup handler for i18n_string API which integrated persistent caching. |
|
1347 |
*/ |
|
1348 |
class i18n_string_textgroup_cached extends i18n_string_textgroup_default { |
|
1349 |
|
|
1350 |
/** |
|
1351 |
* Defines the timeout for the persistent caching. |
|
1352 |
* @var int |
|
1353 |
*/ |
|
1354 |
public $caching_time = CACHE_TEMPORARY; |
|
1355 |
|
|
1356 |
/** |
|
1357 |
* Extends the existing constructor with a cache handling. |
|
1358 |
* |
|
1359 |
* @param string $textgroup |
|
1360 |
* The name of this textgroup. |
|
1361 |
*/ |
|
1362 |
public function __construct($textgroup) { |
|
1363 |
parent::__construct($textgroup); |
|
1364 |
// Fetch persistent caches, the persistent caches contain only metadata. |
|
1365 |
// Those metadata are processed by the related cache_get() methods. |
|
1366 |
foreach (array('cache_multiple', 'strings') as $caches_type) { |
|
1367 |
if (($cache = cache_get('i18n:string:tgroup:' . $this->textgroup . ':' . $caches_type)) && !empty($cache->data)) { |
|
1368 |
$this->{$caches_type} = $cache->data; |
|
1369 |
} |
|
1370 |
} |
|
1371 |
} |
|
1372 |
|
|
1373 |
/** |
|
1374 |
* Class destructor. |
|
1375 |
* |
|
1376 |
* Updates the persistent caches for the next usage. |
|
1377 |
* This function not only stores the data of the textgroup objects but also |
|
1378 |
* of the string objects. That way we ensure that only cacheable string object |
|
1379 |
* go into the persistent cache. |
|
1380 |
*/ |
|
1381 |
public function __destruct() { |
|
1382 |
// Reduce size to cache by removing NULL values. |
|
1383 |
$this->strings = array_filter($this->strings); |
|
1384 |
|
|
1385 |
|
|
1386 |
$strings_to_cache = array(); |
|
1387 |
// Store the persistent caches. We just store the metadata the translations |
|
1388 |
// are stored by the string object itself. However storing the metadata |
|
1389 |
// reduces the number of DB queries executed during runtime. |
|
1390 |
$cache_data = array(); |
|
1391 |
foreach ($this->strings as $context => $i18n_string_object) { |
|
1392 |
$cache_data[$context] = $context; |
|
1393 |
$strings_to_cache[$context] = $i18n_string_object; |
|
1394 |
} |
|
1395 |
cache_set('i18n:string:tgroup:' . $this->textgroup . ':strings', $cache_data, 'cache', $this->caching_time); |
|
1396 |
|
|
1397 |
$cache_data = array(); |
|
1398 |
foreach ($this->cache_multiple as $pattern => $strings) { |
|
1399 |
foreach ($strings as $context => $i18n_string_object) { |
|
1400 |
$cache_data[$pattern][$context] = $context; |
|
1401 |
$strings_to_cache[$context] = $i18n_string_object; |
|
1402 |
} |
|
1403 |
} |
|
1404 |
cache_set('i18n:string:tgroup:' . $this->textgroup . ':cache_multiple', $cache_data, 'cache', $this->caching_time); |
|
1405 |
|
|
1406 |
// Cache the string objects related to this textgroup. |
|
1407 |
// Store only the public visible data into the persistent cache. |
|
1408 |
foreach ($strings_to_cache as $i18n_string_object) { |
|
1409 |
// If this isn't an object it's an unprocessed cache item and doesn't need |
|
1410 |
// to be stored again. |
|
1411 |
if (is_object($i18n_string_object)) { |
|
1412 |
cache_set($i18n_string_object->get_cid(), get_object_vars($i18n_string_object), 'cache', $this->caching_time); |
|
1413 |
} |
|
1414 |
} |
|
1415 |
} |
|
1416 |
|
|
1417 |
/** |
|
1418 |
* Reset cache, needed for tests. |
|
1419 |
* |
|
1420 |
* Takes care of the persistent caches. |
|
1421 |
*/ |
|
1422 |
public function cache_reset() { |
|
1423 |
// Reset the persistent caches. |
|
1424 |
cache_clear_all('i18n:string:tgroup:' . $this->textgroup , 'cache', TRUE); |
|
1425 |
// Reset the complete string object cache too. This will affect string |
|
1426 |
// objects of other textgroups as well. |
|
1427 |
cache_clear_all('i18n:string:obj:', 'cache', TRUE); |
|
1428 |
|
|
1429 |
return parent::cache_reset(); |
|
1430 |
} |
|
1431 |
|
|
1432 |
/** |
|
1433 |
* Get translation from cache. |
|
1434 |
* |
|
1435 |
* Extends the original handler with persistent caching. |
|
1436 |
* |
|
1437 |
* @param string $context |
|
1438 |
* The context to look out for. |
|
1439 |
* @return i18n_string_object|NULL |
|
1440 |
* The string object if available or NULL otherwise. |
|
1441 |
*/ |
|
1442 |
protected function cache_get($context) { |
|
1443 |
if (isset($this->strings[$context])) { |
|
1444 |
// If the cache contains a string re-build i18n_string_object. |
|
1445 |
if (is_string($this->strings[$context])) { |
|
1446 |
$i8n_string_object = new i18n_string_object(array('textgroup' => $this->textgroup)); |
|
1447 |
$i8n_string_object->set_context($context); |
|
1448 |
$this->strings[$context] = $i8n_string_object; |
|
1449 |
} |
|
1450 |
// Now run the original handling. |
|
1451 |
return parent::cache_get($context); |
|
1452 |
} |
|
1453 |
return NULL; |
|
1454 |
} |
|
1455 |
|
|
1456 |
/** |
|
1457 |
* Get strings from multiple cache. |
|
1458 |
* |
|
1459 |
* @param $context array |
|
1460 |
* String context as array with language property at the end. |
|
1461 |
* |
|
1462 |
* @return mixed |
|
1463 |
* Array of strings (may be empty) if we've got a cache hit. |
|
1464 |
* Null otherwise. |
|
1465 |
*/ |
|
1466 |
protected function multiple_cache_get($context) { |
|
1467 |
// Ensure the values from the persistent cache are properly re-build. |
|
1468 |
$cache_key = implode(':', $context); |
|
1469 |
if (isset($this->cache_multiple[$cache_key])) { |
|
1470 |
foreach ($this->cache_multiple[$cache_key] as $cached_context) { |
|
1471 |
if (is_string($cached_context)) { |
|
1472 |
$i8n_string_object = new i18n_string_object(array('textgroup' => $this->textgroup)); |
|
1473 |
$i8n_string_object->set_context($cached_context); |
|
1474 |
$this->cache_multiple[$cache_key][$cached_context] = $i8n_string_object; |
|
1475 |
} |
|
1476 |
} |
|
1477 |
} |
|
1478 |
else { |
|
1479 |
// Now we try more generic keys. For instance, if we are searching 'term:1:*' |
|
1480 |
// we may try too 'term:*:*' and filter out the results. |
|
1481 |
foreach ($context as $key => $value) { |
|
1482 |
if ($value != '*') { |
|
1483 |
$try = array_merge($context, array($key => '*')); |
|
1484 |
return $this->multiple_cache_get($try); |
|
1485 |
} |
|
1486 |
} |
|
1487 |
} |
|
1488 |
return parent::multiple_cache_get($context); |
|
1489 |
} |
|
1490 |
|
|
1491 |
public function string_update($i18nstring, $options = array()) { |
|
1492 |
// Flush persistent cache. |
|
1493 |
cache_clear_all($i18nstring->get_cid(), 'cache', TRUE); |
|
1494 |
return parent::string_update($i18nstring, $options); |
|
1495 |
} |
|
1496 |
|
|
1497 |
public function string_remove($i18nstring, $options = array()) { |
|
1498 |
// Flush persistent cache. |
|
1499 |
cache_clear_all($i18nstring->get_cid(), 'cache', TRUE); |
|
1500 |
return parent::string_remove($i18nstring, $options); |
|
1501 |
} |
|
1502 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules