| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Skin; |
| 4 |
|
| 5 |
use MediaWiki\Html\Html; |
| 6 |
use MediaWiki\Linker\Linker; |
| 7 |
use MediaWiki\Message\Message; |
| 8 |
use MediaWiki\Parser\Sanitizer; |
| 9 |
use MessageLocalizer; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class SkinComponentMenu implements SkinComponent { |
| 16 |
|
| 17 |
|
| 18 |
private $name; |
| 19 |
|
| 20 |
|
| 21 |
private $items; |
| 22 |
|
| 23 |
|
| 24 |
private $localizer; |
| 25 |
|
| 26 |
|
| 27 |
private $content; |
| 28 |
|
| 29 |
|
| 30 |
private $linkOptions; |
| 31 |
|
| 32 |
|
| 33 |
private $htmlAfterContent; |
| 34 |
|
| 35 |
|
| 36 |
private $htmlBeforeContent; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function __construct( |
| 48 |
string $name, |
| 49 |
array $items, |
| 50 |
MessageLocalizer $localizer, |
| 51 |
string $content = '', |
| 52 |
array $linkOptions = [], |
| 53 |
string $htmlAfterContent = '', |
| 54 |
string $htmlBeforeContent = '' |
| 55 |
) { |
| 56 |
$this->name = $name; |
| 57 |
$this->items = $items; |
| 58 |
$this->localizer = $localizer; |
| 59 |
$this->content = $content; |
| 60 |
$this->linkOptions = $linkOptions; |
| 61 |
$this->htmlAfterContent = $htmlAfterContent; |
| 62 |
$this->htmlBeforeContent = $htmlBeforeContent; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private function msg( string $key ): Message { |
| 71 |
return $this->localizer->msg( $key ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
private function getMenuLabel( $name ) { |
| 80 |
|
| 81 |
|
| 82 |
$mappings = [ |
| 83 |
'tb' => 'toolbox', |
| 84 |
'personal' => 'personaltools', |
| 85 |
'lang' => 'otherlanguages', |
| 86 |
]; |
| 87 |
$msgObj = $this->msg( $mappings[ $name ] ?? $name ); |
| 88 |
|
| 89 |
return $msgObj->exists() ? $msgObj->text() : $name; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function getTemplateData(): array { |
| 96 |
$name = $this->name; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if ( $name === 'actions' ) { |
| 101 |
$name = 'cactions'; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if ( $name === 'user-menu' ) { |
| 108 |
$name = 'personal'; |
| 109 |
} |
| 110 |
|
| 111 |
if ( strpos( $name, 'footer' ) === 0 ) { |
| 112 |
|
| 113 |
$id = $name; |
| 114 |
} else { |
| 115 |
$id = Sanitizer::escapeIdForAttribute( "p-$name" ); |
| 116 |
} |
| 117 |
|
| 118 |
$isEmptyContent = !$this->content; |
| 119 |
$isEmptyAfterContent = !$this->htmlAfterContent; |
| 120 |
$isEmptyBeforeContent = !$this->htmlBeforeContent; |
| 121 |
$isEmptyItems = count( $this->items ) === 0; |
| 122 |
$isEmptyPortlet = ( $isEmptyContent && $isEmptyAfterContent && $isEmptyBeforeContent && $isEmptyItems ); |
| 123 |
$data = [ |
| 124 |
'id' => $id, |
| 125 |
|
| 126 |
'class' => 'mw-portlet ' . Sanitizer::escapeClass( "mw-portlet-$name" ), |
| 127 |
'html-tooltip' => Linker::tooltip( $id ), |
| 128 |
'html-items' => '', |
| 129 |
|
| 130 |
'html-after-portal' => '', |
| 131 |
'html-before-portal' => '', |
| 132 |
'is-empty' => $isEmptyPortlet, |
| 133 |
]; |
| 134 |
|
| 135 |
|
| 136 |
if ( !$isEmptyAfterContent ) { |
| 137 |
$data['html-after-portal'] = Html::rawElement( |
| 138 |
'div', |
| 139 |
[ |
| 140 |
'class' => [ |
| 141 |
'after-portlet', |
| 142 |
Sanitizer::escapeClass( "after-portlet-$name" ), |
| 143 |
], |
| 144 |
], |
| 145 |
$this->htmlAfterContent |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
if ( !$isEmptyBeforeContent ) { |
| 150 |
$data['html-before-portal'] = Html::rawElement( |
| 151 |
'div', |
| 152 |
[ |
| 153 |
'class' => [ |
| 154 |
'before-portlet', |
| 155 |
Sanitizer::escapeClass( "before-portlet-$name" ), |
| 156 |
], |
| 157 |
], |
| 158 |
$this->htmlBeforeContent |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
$html = ''; |
| 163 |
$arrayItems = []; |
| 164 |
foreach ( $this->items as $key => $item ) { |
| 165 |
$item = new SkinComponentListItem( $key, $item, $this->localizer, [], $this->linkOptions ); |
| 166 |
$itemData = $item->getTemplateData(); |
| 167 |
$html .= $itemData['html-item']; |
| 168 |
$arrayItems[] = $itemData; |
| 169 |
} |
| 170 |
$data['html-items'] = $html; |
| 171 |
$data['array-items'] = $arrayItems; |
| 172 |
|
| 173 |
$data['label'] = $this->getMenuLabel( $name ); |
| 174 |
$data['class'] .= $isEmptyPortlet ? ' emptyPortlet' : ''; |
| 175 |
return $data; |
| 176 |
} |
| 177 |
} |