| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace MediaWiki\Skin; |
| 20 |
|
| 21 |
use MediaWiki\Html\Html; |
| 22 |
use MediaWiki\Linker\Linker; |
| 23 |
use MediaWiki\Message\Message; |
| 24 |
use MessageLocalizer; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class SkinComponentLink implements SkinComponent { |
| 31 |
|
| 32 |
private $key; |
| 33 |
|
| 34 |
private $item; |
| 35 |
|
| 36 |
private $options; |
| 37 |
|
| 38 |
private $localizer; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct( string $key, array $item, MessageLocalizer $localizer, array $options = [] ) { |
| 47 |
$this->key = $key; |
| 48 |
$this->item = $item; |
| 49 |
$this->localizer = $localizer; |
| 50 |
$this->options = $options; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private function msg( string $key ): Message { |
| 58 |
return $this->localizer->msg( $key ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private function makeLink( $key, $item, $options = [] ) { |
| 129 |
$html = $item['html'] ?? null; |
| 130 |
$icon = $item['icon'] ?? null; |
| 131 |
if ( $html ) { |
| 132 |
return [ |
| 133 |
'html' => $html |
| 134 |
]; |
| 135 |
} |
| 136 |
$text = $item['text'] ?? $this->msg( $item['msg'] ?? $key )->text(); |
| 137 |
|
| 138 |
$html = htmlspecialchars( $text ); |
| 139 |
$isLink = isset( $item['href'] ) || isset( $options['link-fallback'] ); |
| 140 |
|
| 141 |
if ( $html !== '' && isset( $options['text-wrapper'] ) ) { |
| 142 |
$wrapper = $options['text-wrapper']; |
| 143 |
if ( isset( $wrapper['tag'] ) ) { |
| 144 |
$wrapper = [ $wrapper ]; |
| 145 |
} |
| 146 |
while ( count( $wrapper ) > 0 ) { |
| 147 |
$element = array_pop( $wrapper ); |
| 148 |
'@phan-var array $element'; |
| 149 |
|
| 150 |
$attrs = $element['attributes'] ?? []; |
| 151 |
|
| 152 |
|
| 153 |
if ( count( $wrapper ) === 0 && !$isLink ) { |
| 154 |
$this->applyLinkTitleAttribs( |
| 155 |
$item, |
| 156 |
false, |
| 157 |
$attrs |
| 158 |
); |
| 159 |
} |
| 160 |
$html = Html::rawElement( $element['tag'], $attrs, $html ); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
$attrs = []; |
| 165 |
$linkHtmlAttributes = []; |
| 166 |
$classAsProperty = $options['class-as-property'] ?? false; |
| 167 |
if ( $isLink ) { |
| 168 |
$attrs = $item; |
| 169 |
foreach ( [ |
| 170 |
'single-id', 'text', 'msg', 'tooltiponly', 'context', 'primary', |
| 171 |
|
| 172 |
|
| 173 |
'icon', 'button', |
| 174 |
'tooltip-params', 'exists', 'link-html' ] as $k |
| 175 |
) { |
| 176 |
unset( $attrs[$k] ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( isset( $attrs['data'] ) ) { |
| 180 |
foreach ( $attrs['data'] as $key => $value ) { |
| 181 |
if ( $value === null ) { |
| 182 |
continue; |
| 183 |
} |
| 184 |
$attrs[ 'data-' . $key ] = $value; |
| 185 |
} |
| 186 |
unset( $attrs[ 'data' ] ); |
| 187 |
} |
| 188 |
$this->applyLinkTitleAttribs( $item, true, $attrs ); |
| 189 |
$class = $attrs['class'] ?? []; |
| 190 |
if ( isset( $options['link-class'] ) ) { |
| 191 |
$class = SkinComponentUtils::addClassToClassList( |
| 192 |
$class, $options['link-class'] |
| 193 |
); |
| 194 |
} |
| 195 |
$attrs['class'] = is_array( $class ) ? implode( ' ', $class ) : $class; |
| 196 |
foreach ( $attrs as $key => $value ) { |
| 197 |
if ( $value === null ) { |
| 198 |
continue; |
| 199 |
} |
| 200 |
if ( $classAsProperty && $key === 'class' ) { |
| 201 |
continue; |
| 202 |
} |
| 203 |
$linkHtmlAttributes[] = [ 'key' => $key, 'value' => $value ]; |
| 204 |
} |
| 205 |
|
| 206 |
if ( isset( $item['link-html'] ) ) { |
| 207 |
$html = $item['link-html'] . ' ' . $html; |
| 208 |
} |
| 209 |
|
| 210 |
$html = Html::rawElement( isset( $attrs['href'] ) |
| 211 |
? 'a' |
| 212 |
: $options['link-fallback'], $attrs, $html ); |
| 213 |
} |
| 214 |
$data = [ |
| 215 |
'html' => $html, |
| 216 |
'icon' => $icon, |
| 217 |
'array-attributes' => count( $linkHtmlAttributes ) > 0 ? $linkHtmlAttributes : null, |
| 218 |
'text' => trim( $text ), |
| 219 |
]; |
| 220 |
if ( $classAsProperty ) { |
| 221 |
$data['class'] = $attrs['class'] ?? ''; |
| 222 |
} |
| 223 |
return $data; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
private function applyLinkTitleAttribs( $item, $allowAccessKey, &$attrs ) { |
| 235 |
$tooltipId = $item['single-id'] ?? $item['id'] ?? null; |
| 236 |
if ( $tooltipId === null ) { |
| 237 |
return; |
| 238 |
} |
| 239 |
$tooltipParams = $item['tooltip-params'] ?? []; |
| 240 |
$tooltipOption = isset( $item['exists'] ) && $item['exists'] === false ? 'nonexisting' : null; |
| 241 |
|
| 242 |
if ( !$allowAccessKey || !empty( $item['tooltiponly'] ) ) { |
| 243 |
$title = Linker::titleAttrib( $tooltipId, $tooltipOption, $tooltipParams ); |
| 244 |
if ( $title !== false ) { |
| 245 |
$attrs['title'] = $title; |
| 246 |
} |
| 247 |
} else { |
| 248 |
$tip = Linker::tooltipAndAccesskeyAttribs( |
| 249 |
$tooltipId, |
| 250 |
$tooltipParams, |
| 251 |
$tooltipOption |
| 252 |
); |
| 253 |
if ( isset( $tip['title'] ) && $tip['title'] !== false ) { |
| 254 |
$attrs['title'] = $tip['title']; |
| 255 |
} |
| 256 |
if ( isset( $tip['accesskey'] ) && $tip['accesskey'] !== false ) { |
| 257 |
$attrs['accesskey'] = $tip['accesskey']; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public function getTemplateData(): array { |
| 266 |
return $this->makeLink( $this->key, $this->item, $this->options ); |
| 267 |
} |
| 268 |
} |