MediaWiki master
checkDependencies.php
Go to the documentation of this file.
1<?php
24
25require_once __DIR__ . '/Maintenance.php';
26
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;
Checks dependencies for extensions, mostly without loading them.
__construct()
Default constructor.
execute()
Do the actual work.
Load JSON files, and uses a Processor to extract information.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.