Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
43.33% covered (danger)
43.33%
13 / 30
11.11% covered (danger)
11.11%
1 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
MultiGadgetRepo
43.33% covered (danger)
43.33%
13 / 30
11.11% covered (danger)
11.11%
1 / 9
84.69
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGadget
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 getGadgetIds
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 handlePageUpdate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getRepoForGadget
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getGadgetDefinitionTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 titleWithoutPrefix
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validationWarnings
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 isDefinedTwice
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
4.25
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\Gadgets;
22
23use InvalidArgumentException;
24use MediaWiki\Linker\LinkTarget;
25use MediaWiki\Title\Title;
26
27/**
28 * Combine two gadget repos during migrations
29 *
30 * @copyright 2017 Kunal Mehta <legoktm@member.fsf.org>
31 * @copyright 2023 Siddharth VP
32 */
33class MultiGadgetRepo extends GadgetRepo {
34
35    /**
36     * @param GadgetRepo[] $repos
37     */
38    public function __construct( private readonly array $repos ) {
39    }
40
41    /**
42     * @inheritDoc
43     */
44    public function getGadget( string $id ): Gadget {
45        foreach ( $this->repos as $repo ) {
46            try {
47                return $repo->getGadget( $id );
48            } catch ( InvalidArgumentException ) {
49                // Try next repo
50            }
51        }
52
53        throw new InvalidArgumentException( "No gadget registered for '$id'" );
54    }
55
56    /**
57     * @inheritDoc
58     */
59    public function getGadgetIds(): array {
60        $ids = [];
61        foreach ( $this->repos as $repo ) {
62            $ids = array_merge( $ids, $repo->getGadgetIds() );
63        }
64        return array_values( array_unique( $ids ) );
65    }
66
67    /**
68     * @inheritDoc
69     */
70    public function handlePageUpdate( LinkTarget $target ): void {
71        foreach ( $this->repos as $repo ) {
72            $repo->handlePageUpdate( $target );
73        }
74    }
75
76    private function getRepoForGadget( string $id ): GadgetRepo {
77        foreach ( $this->repos as $repo ) {
78            try {
79                $repo->getGadget( $id );
80                // return repo if it didn't throw
81                return $repo;
82            } catch ( InvalidArgumentException ) {
83            }
84        }
85        throw new InvalidArgumentException( "No repo found for gadget $id" );
86    }
87
88    public function getGadgetDefinitionTitle( string $id ): ?Title {
89        return $this->getRepoForGadget( $id )->getGadgetDefinitionTitle( $id );
90    }
91
92    public function titleWithoutPrefix( string $titleText, string $gadgetId ): string {
93        return $this->getRepoForGadget( $gadgetId )->titleWithoutPrefix( $titleText, $gadgetId );
94    }
95
96    public function validationWarnings( Gadget $gadget ): array {
97        $duplicateWarnings = $this->isDefinedTwice( $gadget->getName() ) ? [
98            wfMessage( "gadgets-validate-duplicate", $gadget->getName() )
99        ] : [];
100        return array_merge( $duplicateWarnings, parent::validationWarnings( $gadget ) );
101    }
102
103    /**
104     * Checks if a gadget is defined with the same name in two different repos.
105     * @param string $id Gadget name
106     * @return bool
107     */
108    private function isDefinedTwice( string $id ) {
109        $found = false;
110        foreach ( $this->repos as $repo ) {
111            try {
112                $repo->getGadget( $id );
113                if ( $found ) {
114                    // found it a second time
115                    return true;
116                } else {
117                    $found = true;
118                }
119            } catch ( InvalidArgumentException ) {
120            }
121        }
122        return false;
123    }
124
125}