Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslationStashStorage.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorSandbox;
5
6use MediaWiki\Title\Title;
7use MediaWiki\User\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 $res = $this->db->newSelectQueryBuilder()
30 ->select( [ 'ts_namespace', 'ts_title', 'ts_value', 'ts_metadata' ] )
31 ->from( $this->dbTable )
32 ->where( [ 'ts_user' => $user->getId() ] )
33 ->caller( __METHOD__ )
34 ->fetchResultSet();
35
36 $objects = [];
37 foreach ( $res as $row ) {
38 $objects[] = new StashedTranslation(
39 $user,
40 Title::makeTitle( (int)$row->ts_namespace, $row->ts_title ),
41 $row->ts_value,
42 unserialize( $row->ts_metadata )
43 );
44 }
45
46 return $objects;
47 }
48
49 public function addTranslation( StashedTranslation $item ): void {
50 $row = [
51 'ts_user' => $item->getUser()->getId(),
52 'ts_title' => $item->getTitle()->getDBkey(),
53 'ts_namespace' => $item->getTitle()->getNamespace(),
54 'ts_value' => $item->getValue(),
55 'ts_metadata' => serialize( $item->getMetadata() ),
56 ];
57
58 $indexes = [
59 [ 'ts_user', 'ts_namespace', 'ts_title' ],
60 ];
61
62 $this->db->replace( $this->dbTable, $indexes, $row, __METHOD__ );
63 }
64
65 public function deleteTranslations( User $user ): void {
66 $conds = [ 'ts_user' => $user->getId() ];
67 $this->db->delete( $this->dbTable, $conds, __METHOD__ );
68 }
69}
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.