summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/tag/TPSection.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Translate/tag/TPSection.php')
-rw-r--r--www/wiki/extensions/Translate/tag/TPSection.php87
1 files changed, 73 insertions, 14 deletions
diff --git a/www/wiki/extensions/Translate/tag/TPSection.php b/www/wiki/extensions/Translate/tag/TPSection.php
index 105dd981..e42e2b46 100644
--- a/www/wiki/extensions/Translate/tag/TPSection.php
+++ b/www/wiki/extensions/Translate/tag/TPSection.php
@@ -4,8 +4,7 @@
*
* @file
* @author Niklas Laxström
- * @copyright Copyright © 2009-2013 Niklas Laxström
- * @license GPL-2.0+
+ * @license GPL-2.0-or-later
*/
/**
@@ -20,9 +19,9 @@ class TPSection {
public $id;
/**
- * @var string New name of the section, that will be saved to database.
+ * @var string|null New name of the section, that will be saved to database.
*/
- public $name;
+ public $name = null;
/**
* @var string Section text.
@@ -35,9 +34,33 @@ class TPSection {
public $type;
/**
- * @var string Text of previous version of this section.
+ * @var string|null Text of previous version of this section.
*/
- public $oldText;
+ 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.
@@ -53,7 +76,7 @@ class TPSection {
* @since 2014.07
*/
public function getTextWithVariables() {
- $re = '~<tvar\|([^>]+)>(.*?)</>~u';
+ $re = '~<tvar\|([^>]+)>(.*?)</>~us';
return preg_replace( $re, '$\1', $this->text );
}
@@ -63,17 +86,18 @@ class TPSection {
* @return string Wikitext.
*/
public function getTextForTrans() {
- $re = '~<tvar\|([^>]+)>(.*?)</>~u';
+ $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 = isset( $this->name ) ? $this->name : $this->id;
+ $id = $this->name !== null ? $this->name : $this->id;
$header = "<!--T:{$id}-->";
$re = '~^(=+.*?=+\s*?$)~m';
$rep = "\\1 $header";
@@ -82,7 +106,11 @@ class TPSection {
$text = preg_replace( $re, $rep, $this->text, 1, $count );
if ( $count === 0 ) {
- $text = $header . "\n" . $this->text;
+ if ( $this->inline ) {
+ $text = $header . ' ' . $this->text;
+ } else {
+ $text = $header . "\n" . $this->text;
+ }
}
return $text;
@@ -93,7 +121,7 @@ class TPSection {
* @return string Wikitext.
*/
public function getOldText() {
- return isset( $this->oldText ) ? $this->oldText : $this->text;
+ return $this->oldText !== null ? $this->oldText : $this->text;
}
/**
@@ -102,10 +130,10 @@ class TPSection {
* prefixed with a dollar sign.
*/
public function getVariables() {
- $re = '~<tvar\|([^>]+)>(.*?)</>~u';
- $matches = array();
+ $re = '~<tvar\|([^>]+)>(.*?)</>~us';
+ $matches = [];
preg_match_all( $re, $this->text, $matches, PREG_SET_ORDER );
- $vars = array();
+ $vars = [];
foreach ( $matches as $m ) {
$vars['$' . $m[1]] = $m[2];
@@ -113,4 +141,35 @@ class TPSection {
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;
+ }
}