MediaWiki master
manageForeignResources.php
Go to the documentation of this file.
1<?php
10
11// @codeCoverageIgnoreStart
12require_once __DIR__ . '/Maintenance.php';
13// @codeCoverageIgnoreEnd
14
27 public function __construct() {
28 parent::__construct();
29 $this->addDescription( <<<TEXT
30Manage foreign resources registered with ResourceLoader.
31
32This helps developers with downloading, verifying, and updating local copies of
33upstream libraries registered as ResourceLoader modules. See
34https://www.mediawiki.org/wiki/Foreign_resources
35
36Use the "update" action to download urls specified in foreign-resources.yaml,
37and unpack them to the resources directory. This will also verify them against
38the integrity hashes.
39
40Use the "verify" action to verify the files currently in the resources directory
41match what "update" would replace them with. This is effectively a dry-run and
42will not change any module resources on disk.
43
44Use the "make-sri" action to compute an integrity hash for upstreams that do not
45publish one themselves. Add or update the urls foreign-resources.yaml as needed,
46but omit (or leave empty) the "integrity" key. Then, run the "make-sri" action
47for the module and copy the integrity into the file. Then, you can use "verify"
48or "update" normally.
49
50The "make-cdx" option generates a CycloneDX SBOM file.
51TEXT
52 );
53 $this->addArg( 'action', 'One of "update", "verify", "make-sri" or "make-cdx"', true );
54 $this->addArg( 'module', 'Name of a single module (Default: all)', false );
55 $this->addOption( 'extension', 'Manage foreign resources for the given extension, instead of core',
56 false, true );
57 $this->addOption( 'skin', 'Manage foreign resources for the given skin, instead of core',
58 false, true );
59 $this->addOption( 'verbose', 'Be verbose', false, false, 'v' );
60 }
61
65 public function execute() {
66 global $IP;
67
68 $component = $this->getOption( 'extension' ) ?? $this->getOption( 'skin' ) ?? '#core';
69 $foreignResourcesDirs = ExtensionRegistry::getInstance()->getAttribute( 'ForeignResourcesDir' )
70 + [ '#core' => "{$IP}/resources/lib" ];
71 if ( !array_key_exists( $component, $foreignResourcesDirs ) ) {
72 $this->fatalError( "Unknown component: $component\n" );
73 }
74 $foreignResourcesFile = "{$foreignResourcesDirs[$component]}/foreign-resources.yaml";
75
76 $frm = new ForeignResourceManager(
77 $foreignResourcesFile,
78 dirname( $foreignResourcesFile ),
79 $this->output( ... ),
80 $this->error( ... ),
81 function ( $text ) {
82 if ( $this->hasOption( 'verbose' ) ) {
83 $this->output( $text );
84 }
85 }
86 );
87
88 $action = $this->getArg( 0 );
89 $module = $this->getArg( 1, 'all' );
90
91 try {
92 return $frm->run( $action, $module );
93 } catch ( Exception $e ) {
94 $this->fatalError( "Error: {$e->getMessage()}" );
95 }
96 }
97}
98
99// @codeCoverageIgnoreStart
100$maintClass = ManageForeignResources::class;
101require_once RUN_MAINTENANCE_IF_MAIN;
102// @codeCoverageIgnoreEnd
if(!defined('MEDIAWIKI')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:90
Manage foreign resources registered with ResourceLoader.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
addDescription( $text)
Set the description text.
Load JSON files, and uses a Processor to extract information.
Manage foreign resources registered with ResourceLoader.