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 $extDir = $this->getConfig()->get( 'ExtensionDirectory' );
83 $styleDir = $this->getConfig()->get( 'StyleDirectory' );
86 foreach ( $extensions as $extension ) {
87 $path = "$extDir/$extension/extension.json";
88 if ( file_exists( $path ) ) {
91 $this->addToDependencies( $dependencies, [ $extension ], [], $name );
94 $this->addToDependencies( $dependencies, [ $extension ], [], $name, 'missing' );
98 foreach ( $skins as $skin ) {
99 $path = "$styleDir/$skin/skin.json";
100 if ( file_exists( $path ) ) {
102 $this->addToDependencies( $dependencies, [], [ $skin ], $name );
105 $this->addToDependencies( $dependencies, [], [ $skin ], $name, 'missing' );
110 // Stuff is missing, give up
114 $registry = new ExtensionRegistry();
115 $registry->setCheckDevRequires( $this->checkDev
);
117 $registry->readFromQueue( $queue );
118 } catch ( ExtensionDependencyError
$e ) {
120 if ( $e->incompatibleCore
) {
121 $reason = 'incompatible-core';
122 } elseif ( $e->incompatibleSkins
) {
123 $reason = 'incompatible-skins';
124 } elseif ( $e->incompatibleExtensions
) {
125 $reason = 'incompatible-extensions';
126 } elseif ( $e->missingExtensions ||
$e->missingSkins
) {
127 // There's an extension missing in the dependency tree,
128 // so add those to the dependency list and try again
129 return $this->loadThing(
132 array_merge( $extensions, $e->missingExtensions
),
133 array_merge( $skins, $e->missingSkins
)
136 // missing-phpExtension
142 $this->addToDependencies( $dependencies, $extensions, $skins, $name, $reason, $e->getMessage() );
145 $this->addToDependencies( $dependencies, $extensions, $skins, $name );
148 private function addToDependencies( &$dependencies, $extensions, $skins,
149 $why = null, $status = null, $message = null
151 $mainRegistry = ExtensionRegistry
::getInstance();
152 $iter = [ 'extensions' => $extensions, 'skins' => $skins ];
153 foreach ( $iter as $type => $things ) {
154 foreach ( $things as $thing ) {
155 $preStatus = $dependencies[$type][$thing]['status'] ??
false;
156 if ( $preStatus !== 'loaded' ) {
161 $tStatus = $mainRegistry->isLoaded( $thing ) ?
'loaded' : 'present';
163 $dependencies[$type][$thing]['status'] = $tStatus;
165 if ( $why !== null ) {
166 $dependencies[$type][$thing]['why'][] = $why;
167 // XXX: this is a bit messy
168 $dependencies[$type][$thing]['why'] = array_unique(
169 $dependencies[$type][$thing]['why'] );
172 if ( $message !== null ) {
173 $dependencies[$type][$thing]['message'] = trim( $message );
180 private function formatForHumans( $dependencies ) {
182 foreach ( $dependencies as $type => $things ) {
183 $text .= ucfirst( $type ) . "\n" . str_repeat( '=', strlen( $type ) ) . "\n";
184 foreach ( $things as $thing => $info ) {
185 $why = $info['why'] ??
[];
187 $whyText = '(because: ' . implode( ',', $why ) . ') ';
191 $msg = isset( $info['message'] ) ?
", {$info['message']}" : '';
193 $text .= "$thing: {$info['status']}{$msg} $whyText\n";
198 return trim( $text );
202 $maintClass = CheckDependencies
::class;
203 require_once RUN_MAINTENANCE_IF_MAIN
;