MediaWiki REL1_39
ForeignResourceManager.php
Go to the documentation of this file.
1<?php
23use Symfony\Component\Yaml\Yaml;
24use Wikimedia\AtEase\AtEase;
25
33 private $defaultAlgo = 'sha384';
34
36 private $hasErrors = false;
37
39 private $registryFile;
40
42 private $libDir;
43
45 private $tmpParentDir;
46
48 private $cacheDir;
49
54 private $infoPrinter;
55
60 private $errorPrinter;
65 private $verbosePrinter;
66
68 private $action;
69
71 private $registry;
72
81 public function __construct(
82 $registryFile,
83 $libDir,
84 callable $infoPrinter = null,
85 callable $errorPrinter = null,
86 callable $verbosePrinter = null
87 ) {
88 $this->registryFile = $registryFile;
89 $this->libDir = $libDir;
90 $this->infoPrinter = $infoPrinter ?? static function ( $_ ) {
91 };
92 $this->errorPrinter = $errorPrinter ?? $this->infoPrinter;
93 $this->verbosePrinter = $verbosePrinter ?? static function ( $_ ) {
94 };
95
96 // Use a temporary directory under the destination directory instead
97 // of wfTempDir() because PHP's rename() does not work across file
98 // systems, and the user's /tmp and $IP may be on different filesystems.
99 $this->tmpParentDir = "{$this->libDir}/.foreign/tmp";
100
101 $cacheHome = getenv( 'XDG_CACHE_HOME' ) ? realpath( getenv( 'XDG_CACHE_HOME' ) ) : false;
102 $this->cacheDir = $cacheHome ? "$cacheHome/mw-foreign" : "{$this->libDir}/.foreign/cache";
103 }
104
111 public function run( $action, $module ) {
112 $actions = [ 'update', 'verify', 'make-sri' ];
113 if ( !in_array( $action, $actions ) ) {
114 $this->error( "Invalid action.\n\nMust be one of " . implode( ', ', $actions ) . '.' );
115 return false;
116 }
117 $this->action = $action;
118
119 $this->registry = Yaml::parseFile( $this->registryFile );
120 if ( $module === 'all' ) {
121 $modules = $this->registry;
122 } elseif ( isset( $this->registry[ $module ] ) ) {
123 $modules = [ $module => $this->registry[ $module ] ];
124 } else {
125 $this->error( "Unknown module name.\n\nMust be one of:\n" .
126 wordwrap( implode( ', ', array_keys( $this->registry ) ), 80 ) .
127 '.'
128 );
129 return false;
130 }
131
132 foreach ( $modules as $moduleName => $info ) {
133 $this->verbose( "\n### {$moduleName}\n\n" );
134 $destDir = "{$this->libDir}/$moduleName";
135
136 if ( $this->action === 'update' ) {
137 $this->output( "... updating '{$moduleName}'\n" );
138 $this->verbose( "... emptying directory for $moduleName\n" );
139 wfRecursiveRemoveDir( $destDir );
140 } elseif ( $this->action === 'verify' ) {
141 $this->output( "... verifying '{$moduleName}'\n" );
142 } else {
143 $this->output( "... checking '{$moduleName}'\n" );
144 }
145
146 $this->verbose( "... preparing {$this->tmpParentDir}\n" );
147 wfRecursiveRemoveDir( $this->tmpParentDir );
148 if ( !wfMkdirParents( $this->tmpParentDir ) ) {
149 throw new Exception( "Unable to create {$this->tmpParentDir}" );
150 }
151
152 if ( !isset( $info['type'] ) ) {
153 throw new Exception( "Module '$moduleName' must have a 'type' key." );
154 }
155 switch ( $info['type'] ) {
156 case 'tar':
157 $this->handleTypeTar( $moduleName, $destDir, $info );
158 break;
159 case 'file':
160 $this->handleTypeFile( $moduleName, $destDir, $info );
161 break;
162 case 'multi-file':
163 $this->handleTypeMultiFile( $moduleName, $destDir, $info );
164 break;
165 default:
166 throw new Exception( "Unknown type '{$info['type']}' for '$moduleName'" );
167 }
168 }
169
170 $this->output( "\nDone!\n" );
171 $this->cleanUp();
172 if ( $this->hasErrors ) {
173 // The verify mode should check all modules/files and fail after, not during.
174 return false;
175 }
176
177 return true;
178 }
179
186 private function cacheKey( $src, $integrity ) {
187 $key = basename( $src ) . '_' . substr( $integrity, -12 );
188 $key = preg_replace( '/[.\/+?=_-]+/', '_', $key );
189 return rtrim( $key, '_' );
190 }
191
196 private function cacheGet( $key ) {
197 return AtEase::quietCall( 'file_get_contents', "{$this->cacheDir}/$key.data" );
198 }
199
204 private function cacheSet( $key, $data ) {
205 wfMkdirParents( $this->cacheDir );
206 file_put_contents( "{$this->cacheDir}/$key.data", $data, LOCK_EX );
207 }
208
215 private function fetch( $src, $integrity ) {
216 $key = $this->cacheKey( $src, $integrity );
217 $data = $this->cacheGet( $key );
218 if ( $data ) {
219 return $data;
220 }
221
222 $req = MediaWikiServices::getInstance()->getHttpRequestFactory()
223 ->create( $src, [ 'method' => 'GET', 'followRedirects' => false ], __METHOD__ );
224 if ( !$req->execute()->isOK() ) {
225 throw new Exception( "Failed to download resource at {$src}" );
226 }
227 if ( $req->getStatus() !== 200 ) {
228 throw new Exception( "Unexpected HTTP {$req->getStatus()} response from {$src}" );
229 }
230 $data = $req->getContent();
231 $algo = $integrity === null ? $this->defaultAlgo : explode( '-', $integrity )[0];
232 $actualIntegrity = $algo . '-' . base64_encode( hash( $algo, $data, true ) );
233 if ( $integrity === $actualIntegrity ) {
234 $this->verbose( "... passed integrity check for {$src}\n" );
235 $this->cacheSet( $key, $data );
236 } elseif ( $this->action === 'make-sri' ) {
237 $this->output( "Integrity for {$src}\n\tintegrity: {$actualIntegrity}\n" );
238 } else {
239 $expectedIntegrity = $integrity ?? 'null';
240 throw new Exception( "Integrity check failed for {$src}\n" .
241 "\tExpected: {$expectedIntegrity}\n" .
242 "\tActual: {$actualIntegrity}"
243 );
244 }
245 return $data;
246 }
247
253 private function handleTypeFile( $moduleName, $destDir, array $info ) {
254 if ( !isset( $info['src'] ) ) {
255 throw new Exception( "Module '$moduleName' must have a 'src' key." );
256 }
257 $data = $this->fetch( $info['src'], $info['integrity'] ?? null );
258 $dest = $info['dest'] ?? basename( $info['src'] );
259 $path = "$destDir/$dest";
260 if ( $this->action === 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
261 throw new Exception( "File for '$moduleName' is different." );
262 }
263 if ( $this->action === 'update' ) {
264 wfMkdirParents( $destDir );
265 file_put_contents( "$destDir/$dest", $data );
266 }
267 }
268
274 private function handleTypeMultiFile( $moduleName, $destDir, array $info ) {
275 if ( !isset( $info['files'] ) ) {
276 throw new Exception( "Module '$moduleName' must have a 'files' key." );
277 }
278 foreach ( $info['files'] as $dest => $file ) {
279 if ( !isset( $file['src'] ) ) {
280 throw new Exception( "Module '$moduleName' file '$dest' must have a 'src' key." );
281 }
282 $data = $this->fetch( $file['src'], $file['integrity'] ?? null );
283 $path = "$destDir/$dest";
284 if ( $this->action === 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
285 throw new Exception( "File '$dest' for '$moduleName' is different." );
286 } elseif ( $this->action === 'update' ) {
287 wfMkdirParents( $destDir );
288 file_put_contents( "$destDir/$dest", $data );
289 }
290 }
291 }
292
298 private function handleTypeTar( $moduleName, $destDir, array $info ) {
299 $info += [ 'src' => null, 'integrity' => null, 'dest' => null ];
300 if ( $info['src'] === null ) {
301 throw new Exception( "Module '$moduleName' must have a 'src' key." );
302 }
303 // Download the resource to a temporary file and open it
304 $data = $this->fetch( $info['src'], $info['integrity' ] );
305 $tmpFile = "{$this->tmpParentDir}/$moduleName.tar";
306 $this->verbose( "... writing '$moduleName' src to $tmpFile\n" );
307 file_put_contents( $tmpFile, $data );
308 $p = new PharData( $tmpFile );
309 $tmpDir = "{$this->tmpParentDir}/$moduleName";
310 $p->extractTo( $tmpDir );
311 unset( $data, $p );
312
313 if ( $info['dest'] === null ) {
314 // Default: Replace the entire directory
315 $toCopy = [ $tmpDir => $destDir ];
316 } else {
317 // Expand and normalise the 'dest' entries
318 $toCopy = [];
319 foreach ( $info['dest'] as $fromSubPath => $toSubPath ) {
320 // Use glob() to expand wildcards and check existence
321 $fromPaths = glob( "{$tmpDir}/{$fromSubPath}", GLOB_BRACE );
322 if ( !$fromPaths ) {
323 throw new Exception( "Path '$fromSubPath' of '$moduleName' not found." );
324 }
325 foreach ( $fromPaths as $fromPath ) {
326 $toCopy[$fromPath] = $toSubPath === null
327 ? "$destDir/" . basename( $fromPath )
328 : "$destDir/$toSubPath/" . basename( $fromPath );
329 }
330 }
331 }
332 foreach ( $toCopy as $from => $to ) {
333 if ( $this->action === 'verify' ) {
334 $this->verbose( "... verifying $to\n" );
335 if ( is_dir( $from ) ) {
336 $rii = new RecursiveIteratorIterator( new RecursiveDirectoryIterator(
337 $from,
338 RecursiveDirectoryIterator::SKIP_DOTS
339 ) );
341 foreach ( $rii as $file ) {
342 $remote = $file->getPathname();
343 $local = strtr( $remote, [ $from => $to ] );
344 if ( sha1_file( $remote ) !== sha1_file( $local ) ) {
345 $this->error( "File '$local' is different." );
346 $this->hasErrors = true;
347 }
348 }
349 } elseif ( sha1_file( $from ) !== sha1_file( $to ) ) {
350 $this->error( "File '$to' is different." );
351 $this->hasErrors = true;
352 }
353 } elseif ( $this->action === 'update' ) {
354 $this->verbose( "... moving $from to $to\n" );
355 wfMkdirParents( dirname( $to ) );
356 if ( !rename( $from, $to ) ) {
357 throw new Exception( "Could not move $from to $to." );
358 }
359 }
360 }
361 }
362
366 private function verbose( $text ) {
367 ( $this->verbosePrinter )( $text );
368 }
369
373 private function output( $text ) {
374 ( $this->infoPrinter )( $text );
375 }
376
380 private function error( $text ) {
381 ( $this->errorPrinter )( $text );
382 }
383
384 private function cleanUp() {
385 wfRecursiveRemoveDir( $this->tmpParentDir );
386
387 // Prune the cache of files we don't recognise.
388 $knownKeys = [];
389 foreach ( $this->registry as $info ) {
390 if ( $info['type'] === 'file' || $info['type'] === 'tar' ) {
391 $knownKeys[] = $this->cacheKey( $info['src'], $info['integrity'] );
392 } elseif ( $info['type'] === 'multi-file' ) {
393 foreach ( $info['files'] as $file ) {
394 $knownKeys[] = $this->cacheKey( $file['src'], $file['integrity'] );
395 }
396 }
397 }
398 foreach ( glob( "{$this->cacheDir}/*" ) as $cacheFile ) {
399 if ( !in_array( basename( $cacheFile, '.data' ), $knownKeys ) ) {
400 unlink( $cacheFile );
401 }
402 }
403 }
404}
wfRecursiveRemoveDir( $dir)
Remove a directory and all its content.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Manage foreign resources registered with ResourceLoader.
__construct( $registryFile, $libDir, callable $infoPrinter=null, callable $errorPrinter=null, callable $verbosePrinter=null)
Service locator for MediaWiki core services.
$tester verbose
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42