3 * (C) 2019 Kunal Mehta <legoktm@member.fsf.org>
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.
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.
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
23 require_once __DIR__
. '/Maintenance.php';
26 * Checks dependencies for extensions, mostly without loading them
30 class CheckDependencies
extends Maintenance
{
34 public function __construct() {
35 parent
::__construct();
36 $this->addDescription( 'Check dependencies for extensions' );
37 $this->addOption( 'extensions', 'Comma separated list of extensions to check', false, true );
38 $this->addOption( 'skins', 'Comma separated list of skins to check', false, true );
39 $this->addOption( 'json', 'Output in JSON' );
40 $this->addOption( 'dev', 'Check development dependencies too' );
43 public function execute() {
44 $this->checkDev
= $this->hasOption( 'dev' );
45 $extensions = $this->hasOption( 'extensions' )
46 ?
explode( ',', $this->getOption( 'extensions' ) )
48 $skins = $this->hasOption( 'skins' )
49 ?
explode( ',', $this->getOption( 'skins' ) )
53 // Note that we can only use the main registry if we are
54 // not checking development dependencies.
55 $registry = ExtensionRegistry
::getInstance();
56 foreach ( $extensions as $extension ) {
57 if ( !$this->checkDev
&& $registry->isLoaded( $extension ) ) {
58 // If it's already loaded, we know all the dependencies resolved.
59 $this->addToDependencies( $dependencies, [ $extension ], [] );
62 $this->loadThing( $dependencies, $extension, [ $extension ], [] );
65 foreach ( $skins as $skin ) {
66 if ( !$this->checkDev
&& $registry->isLoaded( $skin ) ) {
67 // If it's already loaded, we know all the dependencies resolved.
68 $this->addToDependencies( $dependencies, [], [ $skin ] );
71 $this->loadThing( $dependencies, $skin, [], [ $skin ] );
74 if ( $this->hasOption( 'json' ) ) {
75 $this->output( json_encode( $dependencies ) . "\n" );
77 $this->output( $this->formatForHumans( $dependencies ) . "\n" );
81 private function loadThing( &$dependencies, $name, $extensions, $skins ) {
82 global $wgExtensionDirectory, $wgStyleDirectory;
85 foreach ( $extensions as $extension ) {
86 $path = "$wgExtensionDirectory/$extension/extension.json";
87 if ( file_exists( $path ) ) {
90 $this->addToDependencies( $dependencies, [ $extension ], [], $name );
93 $this->addToDependencies( $dependencies, [ $extension ], [], $name, 'missing' );
97 foreach ( $skins as $skin ) {
98 $path = "$wgStyleDirectory/$skin/skin.json";
99 if ( file_exists( $path ) ) {
101 $this->addToDependencies( $dependencies, [], [ $skin ], $name );
104 $this->addToDependencies( $dependencies, [], [ $skin ], $name, 'missing' );
109 // Stuff is missing, give up
113 $registry = new ExtensionRegistry();
114 $registry->setCheckDevRequires( $this->checkDev
);
116 $registry->readFromQueue( $queue );
117 } catch ( ExtensionDependencyError
$e ) {
119 if ( $e->incompatibleCore
) {
120 $reason = 'incompatible-core';
121 } elseif ( $e->incompatibleSkins
) {
122 $reason = 'incompatible-skins';
123 } elseif ( $e->incompatibleExtensions
) {
124 $reason = 'incompatible-extensions';
125 } elseif ( $e->missingExtensions ||
$e->missingSkins
) {
126 // There's an extension missing in the dependency tree,
127 // so add those to the dependency list and try again
128 return $this->loadThing(
131 array_merge( $extensions, $e->missingExtensions
),
132 array_merge( $skins, $e->missingSkins
)
135 // missing-phpExtension
141 $this->addToDependencies( $dependencies, $extensions, $skins, $name, $reason, $e->getMessage() );
144 $this->addToDependencies( $dependencies, $extensions, $skins, $name );
147 private function addToDependencies( &$dependencies, $extensions, $skins,
148 $why = null, $status = null, $message = null
150 $mainRegistry = ExtensionRegistry
::getInstance();
151 $iter = [ 'extensions' => $extensions, 'skins' => $skins ];
152 foreach ( $iter as $type => $things ) {
153 foreach ( $things as $thing ) {
154 $preStatus = $dependencies[$type][$thing]['status'] ??
false;
155 if ( $preStatus !== 'loaded' ) {
160 $tStatus = $mainRegistry->isLoaded( $thing ) ?
'loaded' : 'present';
162 $dependencies[$type][$thing]['status'] = $tStatus;
164 if ( $why !== null ) {
165 $dependencies[$type][$thing]['why'][] = $why;
166 // XXX: this is a bit messy
167 $dependencies[$type][$thing]['why'] = array_unique(
168 $dependencies[$type][$thing]['why'] );
171 if ( $message !== null ) {
172 $dependencies[$type][$thing]['message'] = trim( $message );
179 private function formatForHumans( $dependencies ) {
181 foreach ( $dependencies as $type => $things ) {
182 $text .= ucfirst( $type ) . "\n" . str_repeat( '=', strlen( $type ) ) . "\n";
183 foreach ( $things as $thing => $info ) {
184 $why = $info['why'] ??
[];
186 $whyText = '(because: ' . implode( ',', $why ) . ') ';
190 $msg = isset( $info['message'] ) ?
", {$info['message']}" : '';
192 $text .= "$thing: {$info['status']}{$msg} $whyText\n";
197 return trim( $text );
201 $maintClass = CheckDependencies
::class;
202 require_once RUN_MAINTENANCE_IF_MAIN
;