-
Notifications
You must be signed in to change notification settings - Fork 84
/
PostgresTranslatorRepository.php
404 lines (366 loc) · 17.5 KB
/
PostgresTranslatorRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
namespace Vsch\TranslationManager\Repositories;
use Vsch\TranslationManager\Models\Translation;
class PostgresTranslatorRepository extends TranslatorRepository
{
/**
* @param $keys
* @param $group
*/
public function updateUsedTranslationsForGroup($keys, $group)
{
$setKeys = "";
$resetKeys = "";
foreach ($keys as $key => $usage) {
if ($usage) {
if ($setKeys) $setKeys .= ',';
$setKeys .= "'$key'";
} else {
if ($resetKeys) $resetKeys .= ',';
$resetKeys .= "'$key'";
}
}
$ltm_translations = $this->getTranslationsTableName();
if ($setKeys) {
$this->translation->getConnection()->affectingStatement($this->adjustTranslationTable(<<<SQL
UPDATE $ltm_translations SET was_used = 1 WHERE was_used = 0 AND ("group" = ? OR "group" LIKE ? OR "group" LIKE ?) AND "key" IN ($setKeys)
SQL
), [$group, 'vnd:%.' . $group, 'wbn:%.' . $group]);
}
if ($resetKeys) {
$this->translation->getConnection()->affectingStatement($this->adjustTranslationTable(<<<SQL
UPDATE $ltm_translations SET was_used = 0 WHERE was_used <> 0 AND ("group" = ? OR "group" LIKE ? OR "group" LIKE ?) AND "key" IN ($resetKeys)
SQL
), [$group, 'vnd:%.' . $group, 'wbn:%.' . $group]);
}
}
public function updateIsDeletedByGroupAndKey($group, $key, $value)
{
$whereClause = $value == 1 ? 0 : 1;
$ltm_translations = $this->getTranslationsTableName();
return $this->translation->getConnection()->update($this->adjustTranslationTable(<<<SQL
UPDATE $ltm_translations SET is_deleted = ? WHERE is_deleted = $whereClause AND "group" = ? AND "key" LIKE BINARY ?
SQL
), [$value, $group, $key]);
}
public function updateGroupKeyStatusById($group, $key, $id)
{
$ltm_translations = $this->getTranslationsTableName();
$this->translation->getConnection()->update($this->adjustTranslationTable("UPDATE $ltm_translations SET \"group\" = ?, \"key\" LIKE BINARY ?, status = 1 WHERE id = ?"), [$group, $key, $id]);
}
public function selectTranslationsByLocaleAndGroup($locale, $db_group)
{
$ltm_translations = $this->getTranslationsTableName();
return $this->translation->fromQuery($this->adjustTranslationTable(<<<SQL
SELECT * FROM $ltm_translations WHERE locale = ? AND "group" = ?
SQL
), [$locale, $db_group], $this->getTranslation()->getConnectionName());
}
public function selectSourceByGroupAndKey($group, $key)
{
$ltm_translations = $this->getTranslationsTableName();
return $this->translation->getConnection()->select($this->adjustTranslationTable(<<<SQL
SELECT source FROM $ltm_translations WHERE "group" = ? AND "key" LIKE BINARY ?
SQL
), [$group, $key]);
}
/**
* @param array $values element to be inserted, each element was created via getInsertTranslationsElement call for the translation
*/
public function insertTranslations($values)
{
$ltm_translations = $this->getTranslationsTableName();
$sql = $this->adjustTranslationTable("INSERT INTO $ltm_translations (status, locale, \"group\", \"key\", \"value\", created_at, updated_at, source, saved_value, is_deleted, was_used) VALUES "
. implode(",", $values));
$this->translation->getConnection()->unprepared($sql);
}
public function deleteTranslationWhereIsDeleted($group = null)
{
$ltm_translations = $this->getTranslationsTableName();
if (!$group) {
$this->translation->getConnection()->affectingStatement($this->adjustTranslationTable("DELETE FROM $ltm_translations WHERE is_deleted = 1"));
} else {
$this->translation->getConnection()->affectingStatement($this->adjustTranslationTable("DELETE FROM $ltm_translations WHERE is_deleted = 1 AND \"group\" = ?"), [$group]);
}
}
public function deleteTranslationByGroup($group)
{
$ltm_translations = $this->getTranslationsTableName();
$this->translation->getConnection()->affectingStatement($this->adjustTranslationTable("DELETE FROM $ltm_translations WHERE \"group\" = ?"), [$group]);
}
public function deleteTranslationByGroupLocale($group, $locale)
{
$ltm_translations = $this->getTranslationsTableName();
$this->translation->getConnection()->affectingStatement($this->adjustTranslationTable("DELETE FROM $ltm_translations WHERE \"group\" = ? AND locale = ?"), [$group, $locale]);
}
public function updatePublishTranslations($newStatus, $group = null, $locale = null)
{
$ltm_translations = $this->getTranslationsTableName();
if ($group) {
if ($locale) {
$this->translation->getConnection()->affectingStatement($this->adjustTranslationTable(<<<SQL
UPDATE $ltm_translations SET saved_value = "value", status = ?, is_auto_added = 0 WHERE (ifnull(saved_value,'') <> ifnull("value",'') || (status <> ? and status <> ?)) AND "group" = ? AND locale = ?
SQL
), [$newStatus, $newStatus, Translation::STATUS_SAVED, $group, $locale]);
} else {
$this->translation->getConnection()->affectingStatement($this->adjustTranslationTable(<<<SQL
UPDATE $ltm_translations SET saved_value = "value", status = ?, is_auto_added = 0 WHERE (ifnull(saved_value,'') <> ifnull("value",'') || (status <> ? and status <> ?)) AND "group" = ?
SQL
), [$newStatus, $newStatus, Translation::STATUS_SAVED, $group]);
}
} else {
$this->translation->getConnection()->affectingStatement($this->adjustTranslationTable(<<<SQL
UPDATE $ltm_translations SET saved_value = "value", status = ?, is_auto_added = 0 WHERE (ifnull(saved_value,'') <> ifnull("value",'') || (status <> ? and status <> ?))
SQL
), [$newStatus, $newStatus, Translation::STATUS_SAVED]);
}
}
public function searchByRequest($q, $displayWhere, $limit)
{
$limitSQL = $limit > 0 ? 'LIMIT ' . $limit : '';
$ltm_translations = $this->getTranslationsTableName();
return $this->translation->getConnection()->select($this->adjustTranslationTable(<<<SQL
SELECT
id, status, locale, "group", "key", "value", created_at, updated_at, source, saved_value, is_deleted, was_used
FROM $ltm_translations rt WHERE ("key" LIKE ? OR "value" LIKE ?) $displayWhere
UNION ALL
SELECT NULL id, 0 status, lt.locale, kt."group", kt."key", NULL "value", NULL created_at, NULL updated_at, NULL source, NULL saved_value, NULL is_deleted, NULL was_used
FROM (SELECT DISTINCT locale FROM $ltm_translations WHERE 1=1 $displayWhere) lt
CROSS JOIN (SELECT DISTINCT "key", "group" FROM $ltm_translations WHERE 1=1 $displayWhere) kt
WHERE NOT exists(SELECT * FROM $ltm_translations tr WHERE tr."key" LIKE BINARY kt."key" AND tr."group" = kt."group" AND tr.locale = lt.locale)
AND "key" LIKE ?
ORDER BY "key", "group", locale
$limitSQL
SQL
), [$q, $q, $q,]);
}
public function allTranslations($group, $displayLocales)
{
$displayWhere = $displayLocales ? " AND locale IN ('" . implode("','", $displayLocales) . "')" : '';
$ltm_translations = $this->getTranslationsTableName();
return $this->getTranslation()->fromQuery($this->adjustTranslationTable(<<<SQL
SELECT
ltm.id::text,
ltm.status::text,
ltm.locale::text,
ltm."group"::text,
ltm."key"::text,
ltm.value::text,
ltm.created_at::text,
ltm.updated_at::text,
ltm.saved_value::text,
ltm.is_deleted::text,
ltm.was_used::text,
(ltm.source <> '')::text has_source,
ltm.is_auto_added::text
FROM $ltm_translations ltm WHERE "group" = ? $displayWhere
UNION ALL
SELECT DISTINCT
NULL id,
NULL status,
locale,
"group",
"key",
NULL "value",
NULL created_at,
NULL updated_at,
NULL saved_value,
NULL is_deleted,
NULL was_used,
NULL has_source,
NULL is_auto_added
FROM
(SELECT * FROM (SELECT DISTINCT locale FROM $ltm_translations WHERE 1=1 $displayWhere) lcs
CROSS JOIN (SELECT DISTINCT "group", "key" FROM $ltm_translations WHERE "group" = ? $displayWhere) grp) m
WHERE NOT EXISTS(SELECT * FROM $ltm_translations t WHERE t.locale = m.locale AND t."group" = m."group" AND t."key" LIKE BINARY m."key")
ORDER BY "key" ASC
SQL
), [$group, $group], $this->getTranslation()->getConnectionName());
}
public function stats($displayLocales)
{
$displayWhere = $displayLocales ? " AND locale IN ('" . implode("','", $displayLocales) . "')" : '';
$ltm_translations = $this->getTranslationsTableName();
return $this->translation->getConnection()->select($this->adjustTranslationTable(<<<SQL
SELECT (mx.total_keys - lcs.total) missing, lcs.changed, lcs.cached, lcs.deleted, lcs.locale, lcs."group"
FROM
(SELECT sum(total) total, sum(changed) changed, sum(cached) cached, sum(deleted) deleted, "group", locale
FROM
(SELECT count(value) total,
sum(CASE WHEN status = 1 THEN 1 ELSE 0 END) changed,
sum(CASE WHEN status = 2 AND "value" IS NOT NULL THEN 1 ELSE 0 END) cached,
sum(is_deleted) deleted,
"group", locale
FROM $ltm_translations lt WHERE 1=1 $displayWhere GROUP BY "group", locale
UNION ALL
SELECT DISTINCT 0, 0, 0, 0, "group", locale FROM (SELECT DISTINCT locale FROM $ltm_translations WHERE 1=1 $displayWhere) lc
CROSS JOIN (SELECT DISTINCT "group" FROM $ltm_translations) lg) a
GROUP BY "group", locale) lcs
JOIN (SELECT count(DISTINCT "key") total_keys, "group" FROM $ltm_translations WHERE 1=1 $displayWhere GROUP BY "group") mx
ON lcs."group" = mx."group"
WHERE (lcs.total < mx.total_keys OR lcs.changed > 0 OR lcs.cached > 0 OR lcs.deleted > 0) AND locale <> 'json'
SQL
));
}
public function findMismatches($displayLocales, $primaryLocale, $translatingLocale)
{
$displayWhere = $displayLocales ? " AND locale IN ('" . implode("','", $displayLocales) . "')" : '';
$ltm_translations = $this->getTranslationsTableName();
return $this->translation->getConnection()->select($this->adjustTranslationTable(<<<SQL
SELECT DISTINCT lt.*, ft.ru, ft.en
FROM (SELECT * FROM $ltm_translations WHERE 1=1 $displayWhere) lt
JOIN
(SELECT DISTINCT mt."key", BINARY mt.ru ru, BINARY mt.en en
FROM (SELECT lt."group", lt."key", group_concat(CASE lt.locale WHEN '$primaryLocale' THEN "VALUE" ELSE NULL END) en, group_concat(CASE lt.locale WHEN '$translatingLocale' THEN "VALUE" ELSE NULL END) ru
FROM (SELECT "value", "group", "key", locale FROM $ltm_translations WHERE 1=1 $displayWhere
UNION ALL
SELECT NULL, "group", "key", locale FROM ((SELECT DISTINCT locale FROM $ltm_translations WHERE 1=1 $displayWhere) lc
CROSS JOIN (SELECT DISTINCT "group", "key" FROM $ltm_translations WHERE 1=1 $displayWhere) lg)
) lt
GROUP BY "group", "key") mt
JOIN (SELECT lt."group", lt."key", group_concat(CASE lt.locale WHEN '$primaryLocale' THEN "VALUE" ELSE NULL END) en, group_concat(CASE lt.locale WHEN '$translatingLocale' THEN "VALUE" ELSE NULL END) ru
FROM (SELECT "value", "group", "key", locale FROM $ltm_translations WHERE 1=1 $displayWhere
UNION ALL
SELECT NULL, "group", "key", locale FROM ((SELECT DISTINCT locale FROM $ltm_translations WHERE 1=1 $displayWhere) lc
CROSS JOIN (SELECT DISTINCT "group", "key" FROM $ltm_translations WHERE 1=1 $displayWhere) lg)
) lt
GROUP BY "group", "key") ht ON mt."key" LIKE BINARY ht."key"
WHERE (mt.ru NOT LIKE BINARY ht.ru AND mt.en LIKE BINARY ht.en) OR (mt.ru LIKE BINARY ht.ru AND mt.en NOT LIKE BINARY ht.en)
) ft
ON (lt.locale = '$translatingLocale' AND lt.value LIKE BINARY ft.ru) AND lt."key" LIKE BINARY ft.key
ORDER BY "key", "group"
SQL
));
}
public function selectToDeleteTranslations($group, $key, $locale, $rowIds)
{
$ltm_translations = $this->getTranslationsTableName();
return $this->translation->getConnection()->select($this->adjustTranslationTable(<<<SQL
SELECT GROUP_CONCAT(id SEPARATOR ',') ids FROM $ltm_translations tr
WHERE "group" = ? AND "key" LIKE BINARY ? AND locale IN (?) AND id NOT IN ($rowIds)
SQL
), [$group, $key, $locale]);
}
public function selectKeys($src, $dst, $locales, $srcgrp, $srckey, $dstkey, $dstgrp)
{
$userLocales = "'" . implode("','", $locales) . "'";
$ltm_translations = $this->getTranslationsTableName();
if ((substr($src, 0, 1) === '*')) {
if ($dst === null) {
$rows = $this->translation->getConnection()->select($this->adjustTranslationTable($sql = <<<SQL
SELECT DISTINCT "group", "key", locale, id, NULL dst, NULL dstgrp FROM $ltm_translations t1
WHERE "group" = ? AND "key" LIKE BINARY ? AND locale IN ($userLocales)
ORDER BY locale, "key"
SQL
), [
$srcgrp,
'%' . mb_substr($srckey, 1),
]);
} else {
$rows = $this->translation->getConnection()->select($this->adjustTranslationTable($sql = <<<SQL
SELECT DISTINCT "group", "key", locale, id, CONCAT(SUBSTR("key", 1, CHAR_LENGTH("key")-?), ?) dst, ? dstgrp FROM $ltm_translations t1
WHERE "group" = ? AND "key" LIKE BINARY ? AND locale IN ($userLocales)
AND NOT exists(SELECT * FROM $ltm_translations t2 WHERE t2.value IS NOT NULL AND t2."group" = ? AND t1.locale = t2.locale
AND t2."key" LIKE BINARY CONCAT(SUBSTR(t1."key", 1, CHAR_LENGTH(t1."key")-?), ?))
ORDER BY locale, "key"
SQL
), [
mb_strlen($srckey) - 1,
mb_substr($dstkey, 1),
$dstgrp,
$srcgrp,
'%' . mb_substr($srckey, 1),
$dstgrp,
mb_strlen($srckey) - 1,
mb_substr($dstkey, 1),
]);
}
} else if ((substr($src, -1, 1) === '*')) {
if ($dst === null) {
$rows = $this->translation->getConnection()->select($this->adjustTranslationTable($sql = <<<SQL
SELECT DISTINCT "group", "key", locale, id, NULL dst, NULL dstgrp FROM $ltm_translations t1
WHERE "group" = ? AND "key" LIKE BINARY ? AND locale IN ($userLocales)
ORDER BY locale, "key"
SQL
), [
$srcgrp,
mb_substr($srckey, 0, -1) . '%',
]);
} else {
$rows = $this->translation->getConnection()->select($this->adjustTranslationTable($sql = <<<SQL
SELECT DISTINCT "group", "key", locale, id, CONCAT(?, SUBSTR("key", ?+1, CHAR_LENGTH("key")-?)) dst, ? dstgrp FROM $ltm_translations t1
WHERE "group" = ? AND "key" LIKE BINARY ? AND locale IN ($userLocales)
AND NOT exists(SELECT * FROM $ltm_translations t2 WHERE t2.value IS NOT NULL AND t2."group" = ? AND t1.locale = t2.locale
AND t2."key" LIKE BINARY CONCAT(?, SUBSTR(t1."key", ?+1, CHAR_LENGTH(t1."key")-?)))
ORDER BY locale, "key"
SQL
), [
mb_substr($dstkey, 0, -1),
mb_strlen($srckey) - 1,
mb_strlen($srckey) - 1,
$dstgrp,
$srcgrp,
mb_substr($srckey, 0, -1) . '%',
$dstgrp,
mb_substr($dstkey, 0, -1),
mb_strlen($srckey) - 1,
mb_strlen($srckey) - 1,
]);
}
} else {
if ($dst === null) {
$rows = $this->translation->getConnection()->select($this->adjustTranslationTable($sql = <<<SQL
SELECT DISTINCT "group", "key", locale, id, NULL dst, NULL dstgrp FROM $ltm_translations t1
WHERE "group" = ? AND "key" LIKE BINARY ? AND locale IN ($userLocales)
ORDER BY locale, "key"
SQL
), [
$srcgrp,
$srckey,
]);
} else {
$rows = $this->translation->getConnection()->select($this->adjustTranslationTable($sql = <<<SQL
SELECT DISTINCT "group", "key", locale, id, ? dst, ? dstgrp FROM $ltm_translations t1
WHERE "group" = ? AND "key" LIKE BINARY ? AND locale IN ($userLocales)
AND NOT exists(SELECT * FROM $ltm_translations t2 WHERE t2.value IS NOT NULL AND t2."group" = ? AND t1.locale = t2.locale AND t2."key" LIKE ?)
ORDER BY locale, "key"
SQL
), [
$dstkey,
$dstgrp,
$srcgrp,
$srckey,
$dstgrp,
$dstkey,
]);
}
}
return $rows;
}
public function copyKeys($dstgrp, $dstkey, $rowId)
{
$ltm_translations = $this->getTranslationsTableName();
return $this->translation->getConnection()->insert($this->adjustTranslationTable(<<<SQL
INSERT INTO $ltm_translations (status, locale, "group", "key", "value", created_at, updated_at, source, saved_value, is_deleted, was_used)
SELECT
1 status,
locale,
? "group",
? "key",
"value",
current_date created_at,
current_date updated_at,
source,
saved_value,
is_deleted,
was_used
FROM $ltm_translations t1
WHERE id = ?
SQL
), [$dstgrp, $dstkey, $rowId]);
}
public function findFilledGroups()
{
return $this->translation->whereNotNull('value')->select(DB::raw('DISTINCT "group"'))->get('group');
}
}