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';
23 private $database;
25 private $collation;
27 private $dbMaintainance;
29 private $tableExists = null;
30
31 public function __construct(
32 IDatabase $database,
33 Collation $collation,
34 IMaintainableDatabase $dbMaintainance
35 ) {
36 $this->database = $database;
37 $this->collation = $collation;
38 $this->dbMaintainance = $dbMaintainance;
39 }
40
41 public function setStatus( Title $title, TranslatableBundleStatus $status, string $bundleType ): void {
42 if ( !$this->doesTableExist() ) {
43 return;
44 }
45
46 $sortKey = substr( $this->collation->getSortKey( $title->getPrefixedDBkey() ), 0, 255 );
47 $bundleTypeId = $this->getBundleTypeId( $bundleType );
48 $this->database->replace(
49 self::TABLE_NAME,
50 [ 'ttb_page_id' ],
51 [
52 'ttb_page_id' => $title->getArticleID(),
53 'ttb_type' => $bundleTypeId,
54 'ttb_status' => $status->getId(),
55 'ttb_sortkey' => $sortKey
56 ],
57 __METHOD__
58 );
59 }
60
62 public function getAllWithStatus(): array {
63 if ( !$this->doesTableExist() ) {
64 return [];
65 }
66
67 $resultSet = $this->database->newSelectQueryBuilder()
68 ->select( [ 'ttb_page_id' , 'ttb_status' ] )
69 ->from( self::TABLE_NAME )
70 ->fetchResultSet();
71
72 $result = [];
73 foreach ( $resultSet as $row ) {
74 $result[$row->ttb_page_id] = (int)$row->ttb_status;
75 }
76
77 return $result;
78 }
79
80 public function removeStatus( int ...$pageIds ): void {
81 if ( !$this->doesTableExist() ) {
82 return;
83 }
84
85 $this->database->delete(
86 self::TABLE_NAME,
87 [ 'ttb_page_id' => $pageIds ],
88 __METHOD__
89 );
90 }
91
92 private function getBundleTypeId( string $bundle ): int {
93 if ( $bundle === TranslatablePage::class ) {
94 return 1;
95 } elseif ( $bundle === MessageBundle::class ) {
96 return 2;
97 }
98
99 throw new InvalidArgumentException( "Unknown translatable bundle type: $bundle" );
100 }
101
103 private function doesTableExist(): bool {
104 if ( $this->tableExists === null ) {
105 $this->tableExists = $this->dbMaintainance->tableExists( self::TABLE_NAME, __METHOD__ );
106 }
107
108 return $this->tableExists;
109 }
110}
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.