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

namespace MediaWiki\Extensions\Translate\Synchronization;

use FormatJson;
use MessageUpdateJob;
use Serializable;

/**
 * Store params for MessageUpdateJob.
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 * @since 2020.06
 */
class MessageUpdateParameter implements Serializable {
	/** @var string */
	private $pageName;

	/** @var bool */
	private $rename;

	/** @var bool */
	private $fuzzy;

	/** @var string */
	private $content;

	/** @var string */
	private $target;

	/** @var string */
	private $replacement;

	/** @var array */
	private $otherLangs;

	public function __construct( array $params ) {
		$this->assignPropsFromArray( $params );
	}

	public function getPageName(): string {
		return $this->pageName;
	}

	public function isRename(): bool {
		return boolval( $this->rename );
	}

	public function getReplacementValue(): string {
		return $this->replacement;
	}

	public function getTargetValue(): string {
		return $this->target;
	}

	public function getContent(): string {
		return $this->content;
	}

	public function isFuzzy(): bool {
		return $this->fuzzy;
	}

	public function getOtherLangs(): array {
		return $this->otherLangs;
	}

	public function serialize(): string {
		$return = FormatJson::encode( get_object_vars( $this ), false, FormatJson::ALL_OK );
		return $return;
	}

	public function unserialize( $deserialized ) {
		$params = FormatJson::decode( $deserialized, true );
		$this->assignPropsFromArray( $params );
	}

	private function assignPropsFromArray( array $params ) {
		// We are using "rename" as value for $params['rename']
		// at some places otherwise this could be simplified to
		// $params['rename'] ?? false
		$this->rename = isset( $params['rename'] ) && $params['rename'];
		$this->fuzzy = $params['fuzzy'];
		$this->content = $params['content'];
		$this->pageName = $params['title'] ?? $params['pageName'];

		if ( $this->rename ) {
			$this->target = $params['target'];
			$this->replacement = $params['replacement'];
			$this->otherLangs = $params['otherLangs'] ?? [];
		}
	}

	/** Create a new instance of the class from MessageUpdateJob */
	public static function createFromJob( MessageUpdateJob $job ): self {
		$jobParams = $job->getParams();
		$jobParams['title'] = $job->getTitle()->getPrefixedDBkey();
		return new self( $jobParams );
	}
}