MediaWiki  master
checkDependencies.php
Go to the documentation of this file.
1 <?php
24 
25 require_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  return $this->loadThing(
132  $dependencies,
133  $name,
134  array_merge( $extensions, $e->missingExtensions ),
135  array_merge( $skins, $e->missingSkins )
136  );
137  } else {
138  // missing-phpExtension
139  // missing-ability
140  // XXX: ???
141  $this->fatalError( $e->getMessage() );
142  }
143 
144  $this->addToDependencies( $dependencies, $extensions, $skins, $name, $reason, $e->getMessage() );
145  }
146 
147  $this->addToDependencies( $dependencies, $extensions, $skins, $name );
148  }
149 
150  private function addToDependencies( &$dependencies, $extensions, $skins,
151  $why = null, $status = null, $message = null
152  ) {
153  $mainRegistry = ExtensionRegistry::getInstance();
154  $iter = [ 'extensions' => $extensions, 'skins' => $skins ];
155  foreach ( $iter as $type => $things ) {
156  foreach ( $things as $thing ) {
157  $preStatus = $dependencies[$type][$thing]['status'] ?? false;
158  if ( $preStatus !== 'loaded' ) {
159  // OK to overwrite
160  if ( $status ) {
161  $tStatus = $status;
162  } else {
163  $tStatus = $mainRegistry->isLoaded( $thing ) ? 'loaded' : 'present';
164  }
165  $dependencies[$type][$thing]['status'] = $tStatus;
166  }
167  if ( $why !== null ) {
168  $dependencies[$type][$thing]['why'][] = $why;
169  // XXX: this is a bit messy
170  $dependencies[$type][$thing]['why'] = array_unique(
171  $dependencies[$type][$thing]['why'] );
172  }
173 
174  if ( $message !== null ) {
175  $dependencies[$type][$thing]['message'] = trim( $message );
176  }
177 
178  }
179  }
180  }
181 
182  private function formatForHumans( $dependencies ) {
183  $text = '';
184  foreach ( $dependencies as $type => $things ) {
185  $text .= ucfirst( $type ) . "\n" . str_repeat( '=', strlen( $type ) ) . "\n";
186  foreach ( $things as $thing => $info ) {
187  $why = $info['why'] ?? [];
188  if ( $why ) {
189  $whyText = '(because: ' . implode( ',', $why ) . ') ';
190  } else {
191  $whyText = '';
192  }
193  $msg = isset( $info['message'] ) ? ", {$info['message']}" : '';
194 
195  $text .= "$thing: {$info['status']}{$msg} $whyText\n";
196  }
197  $text .= "\n";
198  }
199 
200  return trim( $text );
201  }
202 }
203 
204 $maintClass = CheckDependencies::class;
205 require_once RUN_MAINTENANCE_IF_MAIN;
Checks dependencies for extensions, mostly without loading them.
__construct()
Default constructor.
execute()
Do the actual work.
Copyright (C) 2018 Kunal Mehta legoktm@debian.org
The Registry loads JSON files, and uses a Processor to extract information from them.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
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.