Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
DefaultOverflowBuilder
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 6
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMessageLocalizer
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isAllowed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroup
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
 build
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
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\Minerva\Menu\PageActions;
22
23use MediaWiki\Minerva\Menu\Entries\IMenuEntry;
24use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry;
25use MediaWiki\Minerva\Menu\Group;
26use MediaWiki\Minerva\Permissions\IMinervaPagePermissions;
27use MediaWiki\Title\Title;
28use MessageLocalizer;
29
30class DefaultOverflowBuilder implements IOverflowBuilder {
31
32    private Title $title;
33    private MessageLocalizer $messageLocalizer;
34    private IMinervaPagePermissions $permissions;
35
36    /**
37     * Initialize Default overflow menu Group
38     *
39     * @param Title $title
40     * @param MessageLocalizer $messageLocalizer
41     * @param IMinervaPagePermissions $permissions Minerva permissions system
42     */
43    public function __construct(
44        Title $title,
45        MessageLocalizer $messageLocalizer,
46        IMinervaPagePermissions $permissions
47    ) {
48        $this->title = $title;
49        $this->messageLocalizer = $messageLocalizer;
50        $this->permissions = $permissions;
51    }
52
53    public function getTitle(): Title {
54        return $this->title;
55    }
56
57    public function getMessageLocalizer(): MessageLocalizer {
58        return $this->messageLocalizer;
59    }
60
61    public function isAllowed( string $permission ): bool {
62        return $this->permissions->isAllowed( $permission );
63    }
64
65    /**
66     * @inheritDoc
67     */
68    public function getGroup( array $toolbox, array $actions ): Group {
69        $group = new Group( 'p-tb' );
70
71        $override = $this->isAllowed( IMinervaPagePermissions::EDIT_OR_CREATE ) ? [
72            'editfull' => [
73                'icon' => 'edit',
74                'text' => $this->messageLocalizer->msg( 'minerva-page-actions-editfull' ),
75                'href' => $this->title->getLocalURL( [ 'action' => 'edit', 'section' => 'all' ] ),
76                'class' => 'edit-link',
77            ],
78        ] : [];
79        // watch icon appears in page actions rather than here.
80        $combinedMenu = array_merge( $toolbox, $override, $actions );
81        unset( $combinedMenu[ 'watch' ] );
82        unset( $combinedMenu[ 'unwatch' ] );
83        foreach ( $combinedMenu as $key => $definition ) {
84            $icon = $definition['icon'] ?? null;
85            // Only menu items with icons can be displayed here.
86            if ( $icon ) {
87                $entry = $this->build( $key, $icon, $key, $combinedMenu );
88                if ( $entry ) {
89                    $group->insertEntry( $entry );
90                }
91            }
92        }
93        return $group;
94    }
95
96    /**
97     * Build the single menu entry
98     *
99     * @param string $name
100     * @param string $icon WikimediaUI icon name.
101     * @param string $toolboxIdx
102     * @param array $toolbox An array of common toolbox items from the sidebar menu
103     * @return IMenuEntry|null
104     */
105    private function build( $name, $icon, $toolboxIdx, array $toolbox ): ?IMenuEntry {
106        $href = $toolbox[$toolboxIdx]['href'] ?? null;
107        $originalMsg = $toolbox[$toolboxIdx]['text'] ??
108            $this->messageLocalizer->msg( $toolboxIdx )->text();
109
110        $entry = new SingleMenuEntry(
111            'page-actions-overflow-' . $name,
112            $originalMsg,
113            $href,
114            $toolbox[$name]['class'] ?? false
115        );
116
117        return $href ? $entry->setIcon( $icon )->trackClicks( $name ) : null;
118    }
119}