/[web]/nav/lib.php
ViewVC logotype

Contents of /nav/lib.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3207 - (show annotations) (download)
Sun Apr 20 16:32:11 2014 UTC (10 years ago) by filip
File size: 8113 byte(s)
conversion of mognase to gettext
1 <?php
2 /**
3 * mageia.org global nav bar utilities.
4 *
5 * PHP version 5.4
6 *
7 * @category Mageia
8 * @package Mageia\Web\nav
9 * @author rda <rda@mageia.org>
10 * @link http://nav.mageia.org/
11 *
12 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2+
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License aspublished by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
18 */
19 // definition
20
21 class NCache
22 {
23 function __construct() { }
24
25 /**
26 * Factory.
27 *
28 * @param string $path where cache file store is located, relative to app path.
29 * @param integer $timeout in seconds
30 *
31 * @return NCache
32 */
33 public static function build($path, $timeout = 3600)
34 {
35 $path = __DIR__ . '/' . $path;
36
37 if (!is_dir($path))
38 return null;
39
40 if ($timeout < 60)
41 $timeout = 60;
42
43 $i = new self;
44 $i->_path = $path;
45 $i->_timeout = $timeout;
46
47 return $i;
48 }
49
50 /**
51 * Get value for $key.
52 *
53 * @param mixed $key
54 *
55 * @return mixed
56 */
57 function get($key = null)
58 {
59 if (is_null($key))
60 return false;
61
62 $filename = $this->_get_filename($key);
63
64 if ($this->_is_valid_file($filename, $this->_timeout)) {
65 return unserialize(file_get_contents($filename));
66 }
67
68 return null;
69 }
70
71 /**
72 * Save $value under $key.
73 *
74 * @param mixed $key
75 * @param mixed $value
76 */
77 function set($key, $value)
78 {
79 if (is_null($key))
80 return false;
81
82 $filename = $this->_get_filename($key);
83 file_put_contents($filename, serialize($value));
84
85 return true;
86 }
87
88 /**
89 * Get cache file from key.
90 *
91 * @param mixed $key
92 *
93 * @return string
94 */
95 private function _get_filename($key)
96 {
97 $key = hash('sha1', serialize($key));
98
99 return $this->_path . '/' . $key . '.cache';
100 }
101
102 /**
103 * Check that the cache file exists and has not expired.
104 *
105 * @param string $filename
106 *
107 * @return boolean
108 */
109 private function _is_valid_file($filename, $timeout)
110 {
111 if (!file_exists($filename)) {
112 //error_log(sprintf('Could not find %s', $filename), 0);
113 return false;
114 }
115
116 if (filemtime($filename) + $timeout < time()) {
117 //error_log(sprintf('%s timestamp expired (timeout was %ds.).', $filename, $timeout));
118 unlink($filename);
119 return false;
120 }
121
122 //error_log(sprintf('Found %s', $filename));
123 return true;
124 }
125 }
126
127 class l10n
128 {
129 // public static $t;
130
131 /**
132 * Load langs/$lang.lang into global $_t array.
133 *
134 * @param string $lang
135 *
136 * @return void
137 */
138 public static function load($lang)
139 {
140 global $_t;
141 $_t = array();
142
143 if ($lang == 'en')
144 return;
145
146 $po_file = __DIR__ . '/langs/' . $lang . '.po';
147 $cache_file = __DIR__ . '/var/tmp/cache/nav_lang_' . $lang . '.php';
148 $po_ts = filemtime($po_file);
149
150 if (file_exists($cache_file)) {
151 include $cache_file;
152 if ($_ts > $po_ts)
153 return;
154 }
155
156 if (file_exists($po_file)) {
157 $dictionary = phpmo_parse_po_file($po_file);
158
159 foreach ($dictionary as $key => $value) {
160 if ($key != '') {
161 if ($value['msgstr'][0] != '') {
162 $_t[trim($key)] = trim($value['msgstr'][0]);
163 } else {
164 $_t[trim($key)] = trim($key);
165 }
166 }
167 }
168
169 $_t_data = var_export($_t, true);
170 $cache = <<<P
171 <?php
172 /**! Generated. Do not edit. */
173
174 // filemtime($po_file)
175 \$_ts = $po_ts;
176
177 // $lang strings
178 global \$_t;
179 \$_t = $_t_data;
180 P;
181 file_put_contents($cache_file, $cache);
182 }
183 }
184
185 /**
186 * Get value for key $s in global array $_t.
187 *
188 * @param string $s
189 *
190 * @return string
191 */
192 public static function _t($s) {
193 if (trim($s) == '')
194 return '';
195
196 global $_t;
197
198 $s = array_key_exists($s, $_t) ? $_t[$s] : $s;
199 $s = trim(str_replace(array('{ok}', '{OK}', '{Ok}', '{oK}'), '', $s));
200
201 return $s;
202 }
203 }
204
205 /**
206 * Produce navigation HTML code.
207 *
208 * @param boolean $wrap = false should it be wrapped in a <header id="nav" /> element?
209 * @param string $lang = 'en'
210 * @param string $inject = null
211 * @param string $vhost = 'www.mageia.org'
212 * @param object $cache
213 *
214 * @return string HTML code
215 */
216 function _mgnav_html($wrap = false, $lang = 'en', $inject = null, $vhost = 'www.mageia.org', $cache = null)
217 {
218 $key = array($wrap, $lang, $inject, $vhost);
219
220 if (!is_null($cache) && ($h = $cache->get($key))) {
221 apache_note('navCacheHit', 1);
222 return $h;
223 }
224
225 apache_note('navCacheHit', 0);
226
227 $lang = _lang_check($lang);
228
229 l10n::load($lang, $cache);
230
231 $tn = array(
232 array('mageia', '//$S/$L/map/', 'Mageia', l10n::_t('Go to mageia.org site map.')),
233 array('about', '//$S/$L/about/', l10n::_t('About&nbsp;us'), l10n::_t('Learn more about Mageia.')),
234 array('downloads', '//$S/$L/downloads/', l10n::_t('Downloads'), l10n::_t('Download Mageia ISO and updates.')),
235 array('support', '//$S/$L/support/', l10n::_t('Support'), l10n::_t('Get support from Mageia community.')),
236 array('wiki', '//wiki.mageia.org/', l10n::_t('Wiki'), l10n::_t('Wiki of the Mageia Community')),
237 array('doc', '//$S/$L/doc/', l10n::_t('Docs'), l10n::_t('Documentations of Mageia')),
238 array('community', '//$S/$L/community/', l10n::_t('Community'), l10n::_t('')),
239 array('contribute', '//$S/$L/contribute/', l10n::_t('Contribute'), l10n::_t('You too can build Mageia with us!')),
240 array('donate', '//$S/$L/donate/', l10n::_t('Donate'), l10n::_t('')),
241 array('you', '//identity.mageia.org/', l10n::_t('You'), l10n::_t('Your Mageia online account.')),
242 array('contact', '//$S/$L/contact/', l10n::_t('Contact'), l10n::_t('Contact Us'))
243 // <search>
244 );
245
246 $s = array();
247 foreach ($tn as $i) {
248 $s[] = sprintf('<li><a href="%s" class="%s" title="%s">%s</a></li>',
249 str_replace(
250 array('$L', '$S'),
251 array($lang, $vhost),
252 $i[1]
253 ),
254 $i[0],
255 $i[3],
256 $i[2]
257 );
258 }
259
260 if (!is_null($inject))
261 $s[] = sprintf('<li>%s</li>', $inject);
262
263 $s = implode($s);
264 $h = sprintf('<!--googleoff: all--><nav id="mgnav"><ul id="nav">%s</ul></nav><!--googleon: all-->', $s);
265
266 if ($wrap)
267 $h = sprintf('<header id="hmgn">%s
268 <link rel="icon" type="image/png" href="/g/favicon.png" />
269 </header>', $h);
270
271 if (!is_null($cache))
272 $cache->set($key, $h);
273
274 return $h;
275 }
276
277 /**
278 * Returns CSS definition ready to be inserted into a HTML document.
279 *
280 * @return string
281 */
282 function _mgnav_style()
283 {
284 return '<style>' . file_get_contents(__DIR__ . '/css/source.css') . '</style>';
285 }
286
287 /**
288 * Get the primary language subtag only.<p></p>
289 */
290 function _lang_check($s = null)
291 {
292 if (is_null($s)) {
293 return 'en';
294 }
295
296 $supported = array(
297 'ast',
298 'cs',
299 'de',
300 'el', 'en', 'eo', 'es', 'et',
301 'fi', 'fr',
302 'id', 'it',
303 'lv',
304 'nb', 'nl',
305 'pl', 'pt', 'pt-br',
306 'ro', 'ru',
307 'sl',
308 'tr',
309 'uk', 'ur',
310 'zh-cn', 'zh-tw'
311 );
312
313 if (in_array($s, $supported))
314 return $s;
315
316 $sub = explode('-', $s);
317 $sub = strtolower($sub[0]);
318
319 if (in_array($sub, $supported))
320 return $sub;
321
322 return 'en';
323 }

  ViewVC Help
Powered by ViewVC 1.1.30