You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.1 KiB
PHP

<?php
namespace Isekai\Widgets;
use MediaWiki\Title\Title;
use MediaWiki\Parser\Parser;
use PPFrame;
use MediaWiki\MediaWikiServices;
class FontFaceWidget {
/**
* @param string $text
* @param array $params
* @param \Parser $parser
* @param \PPFrame $frame
* @return string|string[]
*/
public static function create($text, $params, Parser $parser, PPFrame $frame) {
if (empty($params['src']) || empty($params['name'])) {
return '<span class="error">' . wfMessage('isekai-fontface-error-invalid-params')->parse() . '</span>';
}
$service = MediaWikiServices::getInstance();
/** @var \MediaWiki\Parser\ParserOutput */
$out = $parser->getOutput();
$fontName = 'extra-' . $params['name'];
$existsFonts = $out->getExtensionData('IsekaiWidgets.extraFonts');
if (!$existsFonts) {
$existsFonts = [];
}
if (isset($existsFonts[$fontName])) {
return '<span class="error">' .
wfMessage('isekai-fontface-error-font-already-defined', $params['name'])->parse() .
'</span>';
}
if (preg_match('/[`~!@#$%^&*()+=<>?:"{}|,.\/;\'\\\\\[\]]\r\n/', $fontName)) {
return '<span class="error">' .
wfMessage('isekai-fontface-error-font-name-invalid')->parse() .
'</span>';
}
$title = Title::newFromText($params['src'], NS_FILE);
$file = $service->getRepoGroup()->findFile($title);
if (!$file) {
return '<span class="error">' .
wfMessage('isekai-fontface-error-font-not-exists', $params['src'])->parse() .
'</span>';
}
$fontUrl = $file->getUrl();
$fontId = substr(Utils::safeBase64Encode(md5($fontName, true)), 0, 8);
$css = "<span><style>@font-face{src: url('{$fontUrl}');font-family:'{$fontName}'}" .
".isekai-extra-font.font-{$fontId}{font-family:'{$fontName}'}</style></span>";
$existsFonts[$fontName] = $fontId;
$out->setExtensionData('IsekaiWidgets.extraFonts', $existsFonts);
return [$css, "markerType" => 'nowiki'];
}
}