Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
99.32% covered (success)
99.32%
146 / 147
92.86% covered (success)
92.86%
13 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
ComponentFactory
99.32% covered (success)
99.32%
146 / 147
92.86% covered (success)
92.86%
13 / 14
18
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 createMainComponent
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
1
 createContentComponent
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
 createLogoComponent
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 createPrimaryNavComponent
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 createSecondaryNavComponent
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 createNavBarComponent
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 createNavMenuComponent
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 createUserMenuComponent
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 createPageToolsComponent
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 createNotificationAlertComponent
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
2.01
 createSearchFieldComponent
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 createSearchButtonComponent
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 createFooterComponent
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
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 *
17 * @file
18 */
19namespace MediaWiki\Skin\WikimediaApiPortal;
20
21use ExtensionRegistry;
22use MediaWiki\Config\Config;
23use MediaWiki\Config\ServiceOptions;
24use MediaWiki\Linker\Linker;
25use MediaWiki\Page\PageProps;
26use MediaWiki\Permissions\PermissionManager;
27use MediaWiki\Skin\WikimediaApiPortal\Component\ContentComponent;
28use MediaWiki\Skin\WikimediaApiPortal\Component\FooterComponent;
29use MediaWiki\Skin\WikimediaApiPortal\Component\LogoComponent;
30use MediaWiki\Skin\WikimediaApiPortal\Component\MainComponent;
31use MediaWiki\Skin\WikimediaApiPortal\Component\NavBarComponent;
32use MediaWiki\Skin\WikimediaApiPortal\Component\NavMenuComponent;
33use MediaWiki\Skin\WikimediaApiPortal\Component\NotificationAlertComponent;
34use MediaWiki\Skin\WikimediaApiPortal\Component\PageToolsComponent;
35use MediaWiki\Skin\WikimediaApiPortal\Component\PrimaryNavComponent;
36use MediaWiki\Skin\WikimediaApiPortal\Component\SearchButtonComponent;
37use MediaWiki\Skin\WikimediaApiPortal\Component\SearchFieldComponent;
38use MediaWiki\Skin\WikimediaApiPortal\Component\SecondaryNavComponent;
39use MediaWiki\Skin\WikimediaApiPortal\Component\UserMenuComponent;
40use MediaWiki\SpecialPage\SpecialPageFactory;
41use MediaWiki\Title\NamespaceInfo;
42use MediaWiki\Title\TitleFactory;
43use Wikimedia\Message\IMessageFormatterFactory;
44use Wikimedia\Message\MessageValue;
45
46class ComponentFactory {
47    /** @var Config */
48    private $config;
49
50    /** @var IMessageFormatterFactory */
51    private $messageFormatterFactory;
52
53    /** @var TitleFactory */
54    private $titleFactory;
55
56    /** @var SpecialPageFactory */
57    private $specialPageFactory;
58
59    /** @var NamespaceInfo */
60    private $namespaceInfo;
61
62    /** @var PageProps */
63    private $pageProps;
64
65    /** @var PermissionManager */
66    private $permissionManager;
67
68    /** @var ExtensionRegistry */
69    private $extensionRegistry;
70
71    /**
72     * @param Config $config
73     * @param IMessageFormatterFactory $messageFormatterFactory
74     * @param TitleFactory $titleFactory
75     * @param SpecialPageFactory $specialPageFactory
76     * @param NamespaceInfo $namespaceInfo
77     * @param PageProps $pageProps
78     * @param PermissionManager $permissionManager
79     * @param ExtensionRegistry $extensionRegistry
80     */
81    public function __construct(
82        Config $config,
83        IMessageFormatterFactory $messageFormatterFactory,
84        TitleFactory $titleFactory,
85        SpecialPageFactory $specialPageFactory,
86        NamespaceInfo $namespaceInfo,
87        PageProps $pageProps,
88        PermissionManager $permissionManager,
89        ExtensionRegistry $extensionRegistry
90    ) {
91        $this->config = $config;
92        $this->messageFormatterFactory = $messageFormatterFactory;
93        $this->titleFactory = $titleFactory;
94        $this->specialPageFactory = $specialPageFactory;
95        $this->namespaceInfo = $namespaceInfo;
96        $this->pageProps = $pageProps;
97        $this->permissionManager = $permissionManager;
98        $this->extensionRegistry = $extensionRegistry;
99    }
100
101    /**
102     * @param WikimediaApiPortalTemplate $template
103     * @return MainComponent
104     */
105    public function createMainComponent(
106        WikimediaApiPortalTemplate $template
107    ): MainComponent {
108        $siteNotice = $template->data['sitenotice'];
109        $logo = $this->createLogoComponent( $template );
110        $navMenu = $this->createNavMenuComponent( $template );
111        $navBar = $this->createNavBarComponent( $template );
112        $secondaryNav = $this->createSecondaryNavComponent( $template );
113        $userMenu = $this->createUserMenuComponent( $template );
114        $notificationAlert = $this->createNotificationAlertComponent( $template );
115        $searchField = $this->createSearchFieldComponent( $template );
116        $searchButton = $this->createSearchButtonComponent( $template );
117        $content = $this->createContentComponent( $template );
118        $footer = $this->createFooterComponent( $template );
119        return new MainComponent(
120            $this->messageFormatterFactory,
121            $template->getSkin(),
122            $siteNotice,
123            $logo,
124            $navMenu,
125            $navBar,
126            $secondaryNav,
127            $userMenu,
128            $notificationAlert,
129            $searchField,
130            $searchButton,
131            $content,
132            $footer
133        );
134    }
135
136    /**
137     * @param WikimediaApiPortalTemplate $template
138     * @return ContentComponent
139     */
140    private function createContentComponent(
141        WikimediaApiPortalTemplate $template
142    ): ContentComponent {
143        $title = $template->get( 'title' );
144        $subtitle = $template->get( 'subtitle' ) ?: null;
145        $undelete = $template->get( 'undelete' ) ?: null;
146        $pageTools = $this->createPageToolsComponent( $template, false );
147        $bodyContent = $template->get( 'bodytext' );
148        $afterContent = $template->get( 'dataAfterContent' );
149        $catlinks = $template->get( 'catlinks' );
150        return new ContentComponent(
151            $title,
152            $subtitle,
153            $undelete,
154            $pageTools,
155            $bodyContent,
156            $afterContent,
157            $catlinks
158        );
159    }
160
161    /**
162     * @param WikimediaApiPortalTemplate $template
163     * @return LogoComponent
164     */
165    private function createLogoComponent(
166        WikimediaApiPortalTemplate $template
167    ): LogoComponent {
168        $skin = $template->getSkin();
169        $href = $skin->getTitle()->isMainPage() ? '#' : $this->titleFactory->newMainPage()->getLocalURL();
170        $tooltip = Linker::titleAttrib( 'p-logo' );
171        return new LogoComponent(
172            $this->messageFormatterFactory,
173            $skin,
174            $href,
175            $tooltip
176        );
177    }
178
179    /**
180     * @param WikimediaApiPortalTemplate $template
181     * @param string $id
182     * @return PrimaryNavComponent
183     */
184    private function createPrimaryNavComponent(
185        WikimediaApiPortalTemplate $template,
186        string $id
187    ): PrimaryNavComponent {
188        $skin = $template->getSkin();
189        $title = $skin->getTitle();
190        $sidebar = $template->getPrimaryNavSidebar();
191        return new PrimaryNavComponent(
192            $title,
193            $id,
194            $sidebar,
195            $skin
196        );
197    }
198
199    /**
200     * @param WikimediaApiPortalTemplate $template
201     * @return SecondaryNavComponent
202     */
203    private function createSecondaryNavComponent(
204        WikimediaApiPortalTemplate $template
205    ): SecondaryNavComponent {
206        $skin = $template->getSkin();
207        $title = $skin->getTitle();
208        return new SecondaryNavComponent(
209            new ServiceOptions( SecondaryNavComponent::CONSTRUCTOR_OPTIONS, $this->config ),
210            $this->messageFormatterFactory,
211            $skin,
212            $title,
213            $this->namespaceInfo,
214            $this->titleFactory,
215            $this->specialPageFactory,
216            $this->pageProps
217        );
218    }
219
220    /**
221     * @param WikimediaApiPortalTemplate $template
222     * @return NavBarComponent
223     */
224    private function createNavBarComponent(
225        WikimediaApiPortalTemplate $template
226    ): NavBarComponent {
227        return new NavBarComponent(
228            $this->createPrimaryNavComponent( $template, 'mw-navigation' )
229        );
230    }
231
232    /**
233     * @param WikimediaApiPortalTemplate $template
234     * @return NavMenuComponent
235     */
236    private function createNavMenuComponent(
237        WikimediaApiPortalTemplate $template
238    ): NavMenuComponent {
239        $primaryNav = $this->createPrimaryNavComponent( $template, 'mw-navigation' );
240        $secondaryNav = $this->createSecondaryNavComponent( $template );
241        $pageToolsMobile = $this->createPageToolsComponent( $template, true );
242        return new NavMenuComponent(
243            $this->titleFactory,
244            $primaryNav,
245            $secondaryNav,
246            $pageToolsMobile
247        );
248    }
249
250    /**
251     * @param WikimediaApiPortalTemplate $template
252     * @return UserMenuComponent
253     */
254    private function createUserMenuComponent(
255        WikimediaApiPortalTemplate $template
256    ): UserMenuComponent {
257        $skin = $template->getSkin();
258        $user = $skin->getUser();
259        $title = $skin->getTitle();
260        $personalUrls = $template->data['personal_urls'];
261        return new UserMenuComponent(
262            new ServiceOptions( UserMenuComponent::CONSTRUCTOR_OPTIONS, $this->config ),
263            $this->messageFormatterFactory,
264            $skin,
265            $this->titleFactory,
266            $this->specialPageFactory,
267            $user,
268            $title,
269            $personalUrls
270        );
271    }
272
273    /**
274     * @param WikimediaApiPortalTemplate $template
275     * @param bool $mobile
276     * @return PageToolsComponent
277     */
278    private function createPageToolsComponent(
279        WikimediaApiPortalTemplate $template,
280        bool $mobile
281    ): PageToolsComponent {
282        $skin = $template->getSkin();
283        return new PageToolsComponent(
284            $this->messageFormatterFactory,
285            $skin,
286            $this->permissionManager,
287            $skin->getRequest()->getRawVal( 'action', 'view' ),
288            $template->get( 'content_navigation', null ),
289            $mobile
290        );
291    }
292
293    /**
294     * @param WikimediaApiPortalTemplate $template
295     * @return NotificationAlertComponent
296     */
297    private function createNotificationAlertComponent(
298        WikimediaApiPortalTemplate $template
299    ): NotificationAlertComponent {
300        $user = $template->getSkin()->getUser();
301        if ( isset( $template->data['personal_urls']['notifications-alert'] ) ) {
302            $notificationAlert = $template->data['personal_urls']['notifications-alert'];
303        } else {
304            $notificationAlert = [];
305        }
306        return new NotificationAlertComponent(
307            $this->extensionRegistry,
308            $user,
309            $notificationAlert
310        );
311    }
312
313    /**
314     * @param WikimediaApiPortalTemplate $template
315     * @return SearchFieldComponent
316     */
317    private function createSearchFieldComponent(
318        WikimediaApiPortalTemplate $template
319    ): SearchFieldComponent {
320        return new SearchFieldComponent(
321            $this->messageFormatterFactory,
322            $template->getSkin(),
323            new ServiceOptions( SearchFieldComponent::CONSTRUCTOR_OPTIONS, $this->config )
324        );
325    }
326
327    /**
328     * @param WikimediaApiPortalTemplate $template
329     * @return SearchButtonComponent
330     */
331    private function createSearchButtonComponent(
332        WikimediaApiPortalTemplate $template
333    ): SearchButtonComponent {
334        return new SearchButtonComponent(
335            $this->messageFormatterFactory,
336            $template->getSkin(),
337            new ServiceOptions( SearchButtonComponent::CONSTRUCTOR_OPTIONS, $this->config )
338        );
339    }
340
341    /**
342     * @param WikimediaApiPortalTemplate $template
343     * @return FooterComponent
344     */
345    private function createFooterComponent(
346        WikimediaApiPortalTemplate $template
347    ): FooterComponent {
348        $skin = $template->getSkin();
349        $groups = [];
350        $skin->addToSidebarPlain(
351            $groups,
352            $this->messageFormatterFactory->getTextFormatter(
353                $skin->getLanguage()->getCode()
354            )->format( new MessageValue( 'wikimediaapiportal-skin-footer-links' ) )
355        );
356        return new FooterComponent(
357            $this->messageFormatterFactory,
358            $skin,
359            $groups
360        );
361    }
362}