summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/insertables
diff options
context:
space:
mode:
authorYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
committerYaco <franco@reevo.org>2020-06-04 11:01:00 -0300
commitfc7369835258467bf97eb64f184b93691f9a9fd5 (patch)
treedaabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/Translate/insertables
first commit
Diffstat (limited to 'www/wiki/extensions/Translate/insertables')
-rw-r--r--www/wiki/extensions/Translate/insertables/CombinedInsertablesSuggester.php29
-rw-r--r--www/wiki/extensions/Translate/insertables/Insertable.php45
-rw-r--r--www/wiki/extensions/Translate/insertables/InsertablesSuggester.php17
-rw-r--r--www/wiki/extensions/Translate/insertables/MediaWikiInsertablesSuggester.php45
-rw-r--r--www/wiki/extensions/Translate/insertables/NumericalParameterInsertablesSuggester.php29
-rw-r--r--www/wiki/extensions/Translate/insertables/TranslatablePageInsertablesSuggester.php36
6 files changed, 201 insertions, 0 deletions
diff --git a/www/wiki/extensions/Translate/insertables/CombinedInsertablesSuggester.php b/www/wiki/extensions/Translate/insertables/CombinedInsertablesSuggester.php
new file mode 100644
index 00000000..5f3c468a
--- /dev/null
+++ b/www/wiki/extensions/Translate/insertables/CombinedInsertablesSuggester.php
@@ -0,0 +1,29 @@
+<?php
+
+/**
+ * A class to combine multiple insertables suggesters.
+ */
+class CombinedInsertablesSuggester implements InsertablesSuggester {
+
+ /**
+ * @var InsertablesSuggester[]
+ */
+ protected $suggesters = [];
+
+ /**
+ * @param InsertablesSuggester[] $suggesters Array of InsertablesSuggester objects to combine.
+ */
+ public function __construct( $suggesters = [] ) {
+ $this->suggesters = $suggesters;
+ }
+
+ public function getInsertables( $text ) {
+ $insertables = [];
+ foreach ( $this->suggesters as $suggester ) {
+ $new = $suggester->getInsertables( $text );
+ $insertables = array_merge( $insertables, $new );
+ }
+
+ return array_unique( $insertables, SORT_REGULAR );
+ }
+}
diff --git a/www/wiki/extensions/Translate/insertables/Insertable.php b/www/wiki/extensions/Translate/insertables/Insertable.php
new file mode 100644
index 00000000..bab7adff
--- /dev/null
+++ b/www/wiki/extensions/Translate/insertables/Insertable.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * Value object for insertables.
+ *
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0-or-later
+ */
+
+/**
+ * Insertable is a string that usually does not need translation and is
+ * difficult to type manually.
+ * @since 2013.09
+ */
+class Insertable {
+ /** @var string What to show to the user */
+ protected $display;
+ /** @var string What to insert before selection */
+ protected $pre;
+ /** @var string What to insert after selection */
+ protected $post;
+
+ /**
+ * @param string $display What to show to the user
+ * @param string $pre What to insert before selection
+ * @param string $post What to insert after selection
+ */
+ public function __construct( $display, $pre = '', $post = '' ) {
+ $this->display = $display;
+ $this->pre = $pre;
+ $this->post = $post;
+ }
+
+ public function getPreText() {
+ return $this->pre;
+ }
+
+ public function getPostText() {
+ return $this->post;
+ }
+
+ public function getDisplayText() {
+ return $this->display;
+ }
+}
diff --git a/www/wiki/extensions/Translate/insertables/InsertablesSuggester.php b/www/wiki/extensions/Translate/insertables/InsertablesSuggester.php
new file mode 100644
index 00000000..70cc9702
--- /dev/null
+++ b/www/wiki/extensions/Translate/insertables/InsertablesSuggester.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Interface for InsertableSuggesters.
+ *
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0-or-later
+ */
+
+/**
+ * Insertable is a string that usually does not need translation and is
+ * difficult to type manually.
+ * @since 2013.09
+ */
+interface InsertablesSuggester {
+ public function getInsertables( $text );
+}
diff --git a/www/wiki/extensions/Translate/insertables/MediaWikiInsertablesSuggester.php b/www/wiki/extensions/Translate/insertables/MediaWikiInsertablesSuggester.php
new file mode 100644
index 00000000..11aea35b
--- /dev/null
+++ b/www/wiki/extensions/Translate/insertables/MediaWikiInsertablesSuggester.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0-or-later
+ */
+
+/**
+ * Insertable is a string that usually does not need translation and is
+ * difficult to type manually.
+ * @since 2013.09
+ */
+class MediaWikiInsertablesSuggester {
+ public function getInsertables( $text ) {
+ $insertables = [];
+
+ $matches = [];
+ preg_match_all( '/\$(1[a-z]+|[0-9]+)/', $text, $matches, PREG_SET_ORDER );
+ $new = array_map( function ( $match ) {
+ return new Insertable( $match[0], $match[0] );
+ }, $matches );
+ $insertables = array_merge( $insertables, $new );
+
+ $matches = [];
+ preg_match_all(
+ '/({{((?:PLURAL|GENDER|GRAMMAR):[^|]*)\|).*?(}})/i',
+ $text,
+ $matches,
+ PREG_SET_ORDER
+ );
+ $new = array_map( function ( $match ) {
+ return new Insertable( $match[2], $match[1], $match[3] );
+ }, $matches );
+ $insertables = array_merge( $insertables, $new );
+
+ $matches = [];
+ preg_match_all( '/<\/?[a-z]+>/', $text, $matches, PREG_SET_ORDER );
+ $new = array_map( function ( $match ) {
+ return new Insertable( $match[0], $match[0] );
+ }, $matches );
+ $insertables = array_merge( $insertables, $new );
+
+ return $insertables;
+ }
+}
diff --git a/www/wiki/extensions/Translate/insertables/NumericalParameterInsertablesSuggester.php b/www/wiki/extensions/Translate/insertables/NumericalParameterInsertablesSuggester.php
new file mode 100644
index 00000000..13ffd4af
--- /dev/null
+++ b/www/wiki/extensions/Translate/insertables/NumericalParameterInsertablesSuggester.php
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Insertables suggester for numerical parameters such as $1, $2, $3
+ *
+ * @file
+ * @author Geoffrey Mon
+ * @license GPL-2.0-or-later
+ */
+
+class NumericalParameterInsertablesSuggester implements InsertablesSuggester {
+ public function getInsertables( $text ) {
+ $insertables = [];
+
+ // $1, $2, $3 etc.
+ $matches = [];
+ preg_match_all(
+ '/\$\d+/',
+ $text,
+ $matches,
+ PREG_SET_ORDER
+ );
+ $new = array_map( function ( $match ) {
+ return new Insertable( $match[0], $match[0] );
+ }, $matches );
+ $insertables = array_merge( $insertables, $new );
+
+ return $insertables;
+ }
+}
diff --git a/www/wiki/extensions/Translate/insertables/TranslatablePageInsertablesSuggester.php b/www/wiki/extensions/Translate/insertables/TranslatablePageInsertablesSuggester.php
new file mode 100644
index 00000000..61582606
--- /dev/null
+++ b/www/wiki/extensions/Translate/insertables/TranslatablePageInsertablesSuggester.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * @file
+ * @author Niklas Laxström
+ * @license GPL-2.0-or-later
+ */
+
+/**
+ * Special insertables for translatable pages.
+ * @since 2013.11
+ */
+class TranslatablePageInsertablesSuggester extends MediaWikiInsertablesSuggester {
+ public function getInsertables( $text ) {
+ $insertables = parent::getInsertables( $text );
+
+ // Translatable pages allow naming the variables. Basically anything is
+ // allowed in a variable name, but here we are stricter to avoid too many
+ // false positives.
+ $matches = [];
+ preg_match_all( '/\$([a-zA-Z0-9-_]+)/', $text, $matches, PREG_SET_ORDER );
+
+ $new = array_map( function ( $match ) {
+ // Numerical ones are already handled by parent
+ if ( ctype_digit( $match[1] ) ) {
+ return null;
+ }
+
+ return new Insertable( $match[0], $match[0] );
+ }, $matches );
+
+ $new = array_filter( $new );
+ $insertables = array_merge( $insertables, $new );
+
+ return $insertables;
+ }
+}