summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/utils/ExternalMessageSourceStateImporter.php
blob: 495c3fd713a99c271b602d9492fa118e87def961 (plain)
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
<?php

/**
 * Finds external changes for file based message groups.
 *
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @since 2016.02
 */
class ExternalMessageSourceStateImporter {

	public function importSafe( $changeData ) {
		$processed = [];
		$skipped = [];
		$jobs = [];
		$jobs[] = MessageIndexRebuildJob::newJob();

		foreach ( $changeData as $groupId => $changesForGroup ) {
			$group = MessageGroups::getGroup( $groupId );
			if ( !$group ) {
				unset( $changeData[$groupId] );
				continue;
			}

			$processed[$groupId] = 0;

			foreach ( $changesForGroup as $languageCode => $changesForLanguage ) {
				if ( !self::isSafe( $changesForLanguage ) ) {
					$skipped[$groupId] = true;
					continue;
				}

				if ( !isset( $changesForLanguage['addition'] ) ) {
					continue;
				}

				foreach ( $changesForLanguage['addition'] as $addition ) {
					$namespace = $group->getNamespace();
					$name = "{$addition['key']}/$languageCode";

					$title = Title::makeTitleSafe( $namespace, $name );
					if ( !$title ) {
						wfWarn( "Invalid title for group $groupId key {$addition['key']}" );
						continue;
					}

					$jobs[] = MessageUpdateJob::newJob( $title, $addition['content'] );
					$processed[$groupId]++;
				}

				unset( $changeData[$groupId][$languageCode] );

				$cache = new MessageGroupCache( $groupId, $languageCode );
				$cache->create();
			}
		}

		// Remove groups where everything was imported
		$changeData = array_filter( $changeData );
		// Remove groups with no imports
		$processed = array_filter( $processed );

		$name = 'unattended';
		$file = MessageChangeStorage::getCdbPath( $name );
		MessageChangeStorage::writeChanges( $changeData, $file );
		JobQueueGroup::singleton()->push( $jobs );

		return [
			'processed' => $processed,
			'skipped' => $skipped,
			'name' => $name,
		];
	}

	protected static function isSafe( array $changesForLanguage ) {
		foreach ( array_keys( $changesForLanguage ) as $changeType ) {
			if ( $changeType !== 'addition' ) {
				return false;
			}
		}

		return true;
	}
}