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
18
19 public function __construct(
20 private readonly IDatabase $db,
21 private readonly string $dbTable = 'translate_stash',
22 ) {
23 }
24
25 public function getTranslations( User $user ): array {
26 $res = $this->db->newSelectQueryBuilder()
27 ->select( [ 'ts_namespace', 'ts_title', 'ts_value', 'ts_metadata' ] )
28 ->from( $this->dbTable )
29 ->where( [ 'ts_user' => $user->getId() ] )
30 ->caller( __METHOD__ )
31 ->fetchResultSet();
32
33 $objects = [];
34 foreach ( $res as $row ) {
35 $objects[] = new StashedTranslation(
36 $user,
37 Title::makeTitle( (int)$row->ts_namespace, $row->ts_title ),
38 $row->ts_value,
39 unserialize( $row->ts_metadata )
40 );
41 }
42
43 return $objects;
44 }
45
46 public function addTranslation( StashedTranslation $item ): void {
47 $row = [
48 'ts_user' => $item->getUser()->getId(),
49 'ts_title' => $item->getTitle()->getDBkey(),
50 'ts_namespace' => $item->getTitle()->getNamespace(),
51 'ts_value' => $item->getValue(),
52 'ts_metadata' => serialize( $item->getMetadata() ),
53 ];
54
55 $this->db->newReplaceQueryBuilder()
56 ->replaceInto( $this->dbTable )
57 ->uniqueIndexFields( [ 'ts_user', 'ts_namespace', 'ts_title' ] )
58 ->row( $row )
59 ->caller( __METHOD__ )
60 ->execute();
61 }
62
63 public function deleteTranslations( User $user ): void {
64 $this->db->newDeleteQueryBuilder()
65 ->deleteFrom( $this->dbTable )
66 ->where( [ 'ts_user' => $user->getId() ] )
67 ->caller( __METHOD__ )
68 ->execute();
69 }
70}
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.