Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / maintenance / checkDependencies.php
1 <?php
2 /**
3 * (C) 2019 Kunal Mehta <legoktm@member.fsf.org>
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 require_once __DIR__ . '/Maintenance.php';
24
25 /**
26 * Checks dependencies for extensions, mostly without loading them
27 *
28 * @since 1.34
29 */
30 class CheckDependencies extends Maintenance {
31
32 private $checkDev;
33
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' );
41 }
42
43 public function execute() {
44 $this->checkDev = $this->hasOption( 'dev' );
45 $extensions = $this->hasOption( 'extensions' )
46 ? explode( ',', $this->getOption( 'extensions' ) )
47 : [];
48 $skins = $this->hasOption( 'skins' )
49 ? explode( ',', $this->getOption( 'skins' ) )
50 : [];
51
52 $dependencies = [];
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 ], [] );
60 continue;
61 }
62 $this->loadThing( $dependencies, $extension, [ $extension ], [] );
63 }
64
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 ] );
69 continue;
70 }
71 $this->loadThing( $dependencies, $skin, [], [ $skin ] );
72 }
73
74 if ( $this->hasOption( 'json' ) ) {
75 $this->output( json_encode( $dependencies ) . "\n" );
76 } else {
77 $this->output( $this->formatForHumans( $dependencies ) . "\n" );
78 }
79 }
80
81 private function loadThing( &$dependencies, $name, $extensions, $skins ) {
82 global $wgExtensionDirectory, $wgStyleDirectory;
83 $queue = [];
84 $missing = false;
85 foreach ( $extensions as $extension ) {
86 $path = "$wgExtensionDirectory/$extension/extension.json";
87 if ( file_exists( $path ) ) {
88 // 1 is ignored
89 $queue[$path] = 1;
90 $this->addToDependencies( $dependencies, [ $extension ], [], $name );
91 } else {
92 $missing = true;
93 $this->addToDependencies( $dependencies, [ $extension ], [], $name, 'missing' );
94 }
95 }
96
97 foreach ( $skins as $skin ) {
98 $path = "$wgStyleDirectory/$skin/skin.json";
99 if ( file_exists( $path ) ) {
100 $queue[$path] = 1;
101 $this->addToDependencies( $dependencies, [], [ $skin ], $name );
102 } else {
103 $missing = true;
104 $this->addToDependencies( $dependencies, [], [ $skin ], $name, 'missing' );
105 }
106 }
107
108 if ( $missing ) {
109 // Stuff is missing, give up
110 return;
111 }
112
113 $registry = new ExtensionRegistry();
114 $registry->setCheckDevRequires( $this->checkDev );
115 try {
116 $registry->readFromQueue( $queue );
117 } catch ( ExtensionDependencyError $e ) {
118 $reason = false;
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(
129 $dependencies,
130 $name,
131 array_merge( $extensions, $e->missingExtensions ),
132 array_merge( $skins, $e->missingSkins )
133 );
134 } else {
135 // missing-phpExtension
136 // missing-ability
137 // XXX: ???
138 throw $e;
139 }
140
141 $this->addToDependencies( $dependencies, $extensions, $skins, $name, $reason, $e->getMessage() );
142 }
143
144 $this->addToDependencies( $dependencies, $extensions, $skins, $name );
145 }
146
147 private function addToDependencies( &$dependencies, $extensions, $skins,
148 $why = null, $status = null, $message = null
149 ) {
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' ) {
156 // OK to overwrite
157 if ( $status ) {
158 $tStatus = $status;
159 } else {
160 $tStatus = $mainRegistry->isLoaded( $thing ) ? 'loaded' : 'present';
161 }
162 $dependencies[$type][$thing]['status'] = $tStatus;
163 }
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'] );
169 }
170
171 if ( $message !== null ) {
172 $dependencies[$type][$thing]['message'] = trim( $message );
173 }
174
175 }
176 }
177 }
178
179 private function formatForHumans( $dependencies ) {
180 $text = '';
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'] ?? [];
185 if ( $why ) {
186 $whyText = '(because: ' . implode( ',', $why ) . ') ';
187 } else {
188 $whyText = '';
189 }
190 $msg = isset( $info['message'] ) ? ", {$info['message']}" : '';
191
192 $text .= "$thing: {$info['status']}{$msg} $whyText\n";
193 }
194 $text .= "\n";
195 }
196
197 return trim( $text );
198 }
199 }
200
201 $maintClass = CheckDependencies::class;
202 require_once RUN_MAINTENANCE_IF_MAIN;