Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundleStatusStore.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use Collation;
7use InvalidArgumentException;
10use Title;
11use Wikimedia\Rdbms\IDatabase;
12use Wikimedia\Rdbms\IMaintainableDatabase;
13
21 private const TABLE_NAME = 'translate_translatable_bundles';
22 private IDatabase $database;
23 private Collation $collation;
24 private IMaintainableDatabase $dbMaintenance;
25 private ?bool $tableExists = null;
26
27 public function __construct(
28 IDatabase $database,
29 Collation $collation,
30 IMaintainableDatabase $dbMaintenance
31 ) {
32 $this->database = $database;
33 $this->collation = $collation;
34 $this->dbMaintenance = $dbMaintenance;
35 }
36
37 public function setStatus( Title $title, TranslatableBundleStatus $status, string $bundleType ): void {
38 if ( !$this->doesTableExist() ) {
39 return;
40 }
41
42 $sortKey = substr( $this->collation->getSortKey( $title->getPrefixedDBkey() ), 0, 255 );
43 $bundleTypeId = $this->getBundleTypeId( $bundleType );
44 $this->database->replace(
45 self::TABLE_NAME,
46 [ 'ttb_page_id' ],
47 [
48 'ttb_page_id' => $title->getArticleID(),
49 'ttb_type' => $bundleTypeId,
50 'ttb_status' => $status->getId(),
51 'ttb_sortkey' => $sortKey
52 ],
53 __METHOD__
54 );
55 }
56
58 public function getAllWithStatus(): array {
59 if ( !$this->doesTableExist() ) {
60 return [];
61 }
62
63 $resultSet = $this->database->newSelectQueryBuilder()
64 ->select( [ 'ttb_page_id' , 'ttb_status' ] )
65 ->from( self::TABLE_NAME )
66 ->fetchResultSet();
67
68 $result = [];
69 foreach ( $resultSet as $row ) {
70 $result[$row->ttb_page_id] = (int)$row->ttb_status;
71 }
72
73 return $result;
74 }
75
76 public function removeStatus( int ...$pageIds ): void {
77 if ( !$this->doesTableExist() ) {
78 return;
79 }
80
81 $this->database->delete(
82 self::TABLE_NAME,
83 [ 'ttb_page_id' => $pageIds ],
84 __METHOD__
85 );
86 }
87
88 private function getBundleTypeId( string $bundle ): int {
89 if ( $bundle === TranslatablePage::class ) {
90 return 1;
91 } elseif ( $bundle === MessageBundle::class ) {
92 return 2;
93 }
94
95 throw new InvalidArgumentException( "Unknown translatable bundle type: $bundle" );
96 }
97
99 private function doesTableExist(): bool {
100 if ( $this->tableExists === null ) {
101 $this->tableExists = $this->dbMaintenance->tableExists( self::TABLE_NAME, __METHOD__ );
102 }
103
104 return $this->tableExists;
105 }
106}
Store service for looking up and storing status for translatable bundle status.
getAllWithStatus()
Return all bundles in an array with key being page id, value being status.
Mixed bag of methods related to translatable pages.