diff options
author | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
commit | fc7369835258467bf97eb64f184b93691f9a9fd5 (patch) | |
tree | daabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/extensions/Translate/stash |
first commit
Diffstat (limited to 'www/wiki/extensions/Translate/stash')
-rw-r--r-- | www/wiki/extensions/Translate/stash/StashedTranslation.php | 67 | ||||
-rw-r--r-- | www/wiki/extensions/Translate/stash/TranslationStashStorage.php | 87 |
2 files changed, 154 insertions, 0 deletions
diff --git a/www/wiki/extensions/Translate/stash/StashedTranslation.php b/www/wiki/extensions/Translate/stash/StashedTranslation.php new file mode 100644 index 00000000..3eaf1f3b --- /dev/null +++ b/www/wiki/extensions/Translate/stash/StashedTranslation.php @@ -0,0 +1,67 @@ +<?php +/** + * Value object for stashed translation. + * + * @file + * @author Niklas Laxström + * @license GPL-2.0-or-later + */ + +/** + * Value object for stashed translation which you can construct. + * @since 2013.06 + */ +class StashedTranslation { + /** @var User */ + protected $user; + + /** @var Title */ + protected $title; + + /** @var string */ + protected $value; + + /** @var array|null */ + protected $metadata; + + /** + * @param User $user + * @param Title $title + * @param string $value + * @param array|null $metadata + */ + public function __construct( User $user, Title $title, $value, array $metadata = null ) { + $this->user = $user; + $this->title = $title; + $this->value = $value; + $this->metadata = $metadata; + } + + /** + * @return User + */ + public function getUser() { + return $this->user; + } + + /** + * @return Title + */ + public function getTitle() { + return $this->title; + } + + /** + * @return string + */ + public function getValue() { + return $this->value; + } + + /** + * @return array|null + */ + public function getMetadata() { + return $this->metadata; + } +} diff --git a/www/wiki/extensions/Translate/stash/TranslationStashStorage.php b/www/wiki/extensions/Translate/stash/TranslationStashStorage.php new file mode 100644 index 00000000..fb67449f --- /dev/null +++ b/www/wiki/extensions/Translate/stash/TranslationStashStorage.php @@ -0,0 +1,87 @@ +<?php +/** + * Storage class for stashed translations. + * + * @file + * @author Niklas Laxström + * @license GPL-2.0-or-later + */ + +use Wikimedia\Rdbms\IDatabase; + +/** + * Storage class for stashed translations. This one uses sql tables as storage. + * @since 2013.06 + */ +class TranslationStashStorage { + /** @var IDatabase */ + protected $db; + + /** @var string */ + protected $dbTable; + + /** + * @param IDatabase $db + * @param string $table + */ + public function __construct( IDatabase $db, $table = 'translate_stash' ) { + $this->db = $db; + $this->dbTable = $table; + } + + /** + * Adds a new translation to the stash. If the same key already exists, the + * previous translation and metadata will be replaced with the new one. + * + * @param StashedTranslation $item + */ + public function addTranslation( StashedTranslation $item ) { + $row = [ + 'ts_user' => $item->getUser()->getId(), + 'ts_title' => $item->getTitle()->getDBkey(), + 'ts_namespace' => $item->getTitle()->getNamespace(), + 'ts_value' => $item->getValue(), + 'ts_metadata' => serialize( $item->getMetadata() ), + ]; + + $indexes = [ + [ 'ts_user', 'ts_namespace', 'ts_title' ] + ]; + + $this->db->replace( $this->dbTable, $indexes, $row, __METHOD__ ); + } + + /** + * Gets all stashed translations for the given user. + * @param User $user + * @return StashedTranslation[] + */ + public function getTranslations( User $user ) { + $conds = [ 'ts_user' => $user->getId() ]; + $fields = [ 'ts_namespace', 'ts_title', 'ts_value', 'ts_metadata' ]; + + $res = $this->db->select( $this->dbTable, $fields, $conds, __METHOD__ ); + + $objects = []; + foreach ( $res as $row ) { + $objects[] = new StashedTranslation( + $user, + Title::makeTitle( $row->ts_namespace, $row->ts_title ), + $row->ts_value, + unserialize( $row->ts_metadata ) + ); + } + + return $objects; + } + + /** + * Delete all stashed translations for the given user. + * @param User $user + * @since 2013.10 + */ + public function deleteTranslations( User $user ) { + $conds = [ 'ts_user' => $user->getId() ]; + $this->db->delete( $this->dbTable, $conds, __METHOD__ ); + } +} |