Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
58.33% covered (warning)
58.33%
14 / 24
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContributeFactory
58.33% covered (warning)
58.33%
14 / 24
50.00% covered (danger)
50.00%
2 / 4
8.60
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCards
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 isEnabledOnCurrentSkin
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getAssociatedNavigationLinks
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
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\Specials\Contribute;
22
23use MediaWiki\HookContainer\HookRunner;
24use MediaWiki\SpecialPage\SpecialPage;
25use MediaWiki\Specials\Contribute\Card\ContributeCard;
26use MediaWiki\Specials\Contribute\Card\ContributeCardActionLink;
27use MediaWiki\User\UserIdentity;
28use MessageLocalizer;
29use Skin;
30
31class ContributeFactory {
32
33    private MessageLocalizer $localizer;
34    private HookRunner $hookRunner;
35
36    /**
37     * @param MessageLocalizer $localizer
38     * @param HookRunner $hookRunner
39     */
40    public function __construct( MessageLocalizer $localizer, HookRunner $hookRunner ) {
41        $this->localizer = $localizer;
42        $this->hookRunner = $hookRunner;
43    }
44
45    /**
46     * @return array
47     */
48    public function getCards(): array {
49        $cards = [];
50
51        $this->hookRunner->onContributeCards( $cards );
52
53        $cards[] = ( new ContributeCard(
54            $this->localizer->msg( 'newpage' )->text(),
55            $this->localizer->msg( 'newpage-desc' )->text(),
56            'article',
57            new ContributeCardActionLink(
58                SpecialPage::getTitleFor( 'Wantedpages' )->getLocalURL(),
59                $this->localizer->msg( 'view-missing-pages' )->text()
60            )
61        ) )->toArray();
62
63        return $cards;
64    }
65
66    /**
67     * Check if the Special:Contribute page is enabled for the current skin
68     * This can be removed when T323083 is resolved ie. the Special:Contribute feature
69     * has been shipped by the WMF Language Team.
70     *
71     * @param Skin $skin
72     * @param array $specialContributeSkinsEnabled
73     *
74     * @return bool
75     */
76    public static function isEnabledOnCurrentSkin(
77        Skin $skin,
78        array $specialContributeSkinsEnabled = []
79    ): bool {
80        return in_array(
81                $skin->getSkinName(),
82                $specialContributeSkinsEnabled
83            );
84    }
85
86    /**
87     * @param UserIdentity $viewingUser
88     * @param ?UserIdentity $targetUser
89     *
90     * @return array
91     */
92    public static function getAssociatedNavigationLinks(
93        UserIdentity $viewingUser,
94        ?UserIdentity $targetUser
95    ): array {
96        if ( $targetUser === null || !$viewingUser->equals( $targetUser ) ) {
97            return [];
98        }
99
100        return [
101            SpecialPage::getTitleFor( 'Contribute' )->getFullText(),
102            SpecialPage::getTitleFor( 'Contributions', $targetUser->getName() )->getFullText(),
103        ];
104    }
105}