| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Extension\Gadgets; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\Linker\LinkTarget; |
| 25 |
use MediaWiki\Title\Title; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class MultiGadgetRepo extends GadgetRepo { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private array $repos; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct( array $repos ) { |
| 44 |
$this->repos = $repos; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function getGadget( string $id ): Gadget { |
| 51 |
foreach ( $this->repos as $repo ) { |
| 52 |
try { |
| 53 |
return $repo->getGadget( $id ); |
| 54 |
} catch ( InvalidArgumentException $e ) { |
| 55 |
|
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
throw new InvalidArgumentException( "No gadget registered for '$id'" ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function getGadgetIds(): array { |
| 66 |
$ids = []; |
| 67 |
foreach ( $this->repos as $repo ) { |
| 68 |
$ids = array_merge( $ids, $repo->getGadgetIds() ); |
| 69 |
} |
| 70 |
return array_values( array_unique( $ids ) ); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function handlePageUpdate( LinkTarget $target ): void { |
| 77 |
foreach ( $this->repos as $repo ) { |
| 78 |
$repo->handlePageUpdate( $target ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
private function getRepoForGadget( string $id ): GadgetRepo { |
| 83 |
foreach ( $this->repos as $repo ) { |
| 84 |
try { |
| 85 |
$repo->getGadget( $id ); |
| 86 |
|
| 87 |
return $repo; |
| 88 |
} catch ( InvalidArgumentException $e ) { |
| 89 |
} |
| 90 |
} |
| 91 |
throw new InvalidArgumentException( "No repo found for gadget $id" ); |
| 92 |
} |
| 93 |
|
| 94 |
public function getGadgetDefinitionTitle( string $id ): ?Title { |
| 95 |
return $this->getRepoForGadget( $id )->getGadgetDefinitionTitle( $id ); |
| 96 |
} |
| 97 |
|
| 98 |
public function titleWithoutPrefix( string $titleText, string $gadgetId ): string { |
| 99 |
return $this->getRepoForGadget( $gadgetId )->titleWithoutPrefix( $titleText, $gadgetId ); |
| 100 |
} |
| 101 |
|
| 102 |
public function validationWarnings( Gadget $gadget ): array { |
| 103 |
$duplicateWarnings = $this->isDefinedTwice( $gadget->getName() ) ? [ |
| 104 |
wfMessage( "gadgets-validate-duplicate", $gadget->getName() ) |
| 105 |
] : []; |
| 106 |
return array_merge( $duplicateWarnings, parent::validationWarnings( $gadget ) ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private function isDefinedTwice( string $id ) { |
| 115 |
$found = false; |
| 116 |
foreach ( $this->repos as $repo ) { |
| 117 |
try { |
| 118 |
$repo->getGadget( $id ); |
| 119 |
if ( $found ) { |
| 120 |
|
| 121 |
return true; |
| 122 |
} else { |
| 123 |
$found = true; |
| 124 |
} |
| 125 |
} catch ( InvalidArgumentException $e ) { |
| 126 |
} |
| 127 |
} |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
} |