Files
karatewiki/scripts/stream-linked-images.php
T
2026-10-24 19:08:22 +02:00

78 lines
2.6 KiB
PHP

<?php
/**
* Wird per SSH (stdin) auf dem Strato-Server ausgefuehrt (100% read-only).
* Ermittelt aus der Datenbank alle tatsaechlich in Wiki-Seiten eingebundenen Dateien
* (Karate_imagelinks + <html5media>-Tags = 566 Dateien) und streamt exakt diese
* als unkomprimiertes tar-Archiv nach stdout.
*/
$c = file_get_contents( 'Karate_Wiki_135/LocalSettings.php' );
if ( !preg_match( '/\$wgDBpassword\s*=\s*"([^"]+)"/', $c, $m ) ) {
fwrite( STDERR, "Konnte wgDBpassword nicht lesen.\n" );
exit( 1 );
}
$db = new mysqli( 'rdbms.strato.de', 'dbu1061679', $m[1], 'dbs1754262' );
if ( $db->connect_error ) {
fwrite( STDERR, "DB-Verbindungsfehler: {$db->connect_error}\n" );
exit( 1 );
}
$usedNames = [];
// 1. Alle <html5media>-Einbindungen aus den aktuellen Seitentexten sammeln
$sqlPages = "
SELECT t.old_text
FROM Karate_page p
JOIN Karate_revision r ON p.page_latest = r.rev_id
LEFT JOIN Karate_slots s ON r.rev_id = s.slot_revision_id
LEFT JOIN Karate_content co ON s.slot_content_id = co.content_id
LEFT JOIN Karate_text t ON CAST(SUBSTRING(co.content_address, 4) AS UNSIGNED) = t.old_id
";
$res = $db->query( $sqlPages );
while ( $row = $res->fetch_assoc() ) {
$txt = $row['old_text'] ?? '';
if ( preg_match_all( '/<html5media[^>]*>\s*(?:File:|Datei:|Image:|Bild:)?\s*([^<\r\n]+?)\s*<\/html5media>/i', $txt, $matches ) ) {
foreach ( $matches[1] as $fn ) {
$norm = str_replace( ' ', '_', ucfirst( trim( $fn ) ) );
$usedNames[$norm] = true;
}
}
}
// 2. Alle [[Datei:...]] / [[File:...]] Einbindungen aus Karate_imagelinks sammeln
$resLinks = $db->query( "SELECT DISTINCT il_to FROM Karate_imagelinks" );
while ( $row = $resLinks->fetch_assoc() ) {
$usedNames[$row['il_to']] = true;
}
// 3. Existierende Dateien im images/-Ordner ueber ihren MediaWiki-MD5-Pfad aufloesen
$baseDir = 'Karate_Wiki_135/images';
$filesToStream = [];
if ( file_exists( "$baseDir/.htaccess" ) ) {
$filesToStream[] = '.htaccess';
}
$resImg = $db->query( "SELECT img_name FROM Karate_image" );
while ( $row = $resImg->fetch_assoc() ) {
$name = $row['img_name'];
if ( !isset( $usedNames[$name] ) ) {
continue;
}
$hash = md5( $name );
$relPath = substr( $hash, 0, 1 ) . '/' . substr( $hash, 0, 2 ) . '/' . $name;
if ( file_exists( "$baseDir/$relPath" ) ) {
$filesToStream[] = $relPath;
}
}
fwrite( STDERR, "Streame " . count( $filesToStream ) . " verdrahtete Mediendateien von Strato...\n" );
$tmpList = tempnam( sys_get_temp_dir(), 'mw_files_' );
file_put_contents( $tmpList, implode( "\n", $filesToStream ) . "\n" );
$cmd = sprintf( 'tar -cf - -C %s -T %s', escapeshellarg( $baseDir ), escapeshellarg( $tmpList ) );
passthru( $cmd, $ret );
@unlink( $tmpList );
exit( $ret );