Page MenuHomePhabricator
Paste P9485

RL hashed module names test script
ActivePublic

Authored by hoo on Sun, Oct 27, 8:33 AM.
#!/bin/env php
<?php
$fileName = $argv[1];
$origStartup = file_get_contents( $fileName );
$start = 'mw.loader.register([';
$end = ']);';
$moduleSectionStart = strpos( $origStartup, $start ) + strlen( $start ) - 1;
$moduleSectionEnd = strpos( $origStartup, $end, $moduleSectionStart + 1 ) + 1;
$preModuleSection = substr( $origStartup, 0, $moduleSectionStart );
$moduleSection = substr( $origStartup, $moduleSectionStart, ( $moduleSectionEnd - $moduleSectionStart ) );
$postModuleSection = substr( $origStartup, $moduleSectionEnd );
$modules = json_decode( $moduleSection );
$moduleSection = json_encode( $modules );
$hashedModules = [];
foreach ( $modules as $module ) {
$hashedModules[] = [ substr( md5( $module[0] ), 0, 5), array_slice( $module, 1 ) ];
}
$hashedModuleSection = json_encode( $hashedModules );
file_put_contents( $fileName . '-no-new-lines', $preModuleSection . $moduleSection . $postModuleSection );
file_put_contents( $fileName . '-no-new-lines-hashed-module-names', $preModuleSection . $hashedModuleSection . $postModuleSection );