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

namespace MediaWiki\Extension\Translate\PageTranslation;

use InvalidArgumentException;
use Language;

/**
 * Represents a parsing output produced by TranslatablePageParser.
 *
 * It is required generate translatable and translation page sources or just get the list of
 * translations units.
 *
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @since 2020.08
 */
class ParserOutput {
	/** @var string */
	private $template;
	/** @var Section[] */
	private $sectionMap;
	/** @var TranslationUnit[] */
	private $unitMap;

	public function __construct( string $template, array $sectionMap, array $unitMap ) {
		$this->assertContainsOnlyInstancesOf( Section::class, '$sectionMap', $sectionMap );
		$this->assertContainsOnlyInstancesOf( TranslationUnit::class, '$unitMap', $unitMap );

		$this->template = $template;
		$this->sectionMap = $sectionMap;
		$this->unitMap = $unitMap;
	}

	/** Returns template that contains <translate> tags */
	public function sourcePageTemplate(): string {
		$replacements = [];
		foreach ( $this->sectionMap as $ph => $section ) {
			$replacements[$ph] = $section->wrappedContents();
		}

		return strtr( $this->template, $replacements );
	}

	/** Returns template that does not contain <translate> tags */
	public function translationPageTemplate(): string {
		$replacements = [];
		foreach ( $this->sectionMap as $ph => $section ) {
			$replacements[$ph] = $section->contents();
		}

		return strtr( $this->template, $replacements );
	}

	/** @return TranslationUnit[] */
	public function units(): array {
		return $this->unitMap;
	}

	/** Returns the source page wikitext used for rendering the page. */
	public function sourcePageTextForRendering( Language $sourceLanguage ) {
		$text = $this->translationPageTemplate();

		foreach ( $this->unitMap as $ph => $s ) {
			$t = $s->getTextForRendering( null, $sourceLanguage, $sourceLanguage, false );
			$text = str_replace( $ph, $t, $text );
		}

		return $text;
	}

	/** Returns the source page with translation unit markers. */
	public function sourcePageTextForSaving() {
		$text = $this->sourcePageTemplate();

		foreach ( $this->unitMap as $ph => $s ) {
			$text = str_replace( $ph, $s->getMarkedText(), $text );
		}

		return $text;
	}

	private function assertContainsOnlyInstancesOf(
		string $expected,
		string $name,
		array $x
	): void {
		foreach ( $x as $item ) {
			if ( !$item instanceof $expected ) {
				$actual = gettype( $item );
				throw new InvalidArgumentException(
					"Parameter $name must only contain instances of class $expected. Got $actual."
				);
			}
		}
	}
}

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