Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ManageForeignResources
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7use MediaWiki\Maintenance\Maintenance;
8use MediaWiki\Registration\ExtensionRegistry;
9use MediaWiki\ResourceLoader\ForeignResourceManager;
10
11// @codeCoverageIgnoreStart
12require_once __DIR__ . '/Maintenance.php';
13// @codeCoverageIgnoreEnd
14
15/**
16 * Manage foreign resources registered with ResourceLoader.
17 *
18 * This uses the MediaWiki\ResourceLoader\ForeignResourceManager class internally.
19 * See also ForeignResourceStructureTest, which runs the "verify" action in CI.
20 *
21 * @ingroup Maintenance
22 * @ingroup ResourceLoader
23 * @since 1.32
24 * @see https://www.mediawiki.org/wiki/Manual:ManageForeignResources.php
25 */
26class ManageForeignResources extends Maintenance {
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
62    /**
63     * @return bool
64     */
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            function ( $text ) {
80                $this->output( $text );
81            },
82            function ( $text ) {
83                $this->error( $text );
84            },
85            function ( $text ) {
86                if ( $this->hasOption( 'verbose' ) ) {
87                    $this->output( $text );
88                }
89            }
90        );
91
92        $action = $this->getArg( 0 );
93        $module = $this->getArg( 1, 'all' );
94
95        try {
96            return $frm->run( $action, $module );
97        } catch ( Exception $e ) {
98            $this->fatalError( "Error: {$e->getMessage()}" );
99        }
100    }
101}
102
103// @codeCoverageIgnoreStart
104$maintClass = ManageForeignResources::class;
105require_once RUN_MAINTENANCE_IF_MAIN;
106// @codeCoverageIgnoreEnd