Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
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 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21use MediaWiki\ResourceLoader\ForeignResourceManager;
22
23require_once __DIR__ . '/Maintenance.php';
24
25/**
26 * Manage foreign resources registered with ResourceLoader.
27 *
28 * This uses the MediaWiki\ResourceLoader\ForeignResourceManager class internally.
29 * See also ForeignResourceStructureTest, which runs the "verify" action in CI.
30 *
31 * @ingroup Maintenance
32 * @ingroup ResourceLoader
33 * @since 1.32
34 * @see https://www.mediawiki.org/wiki/Manual:ManageForeignResources.php
35 */
36class ManageForeignResources extends Maintenance {
37    public function __construct() {
38        parent::__construct();
39        $this->addDescription( <<<TEXT
40Manage foreign resources registered with ResourceLoader.
41
42This helps developers with downloading, verifying, and updating local copies of
43upstream libraries registered as ResourceLoader modules. See
44https://www.mediawiki.org/wiki/Foreign_resources
45
46Use the "update" action to download urls specified in foreign-resources.yaml,
47and unpack them to the resources directory. This will also verify them against
48the integrity hashes.
49
50Use the "verify" action to verify the files currently in the resources directory
51match what "update" would replace them with. This is effectively a dry-run and
52will not change any module resources on disk.
53
54Use the "make-sri" action to compute an integrity hash for upstreams that do not
55publish one themselves. Add or update the urls foreign-resources.yaml as needed,
56but omit (or leave empty) the "integrity" key. Then, run the "make-sri" action
57for the module and copy the integrity into the file. Then, you can use "verify"
58or "update" normally.
59TEXT
60        );
61        $this->addArg( 'action', 'One of "update", "verify" or "make-sri"', true );
62        $this->addArg( 'module', 'Name of a single module (Default: all)', false );
63        $this->addOption( 'extension', 'Manage foreign resources for the given extension, instead of core',
64            false, true );
65        $this->addOption( 'skin', 'Manage foreign resources for the given skin, instead of core',
66            false, true );
67        $this->addOption( 'verbose', 'Be verbose', false, false, 'v' );
68    }
69
70    /**
71     * @return bool
72     */
73    public function execute() {
74        global $IP;
75
76        $component = $this->getOption( 'extension' ) ?? $this->getOption( 'skin' ) ?? '#core';
77        $foreignResourcesDirs = ExtensionRegistry::getInstance()->getAttribute( 'ForeignResourcesDir' )
78            + [ '#core' => "{$IP}/resources/lib" ];
79        if ( !array_key_exists( $component, $foreignResourcesDirs ) ) {
80            $this->fatalError( "Unknown component: $component\n" );
81        }
82        $foreignResourcesFile = "{$foreignResourcesDirs[$component]}/foreign-resources.yaml";
83
84        $frm = new ForeignResourceManager(
85            $foreignResourcesFile,
86            dirname( $foreignResourcesFile ),
87            function ( $text ) {
88                $this->output( $text );
89            },
90            function ( $text ) {
91                $this->error( $text );
92            },
93            function ( $text ) {
94                if ( $this->hasOption( 'verbose' ) ) {
95                    $this->output( $text );
96                }
97            }
98        );
99
100        $action = $this->getArg( 0 );
101        $module = $this->getArg( 1, 'all' );
102
103        try {
104            return $frm->run( $action, $module );
105        } catch ( Exception $e ) {
106            $this->fatalError( "Error: {$e->getMessage()}" );
107        }
108    }
109}
110
111$maintClass = ManageForeignResources::class;
112require_once RUN_MAINTENANCE_IF_MAIN;