Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
TranslatablePageMessageGroupFactory | |
0.00% |
0 / 26 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getCacheKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCacheVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDependencies | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getData | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
createGroups | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\PageTranslation; |
5 | |
6 | use CacheDependency; |
7 | use MainConfigDependency; |
8 | use MediaWiki\Config\ServiceOptions; |
9 | use MediaWiki\Extension\Translate\MessageGroupProcessing\CachedMessageGroupFactory; |
10 | use MediaWiki\Extension\Translate\MessageGroupProcessing\RevTagStore; |
11 | use MediaWiki\Title\Title; |
12 | use MessageGroup; |
13 | use Wikimedia\Rdbms\IReadableDatabase; |
14 | use WikiPageMessageGroup; |
15 | |
16 | /** |
17 | * Translatable page message group factories that uses caching. |
18 | * @since 2024.05 |
19 | * @license GPL-2.0-or-later |
20 | * @author Niklas Laxström |
21 | */ |
22 | final class TranslatablePageMessageGroupFactory implements CachedMessageGroupFactory { |
23 | public const SERVICE_OPTIONS = [ |
24 | 'EnablePageTranslation' |
25 | ]; |
26 | |
27 | private ServiceOptions $options; |
28 | |
29 | public function __construct( ServiceOptions $options ) { |
30 | $options->assertRequiredOptions( self::SERVICE_OPTIONS ); |
31 | $this->options = $options; |
32 | } |
33 | |
34 | public function getCacheKey(): string { |
35 | return 'translatable-pages'; |
36 | } |
37 | |
38 | public function getCacheVersion(): int { |
39 | return 1; |
40 | } |
41 | |
42 | /** @return CacheDependency[] */ |
43 | public function getDependencies(): array { |
44 | return [ new MainConfigDependency( 'EnablePageTranslation' ) ]; |
45 | } |
46 | |
47 | /** @return string[] */ |
48 | public function getData( IReadableDatabase $db ): array { |
49 | if ( !$this->options->get( 'EnablePageTranslation' ) ) { |
50 | return []; |
51 | } |
52 | |
53 | $groupTitles = []; |
54 | $res = $db->newSelectQueryBuilder() |
55 | ->select( [ 'page_id', 'page_namespace', 'page_title' ] ) |
56 | ->from( 'page' ) |
57 | ->join( 'revtag', null, 'page_id=rt_page' ) |
58 | ->where( [ 'rt_type' => RevTagStore::TP_MARK_TAG ] ) |
59 | ->caller( __METHOD__ ) |
60 | ->groupBy( [ 'rt_page', 'page_id', 'page_namespace', 'page_title' ] ) |
61 | ->fetchResultSet(); |
62 | |
63 | foreach ( $res as $r ) { |
64 | $title = Title::newFromRow( $r ); |
65 | $groupTitles[] = $title->getPrefixedText(); |
66 | } |
67 | |
68 | return $groupTitles; |
69 | } |
70 | |
71 | /** |
72 | * @param string[] $data |
73 | * @return MessageGroup[] |
74 | */ |
75 | public function createGroups( $data ): array { |
76 | $groups = []; |
77 | foreach ( $data as $title ) { |
78 | $title = Title::newFromText( $title ); |
79 | $id = TranslatablePage::getMessageGroupIdFromTitle( $title ); |
80 | $groups[$id] = new WikiPageMessageGroup( $id, $title ); |
81 | } |
82 | |
83 | return $groups; |
84 | } |
85 | } |