Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslationStashStorage.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorSandbox;
5
6use Title;
7use User;
8use Wikimedia\Rdbms\IDatabase;
9
19 protected $db;
21 protected $dbTable;
22
23 public function __construct( IDatabase $db, string $table = 'translate_stash' ) {
24 $this->db = $db;
25 $this->dbTable = $table;
26 }
27
28 public function getTranslations( User $user ): array {
29 $conds = [ 'ts_user' => $user->getId() ];
30 $fields = [ 'ts_namespace', 'ts_title', 'ts_value', 'ts_metadata' ];
31
32 $res = $this->db->select( $this->dbTable, $fields, $conds, __METHOD__ );
33
34 $objects = [];
35 foreach ( $res as $row ) {
36 $objects[] = new StashedTranslation(
37 $user,
38 Title::makeTitle( (int)$row->ts_namespace, $row->ts_title ),
39 $row->ts_value,
40 unserialize( $row->ts_metadata )
41 );
42 }
43
44 return $objects;
45 }
46
47 public function addTranslation( StashedTranslation $item ): void {
48 $row = [
49 'ts_user' => $item->getUser()->getId(),
50 'ts_title' => $item->getTitle()->getDBkey(),
51 'ts_namespace' => $item->getTitle()->getNamespace(),
52 'ts_value' => $item->getValue(),
53 'ts_metadata' => serialize( $item->getMetadata() ),
54 ];
55
56 $indexes = [
57 [ 'ts_user', 'ts_namespace', 'ts_title' ],
58 ];
59
60 $this->db->replace( $this->dbTable, $indexes, $row, __METHOD__ );
61 }
62
63 public function deleteTranslations( User $user ): void {
64 $conds = [ 'ts_user' => $user->getId() ];
65 $this->db->delete( $this->dbTable, $conds, __METHOD__ );
66 }
67}
Value object for stashed translation which you can construct.
getTranslations(User $user)
Gets all stashed translations for the given user.
addTranslation(StashedTranslation $item)
Adds a new translation to the stash.
deleteTranslations(User $user)
Delete all stashed translations for the given user.