Created
April 4, 2024 00:50
-
-
Save Krinkle/6be1955eebe568de3d1c1707256ac4b3 to your computer and use it in GitHub Desktop.
MediaWiki ResourceLoader startup diff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$startupUrls = [ | |
'enwiki-canonical' => 'https://en.wikipedia.org/w/load.php?lang=en&modules=startup&only=scripts&raw=1&skin=vector', | |
'enwiki-mobile' => 'https://en.m.wikipedia.org/w/load.php?lang=en&modules=startup&only=scripts&raw=1&skin=minerva&target=mobile', | |
]; | |
define( 'STARTUPREG_JSON_START', 'mw.loader.register([' ); | |
define( 'STARTUPREG_JSON_START_OFFSET', 'mw.loader.register(' ); | |
define( 'STARTUPREG_JSON_END', ']);' ); | |
define( 'STARTUPREG_JSON_END_OFFSET', ']' ); | |
define( 'STARTUPREG_CACHE_PREFIX', __DIR__ . '/startup-reg-cache-' ); | |
function httpGet( string $url ): string { | |
ini_set( | |
'user_agent', | |
'rl-startup-reg-diff.php (Author: <https://meta.wikimedia.org/wiki/User:Krinkle>)' | |
); | |
$result = file_get_contents( $url ); | |
if ( !$result ) { | |
throw new Exception( 'HTTP request failed' ); | |
} | |
return $result; | |
} | |
function getModuleVersions( string $label, string $url ): array { | |
$js = httpGet( $url ); | |
$pos = strpos( $js, STARTUPREG_JSON_START ); | |
if ( $pos === false ) { | |
throw new Exception( 'Startup response did not contain STARTUPREG_JSON_START' ); | |
} | |
$js = substr( $js, $pos + strlen( STARTUPREG_JSON_START_OFFSET ) ); | |
$pos = strpos( $js, STARTUPREG_JSON_END ); | |
if ( $pos === false ) { | |
throw new Exception( 'Startup response did not contain STARTUPREG_JSON_END' ); | |
} | |
$js = substr( $js, 0, $pos + strlen( STARTUPREG_JSON_END_OFFSET ) ); | |
// echo $js; | |
$data = json_decode( $js, true, 512, JSON_THROW_ON_ERROR ); | |
// var_dump( $data ); | |
$versions = []; | |
foreach ( $data as $module ) { | |
list( $name, $version ) = $module; | |
$versions[$name] = $version; | |
} | |
return $versions; | |
} | |
foreach ( $startupUrls as $label => $url ) { | |
$cacheFile = STARTUPREG_CACHE_PREFIX . $label . '.json'; | |
$oldVersions = @file_get_contents( $cacheFile ); | |
$oldVersions = $oldVersions ? json_decode( $oldVersions, true ) : false; | |
$versions = getModuleVersions( $label, $url ); | |
if ( $oldVersions ) { | |
foreach ( $versions as $module => $newVersion ) { | |
$oldVersion = $oldVersions[$module] ?? null; | |
if ( !$oldVersion ) { | |
echo "[$label] New module: $module\n"; | |
continue; | |
} | |
if ( !$newVersion ) { | |
echo "[$label] Removed module: $module\n"; | |
continue; | |
} | |
if ( $oldVersion !== $newVersion ) { | |
echo "[$label] Changed module: $module `$oldVersion` => `$newVersion`\n"; | |
continue; | |
} | |
} | |
} | |
file_put_contents( $cacheFile, json_encode( $versions, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment