Automatische Umwandlung von <html5media> in [[Datei:...]] beim Speichern (ParserPreSaveTransformComplete)
Deploy Development / deploy (push) Successful in 17s
Deploy Development / deploy (push) Successful in 17s
This commit is contained in:
1 parent
5f7e304f28
commit
802c777744
3 files changed
+51
-1
No files matched your search
@@ -17,6 +17,7 @@
|
||||
"Hooks": {
|
||||
"BeforeInitialize": "MediaWiki\\Extension\\KarateMedia\\KarateMediaHooks::onBeforeInitialize",
|
||||
"ParserFirstCallInit": "MediaWiki\\Extension\\KarateMedia\\KarateMediaHooks::onParserFirstCallInit",
|
||||
"ParserPreSaveTransformComplete": "MediaWiki\\Extension\\KarateMedia\\KarateMediaHooks::onParserPreSaveTransformComplete",
|
||||
"BeforePageDisplay": "MediaWiki\\Extension\\KarateMedia\\KarateMediaHooks::onBeforePageDisplay"
|
||||
},
|
||||
"MediaHandlers": {
|
||||
|
||||
@@ -28,13 +28,61 @@ class KarateMediaHooks {
|
||||
}
|
||||
|
||||
/**
|
||||
* Registriert das <html5media>-Tag fuer Abwaertskompatibilitaet mit bestehenden Wiki-Seiten.
|
||||
* Registriert das <html5media>-Tag fuer Abwaertskompatibilitaet (z.B. in der Vorschau).
|
||||
*/
|
||||
public static function onParserFirstCallInit( $parser ): bool {
|
||||
$parser->setHook( 'html5media', [ self::class, 'renderHtml5MediaTag' ] );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wandelt <html5media>-Tags beim Speichern einer Wiki-Seite automatisch
|
||||
* in den offiziellen MediaWiki-Standard [[Datei:...|...px]] um.
|
||||
*/
|
||||
public static function onParserPreSaveTransformComplete( $parser, string &$text ): void {
|
||||
if ( stripos( $text, '<html5media' ) === false ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$converted = preg_replace_callback(
|
||||
'/<html5media([^>]*)>(.*?)<\/html5media>/is',
|
||||
static function ( array $m ): string {
|
||||
$attrString = $m[1];
|
||||
$inner = trim( $m[2] );
|
||||
$cleanFile = trim( preg_replace( '/^(file|datei|image|bild)\s*:\s*/i', '', $inner ) );
|
||||
if ( $cleanFile === '' ) {
|
||||
return $m[0];
|
||||
}
|
||||
|
||||
$width = null;
|
||||
$height = null;
|
||||
if ( preg_match( '/\bwidth\s*=\s*["\']?(\d+)["\']?/i', $attrString, $wm ) ) {
|
||||
$width = (int)$wm[1];
|
||||
}
|
||||
if ( preg_match( '/\bheight\s*=\s*["\']?(\d+)["\']?/i', $attrString, $hm ) ) {
|
||||
$height = (int)$hm[1];
|
||||
}
|
||||
|
||||
$sizeParam = '';
|
||||
if ( $width && $height ) {
|
||||
$sizeParam = "|{$width}x{$height}px";
|
||||
} elseif ( $width ) {
|
||||
$sizeParam = "|{$width}px";
|
||||
} elseif ( $height ) {
|
||||
$calcWidth = (int)round( $height * 16 / 9 );
|
||||
$sizeParam = "|{$calcWidth}x{$height}px";
|
||||
}
|
||||
|
||||
return "[[Datei:{$cleanFile}{$sizeParam}]]";
|
||||
},
|
||||
$text
|
||||
);
|
||||
|
||||
if ( $converted !== null ) {
|
||||
$text = $converted;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Laedt CSS und JS fuer die Video-Steuerung sowie die gestaltete Vereins-Anmeldeseite.
|
||||
* Fängt auch MediaWikis interne "Special:Badtitle" ("Anmeldung erforderlich") Seite ab
|
||||
|
||||
@@ -139,3 +139,4 @@ class MigrateHtml5MediaTags extends Maintenance {
|
||||
|
||||
$maintClass = MigrateHtml5MediaTags::class;
|
||||
require_once RUN_MAINTENANCE_IF_MAIN;
|
||||
|
||||
Reference in new issue
Block a user