Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundleFactory.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use InvalidArgumentException;
10use MediaWiki\Title\Title;
11
20 private TranslatablePageStore $translatablePageStore;
21 private MessageBundleStore $messageBundleStore;
22
23 public function __construct(
24 TranslatablePageStore $translatablePageStore,
25 MessageBundleStore $messageBundleStore
26 ) {
27 $this->translatablePageStore = $translatablePageStore;
28 $this->messageBundleStore = $messageBundleStore;
29 }
30
32 public function getBundle( Title $title ): ?TranslatableBundle {
33 if ( TranslatablePage::isSourcePage( $title ) ) {
34 return TranslatablePage::newFromTitle( $title );
35 } elseif ( MessageBundle::isSourcePage( $title ) ) {
36 return new MessageBundle( $title );
37 }
38
39 return null;
40 }
41
43 public function getValidBundle( Title $title ): TranslatableBundle {
44 $bundle = $this->getBundle( $title );
45 if ( $bundle ) {
46 return $bundle;
47 }
48
49 throw new InvalidArgumentException( "{$title->getPrefixedText()} is not a TranslatableBundle" );
50 }
51
52 public function getBundleFromClass( Title $title, string $bundleType ): TranslatableBundle {
53 if ( $bundleType === MessageBundle::class ) {
54 return new MessageBundle( $title );
55 } else {
56 return TranslatablePage::newFromTitle( $title );
57 }
58 }
59
60 public function getPageMoveLogger( TranslatableBundle $bundle ): PageMoveLogger {
61 if ( $bundle instanceof TranslatablePage ) {
62 return new PageMoveLogger( $bundle->getTitle(), 'pagetranslation' );
63 } elseif ( $bundle instanceof MessageBundle ) {
64 return new PageMoveLogger( $bundle->getTitle(), 'messagebundle' );
65 }
66
67 throw new InvalidArgumentException( 'Unknown TranslatableBundle type: ' . get_class( $bundle ) );
68 }
69
70 public function getPageDeleteLogger( TranslatableBundle $bundle ): PageDeleteLogger {
71 if ( $bundle instanceof TranslatablePage ) {
72 return new PageDeleteLogger( $bundle->getTitle(), 'pagetranslation' );
73 } elseif ( $bundle instanceof MessageBundle ) {
74 return new PageDeleteLogger( $bundle->getTitle(), 'messagebundle' );
75 }
76
77 throw new InvalidArgumentException( 'Unknown TranslatableBundle type: ' . get_class( $bundle ) );
78 }
79
80 public function getStore( TranslatableBundle $bundle ): TranslatableBundleStore {
81 if ( $bundle instanceof TranslatablePage ) {
82 return $this->translatablePageStore;
83 } elseif ( $bundle instanceof MessageBundle ) {
84 return $this->messageBundleStore;
85 }
86
87 throw new InvalidArgumentException( "Unknown TranslatableBundle type: " . get_class( $bundle ) );
88 }
89}
Create instances of various classes based on the type of TranslatableBundle.
getBundle(Title $title)
Returns a TranslatableBundle if Title is a valid translatable bundle else returns null.
getValidBundle(Title $title)
Return a TranslatableBundle from the Title, throwing an error if it is not a TranslatableBundle.
Translatable bundle represents a message group where its translatable content is defined on a wiki pa...
Mixed bag of methods related to translatable pages.