120 |
120 |
121 function fetch($allow_cache = true) |
121 function fetch($allow_cache = true) |
122 { |
122 { |
123 global $db, $session, $paths, $template, $plugins; // Common objects |
123 global $db, $session, $paths, $template, $plugins; // Common objects |
124 |
124 |
125 $lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php"; |
|
126 // Attempt to load the strings from a cache file |
125 // Attempt to load the strings from a cache file |
127 if ( file_exists($lang_file) && $allow_cache ) |
126 $loaded = false; |
128 { |
127 |
129 // Yay! found it |
128 if ( $allow_cache ) |
130 $this->load_cache_file($lang_file); |
129 { |
131 } |
130 // Load the cache manager |
132 else |
131 global $cache; |
|
132 |
|
133 if ( $cached = $cache->fetch("lang_{$this->lang_id}") ) |
|
134 { |
|
135 $this->merge($cached); |
|
136 $loaded = true; |
|
137 } |
|
138 } |
|
139 if ( !$loaded ) |
133 { |
140 { |
134 // No cache file - select and retrieve from the database |
141 // No cache file - select and retrieve from the database |
135 $q = $db->sql_unbuffered_query("SELECT string_category, string_name, string_content FROM " . table_prefix . "language_strings WHERE lang_id = {$this->lang_id};"); |
142 $q = $db->sql_unbuffered_query("SELECT string_category, string_name, string_content FROM " . table_prefix . "language_strings WHERE lang_id = {$this->lang_id};"); |
136 if ( !$q ) |
143 if ( !$q ) |
137 $db->_die('lang.php - selecting language string data'); |
144 $db->_die('lang.php - selecting language string data'); |
148 $strings[$cat][ $row['string_name'] ] = $row['string_content']; |
155 $strings[$cat][ $row['string_name'] ] = $row['string_content']; |
149 } |
156 } |
150 while ( $row = $db->fetchrow() ); |
157 while ( $row = $db->fetchrow() ); |
151 // all done fetching |
158 // all done fetching |
152 $this->merge($strings); |
159 $this->merge($strings); |
|
160 $this->regen_caches(false); |
153 } |
161 } |
154 else |
162 else |
155 { |
163 { |
156 if ( !defined('ENANO_ALLOW_LOAD_NOLANG') ) |
164 if ( !defined('ENANO_ALLOW_LOAD_NOLANG') ) |
157 $db->_die('lang.php - No strings for language ' . $this->lang_code); |
165 $db->_die('lang.php - No strings for language ' . $this->lang_code); |
526 |
534 |
527 /** |
535 /** |
528 * Refetches the strings and writes out the cache file. |
536 * Refetches the strings and writes out the cache file. |
529 */ |
537 */ |
530 |
538 |
531 function regen_caches() |
539 function regen_caches($refetch = true) |
532 { |
540 { |
533 global $db, $session, $paths, $template, $plugins; // Common objects |
541 global $db, $session, $paths, $template, $plugins; // Common objects |
534 |
|
535 $lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php"; |
|
536 |
542 |
537 // Refresh the strings in RAM to the latest copies in the DB |
543 // Refresh the strings in RAM to the latest copies in the DB |
538 $this->fetch(false); |
544 if ( $refetch ) |
539 |
545 $this->fetch(false); |
540 $handle = @fopen($lang_file, 'w'); |
546 |
541 if ( !$handle ) |
547 // Load the cache manager |
542 // Couldn't open the file. Silently fail and let the strings come from the database. |
548 global $cache; |
543 return false; |
549 |
544 |
550 // Store it |
545 // The file's open, that means we should be good. |
551 $cache->store("lang_{$this->lang_id}", $this->strings, -1); |
546 fwrite($handle, '<?php |
|
547 // This file was generated automatically by Enano. You should not edit this file because any changes you make |
|
548 // to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel. |
|
549 |
|
550 $lang_cache = '); |
|
551 |
|
552 $exported = $this->var_export_string($this->strings); |
|
553 if ( empty($exported) ) |
|
554 // Ehh, that's not good |
|
555 $db->_die('lang.php - var_export_string() failed'); |
|
556 |
|
557 fwrite($handle, $exported . '; ?>'); |
|
558 |
552 |
559 // Update timestamp in database |
553 // Update timestamp in database |
560 $q = $db->sql_query('UPDATE ' . table_prefix . 'language SET last_changed = ' . time() . ' WHERE lang_id = ' . $this->lang_id . ';'); |
554 $q = $db->sql_query('UPDATE ' . table_prefix . 'language SET last_changed = ' . time() . ' WHERE lang_id = ' . $this->lang_id . ';'); |
561 if ( !$q ) |
555 if ( !$q ) |
562 $db->_die('lang.php - updating timestamp on language'); |
556 $db->_die('lang.php - updating timestamp on language'); |
563 |
|
564 // Done =) |
|
565 fclose($handle); |
|
566 } |
557 } |
567 |
558 |
568 /** |
559 /** |
569 * Calls var_export() on whatever, and returns the function's output. |
560 * Calls var_export() on whatever, and returns the function's output. |
570 * @param mixed Whatever you want var_exported. Usually an array. |
561 * @param mixed Whatever you want var_exported. Usually an array. |