MediaWiki master
checkDependencies.php
Go to the documentation of this file.
1<?php
8
13
14// @codeCoverageIgnoreStart
15require_once __DIR__ . '/Maintenance.php';
16// @codeCoverageIgnoreEnd
17
24
26 private $checkDev;
27
28 public function __construct() {
29 parent::__construct();
30 $this->addDescription( 'Check dependencies for extensions' );
31 $this->addOption( 'extensions', 'Comma separated list of extensions to check', false, true );
32 $this->addOption( 'skins', 'Comma separated list of skins to check', false, true );
33 $this->addOption( 'json', 'Output in JSON' );
34 $this->addOption( 'dev', 'Check development dependencies too' );
35 }
36
37 public function execute() {
38 $this->checkDev = $this->hasOption( 'dev' );
39 $extensions = $this->hasOption( 'extensions' )
40 ? explode( ',', $this->getOption( 'extensions' ) )
41 : [];
42 $skins = $this->hasOption( 'skins' )
43 ? explode( ',', $this->getOption( 'skins' ) )
44 : [];
45
46 $dependencies = [];
47 // Note that we can only use the main registry if we are
48 // not checking development dependencies.
50 foreach ( $extensions as $extension ) {
51 if ( !$this->checkDev && $registry->isLoaded( $extension ) ) {
52 // If it's already loaded, we know all the dependencies resolved.
53 $this->addToDependencies( $dependencies, [ $extension ], [] );
54 continue;
55 }
56 $this->loadThing( $dependencies, $extension, [ $extension ], [] );
57 }
58
59 foreach ( $skins as $skin ) {
60 if ( !$this->checkDev && $registry->isLoaded( $skin ) ) {
61 // If it's already loaded, we know all the dependencies resolved.
62 $this->addToDependencies( $dependencies, [], [ $skin ] );
63 continue;
64 }
65 $this->loadThing( $dependencies, $skin, [], [ $skin ] );
66 }
67
68 if ( $this->hasOption( 'json' ) ) {
69 $this->output( json_encode( $dependencies ) . "\n" );
70 } else {
71 $this->output( $this->formatForHumans( $dependencies ) . "\n" );
72 }
73 }
74
75 private function loadThing( array &$dependencies, string $name, array $extensions, array $skins ) {
76 $extDir = $this->getConfig()->get( MainConfigNames::ExtensionDirectory );
77 $styleDir = $this->getConfig()->get( MainConfigNames::StyleDirectory );
78 $queue = [];
79 $missing = false;
80 foreach ( $extensions as $extension ) {
81 $path = "$extDir/$extension/extension.json";
82 if ( file_exists( $path ) ) {
83 // 1 is ignored
84 $queue[$path] = 1;
85 $this->addToDependencies( $dependencies, [ $extension ], [], $name );
86 } else {
87 $missing = true;
88 $this->addToDependencies( $dependencies, [ $extension ], [], $name, 'missing' );
89 }
90 }
91
92 foreach ( $skins as $skin ) {
93 $path = "$styleDir/$skin/skin.json";
94 if ( file_exists( $path ) ) {
95 $queue[$path] = 1;
96 $this->addToDependencies( $dependencies, [], [ $skin ], $name );
97 } else {
98 $missing = true;
99 $this->addToDependencies( $dependencies, [], [ $skin ], $name, 'missing' );
100 }
101 }
102
103 if ( $missing ) {
104 // Stuff is missing, give up
105 return;
106 }
107
108 $registry = new ExtensionRegistry();
109 $registry->setCheckDevRequires( $this->checkDev );
110 try {
111 $registry->readFromQueue( $queue );
112 } catch ( ExtensionDependencyError $e ) {
113 $reason = false;
114 if ( $e->incompatibleCore ) {
115 $reason = 'incompatible-core';
116 } elseif ( $e->incompatibleSkins ) {
117 $reason = 'incompatible-skins';
118 } elseif ( $e->incompatibleExtensions ) {
119 $reason = 'incompatible-extensions';
120 } elseif ( $e->missingExtensions || $e->missingSkins ) {
121 // There's an extension missing in the dependency tree,
122 // so add those to the dependency list and try again
123 $this->loadThing(
124 $dependencies,
125 $name,
126 array_merge( $extensions, $e->missingExtensions ),
127 array_merge( $skins, $e->missingSkins )
128 );
129 return;
130 } else {
131 // missing-phpExtension
132 // missing-ability
133 // XXX: ???
134 $this->fatalError( $e->getMessage() );
135 }
136
137 $this->addToDependencies( $dependencies, $extensions, $skins, $name, $reason, $e->getMessage() );
138 }
139
140 $this->addToDependencies( $dependencies, $extensions, $skins, $name );
141 }
142
143 private function addToDependencies( array &$dependencies, array $extensions, array $skins,
144 ?string $why = null, ?string $status = null, ?string $message = null
145 ) {
146 $mainRegistry = ExtensionRegistry::getInstance();
147 $iter = [ 'extensions' => $extensions, 'skins' => $skins ];
148 foreach ( $iter as $type => $things ) {
149 foreach ( $things as $thing ) {
150 $preStatus = $dependencies[$type][$thing]['status'] ?? false;
151 if ( $preStatus !== 'loaded' ) {
152 // OK to overwrite
153 if ( $status ) {
154 $tStatus = $status;
155 } else {
156 $tStatus = $mainRegistry->isLoaded( $thing ) ? 'loaded' : 'present';
157 }
158 $dependencies[$type][$thing]['status'] = $tStatus;
159 }
160 if ( $why !== null ) {
161 $dependencies[$type][$thing]['why'][] = $why;
162 // XXX: this is a bit messy
163 $dependencies[$type][$thing]['why'] = array_unique(
164 $dependencies[$type][$thing]['why'] );
165 }
166
167 if ( $message !== null ) {
168 $dependencies[$type][$thing]['message'] = trim( $message );
169 }
170
171 }
172 }
173 }
174
175 private function formatForHumans( array $dependencies ): string {
176 $text = '';
177 foreach ( $dependencies as $type => $things ) {
178 $text .= ucfirst( $type ) . "\n" . str_repeat( '=', strlen( $type ) ) . "\n";
179 foreach ( $things as $thing => $info ) {
180 $why = $info['why'] ?? [];
181 if ( $why ) {
182 $whyText = '(because: ' . implode( ',', $why ) . ') ';
183 } else {
184 $whyText = '';
185 }
186 $msg = isset( $info['message'] ) ? ", {$info['message']}" : '';
187
188 $text .= "$thing: {$info['status']}{$msg} $whyText\n";
189 }
190 $text .= "\n";
191 }
192
193 return trim( $text );
194 }
195}
196
197// @codeCoverageIgnoreStart
198$maintClass = CheckDependencies::class;
199require_once RUN_MAINTENANCE_IF_MAIN;
200// @codeCoverageIgnoreEnd
$maintClass
Checks dependencies for extensions, mostly without loading them.
A class containing constants representing the names of configuration variables.
const ExtensionDirectory
Name constant for the ExtensionDirectory setting, for use with Config\get().
const StyleDirectory
Name constant for the StyleDirectory setting, for use with Config\get().
Load JSON files, and uses a Processor to extract information.