| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Registration\ExtensionRegistry; |
| 22 |
use MediaWiki\ResourceLoader\ForeignResourceManager; |
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ManageForeignResources extends Maintenance { |
| 40 |
public function __construct() { |
| 41 |
parent::__construct(); |
| 42 |
$this->addDescription( <<<TEXT |
| 43 |
Manage foreign resources registered with ResourceLoader. |
| 44 |
|
| 45 |
This helps developers with downloading, verifying, and updating local copies of |
| 46 |
upstream libraries registered as ResourceLoader modules. See |
| 47 |
https://www.mediawiki.org/wiki/Foreign_resources |
| 48 |
|
| 49 |
Use the "update" action to download urls specified in foreign-resources.yaml, |
| 50 |
and unpack them to the resources directory. This will also verify them against |
| 51 |
the integrity hashes. |
| 52 |
|
| 53 |
Use the "verify" action to verify the files currently in the resources directory |
| 54 |
match what "update" would replace them with. This is effectively a dry-run and |
| 55 |
will not change any module resources on disk. |
| 56 |
|
| 57 |
Use the "make-sri" action to compute an integrity hash for upstreams that do not |
| 58 |
publish one themselves. Add or update the urls foreign-resources.yaml as needed, |
| 59 |
but omit (or leave empty) the "integrity" key. Then, run the "make-sri" action |
| 60 |
for the module and copy the integrity into the file. Then, you can use "verify" |
| 61 |
or "update" normally. |
| 62 |
|
| 63 |
The "make-cdx" option generates a CycloneDX SBOM file. |
| 64 |
TEXT |
| 65 |
); |
| 66 |
$this->addArg( 'action', 'One of "update", "verify", "make-sri" or "make-cdx"', true ); |
| 67 |
$this->addArg( 'module', 'Name of a single module (Default: all)', false ); |
| 68 |
$this->addOption( 'extension', 'Manage foreign resources for the given extension, instead of core', |
| 69 |
false, true ); |
| 70 |
$this->addOption( 'skin', 'Manage foreign resources for the given skin, instead of core', |
| 71 |
false, true ); |
| 72 |
$this->addOption( 'verbose', 'Be verbose', false, false, 'v' ); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function execute() { |
| 79 |
global $IP; |
| 80 |
|
| 81 |
$component = $this->getOption( 'extension' ) ?? $this->getOption( 'skin' ) ?? '#core'; |
| 82 |
$foreignResourcesDirs = ExtensionRegistry::getInstance()->getAttribute( 'ForeignResourcesDir' ) |
| 83 |
+ [ '#core' => "{$IP}/resources/lib" ]; |
| 84 |
if ( !array_key_exists( $component, $foreignResourcesDirs ) ) { |
| 85 |
$this->fatalError( "Unknown component: $component\n" ); |
| 86 |
} |
| 87 |
$foreignResourcesFile = "{$foreignResourcesDirs[$component]}/foreign-resources.yaml"; |
| 88 |
|
| 89 |
$frm = new ForeignResourceManager( |
| 90 |
$foreignResourcesFile, |
| 91 |
dirname( $foreignResourcesFile ), |
| 92 |
function ( $text ) { |
| 93 |
$this->output( $text ); |
| 94 |
}, |
| 95 |
function ( $text ) { |
| 96 |
$this->error( $text ); |
| 97 |
}, |
| 98 |
function ( $text ) { |
| 99 |
if ( $this->hasOption( 'verbose' ) ) { |
| 100 |
$this->output( $text ); |
| 101 |
} |
| 102 |
} |
| 103 |
); |
| 104 |
|
| 105 |
$action = $this->getArg( 0 ); |
| 106 |
$module = $this->getArg( 1, 'all' ); |
| 107 |
|
| 108 |
try { |
| 109 |
return $frm->run( $action, $module ); |
| 110 |
} catch ( Exception $e ) { |
| 111 |
$this->fatalError( "Error: {$e->getMessage()}" ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
$maintClass = ManageForeignResources::class; |
| 118 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 119 |
|