From ae803f79a80b07545cc94a1e2129110b201cdba9 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 28 Mar 2016 20:04:26 -0500
Subject: [PATCH] i18n: Add MAJOR_VERSION to the language packs

Since two major versions will internationalization support will soon be
available, the language pack system will need a way to indicate which major
version of osTicket the phrases were compiled for. This patch adds a
`Phrases-Version` tag to the MANIFEST, which will be a copy of the
MAJOR_VERSION included in the messages.pot file created via `make-pot`.
Then, a `Build-Major-Version` tag is added to the MANIFEST which represents
the version of osTicket used to compile the language pack.

Ordinarily, both strings should be the same, and they should also both match
the MAJOR_VERSION of osTicket when the language packs are loaded on a
production system.

This also adds support for PHAR compression in the build process. Since
accessing a PHAR file in a request burns 1ms per megabyte of PHAR file,
reducing the PHAR file size by up to 77% means that much less time spent
loading PHAR files. This can be especially useful for a busy server with
support for several languages. But, ZLIB or BZIP2 support would be required
for all production servers.
---
 include/cli/modules/i18n.php | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/include/cli/modules/i18n.php b/include/cli/modules/i18n.php
index f4c06caec..0931361dd 100644
--- a/include/cli/modules/i18n.php
+++ b/include/cli/modules/i18n.php
@@ -40,6 +40,10 @@ class i18n_Compiler extends Module {
             'help' => 'Add a domain to the path/context of PO strings'),
         'dns' => array('-d', '--dns', 'default' => false, 'metavar' => 'zone-id',
             'help' => 'Write signature to DNS (via this AWS HostedZoneId)'),
+        'zlib' => array('-z', '--zlib', 'default' => false,
+            'action' => 'store_true', 'help' => 'Compress PHAR with zlib'),
+        'bzip2' => array('-j', '--bzip2', 'default' => false,
+            'action' => 'store_true', 'help' => 'Compress PHAR with bzip2'),
     );
 
     var $epilog = "Note: If updating DNS, you will need to set
@@ -100,7 +104,7 @@ class i18n_Compiler extends Module {
                 $this->fail('API key is required');
             if (!$options['lang'])
                 $this->fail('Language code is required. See `list`');
-            $this->_build($options['lang']);
+            $this->_build($options['lang'], $options);
             break;
         case 'similar':
             $this->find_similar($options);
@@ -146,7 +150,7 @@ class i18n_Compiler extends Module {
         }
     }
 
-    function _build($lang) {
+    function _build($lang, $options) {
         list($code, $zip) = $this->_request("download/$lang.zip");
 
         if ($code !== 200)
@@ -164,6 +168,10 @@ class i18n_Compiler extends Module {
         @unlink(I18N_DIR."$lang.phar");
         $phar = new Phar(I18N_DIR."$lang.phar");
         $phar->startBuffering();
+        if ($options['zlib'])
+            $phar->compress(Phar::GZ, 'phar');
+        if ($options['bzip2'])
+            $phar->compress(Phar::BZ2, 'phar');
 
         $po_file = false;
 
@@ -190,9 +198,9 @@ class i18n_Compiler extends Module {
         }
         foreach ($langs as $l) {
             list($code, $js) = $this->_http_get(
-                'http://imperavi.com/webdownload/redactor/lang/?lang='
-                .strtolower($l));
-            if ($code == 200 && ($js != 'File not found')) {
+                sprintf('https://imperavi.com/download/redactor/langs/%s/',
+                    strtolower($l)));
+            if ($code == 200 && strlen($js) > 100) {
                 $phar->addFromString('js/redactor.js', $js);
                 break;
             }
@@ -277,7 +285,9 @@ class i18n_Compiler extends Module {
         $po_header = Mail_Parse::splitHeaders($mo['']);
         $info = array(
             'Build-Date' => date(DATE_RFC822),
+            'Phrases-Version' => $po_header['X-Osticket-Major-Version'],
             'Build-Version' => trim(`git describe`),
+            'Build-Major-Version' => MAJOR_VERSION,
             'Language' => $po_header['Language'],
             #'Phrases' =>
             #'Translated' =>
-- 
GitLab