Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 101
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckDependencies
0.00% covered (danger)
0.00%
0 / 98
0.00% covered (danger)
0.00%
0 / 5
1332
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
110
 loadThing
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
156
 addToDependencies
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
72
 formatForHumans
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * (C) 2019 Kunal Mehta <legoktm@debian.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23use MediaWiki\MainConfigNames;
24
25require_once __DIR__ . '/Maintenance.php';
26
27/**
28 * Checks dependencies for extensions, mostly without loading them
29 *
30 * @since 1.34
31 */
32class CheckDependencies extends Maintenance {
33
34    private $checkDev;
35
36    public function __construct() {
37        parent::__construct();
38        $this->addDescription( 'Check dependencies for extensions' );
39        $this->addOption( 'extensions', 'Comma separated list of extensions to check', false, true );
40        $this->addOption( 'skins', 'Comma separated list of skins to check', false, true );
41        $this->addOption( 'json', 'Output in JSON' );
42        $this->addOption( 'dev', 'Check development dependencies too' );
43    }
44
45    public function execute() {
46        $this->checkDev = $this->hasOption( 'dev' );
47        $extensions = $this->hasOption( 'extensions' )
48            ? explode( ',', $this->getOption( 'extensions' ) )
49            : [];
50        $skins = $this->hasOption( 'skins' )
51            ? explode( ',', $this->getOption( 'skins' ) )
52            : [];
53
54        $dependencies = [];
55        // Note that we can only use the main registry if we are
56        // not checking development dependencies.
57        $registry = ExtensionRegistry::getInstance();
58        foreach ( $extensions as $extension ) {
59            if ( !$this->checkDev && $registry->isLoaded( $extension ) ) {
60                // If it's already loaded, we know all the dependencies resolved.
61                $this->addToDependencies( $dependencies, [ $extension ], [] );
62                continue;
63            }
64            $this->loadThing( $dependencies, $extension, [ $extension ], [] );
65        }
66
67        foreach ( $skins as $skin ) {
68            if ( !$this->checkDev && $registry->isLoaded( $skin ) ) {
69                // If it's already loaded, we know all the dependencies resolved.
70                $this->addToDependencies( $dependencies, [], [ $skin ] );
71                continue;
72            }
73            $this->loadThing( $dependencies, $skin, [], [ $skin ] );
74        }
75
76        if ( $this->hasOption( 'json' ) ) {
77            $this->output( json_encode( $dependencies ) . "\n" );
78        } else {
79            $this->output( $this->formatForHumans( $dependencies ) . "\n" );
80        }
81    }
82
83    private function loadThing( &$dependencies, $name, $extensions, $skins ) {
84        $extDir = $this->getConfig()->get( MainConfigNames::ExtensionDirectory );
85        $styleDir = $this->getConfig()->get( MainConfigNames::StyleDirectory );
86        $queue = [];
87        $missing = false;
88        foreach ( $extensions as $extension ) {
89            $path = "$extDir/$extension/extension.json";
90            if ( file_exists( $path ) ) {
91                // 1 is ignored
92                $queue[$path] = 1;
93                $this->addToDependencies( $dependencies, [ $extension ], [], $name );
94            } else {
95                $missing = true;
96                $this->addToDependencies( $dependencies, [ $extension ], [], $name, 'missing' );
97            }
98        }
99
100        foreach ( $skins as $skin ) {
101            $path = "$styleDir/$skin/skin.json";
102            if ( file_exists( $path ) ) {
103                $queue[$path] = 1;
104                $this->addToDependencies( $dependencies, [], [ $skin ], $name );
105            } else {
106                $missing = true;
107                $this->addToDependencies( $dependencies, [], [ $skin ], $name, 'missing' );
108            }
109        }
110
111        if ( $missing ) {
112            // Stuff is missing, give up
113            return;
114        }
115
116        $registry = new ExtensionRegistry();
117        $registry->setCheckDevRequires( $this->checkDev );
118        try {
119            $registry->readFromQueue( $queue );
120        } catch ( ExtensionDependencyError $e ) {
121            $reason = false;
122            if ( $e->incompatibleCore ) {
123                $reason = 'incompatible-core';
124            } elseif ( $e->incompatibleSkins ) {
125                $reason = 'incompatible-skins';
126            } elseif ( $e->incompatibleExtensions ) {
127                $reason = 'incompatible-extensions';
128            } elseif ( $e->missingExtensions || $e->missingSkins ) {
129                // There's an extension missing in the dependency tree,
130                // so add those to the dependency list and try again
131                $this->loadThing(
132                    $dependencies,
133                    $name,
134                    array_merge( $extensions, $e->missingExtensions ),
135                    array_merge( $skins, $e->missingSkins )
136                );
137                return;
138            } else {
139                // missing-phpExtension
140                // missing-ability
141                // XXX: ???
142                $this->fatalError( $e->getMessage() );
143            }
144
145            $this->addToDependencies( $dependencies, $extensions, $skins, $name, $reason, $e->getMessage() );
146        }
147
148        $this->addToDependencies( $dependencies, $extensions, $skins, $name );
149    }
150
151    private function addToDependencies( &$dependencies, $extensions, $skins,
152        $why = null, $status = null, $message = null
153    ) {
154        $mainRegistry = ExtensionRegistry::getInstance();
155        $iter = [ 'extensions' => $extensions, 'skins' => $skins ];
156        foreach ( $iter as $type => $things ) {
157            foreach ( $things as $thing ) {
158                $preStatus = $dependencies[$type][$thing]['status'] ?? false;
159                if ( $preStatus !== 'loaded' ) {
160                    // OK to overwrite
161                    if ( $status ) {
162                        $tStatus = $status;
163                    } else {
164                        $tStatus = $mainRegistry->isLoaded( $thing ) ? 'loaded' : 'present';
165                    }
166                    $dependencies[$type][$thing]['status'] = $tStatus;
167                }
168                if ( $why !== null ) {
169                    $dependencies[$type][$thing]['why'][] = $why;
170                    // XXX: this is a bit messy
171                    $dependencies[$type][$thing]['why'] = array_unique(
172                        $dependencies[$type][$thing]['why'] );
173                }
174
175                if ( $message !== null ) {
176                    $dependencies[$type][$thing]['message'] = trim( $message );
177                }
178
179            }
180        }
181    }
182
183    private function formatForHumans( $dependencies ) {
184        $text = '';
185        foreach ( $dependencies as $type => $things ) {
186            $text .= ucfirst( $type ) . "\n" . str_repeat( '=', strlen( $type ) ) . "\n";
187            foreach ( $things as $thing => $info ) {
188                $why = $info['why'] ?? [];
189                if ( $why ) {
190                    $whyText = '(because: ' . implode( ',', $why ) . ') ';
191                } else {
192                    $whyText = '';
193                }
194                $msg = isset( $info['message'] ) ? "{$info['message']}" : '';
195
196                $text .= "$thing{$info['status']}{$msg} $whyText\n";
197            }
198            $text .= "\n";
199        }
200
201        return trim( $text );
202    }
203}
204
205$maintClass = CheckDependencies::class;
206require_once RUN_MAINTENANCE_IF_MAIN;