Konvertiere alle <html5media>-Tags im Wikitext auf MediaWiki-Standard [[Datei:...]]
Deploy Development / deploy (push) Successful in 39s
Deploy Development / deploy (push) Successful in 39s
This commit is contained in:
1 parent
86b9bbdb78
commit
5f7e304f28
5 files changed
+155
-2
No files matched your search
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
/**
|
||||
* Konvertiert alle veralteten <html5media ...>File:...</html5media> Tags
|
||||
* im Wikitext in den offiziellen MediaWiki-Standard [[Datei:...|...px]].
|
||||
*
|
||||
* Aufruf:
|
||||
* php maintenance/run.php scripts/migrate-html5media-tags.php [--dry-run]
|
||||
*/
|
||||
|
||||
use MediaWiki\CommentStore\CommentStoreComment;
|
||||
use MediaWiki\Content\ContentHandler;
|
||||
use MediaWiki\Content\TextContent;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Revision\SlotRecord;
|
||||
use MediaWiki\Title\Title;
|
||||
use MediaWiki\User\User;
|
||||
|
||||
require_once __DIR__ . '/../maintenance/Maintenance.php';
|
||||
|
||||
class MigrateHtml5MediaTags extends Maintenance {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->addDescription( 'Ersetzt <html5media>-Tags durch MediaWiki-Standard [[Datei:...]] Syntax.' );
|
||||
$this->addOption( 'dry-run', 'Nur anzeigen, was geaendert wuerde, ohne zu speichern', false, false );
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
$dryRun = $this->hasOption( 'dry-run' );
|
||||
$services = MediaWikiServices::getInstance();
|
||||
$dbr = $services->getConnectionProvider()->getReplicaDatabase();
|
||||
$wikiPageFactory = $services->getWikiPageFactory();
|
||||
$revisionLookup = $services->getRevisionLookup();
|
||||
|
||||
$res = $dbr->newSelectQueryBuilder()
|
||||
->select( [ 'page_id', 'page_namespace', 'page_title' ] )
|
||||
->from( 'page' )
|
||||
->orderBy( 'page_id' )
|
||||
->caller( __METHOD__ )
|
||||
->fetchResultSet();
|
||||
|
||||
$systemUser = User::newSystemUser( 'MediaWiki maintenance', [ 'steal' => true ] );
|
||||
$pagesChanged = 0;
|
||||
$tagsReplacedTotal = 0;
|
||||
|
||||
foreach ( $res as $row ) {
|
||||
$title = Title::newFromRow( $row );
|
||||
$rev = $revisionLookup->getRevisionByTitle( $title );
|
||||
if ( !$rev ) {
|
||||
continue;
|
||||
}
|
||||
$content = $rev->getContent( SlotRecord::MAIN );
|
||||
if ( !$content instanceof TextContent ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$oldText = $content->getText();
|
||||
if ( stripos( $oldText, '<html5media' ) === false ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tagCount = 0;
|
||||
$newText = preg_replace_callback(
|
||||
'/<html5media([^>]*)>(.*?)<\/html5media>/is',
|
||||
function ( array $m ) use ( &$tagCount ): string {
|
||||
$attrString = $m[1];
|
||||
$inner = trim( $m[2] );
|
||||
|
||||
// Entferne vorangestelltes File: / Datei: / Image: / Bild:
|
||||
$cleanFile = preg_replace( '/^(file|datei|image|bild)\s*:\s*/i', '', $inner );
|
||||
$cleanFile = trim( $cleanFile );
|
||||
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";
|
||||
}
|
||||
|
||||
$tagCount++;
|
||||
return "[[Datei:{$cleanFile}{$sizeParam}]]";
|
||||
},
|
||||
$oldText
|
||||
);
|
||||
|
||||
if ( $newText === null || $newText === $oldText || $tagCount === 0 ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$pagesChanged++;
|
||||
$tagsReplacedTotal += $tagCount;
|
||||
|
||||
$this->output( sprintf(
|
||||
"[%s] %s (%d Tag%s)\n",
|
||||
$dryRun ? 'DRY-RUN' : 'UPDATE',
|
||||
$title->getPrefixedText(),
|
||||
$tagCount,
|
||||
$tagCount === 1 ? '' : 's'
|
||||
) );
|
||||
|
||||
if ( !$dryRun ) {
|
||||
$wikiPage = $wikiPageFactory->newFromTitle( $title );
|
||||
$updater = $wikiPage->newPageUpdater( $systemUser );
|
||||
$newContent = ContentHandler::makeContent( $newText, $title );
|
||||
$updater->setContent( SlotRecord::MAIN, $newContent );
|
||||
$comment = CommentStoreComment::newUnsavedComment(
|
||||
'Migration: <html5media> durch MediaWiki-Standard [[Datei:...]] ersetzt'
|
||||
);
|
||||
$updater->saveRevision(
|
||||
$comment,
|
||||
EDIT_UPDATE | EDIT_MINOR | EDIT_FORCE_BOT | EDIT_SUPPRESS_RC
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->output( sprintf(
|
||||
"\nFertig: %d Seiten (%d <html5media>-Tags) %s.\n",
|
||||
$pagesChanged,
|
||||
$tagsReplacedTotal,
|
||||
$dryRun ? 'gefunden (Dry-Run)' : 'erfolgreich auf [[Datei:...]] umgestellt'
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
$maintClass = MigrateHtml5MediaTags::class;
|
||||
require_once RUN_MAINTENANCE_IF_MAIN;
|
||||
Reference in new issue
Block a user