Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
MessageBundle
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 9
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMessageGroupId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTranslationPages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTranslationUnitPages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isMoveable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isDeletable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isSourcePage
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
6
 clearSourcePageCache
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageBundleTranslation;
5
6use MediaWiki\Extension\Translate\MessageGroupProcessing\RevTagStore;
7use MediaWiki\Extension\Translate\MessageGroupProcessing\TranslatableBundle;
8use MediaWiki\MediaWikiServices;
9use MediaWiki\Title\Title;
10use Wikimedia\Rdbms\Database;
11
12/**
13 * @author Abijeet Patro
14 * @author Niklas Laxström
15 * @since 2022.04
16 * @license GPL-2.0-or-later
17 */
18class MessageBundle extends TranslatableBundle {
19    private Title $title;
20
21    public function __construct( Title $title ) {
22        $this->title = $title;
23    }
24
25    /** @inheritDoc */
26    public function getTitle(): Title {
27        return $this->title;
28    }
29
30    /** @inheritDoc */
31    public function getMessageGroupId(): string {
32        return MessageBundleMessageGroup::getGroupId( $this->title->getPrefixedText() );
33    }
34
35    /** @inheritDoc */
36    public function getTranslationPages(): array {
37        // MessageBundle do not have translation pages
38        return [];
39    }
40
41    /** @inheritDoc */
42    public function getTranslationUnitPages( ?string $code = null ): array {
43        return $this->getTranslationUnitPagesByTitle( $this->title, $code );
44    }
45
46    /** @inheritDoc */
47    public function isMoveable(): bool {
48        return true;
49    }
50
51    /** @inheritDoc */
52    public function isDeletable(): bool {
53        return true;
54    }
55
56    public static function isSourcePage( Title $title ): bool {
57        if ( !$title->exists() ) {
58            return false;
59        }
60
61        $mwServices = MediaWikiServices::getInstance();
62        $cache = $mwServices->getMainWANObjectCache();
63        $cacheKey = $cache->makeKey( 'messagebundle', 'source' );
64
65        $messageBundleIds = $cache->getWithSetCallback(
66            $cacheKey,
67            $cache::TTL_HOUR * 2,
68            static function ( $oldValue, &$ttl, array &$setOpts ) use ( $mwServices ) {
69                $dbr = $mwServices->getDBLoadBalancer()->getConnection( DB_REPLICA );
70                $setOpts += Database::getCacheSetOptions( $dbr );
71
72                $ids = RevTagStore::getTranslatableBundleIds( RevTagStore::MB_VALID_TAG );
73                // Adding a comma at the end and beginning so that we can check for page Id
74                // existence with the "," delimiters
75                return ',' . implode( ',', $ids ) . ',';
76            },
77            [
78                'checkKeys' => [ $cacheKey ],
79                'pcTTL' => $cache::TTL_PROC_SHORT,
80                'pcGroup' => __CLASS__ . ':1',
81                'version' => 3,
82            ]
83        );
84
85        return str_contains( $messageBundleIds, ( ',' . $title->getArticleID() . ',' ) );
86    }
87
88    public static function clearSourcePageCache(): void {
89        $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
90        $cache->touchCheckKey( $cache->makeKey( 'messagebundle', 'source' ) );
91    }
92}