Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialGadgets
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 6
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 getSubpage
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 setSubtitle
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
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\Special;
22
23use ErrorPageError;
24use MediaWiki\Message\Message;
25use MediaWiki\SpecialPage\SpecialPage;
26use Wikimedia\ObjectFactory\ObjectFactory;
27
28/**
29 * Wrapper page for Special:Gadgets subpages
30 */
31class SpecialGadgets extends SpecialPage {
32    /**
33     * List of subpage names to the subclass of ActionPage which handles them.
34     */
35    private const SUBPAGE_LIST = [
36        'list' => [
37            'class' => ListPage::class,
38            'services' => [
39                'GadgetsRepo',
40                'ContentLanguage',
41                'SkinFactory'
42            ],
43        ],
44        'export' => [
45            'class' => ExportPage::class,
46            'services' => [
47                'GadgetsRepo'
48            ]
49        ]
50    ];
51
52    public function __construct( private readonly ObjectFactory $objectFactory ) {
53        parent::__construct( 'Gadgets' );
54    }
55
56    /**
57     * Return title for Special:Gadgets heading and Special:Specialpages link.
58     *
59     * @return Message
60     */
61    public function getDescription() {
62        return $this->msg( 'special-gadgets' );
63    }
64
65    /**
66     * @inheritDoc
67     */
68    protected function getGroupName() {
69        return 'wiki';
70    }
71
72    /**
73     * @param string|null $par Parameters passed to the page
74     */
75    public function execute( $par ) {
76        $parts = $par ? explode( '/', $par, 1 ) : [];
77        $subPage = $parts[0] ?? 'list';
78
79        $params = explode( '/', $subPage );
80        $pageName = array_shift( $params );
81        $page = $this->getSubpage( $pageName );
82        if ( !$page ) {
83            $this->getOutput()->setStatusCode( 404 );
84            throw new ErrorPageError( 'error', 'gadgets-subpage-invalid', [ $pageName ] );
85        }
86
87        if ( !( $page instanceof ListPage ) ) {
88            $this->setSubtitle();
89        }
90
91        $this->setHeaders();
92        $this->addHelpLink( 'Extension:Gadgets' );
93        $page->execute( $params );
94    }
95
96    /**
97     * Get a _ActionPage subclass object for the given subpage name
98     * @param string $name
99     * @return null|ActionPage
100     */
101    private function getSubpage( string $name ) {
102        if ( !isset( self::SUBPAGE_LIST[$name] ) ) {
103            return null;
104        }
105        /** @var ActionPage $page */
106        // ObjectFactory::createObject accepts an array, not just a callable (phan bug)
107        // @phan-suppress-next-line PhanTypeInvalidCallableArrayKey
108        $page = $this->objectFactory->createObject(
109            self::SUBPAGE_LIST[$name],
110            [
111                'extraArgs' => [ $this ],
112                'assertClass' => ActionPage::class,
113            ]
114        );
115        return $page;
116    }
117
118    /**
119     * Set a navigation subtitle.
120     */
121    private function setSubtitle() {
122        $title = $this->getPageTitle();
123        $linkRenderer = $this->getLinkRenderer();
124        $subtitle = '&lt; ' . $linkRenderer->makeKnownLink( $title, $this->msg( 'gadgets-title' )->text() );
125        $this->getOutput()->setSubtitle( $subtitle );
126    }
127}