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