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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
|
<?php
/**
* Implements special page for group management, where file based message
* groups are be managed.
*
* @file
* @author Niklas Laxström
* @author Siebrand Mazeland
* @license GPL-2.0-or-later
*/
use MediaWiki\Extensions\Translate\MessageSync\MessageSourceChange;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
/**
* Class for special page Special:ManageMessageGroups. On this special page
* file based message groups can be managed (FileBasedMessageGroup). This page
* allows updating of the file cache, import and fuzzy for source language
* messages, as well as import/update of messages in other languages.
*
* @ingroup SpecialPage TranslateSpecialPage
* Rewritten in 2012-04-23
*/
class SpecialManageGroups extends SpecialPage {
private const RIGHT = 'translate-manage';
/**
* @var DifferenceEngine
*/
protected $diff;
/**
* @var string Path to the change cdb file.
*/
protected $cdb;
/**
* @var bool Has the necessary right specified by the RIGHT constant
*/
protected $hasRight = false;
public function __construct() {
// Anyone is allowed to see, but actions are restricted
parent::__construct( 'ManageMessageGroups' );
}
public function doesWrites() {
return true;
}
protected function getGroupName() {
return 'translation';
}
public function getDescription() {
return $this->msg( 'managemessagegroups' )->text();
}
public function execute( $par ) {
$this->setHeaders();
$out = $this->getOutput();
$out->addModuleStyles( 'ext.translate.special.managegroups.styles' );
$out->addModules( 'ext.translate.special.managegroups' );
$out->addHelpLink( 'Help:Extension:Translate/Group_management' );
$name = $par ?: MessageChangeStorage::DEFAULT_NAME;
$this->cdb = MessageChangeStorage::getCdbPath( $name );
if ( !MessageChangeStorage::isValidCdbName( $name ) || !file_exists( $this->cdb ) ) {
// @todo Tell them when changes was last checked/process
// or how to initiate recheck.
$out->addWikiMsg( 'translate-smg-nochanges' );
return;
}
$user = $this->getUser();
$this->hasRight = $user->isAllowed( self::RIGHT );
$req = $this->getRequest();
if ( !$req->wasPosted() ) {
$this->showChanges( $this->getLimit() );
return;
}
$token = $req->getVal( 'token' );
if ( !$this->hasRight || !$user->matchEditToken( $token ) ) {
throw new PermissionsError( self::RIGHT );
}
$this->processSubmit();
}
/**
* How many changes can be shown per page.
* @return int
*/
protected function getLimit() {
$limits = [
1000, // Default max
ini_get( 'max_input_vars' ),
ini_get( 'suhosin.post.max_vars' ),
ini_get( 'suhosin.request.max_vars' )
];
// Ignore things not set
$limits = array_filter( $limits );
return min( $limits );
}
protected function getLegend() {
$text = $this->diff->addHeader(
'',
$this->msg( 'translate-smg-left' )->escaped(),
$this->msg( 'translate-smg-right' )->escaped()
);
return Html::rawElement( 'div', [ 'class' => 'mw-translate-smg-header' ], $text );
}
protected function showChanges( $limit ) {
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
$diff = new DifferenceEngine( $this->getContext() );
$diff->showDiffStyle();
$diff->setReducedLineNumbers();
$this->diff = $diff;
$out = $this->getOutput();
$out->addHTML(
'' .
Html::openElement( 'form', [ 'method' => 'post' ] ) .
Html::hidden( 'title', $this->getPageTitle()->getPrefixedText(), [
'id' => 'smgPageTitle'
] ) .
Html::hidden( 'token', $this->getUser()->getEditToken() ) .
Html::hidden( 'changesetModifiedTime',
MessageChangeStorage::getLastModifiedTime( $this->cdb ) ) .
$this->getLegend()
);
// The above count as three
$limit = $limit - 3;
$reader = \Cdb\Reader::open( $this->cdb );
$groups = $this->getGroupsFromCdb( $reader );
foreach ( $groups as $id => $group ) {
$sourceChanges = MessageSourceChange::loadModifications(
TranslateUtils::deserialize( $reader->get( $id ) )
);
$out->addHTML( Html::element( 'h2', [], $group->getLabel() ) );
// Reduce page existance queries to one per group
$lb = new LinkBatch();
$ns = $group->getNamespace();
$isCap = MWNamespace::isCapitalized( $ns );
$languages = $sourceChanges->getLanguages();
foreach ( $languages as $language ) {
$languageChanges = $sourceChanges->getModificationsForLanguage( $language );
foreach ( $languageChanges as $type => $changes ) {
foreach ( $changes as $params ) {
// Constructing title objects is way slower
$key = $params['key'];
if ( $isCap ) {
$key = $contLang->ucfirst( $key );
}
$lb->add( $ns, "$key/$language" );
}
}
}
$lb->execute();
foreach ( $languages as $language ) {
// Handle and generate UI for additions, deletions, change
$changes = [];
$changes[ MessageSourceChange::ADDITION ] = $sourceChanges->getAdditions( $language );
$changes[ MessageSourceChange::DELETION ] = $sourceChanges->getDeletions( $language );
$changes[ MessageSourceChange::CHANGE ] = $sourceChanges->getChanges( $language );
foreach ( $changes as $type => $messages ) {
foreach ( $messages as $params ) {
$change = $this->formatChange( $group, $language, $type, $params, $limit );
$out->addHTML( $change );
if ( $limit <= 0 ) {
// We need to restrict the changes per page per form submission
// limitations as well as performance.
$out->wrapWikiMsg( "<div class=warning>\n$1\n</div>", 'translate-smg-more' );
break 4;
}
}
}
// Handle and generate UI for renames
$this->showRenames( $group, $sourceChanges, $out, $language, $limit );
}
}
$attribs = [ 'type' => 'submit', 'class' => 'mw-translate-smg-submit' ];
if ( !$this->hasRight ) {
$attribs['disabled'] = 'disabled';
$attribs['title'] = $this->msg( 'translate-smg-notallowed' )->text();
}
$button = Html::element( 'button', $attribs, $this->msg( 'translate-smg-submit' )->text() );
$out->addHTML( $button );
$out->addHTML( Html::closeElement( 'form' ) );
}
/**
* @param MessageGroup $group
* @param string $language
* @param string $type
* @param array $params
* @param int &$limit
* @return string HTML
*/
protected function formatChange( MessageGroup $group, $language, $type, $params, &$limit ) {
$key = $params['key'];
$title = Title::makeTitleSafe( $group->getNamespace(), "$key/$language" );
$id = self::changeId( $group->getId(), $language, $type, $key );
if ( $title && $type === 'addition' && $title->exists() ) {
// The message has for some reason dropped out from cache
// or perhaps it is being reused. In any case treat it
// as a change for display, so the admin can see if
// action is needed and let the message be processed.
// Otherwise it will end up in the postponed category
// forever and will prevent rebuilding the cache, which
// leads to many other annoying problems.
$type = 'change';
} elseif ( $title && ( $type === 'deletion' || $type === 'change' ) && !$title->exists() ) {
// This happens if a message key has been renamed
// The change can be ignored.
return '';
}
$text = '';
$titleLink = $this->getLinkRenderer()->makeLink( $title );
if ( $type === 'deletion' ) {
$wiki = ContentHandler::getContentText(
MediaWikiServices::getInstance()
->getRevisionLookup()
->getRevisionByTitle( $title )
->getContent( SlotRecord::MAIN )
);
$oldContent = ContentHandler::makeContent( $wiki, $title );
$newContent = ContentHandler::makeContent( '', $title );
$this->diff->setContent( $oldContent, $newContent );
$text = $this->diff->getDiff( $titleLink, '' );
} elseif ( $type === 'addition' ) {
$oldContent = ContentHandler::makeContent( '', $title );
$newContent = ContentHandler::makeContent( $params['content'], $title );
$this->diff->setContent( $oldContent, $newContent );
$menu = '';
if ( $group->getSourceLanguage() === $language && $this->hasRight ) {
$menu = Html::rawElement( 'button', [
'class' => 'smg-rename-actions', 'type' => 'button',
'data-group-id' => $group->getId(), 'data-lang' => $language, 'data-msgkey' => $key,
'data-msgtitle' => $title->getFullText() ], '' );
}
$text = $this->diff->getDiff( '', $titleLink . $menu );
} elseif ( $type === 'change' ) {
$wiki = TranslateUtils::getContentForTitle( $title, true );
$actions = '';
$importSelected = true;
if ( $group->getSourceLanguage() === $language ) {
$importSelected = false;
$label = $this->msg( 'translate-manage-action-fuzzy' )->text();
$actions .= Xml::radioLabel( $label, "msg/$id", "fuzzy", "f/$id", true );
}
$label = $this->msg( 'translate-manage-action-import' )->text();
$actions .= Xml::radioLabel( $label, "msg/$id", "import", "imp/$id", $importSelected );
$label = $this->msg( 'translate-manage-action-ignore' )->text();
$actions .= Xml::radioLabel( $label, "msg/$id", "ignore", "i/$id" );
$limit--;
$oldContent = ContentHandler::makeContent( $wiki, $title );
$newContent = ContentHandler::makeContent( $params['content'], $title );
$this->diff->setContent( $oldContent, $newContent );
$text .= $this->diff->getDiff( $titleLink, $actions );
}
$hidden = Html::hidden( $id, 1 );
$limit--;
$text .= $hidden;
$classes = "mw-translate-smg-change smg-change-$type";
if ( $limit < 0 ) {
// Don't add if one of the fields might get dropped of at submission
return '';
}
return Html::rawElement( 'div', [ 'class' => $classes ], $text );
}
protected function processSubmit() {
$req = $this->getRequest();
$out = $this->getOutput();
$errorGroups = [];
$modificationJobs = $renameJobData = [];
$lastModifiedTime = intval( $req->getVal( 'changesetModifiedTime' ) );
if ( !MessageChangeStorage::isModifiedSince( $this->cdb, $lastModifiedTime ) ) {
$out->addWikiMsg( 'translate-smg-changeset-modified' );
return;
}
$reader = \Cdb\Reader::open( $this->cdb );
$groups = $this->getGroupsFromCdb( $reader );
$postponed = [];
foreach ( $groups as $groupId => $group ) {
try {
$sourceChanges = MessageSourceChange::loadModifications(
TranslateUtils::deserialize( $reader->get( $groupId ) )
);
$groupModificationJobs = [];
$groupRenameJobData = [];
$languages = $sourceChanges->getLanguages();
foreach ( $languages as $language ) {
// Handle changes, additions, deletions
$this->handleModificationsSubmit( $group, $sourceChanges, $req,
$language, $postponed, $groupModificationJobs );
// Handle renames, this might also add modification jobs based on user selection.
$this->handleRenameSubmit( $group, $sourceChanges, $req, $language,
$postponed, $groupRenameJobData, $groupModificationJobs );
if ( !isset( $postponed[$groupId][$language] ) ) {
$group->getMessageGroupCache( $language )->create();
}
}
$modificationJobs = array_merge( $modificationJobs, $groupModificationJobs );
$renameJobData = array_merge( $renameJobData, $groupRenameJobData );
} catch ( Exception $e ) {
error_log(
"SpecialManageGroups: Error in processSubmit. Group: $groupId\n" .
"Exception: $e"
);
$errorGroups[] = $group->getLabel();
}
}
JobQueueGroup::singleton()->push( MessageIndexRebuildJob::newJob() );
JobQueueGroup::singleton()->push( $modificationJobs );
JobQueueGroup::singleton()->push( $this->createRenameJobs( $renameJobData ) );
$reader->close();
rename( $this->cdb, $this->cdb . '-' . wfTimestamp() );
if ( $errorGroups ) {
$errorMsg = $this->getProcessingErrorMessage( $errorGroups, count( $groups ) );
$out->addElement(
'p',
[ 'class' => 'warningbox mw-translate-smg-submitted' ],
$errorMsg
);
}
if ( count( $postponed ) ) {
$postponedSourceChanges = [];
foreach ( $postponed as $groupId => $changes ) {
$postponedSourceChanges[$groupId] = MessageSourceChange::loadModifications( $changes );
}
MessageChangeStorage::writeChanges( $postponedSourceChanges, $this->cdb );
$this->showChanges( $this->getLimit() );
} elseif ( $errorGroups === [] ) {
$out->addWikiMsg( 'translate-smg-submitted' );
}
}
protected static function changeId( $groupId, $language, $type, $key ) {
return 'smg/' . substr( sha1( "$groupId/$language/$type/$key" ), 0, 7 );
}
/**
* Adds the task-based tabs on Special:Translate and few other special pages.
* Hook: SkinTemplateNavigation::SpecialPage
* @since 2012-05-14
* @param Skin $skin
* @param array &$tabs
* @return true
*/
public static function tabify( Skin $skin, array &$tabs ) {
$title = $skin->getTitle();
$specialPageFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
[ $alias, ] = $specialPageFactory->resolveAlias( $title->getText() );
$pagesInGroup = [
'ManageMessageGroups' => 'namespaces',
'AggregateGroups' => 'namespaces',
'SupportedLanguages' => 'views',
'TranslationStats' => 'views',
];
if ( !isset( $pagesInGroup[$alias] ) ) {
return true;
}
$skin->getOutput()->addModuleStyles( 'ext.translate.tabgroup' );
$tabs['namespaces'] = [];
foreach ( $pagesInGroup as $spName => $section ) {
$spClass = $specialPageFactory->getPage( $spName );
// DisabledSpecialPage was added in MW 1.33
if ( $spClass === null || $spClass instanceof DisabledSpecialPage ) {
continue; // Page explicitly disabled
}
$spTitle = $spClass->getPageTitle();
$tabs[$section][strtolower( $spName )] = [
'text' => $spClass->getDescription(),
'href' => $spTitle->getLocalURL(),
'class' => $alias === $spName ? 'selected' : '',
];
}
return true;
}
/**
* Displays renames
* @param MessageGroup $group
* @param MessageSourceChange $sourceChanges
* @param OutputPage $out
* @param string $language
* @param int &$limit
*/
protected function showRenames(
MessageGroup $group, MessageSourceChange $sourceChanges, OutputPage $out, $language, &$limit
) {
$changes = $sourceChanges->getRenames( $language );
foreach ( $changes as $key => $params ) {
if ( !isset( $changes[$key] ) ) {
continue;
}
if ( $group->getSourceLanguage() !== $language &&
$sourceChanges->isEqual( $language, $key ) ) {
// This is a translation rename, that does not have any changes.
// We can group this along with the source rename.
continue;
}
// Determine added key, and corresponding removed key.
$firstMsg = $params;
$secondKey = $sourceChanges->getMatchedKey( $language, $key );
$secondMsg = $sourceChanges->getMatchedMessage( $language, $key );
if ( $sourceChanges->isPreviousState(
$language, $key, [ MessageSourceChange::ADDITION, MessageSourceChange::CHANGE ]
) ) {
$addedMsg = $firstMsg;
$deletedMsg = $secondMsg;
} else {
$addedMsg = $secondMsg;
$deletedMsg = $firstMsg;
}
$change = $this->formatRename(
$group,
$addedMsg,
$deletedMsg,
$language,
$sourceChanges->isEqual( $language, $key ),
$limit
);
$out->addHTML( $change );
// no need to process the second key again.
unset( $changes[$secondKey] );
if ( $limit <= 0 ) {
// We need to restrict the changes per page per form submission
// limitations as well as performance.
$out->wrapWikiMsg( "<div class=warning>\n$1\n</div>", 'translate-smg-more' );
break;
}
}
}
/**
* @param MessageGroup $group
* @param array $addedMsg
* @param array $deletedMsg
* @param string $language
* @param bool $isEqual Are the renamed messages equal
* @param int &$limit
* @return string HTML
*/
protected function formatRename(
MessageGroup $group, $addedMsg, $deletedMsg, $language, $isEqual, &$limit
) {
$addedKey = $addedMsg['key'];
$deletedKey = $deletedMsg['key'];
$actions = '';
$addedTitle = Title::makeTitleSafe( $group->getNamespace(), "$addedKey/$language" );
$deletedTitle = Title::makeTitleSafe( $group->getNamespace(), "$deletedKey/$language" );
$id = self::changeId( $group->getId(), $language, MessageSourceChange::RENAME, $addedKey );
$addedTitleLink = $this->getLinkRenderer()->makeLink( $addedTitle );
$deletedTitleLink = $this->getLinkRenderer()->makeLink( $deletedTitle );
$renameSelected = true;
if ( $group->getSourceLanguage() === $language ) {
if ( !$isEqual ) {
$renameSelected = false;
$label = $this->msg( 'translate-manage-action-rename-fuzzy' )->text();
$actions .= Xml::radioLabel( $label, "msg/$id", "renamefuzzy", "rf/$id", true );
}
$label = $this->msg( 'translate-manage-action-rename' )->text();
$actions .= Xml::radioLabel( $label, "msg/$id", "rename", "imp/$id", $renameSelected );
} else {
$label = $this->msg( 'translate-manage-action-import' )->text();
$actions .= Xml::radioLabel( $label, "msg/$id", "import", "imp/$id", true );
}
if ( $group->getSourceLanguage() !== $language ) {
// Allow user to ignore changes to non-source languages.
$label = $this->msg( 'translate-manage-action-ignore-change' )->text();
$actions .= Xml::radioLabel( $label, "msg/$id", "ignore", "i/$id" );
}
$limit--;
$addedContent = ContentHandler::makeContent( $addedMsg['content'], $addedTitle );
$deletedContent = ContentHandler::makeContent( $deletedMsg['content'], $deletedTitle );
$this->diff->setContent( $deletedContent, $addedContent );
$menu = '';
if ( $group->getSourceLanguage() === $language && $this->hasRight ) {
// Only show rename and add as new option for source language.
$menu = Html::rawElement( 'button', [
'class' => 'smg-rename-actions', 'type' => 'button',
'data-group-id' => $group->getId(), 'data-msgkey' => $addedKey,
'data-msgtitle' => $addedTitle->getFullText()
], '' );
}
$actions = Html::rawElement( 'div', [ 'class' => 'smg-change-import-options' ], $actions );
$text = $this->diff->getDiff(
$deletedTitleLink,
$addedTitleLink . $menu . $actions,
$isEqual ? htmlspecialchars( $addedMsg['content'] ) : ''
);
$hidden = Html::hidden( $id, 1 );
$limit--;
$text .= $hidden;
return Html::rawElement( 'div',
[ 'class' => 'mw-translate-smg-change smg-change-rename' ], $text );
}
/**
* @param array $currentMsg
* @param MessageSourceChange $sourceChanges
* @param string $languageCode
* @param int $groupNamespace
* @param string $selectedVal
* @param bool $isSourceLang
* @return ?array
*/
protected function getRenameJobParams(
$currentMsg, MessageSourceChange $sourceChanges, $languageCode,
$groupNamespace, $selectedVal, $isSourceLang = true
) {
if ( $selectedVal === 'ignore' ) {
return null;
}
$params = [];
$replacementContent = '';
$currentMsgKey = $currentMsg['key'];
$matchedMsg = $sourceChanges->getMatchedMessage( $languageCode, $currentMsgKey );
$matchedMsgKey = $matchedMsg['key'];
if ( $sourceChanges->isPreviousState( $languageCode, $currentMsgKey, [
MessageSourceChange::ADDITION, MessageSourceChange::CHANGE
] ) ) {
$params['target'] = $matchedMsgKey;
$params['replacement'] = $currentMsgKey;
$replacementContent = $currentMsg['content'];
} else {
$params['target'] = $currentMsgKey;
$params['replacement'] = $matchedMsgKey;
$replacementContent = $matchedMsg['content'];
}
if ( $selectedVal === 'renamefuzzy' ) {
$params['fuzzy'] = 'fuzzy';
} else {
$params['fuzzy'] = false;
}
$params['content'] = $replacementContent;
if ( $isSourceLang ) {
$params['targetTitle'] = Title::newFromText(
TranslateUtils::title( $params['target'], $languageCode, $groupNamespace ),
$groupNamespace
);
$params['others'] = [];
}
return $params;
}
protected function handleRenameSubmit( MessageGroup $group, MessageSourceChange $sourceChanges,
WebRequest $req, $language, &$postponed, &$jobData, &$modificationJobs
) {
$groupId = $group->getId();
$renames = $sourceChanges->getRenames( $language );
$isSourceLang = $group->getSourceLanguage() === $language;
$groupNamespace = $group->getNamespace();
foreach ( $renames as $key => $params ) {
if ( !isset( $renames[ $key] ) ) {
continue;
}
$id = self::changeId( $groupId, $language, MessageSourceChange::RENAME, $key );
[ $renameMissing, $isCurrentKeyPresent ] = $this->isRenameMissing(
$req, $sourceChanges, $id, $key, $language, $groupId, $isSourceLang
);
if ( $renameMissing ) {
// we probably hit the limit with number of post parameters since neither
// addition or deletion key is present.
$postponed[$groupId][$language][MessageSourceChange::RENAME][$key] = $params;
continue;
}
if ( !$isCurrentKeyPresent ) {
// still don't process this key, and wait for the matched rename
continue;
}
$selectedVal = $req->getVal( "msg/$id" );
$jobParams = $this->getRenameJobParams(
$params, $sourceChanges, $language, $groupNamespace, $selectedVal, $isSourceLang
);
if ( $jobParams === null ) {
continue;
}
$targetStr = $jobParams[ 'target' ];
if ( $isSourceLang ) {
$jobData[ $targetStr ] = $jobParams;
} elseif ( isset( $jobData[ $targetStr ] ) ) {
// We are grouping the source rename, and content changes in other languages
// for the message together into a single job in order to avoid race conditions
// since jobs are not guaranteed to be run in order.
$jobData[ $targetStr ][ 'others' ][ $language ] = $jobParams[ 'content' ];
} else {
// the source was probably ignored, we should add this as a modification instead,
// since the source is not going to be renamed.
$title = Title::newFromText(
TranslateUtils::title( $targetStr, $language, $groupNamespace ),
$groupNamespace
);
$modificationJobs[] = MessageUpdateJob::newJob( $title, $jobParams['content'] );
}
// remove the matched key in order to avoid double processing.
$matchedKey = $sourceChanges->getMatchedKey( $language, $key );
unset( $renames[$matchedKey] );
}
}
protected function handleModificationsSubmit(
MessageGroup $group, MessageSourceChange $sourceChanges, WebRequest $req,
$language, &$postponed, &$messageUpdateJob
) {
$groupId = $group->getId();
$subchanges = $sourceChanges->getModificationsForLanguage( $language );
// Ignore renames
unset( $subchanges[ MessageSourceChange::RENAME ] );
// Handle additions, deletions, and changes.
foreach ( $subchanges as $type => $messages ) {
foreach ( $messages as $index => $params ) {
$key = $params['key'];
$id = self::changeId( $groupId, $language, $type, $key );
$title = Title::makeTitleSafe( $group->getNamespace(), "$key/$language" );
if ( !$this->isTitlePresent( $title, $type ) ) {
continue;
}
if ( !$req->getCheck( $id ) ) {
// We probably hit the limit with number of post parameters.
$postponed[$groupId][$language][$type][$index] = $params;
continue;
}
$selectedVal = $req->getVal( "msg/$id" );
if ( $type === MessageSourceChange::DELETION || $selectedVal === 'ignore' ) {
continue;
}
$fuzzy = $selectedVal === 'fuzzy' ? 'fuzzy' : false;
$messageUpdateJob[] = MessageUpdateJob::newJob( $title, $params['content'], $fuzzy );
}
}
}
protected function createRenameJobs( $jobParams ) {
$jobs = [];
foreach ( $jobParams as $params ) {
$jobs[] = MessageUpdateJob::newRenameJob(
$params['targetTitle'], $params['target'],
$params['replacement'], $params['fuzzy'], $params['content'],
$params['others']
);
}
return $jobs;
}
/**
* Checks if a title still exists and can be processed.
*
* @param Title $title
* @param string $type
* @return bool
*/
protected function isTitlePresent( Title $title, $type ) {
if ( ( $type === MessageSourceChange::DELETION || $type === MessageSourceChange::CHANGE ) &&
!$title->exists()
) {
// This means that this change was probably introduced due to a rename
// which removed the key. No need to process.
return false;
}
return true;
}
/**
* Checks if a renamed message key is missing from the user request submission.
* Checks the current key and the matched key. This is needed because as the
* keys in the wiki are not submitted along with the request, only the incoming
* modified keys are submitted.
* @param WebRequest $req
* @param MessageSourceChange $sourceChanges
* @param string $id
* @param string $key
* @param string $language
* @param string $groupId
* @param bool $isSourceLang
* @return bool[] $response
* $response = [
* 0 => (bool) True if rename is missing, false otherwise.
* 1 => (bool) Was the current $id found?
* ]
*/
protected function isRenameMissing(
WebRequest $req, MessageSourceChange $sourceChanges, $id, $key,
$language, $groupId, $isSourceLang
) {
if ( $req->getCheck( $id ) ) {
return [ false, true ];
}
$isCurrentKeyPresent = false;
// Checked the matched key is also missing to confirm if its truly missing
$matchedKey = $sourceChanges->getMatchedKey( $language, $key );
$matchedId = self::changeId( $groupId, $language, MessageSourceChange::RENAME, $matchedKey );
if ( $req->getCheck( $matchedId ) ) {
return [ false, $isCurrentKeyPresent ];
}
if ( $isSourceLang === false && $sourceChanges->isEqual( $language, $matchedKey ) ) {
// For non source language, if strings are equal, they are not shown on the UI
// and hence not submitted.
return [ false, $isCurrentKeyPresent ];
}
return [ true, $isCurrentKeyPresent ];
}
protected function getProcessingErrorMessage(
array $errorGroups, int $totalGroupCount
): string {
// Number of error groups, are less than the total groups processed.
if ( count( $errorGroups ) < $totalGroupCount ) {
$errorMsg = $this->msg( 'translate-smg-submitted-with-failure' )
->numParams( count( $errorGroups ) )
->params(
$this->getLanguage()->commaList( $errorGroups ),
$this->msg( 'translate-smg-submitted-others-processing' )
)->text();
} else {
$errorMsg = trim(
$this->msg( 'translate-smg-submitted-with-failure' )
->numParams( count( $errorGroups ) )
->params( $this->getLanguage()->commaList( $errorGroups ), '' )
->text()
);
}
return $errorMsg;
}
/**
* @param \Cdb\Reader $reader
* @return FileBasedMessageGroup[]
*/
private function getGroupsFromCdb( \Cdb\Reader $reader ): array {
$groups = [];
$groupIds = TranslateUtils::deserialize( $reader->get( '#keys' ) );
foreach ( $groupIds as $id ) {
$groups[$id] = MessageGroups::getGroup( $id );
}
return array_filter( $groups );
}
}
|