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 Title;
11
21 private $translatablePageStore;
23 private $messageBundleStore;
24
25 public function __construct(
26 TranslatablePageStore $translatablePageStore,
27 MessageBundleStore $messageBundleStore
28 ) {
29 $this->translatablePageStore = $translatablePageStore;
30 $this->messageBundleStore = $messageBundleStore;
31 }
32
34 public function getBundle( Title $title ): ?TranslatableBundle {
35 if ( TranslatablePage::isSourcePage( $title ) ) {
36 $translatablePage = TranslatablePage::newFromTitle( $title );
37 return $translatablePage;
38 } elseif ( MessageBundle::isSourcePage( $title ) ) {
39 $messageBundle = new MessageBundle( $title );
40 return $messageBundle;
41 }
42
43 return null;
44 }
45
47 public function getValidBundle( Title $title ): TranslatableBundle {
48 $bundle = $this->getBundle( $title );
49 if ( $bundle ) {
50 return $bundle;
51 }
52
53 throw new InvalidArgumentException( "{$title->getPrefixedText()} is not a TranslatableBundle" );
54 }
55
56 public function getBundleFromClass( Title $title, string $bundleType ): TranslatableBundle {
57 if ( $bundleType === MessageBundle::class ) {
58 return new MessageBundle( $title );
59 } else {
60 return TranslatablePage::newFromTitle( $title );
61 }
62 }
63
64 public function getPageMoveLogger( TranslatableBundle $bundle ): PageMoveLogger {
65 if ( $bundle instanceof TranslatablePage ) {
66 return new PageMoveLogger( $bundle->getTitle(), 'pagetranslation' );
67 } elseif ( $bundle instanceof MessageBundle ) {
68 return new PageMoveLogger( $bundle->getTitle(), 'messagebundle' );
69 }
70
71 throw new InvalidArgumentException( "Unknown TranslatableBundle type: " . get_class( $bundle ) );
72 }
73
74 public function getPageDeleteLogger( TranslatableBundle $bundle ): PageDeleteLogger {
75 if ( $bundle instanceof TranslatablePage ) {
76 return new PageDeleteLogger( $bundle->getTitle(), 'pagetranslation' );
77 } elseif ( $bundle instanceof MessageBundle ) {
78 return new PageDeleteLogger( $bundle->getTitle(), 'messagebundle' );
79 }
80
81 throw new InvalidArgumentException( "Unknown TranslatableBundle type: " . get_class( $bundle ) );
82 }
83
84 public function getStore( TranslatableBundle $bundle ): TranslatableBundleStore {
85 if ( $bundle instanceof TranslatablePage ) {
86 return $this->translatablePageStore;
87 } elseif ( $bundle instanceof MessageBundle ) {
88 return $this->messageBundleStore;
89 }
90
91 throw new InvalidArgumentException( "Unknown TranslatableBundle type: " . get_class( $bundle ) );
92 }
93}
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.