summaryrefslogtreecommitdiff
blob: f2f70b4e3af159a2b59ef3877b6c0dd97e91a769 (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
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
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\TtmServer;

use FormatJson;
use Language;
use MediaWiki\Extension\Translate\Utilities\BaseMaintenanceScript;
use MediaWiki\Extension\Translate\Utilities\ParallelExecutor;
use MediaWiki\MediaWikiServices;
use MessageGroup;
use MessageGroups;
use MessageGroupStats;
use MessageHandle;
use TMessage;
use WikiMap;

/**
 * @since 2020.11
 * @license GPL-2.0-or-later
 * @author Niklas Laxström
 */
class ExportTtmServerDumpMaintenanceScript extends BaseMaintenanceScript {
	/** @var Language */
	private $contentLanguage;

	public function __construct() {
		parent::__construct();
		$this->addDescription( 'Creates a dump file that can be imported to a TTMServer' );

		$this->addOption(
			'output-directory',
			'Which directory to output files to',
			self::REQUIRED,
			self::HAS_ARG,
			'o'
		);
		$this->addOption(
			'threads',
			'How many threads to use',
			self::OPTIONAL,
			self::HAS_ARG,
			'n'
		);

		$availableMethods = array_keys( $this->getAvailableCompressionWrappers() );
		$values = count( $availableMethods ) ? implode( ', ', $availableMethods ) : 'NONE';
		$this->addOption(
			'compress',
			"Use a compression filter. Possible values: $values",
			self::OPTIONAL,
			self::HAS_ARG,
			'c'
		);

		$this->requireExtension( 'Translate' );
	}

	/** @return string[] */
	private function getAvailableCompressionWrappers(): array {
		$out = [];
		$filters = stream_get_filters();
		foreach ( $filters as $f ) {
			if ( preg_match( '/^compress\..+$/', $f ) ) {
				$out[$f] = $f . '://';
			}
		}
		return $out;
	}

	public function execute() {
		$this->contentLanguage = MediaWikiServices::getInstance()->getContentLanguage();

		$threads = (int)$this->getOption( 'threads', 1 );
		$outputDir = $this->getOption( 'output-directory' );
		$requestedWrapper = $this->getOption( 'compress' );
		$availableWrappers = $this->getAvailableCompressionWrappers();
		if ( $requestedWrapper && !isset( $availableWrappers[$requestedWrapper] ) ) {
			$this->fatalError(
				"Compression wrapper '$requestedWrapper' is not supported"
			);
		}
		$wrapper = $availableWrappers[$requestedWrapper] ?? '';
		$suffix = $requestedWrapper ? ".$requestedWrapper" : '';

		$executor = new ParallelExecutor( $threads );

		$groups = $this->getGroupsInPerformanceOrder();
		foreach ( $groups as $groupId => $group ) {
			$path = $wrapper . rtrim( $outputDir, '/' ) . '/' . $groupId . '.json' . $suffix;

			$executor->runInParallel(
				function ( int $pid ) use ( $groupId ) {
					$this->output( "Forked process $pid to process $groupId\n" );
				},
				function () use ( $group, $path ) {
					$output = FormatJson::encode(
						$this->getOutput( $group ),
						true,
						FormatJson::ALL_OK
					);
					file_put_contents( $path, $output );
				}
			);
		}

		$this->output( "Done.\n" );
	}

	/**
	 * Return groups sorted by number of messages.
	 *
	 * For parallel processing, it makes sense to process large groups first so that smaller
	 * ones can execute in parallel threads, rather than waiting for large group(s) to process
	 * while other threads have nothing to do. Do not spend time on gathering statistics in case
	 * they are not present.
	 *
	 * @return MessageGroup[]
	 */
	private function getGroupsInPerformanceOrder(): array {
		$groupStats = MessageGroupStats::forLanguage(
			$this->contentLanguage->getCode(),
			MessageGroupStats::FLAG_CACHE_ONLY
		);

		uasort(
			$groupStats,
			function ( array $a, array $b ): int {
				return -1 * $this->sortGroupsBySize( $a, $b );
			}
		);

		$groups = [];
		foreach ( array_keys( $groupStats ) as $groupId ) {
			$group = MessageGroups::getGroup( $groupId );
			if ( $group->isMeta() ) {
				continue;
			}

			$groups[$group->getId()] = $group;
		}

		return $groups;
	}

	private function sortGroupsBySize( array $a, array $b ): int {
		return $a[MessageGroupStats::TOTAL] <=> $b[MessageGroupStats::TOTAL];
	}

	private function getOutput( MessageGroup $group ): array {
		$out = [];

		$groupId = $group->getId();
		$sourceLanguage = $group->getSourceLanguage();

		$stats = MessageGroupStats::forGroup( $groupId );
		$collection = $group->initCollection( $sourceLanguage );
		foreach ( $stats as $language => $numbers ) {
			if ( $numbers[MessageGroupStats::TRANSLATED] === 0 ) {
				continue;
			}

			$collection->resetForNewLanguage( $language );
			$collection->filter( 'ignored' );
			$collection->filter( 'translated', false );
			$collection->loadTranslations();

			foreach ( $collection->keys() as $mkey => $titleValue ) {
				$handle = new MessageHandle( $titleValue );
				/** @var TMessage $message */
				$message = $collection[$mkey];

				if ( !isset( $out[$mkey] ) ) {
					$out[$mkey] = [
						'wikiId' => WikiMap::getCurrentWikiId(),
						'title' => $handle->getTitleForBase()->getPrefixedText(),
						'sourceLanguage' => $sourceLanguage,
						'primaryGroup' => $groupId,
						'values' => [],
					];
				}

				$out[$mkey]['values'][] = [
					'language' => $language,
					'value' => $message->translation(),
					'revision' => $message->getProperty( 'revision' ),
				];
			}
		}

		return array_values( $out );
	}
}

class_alias(
	ExportTtmServerDumpMaintenanceScript::class,
	'\MediaWiki\Extensions\Translate\ExportTtmServerDumpMaintenanceScript'
);