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 MediaWiki\Title\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 ->caller( __METHOD__ )
67 ->fetchResultSet();
68
69 $result = [];
70 foreach ( $resultSet as $row ) {
71 $result[$row->ttb_page_id] = (int)$row->ttb_status;
72 }
73
74 return $result;
75 }
76
77 public function removeStatus( int ...$pageIds ): void {
78 if ( !$this->doesTableExist() ) {
79 return;
80 }
81
82 $this->database->delete(
83 self::TABLE_NAME,
84 [ 'ttb_page_id' => $pageIds ],
85 __METHOD__
86 );
87 }
88
89 private function getBundleTypeId( string $bundle ): int {
90 if ( $bundle === TranslatablePage::class ) {
91 return 1;
92 } elseif ( $bundle === MessageBundle::class ) {
93 return 2;
94 }
95
96 throw new InvalidArgumentException( "Unknown translatable bundle type: $bundle" );
97 }
98
100 private function doesTableExist(): bool {
101 if ( $this->tableExists === null ) {
102 $this->tableExists = $this->dbMaintenance->tableExists( self::TABLE_NAME, __METHOD__ );
103 }
104
105 return $this->tableExists;
106 }
107}
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.