1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* @author hashashiyyin [email protected] / [email protected] |
5
|
|
|
* Date: 02/08/24 |
6
|
|
|
* Time: 17:51 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Matecat\XliffParser\XliffReplacer; |
11
|
|
|
|
12
|
|
|
use Matecat\XliffParser\Utils\Strings; |
13
|
|
|
|
14
|
|
|
class Xliff20 extends AbstractXliffReplacer { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
private int $mdaGroupCounter = 0; |
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected bool $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*) |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected array $nodesToBuffer = [ |
28
|
|
|
'source', |
29
|
|
|
'mda:metadata', |
30
|
|
|
'memsource:additionalTagData', |
31
|
|
|
'originalData', |
32
|
|
|
'note' |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
|
|
protected function tagOpen( $parser, string $name, array $attr ) { |
39
|
|
|
|
40
|
|
|
$this->handleOpenUnit( $name, $attr ); |
41
|
|
|
|
42
|
|
|
if ( 'mda:metadata' === $name ) { |
43
|
|
|
$this->unitContainsMda = true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->checkSetInTarget( $name ); |
47
|
|
|
|
48
|
|
|
// open buffer |
49
|
|
|
$this->setInBuffer( $name ); |
50
|
|
|
|
51
|
|
|
// check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
52
|
|
|
// <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
53
|
|
|
if ( !$this->inTarget ) { |
54
|
|
|
|
55
|
|
|
$tag = ''; |
56
|
|
|
|
57
|
|
|
// |
58
|
|
|
// ============================================ |
59
|
|
|
// only for Xliff 2.* |
60
|
|
|
// ============================================ |
61
|
|
|
// |
62
|
|
|
// In xliff v2 we MUST add <mda:metadata> BEFORE <notes>/<originalData>/<segment>/<ignorable> |
63
|
|
|
// |
64
|
|
|
// As documentation says, <unit> contains: |
65
|
|
|
// |
66
|
|
|
// - elements from other namespaces, OPTIONAL |
67
|
|
|
// - Zero or one <notes> elements followed by |
68
|
|
|
// - Zero or one <originalData> element followed by |
69
|
|
|
// - One or more <segment> or <ignorable> elements in any order. |
70
|
|
|
// |
71
|
|
|
// For more info please refer to: |
72
|
|
|
// |
73
|
|
|
// http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#unit |
74
|
|
|
// |
75
|
|
|
if ( |
76
|
|
|
( $name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable' ) && |
77
|
|
|
$this->unitContainsMda === false && |
78
|
|
|
!empty( $this->transUnits[ $this->currentTransUnitId ] ) && |
79
|
|
|
!$this->hasWrittenCounts |
80
|
|
|
) { |
81
|
|
|
// we need to update counts here |
82
|
|
|
$this->updateCounts(); |
83
|
|
|
$this->hasWrittenCounts = true; |
84
|
|
|
$tag .= $this->getWordCountGroupForXliffV2(); |
85
|
|
|
$this->unitContainsMda = true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// construct tag |
89
|
|
|
$tag .= "<$name "; |
90
|
|
|
|
91
|
|
|
foreach ( $attr as $k => $v ) { |
92
|
|
|
//normal tag flux, put attributes in it but skip for translation state and set the right value for the attribute |
93
|
|
|
if ( $k != 'state' ) { |
94
|
|
|
$tag .= "$k=\"$v\" "; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$seg = $this->getCurrentSegment(); |
99
|
|
|
|
100
|
|
|
if ( $name === $this->tuTagName and !empty( $seg ) and isset( $seg[ 'sid' ] ) ) { |
|
|
|
|
101
|
|
|
|
102
|
|
|
// add `mtc:segment-id` to xliff v.2* |
103
|
|
|
if ( strpos( $tag, 'mtc:segment-id' ) === false ) { |
104
|
|
|
$tag .= "mtc:segment-id=\"{$seg[ 'sid' ]}\" "; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// replace state for xliff v2 |
110
|
|
|
if ( 'segment' === $name ) { // add state to segment in Xliff v2 |
111
|
|
|
[ $stateProp, ] = StatusToStateAttribute::getState( $this->xliffVersion, $seg[ 'status' ] ); |
112
|
|
|
$tag .= $stateProp; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$tag = $this->handleOpenXliffTag( $name, $attr, $tag ); |
116
|
|
|
|
117
|
|
|
$this->checkForSelfClosedTagAndFlush( $parser, $tag ); |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $name |
125
|
|
|
* @param array $attr |
126
|
|
|
* @param string $tag |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string { |
131
|
|
|
$tag = parent::handleOpenXliffTag( $name, $attr, $tag ); |
132
|
|
|
// add oasis xliff 20 namespace |
133
|
|
|
if ( $name === 'xliff' && !array_key_exists( 'xmlns:mda', $attr ) ) { |
134
|
|
|
$tag .= 'xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0"'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $tag; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritDoc |
142
|
|
|
*/ |
143
|
|
|
protected function tagClose( $parser, string $name ) { |
144
|
|
|
$tag = ''; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* if is a tag within <target> or |
148
|
|
|
* if it is an empty tag, do not add closing tag because we have already closed it in |
149
|
|
|
* |
150
|
|
|
* self::tagOpen method |
151
|
|
|
*/ |
152
|
|
|
if ( !$this->isEmpty ) { |
153
|
|
|
|
154
|
|
|
if ( !$this->inTarget ) { |
155
|
|
|
$tag = "</$name>"; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ( 'target' == $name ) { |
159
|
|
|
|
160
|
|
|
if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
161
|
|
|
|
162
|
|
|
$seg = $this->getCurrentSegment(); |
163
|
|
|
|
164
|
|
|
// update counts |
165
|
|
|
if ( !$this->hasWrittenCounts && !empty( $seg ) ) { |
166
|
|
|
$this->updateSegmentCounts( $seg ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// delete translations so the prepareSegment |
170
|
|
|
// will put source content in target tag |
171
|
|
|
if ( $this->sourceInTarget ) { |
172
|
|
|
$seg[ 'translation' ] = ''; |
173
|
|
|
$this->resetCounts(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// append $translation |
177
|
|
|
$translation = $this->prepareTranslation( $seg ); |
178
|
|
|
|
179
|
|
|
//append translation |
180
|
|
|
$tag = "<target>$translation</target>"; |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// signal we are leaving a target |
185
|
|
|
$this->targetWasWritten = true; |
186
|
|
|
$this->inTarget = false; |
187
|
|
|
$this->postProcAndFlush( $this->outputFP, $tag, true ); |
188
|
|
|
|
189
|
|
|
} elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
190
|
|
|
|
191
|
|
|
$this->bufferIsActive = false; |
192
|
|
|
|
193
|
|
|
// only for Xliff 2.* |
194
|
|
|
// write here <mda:metaGroup> and <mda:meta> if already present in the <unit> |
195
|
|
|
if ( 'mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts ) { |
196
|
|
|
|
197
|
|
|
// we need to update counts here |
198
|
|
|
$this->updateCounts(); |
199
|
|
|
$this->hasWrittenCounts = true; |
200
|
|
|
|
201
|
|
|
$tag = $this->CDATABuffer; |
202
|
|
|
$tag .= $this->getWordCountGroupForXliffV2( false ); |
203
|
|
|
$tag .= " </mda:metadata>"; |
204
|
|
|
|
205
|
|
|
} else { |
206
|
|
|
$tag = $this->CDATABuffer . "</$name>"; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$this->CDATABuffer = ""; |
210
|
|
|
|
211
|
|
|
//flush to the pointer |
212
|
|
|
$this->postProcAndFlush( $this->outputFP, $tag ); |
213
|
|
|
|
214
|
|
|
} elseif ( 'segment' === $name ) { |
215
|
|
|
|
216
|
|
|
// only for Xliff 2.* |
217
|
|
|
// if segment has no <target> add it BEFORE </segment> |
218
|
|
|
if ( !$this->targetWasWritten ) { |
219
|
|
|
|
220
|
|
|
$seg = $this->getCurrentSegment(); |
221
|
|
|
|
222
|
|
|
if ( isset( $seg[ 'translation' ] ) ) { |
223
|
|
|
|
224
|
|
|
$translation = $this->prepareTranslation( $seg ); |
225
|
|
|
// replace the tag |
226
|
|
|
$tag = "<target>$translation</target>"; |
227
|
|
|
|
228
|
|
|
$tag .= '</segment>'; |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// update segmentPositionInTu |
235
|
|
|
$this->segmentInUnitPosition++; |
236
|
|
|
|
237
|
|
|
$this->postProcAndFlush( $this->outputFP, $tag ); |
238
|
|
|
|
239
|
|
|
// we are leaving <segment>, reset $segmentHasTarget |
240
|
|
|
$this->targetWasWritten = false; |
241
|
|
|
|
242
|
|
|
} elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
243
|
|
|
$this->CDATABuffer .= "</$name>"; |
244
|
|
|
// Do NOT Flush |
245
|
|
|
} else { //generic tag closure do Nothing |
246
|
|
|
// flush to pointer |
247
|
|
|
$this->postProcAndFlush( $this->outputFP, $tag ); |
248
|
|
|
} |
249
|
|
|
} else { |
250
|
|
|
//ok, nothing to be done; reset flag for next coming tag |
251
|
|
|
$this->isEmpty = false; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
255
|
|
|
if ( $this->tuTagName === $name ) { |
256
|
|
|
$this->currentTransUnitIsTranslatable = null; |
257
|
|
|
$this->inTU = false; |
258
|
|
|
$this->unitContainsMda = false; |
259
|
|
|
$this->hasWrittenCounts = false; |
260
|
|
|
|
261
|
|
|
$this->resetCounts(); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Update counts |
267
|
|
|
*/ |
268
|
|
|
private function updateCounts() { |
269
|
|
|
|
270
|
|
|
$seg = $this->getCurrentSegment(); |
271
|
|
|
if ( !empty( $seg ) ) { |
272
|
|
|
$this->updateSegmentCounts( $seg ); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param bool $withMetadataTag |
279
|
|
|
* |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
|
|
private function getWordCountGroupForXliffV2( bool $withMetadataTag = true ): string { |
283
|
|
|
|
284
|
|
|
$this->mdaGroupCounter++; |
285
|
|
|
$segments_count_array = $this->counts[ 'segments_count_array' ]; |
286
|
|
|
|
287
|
|
|
$tag = ''; |
288
|
|
|
|
289
|
|
|
if ( $withMetadataTag === true ) { |
290
|
|
|
$tag .= '<mda:metadata>'; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
$index = 0; |
294
|
|
|
foreach ( $segments_count_array as $segments_count_item ) { |
295
|
|
|
|
296
|
|
|
$id = 'word_count_tu[' . $this->currentTransUnitId . '][' . $index . ']'; |
297
|
|
|
$index++; |
298
|
|
|
|
299
|
|
|
$tag .= " <mda:metaGroup id=\"" . $id . "\" category=\"row_xml_attribute\"> |
300
|
|
|
<mda:meta type=\"x-matecat-raw\">" . $segments_count_item[ 'raw_word_count' ] . "</mda:meta> |
301
|
|
|
<mda:meta type=\"x-matecat-weighted\">" . $segments_count_item[ 'eq_word_count' ] . "</mda:meta> |
302
|
|
|
</mda:metaGroup>"; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if ( $withMetadataTag === true ) { |
306
|
|
|
$tag .= '</mda:metadata>'; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $tag; |
310
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* prepare segment tagging for xliff insertion |
315
|
|
|
* |
316
|
|
|
* @param array $seg |
317
|
|
|
* |
318
|
|
|
* @return string |
319
|
|
|
*/ |
320
|
|
|
protected function prepareTranslation( array $seg ): string { |
321
|
|
|
|
322
|
|
|
$segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
323
|
|
|
$translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
324
|
|
|
$dataRefMap = ( isset( $seg[ 'data_ref_map' ] ) ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : []; |
325
|
|
|
|
326
|
|
|
if ( $seg [ 'translation' ] == '' ) { |
327
|
|
|
$translation = $segment; |
328
|
|
|
} else { |
329
|
|
|
if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
330
|
|
|
$error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
331
|
|
|
if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) { |
332
|
|
|
$translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $translation; |
338
|
|
|
|
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.