summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/tag/TPSection.php
blob: e42e2b46714d3606f25c9b1d6a6a37a6eb3cee38 (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
<?php
/**
 * Helper for TPParse.
 *
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * This class represents one individual section in translatable page.
 *
 * @ingroup PageTranslation
 */
class TPSection {
	/**
	 * @var string Section name
	 */
	public $id;

	/**
	 * @var string|null New name of the section, that will be saved to database.
	 */
	public $name = null;

	/**
	 * @var string Section text.
	 */
	public $text;

	/**
	 * @var string Is this new, existing, changed or deleted section.
	 */
	public $type;

	/**
	 * @var string|null Text of previous version of this section.
	 */
	public $oldText = null;

	/**
	 * @var bool Whether this section is inline section.
	 * E.g. "Something <translate>foo</translate> bar".
	 */
	protected $inline = false;

	/**
	 * @var int Version number for the serialization.
	 */
	private $version = 1;

	/**
	 * @var string[] List of properties to serialize.
	 */
	private static $properties = [ 'version', 'id', 'name', 'text', 'type', 'oldText', 'inline' ];

	public function setIsInline( $value ) {
		$this->inline = (bool)$value;
	}

	public function isInline() {
		return $this->inline;
	}

	/**
	 * Returns section text unmodified.
	 * @return string Wikitext.
	 */
	public function getText() {
		return $this->text;
	}

	/**
	 * Returns the text with tvars replaces with placeholders.
	 * @return string Wikitext.
	 * @since 2014.07
	 */
	public function getTextWithVariables() {
		$re = '~<tvar\|([^>]+)>(.*?)</>~us';

		return preg_replace( $re, '$\1', $this->text );
	}

	/**
	 * Returns section text with variables replaced.
	 * @return string Wikitext.
	 */
	public function getTextForTrans() {
		$re = '~<tvar\|([^>]+)>(.*?)</>~us';

		return preg_replace( $re, '\2', $this->text );
	}

	/**
	 * Returns the section text with updated or added section marker.
	 *
	 * @return string Wikitext.
	 */
	public function getMarkedText() {
		$id = $this->name !== null ? $this->name : $this->id;
		$header = "<!--T:{$id}-->";
		$re = '~^(=+.*?=+\s*?$)~m';
		$rep = "\\1 $header";
		$count = 0;

		$text = preg_replace( $re, $rep, $this->text, 1, $count );

		if ( $count === 0 ) {
			if ( $this->inline ) {
				$text = $header . ' ' . $this->text;
			} else {
				$text = $header . "\n" . $this->text;
			}
		}

		return $text;
	}

	/**
	 * Returns oldtext, or current text if not available.
	 * @return string Wikitext.
	 */
	public function getOldText() {
		return $this->oldText !== null ? $this->oldText : $this->text;
	}

	/**
	 * Returns array of variables defined on this section.
	 * @return array ( string => string ) Values indexed with keys which are
	 * prefixed with a dollar sign.
	 */
	public function getVariables() {
		$re = '~<tvar\|([^>]+)>(.*?)</>~us';
		$matches = [];
		preg_match_all( $re, $this->text, $matches, PREG_SET_ORDER );
		$vars = [];

		foreach ( $matches as $m ) {
			$vars['$' . $m[1]] = $m[2];
		}

		return $vars;
	}

	/**
	 * Serialize this object to a PHP array.
	 * @return array
	 * @since 2018.07
	 */
	public function serializeToArray() {
		$data = [];
		foreach ( self::$properties as $index => $property ) {
			// Because this is used for the JobQueue, use a list
			// instead of an array to save space.
			$data[ $index ] = $this->$property;
		}

		return $data;
	}

	/**
	 * Construct an object from previously serialized array.
	 * @param array $data
	 * @return self
	 * @since 2018.07
	 */
	public static function unserializeFromArray( $data ) {
		$section = new self;
		foreach ( self::$properties as $index => $property ) {
			$section->$property = $data[ $index ];
		}

		return $section;
	}
}