23 use Wikimedia\AtEase\AtEase;
73 $this->infoPrinter =
$infoPrinter ??
static function ( $_ ) {
82 $this->tmpParentDir =
"{$this->libDir}/.foreign/tmp";
84 $cacheHome = getenv(
'XDG_CACHE_HOME' ) ? realpath( getenv(
'XDG_CACHE_HOME' ) ) :
false;
85 $this->cacheDir = $cacheHome ?
"$cacheHome/mw-foreign" :
"{$this->libDir}/.foreign/cache";
95 $actions = [
'update',
'verify',
'make-sri' ];
96 if ( !in_array(
$action, $actions ) ) {
97 $this->
error(
"Invalid action.\n\nMust be one of " . implode(
', ', $actions ) .
'.' );
102 $this->registry = $this->
parseBasicYaml( file_get_contents( $this->registryFile ) );
103 if ( $module ===
'all' ) {
105 } elseif ( isset( $this->registry[ $module ] ) ) {
106 $modules = [ $module => $this->registry[ $module ] ];
108 $this->
error(
"Unknown module name.\n\nMust be one of:\n" .
109 wordwrap( implode(
', ', array_keys( $this->registry ) ), 80 ) .
115 foreach (
$modules as $moduleName => $info ) {
116 $this->
verbose(
"\n### {$moduleName}\n\n" );
117 $destDir =
"{$this->libDir}/$moduleName";
119 if ( $this->action ===
'update' ) {
120 $this->
output(
"... updating '{$moduleName}'\n" );
121 $this->
verbose(
"... emptying directory for $moduleName\n" );
123 } elseif ( $this->action ===
'verify' ) {
124 $this->
output(
"... verifying '{$moduleName}'\n" );
126 $this->
output(
"... checking '{$moduleName}'\n" );
129 $this->
verbose(
"... preparing {$this->tmpParentDir}\n" );
132 throw new Exception(
"Unable to create {$this->tmpParentDir}" );
135 if ( !isset( $info[
'type'] ) ) {
136 throw new Exception(
"Module '$moduleName' must have a 'type' key." );
138 switch ( $info[
'type'] ) {
149 throw new Exception(
"Unknown type '{$info['type']}' for '$moduleName'" );
153 $this->
output(
"\nDone!\n" );
155 if ( $this->hasErrors ) {
164 $key = basename( $src ) .
'_' . substr( $integrity, -12 );
165 $key = preg_replace(
'/[.\/+?=_-]+/',
'_', $key );
166 return rtrim( $key,
'_' );
174 return AtEase::quietCall(
'file_get_contents',
"{$this->cacheDir}/$key.data" );
179 file_put_contents(
"{$this->cacheDir}/$key.data", $data, LOCK_EX );
182 private function fetch( $src, $integrity ) {
183 $key = $this->
cacheKey( $src, $integrity );
189 $req = MediaWikiServices::getInstance()->getHttpRequestFactory()
190 ->create( $src, [
'method' =>
'GET',
'followRedirects' =>
false ], __METHOD__ );
191 if ( !$req->execute()->isOK() ) {
192 throw new Exception(
"Failed to download resource at {$src}" );
194 if ( $req->getStatus() !== 200 ) {
195 throw new Exception(
"Unexpected HTTP {$req->getStatus()} response from {$src}" );
197 $data = $req->getContent();
198 $algo = $integrity ===
null ? $this->defaultAlgo : explode(
'-', $integrity )[0];
199 $actualIntegrity = $algo .
'-' . base64_encode( hash( $algo, $data,
true ) );
200 if ( $integrity === $actualIntegrity ) {
201 $this->
verbose(
"... passed integrity check for {$src}\n" );
203 } elseif ( $this->action ===
'make-sri' ) {
204 $this->
output(
"Integrity for {$src}\n\tintegrity: ${actualIntegrity}\n" );
206 $expectedIntegrity = $integrity ??
'null';
207 throw new Exception(
"Integrity check failed for {$src}\n" .
208 "\tExpected: {$expectedIntegrity}\n" .
209 "\tActual: {$actualIntegrity}"
216 if ( !isset( $info[
'src'] ) ) {
217 throw new Exception(
"Module '$moduleName' must have a 'src' key." );
219 $data = $this->
fetch( $info[
'src'], $info[
'integrity'] ??
null );
220 $dest = $info[
'dest'] ?? basename( $info[
'src'] );
221 $path =
"$destDir/$dest";
222 if ( $this->action ===
'verify' && sha1_file(
$path ) !== sha1( $data ) ) {
223 throw new Exception(
"File for '$moduleName' is different." );
225 if ( $this->action ===
'update' ) {
227 file_put_contents(
"$destDir/$dest", $data );
232 if ( !isset( $info[
'files'] ) ) {
233 throw new Exception(
"Module '$moduleName' must have a 'files' key." );
235 foreach ( $info[
'files'] as $dest =>
$file ) {
236 if ( !isset(
$file[
'src'] ) ) {
237 throw new Exception(
"Module '$moduleName' file '$dest' must have a 'src' key." );
240 $path =
"$destDir/$dest";
241 if ( $this->action ===
'verify' && sha1_file(
$path ) !== sha1( $data ) ) {
242 throw new Exception(
"File '$dest' for '$moduleName' is different." );
243 } elseif ( $this->action ===
'update' ) {
245 file_put_contents(
"$destDir/$dest", $data );
251 $info += [
'src' =>
null,
'integrity' =>
null,
'dest' => null ];
252 if ( $info[
'src'] ===
null ) {
253 throw new Exception(
"Module '$moduleName' must have a 'src' key." );
256 $data = $this->
fetch( $info[
'src'], $info[
'integrity' ] );
257 $tmpFile =
"{$this->tmpParentDir}/$moduleName.tar";
258 $this->
verbose(
"... writing '$moduleName' src to $tmpFile\n" );
259 file_put_contents( $tmpFile, $data );
260 $p =
new PharData( $tmpFile );
261 $tmpDir =
"{$this->tmpParentDir}/$moduleName";
262 $p->extractTo( $tmpDir );
265 if ( $info[
'dest'] ===
null ) {
267 $toCopy = [ $tmpDir => $destDir ];
271 foreach ( $info[
'dest'] as $fromSubPath => $toSubPath ) {
273 $fromPaths = glob(
"{$tmpDir}/{$fromSubPath}", GLOB_BRACE );
275 throw new Exception(
"Path '$fromSubPath' of '$moduleName' not found." );
277 foreach ( $fromPaths as $fromPath ) {
278 $toCopy[$fromPath] = $toSubPath ===
null
279 ?
"$destDir/" . basename( $fromPath )
280 :
"$destDir/$toSubPath/" . basename( $fromPath );
284 foreach ( $toCopy as $from => $to ) {
285 if ( $this->action ===
'verify' ) {
286 $this->
verbose(
"... verifying $to\n" );
287 if ( is_dir( $from ) ) {
288 $rii =
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
290 RecursiveDirectoryIterator::SKIP_DOTS
293 foreach ( $rii as
$file ) {
294 $remote =
$file->getPathname();
295 $local = strtr( $remote, [ $from => $to ] );
296 if ( sha1_file( $remote ) !== sha1_file( $local ) ) {
297 $this->
error(
"File '$local' is different." );
298 $this->hasErrors =
true;
301 } elseif ( sha1_file( $from ) !== sha1_file( $to ) ) {
302 $this->
error(
"File '$to' is different." );
303 $this->hasErrors =
true;
305 } elseif ( $this->action ===
'update' ) {
306 $this->
verbose(
"... moving $from to $to\n" );
308 if ( !rename( $from, $to ) ) {
309 throw new Exception(
"Could not move $from to $to." );
332 foreach ( $this->registry as $info ) {
333 if ( $info[
'type'] ===
'file' || $info[
'type'] ===
'tar' ) {
334 $knownKeys[] = $this->
cacheKey( $info[
'src'], $info[
'integrity'] );
335 } elseif ( $info[
'type'] ===
'multi-file' ) {
336 foreach ( $info[
'files'] as
$file ) {
337 $knownKeys[] = $this->
cacheKey( $file[
'src'],
$file[
'integrity'] );
341 foreach ( glob(
"{$this->cacheDir}/*" ) as $cacheFile ) {
342 if ( !in_array( basename( $cacheFile,
'.data' ), $knownKeys ) ) {
343 unlink( $cacheFile );
358 $lines = explode(
"\n", $input );
362 foreach (
$lines as $i => $text ) {
364 $trimmed = ltrim( $text,
' ' );
365 if ( $trimmed ===
'' || $trimmed[0] ===
'#' ) {
368 $indent = strlen( $text ) - strlen( $trimmed );
369 if ( $indent % 2 !== 0 ) {
370 throw new Exception( __METHOD__ .
": Odd indentation on line $line." );
372 $depth = $indent === 0 ? 0 : ( $indent / 2 );
373 if ( $depth < $prev ) {
375 array_splice( $stack, $depth + 1 );
377 if ( !array_key_exists( $depth, $stack ) ) {
378 throw new Exception( __METHOD__ .
": Too much indentation on line $line." );
380 if ( strpos( $trimmed,
':' ) ===
false ) {
381 throw new Exception( __METHOD__ .
": Missing colon on line $line." );
383 $dest =& $stack[ $depth ];
384 if ( $dest ===
null ) {
388 list( $key, $val ) = explode(
':', $trimmed, 2 );
389 $val = ltrim( $val,
' ' );
392 $dest[ $key ] = $val;
397 $dest[ $key ] = &$val;
400 unset( $dest, $val );
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.
callable Closure $errorPrinter
cacheKey( $src, $integrity)
callable Closure $infoPrinter
callable Closure $verbosePrinter
parseBasicYaml( $input)
Basic YAML parser.
handleTypeTar( $moduleName, $destDir, array $info)
handleTypeMultiFile( $moduleName, $destDir, array $info)
__construct( $registryFile, $libDir, callable $infoPrinter=null, callable $errorPrinter=null, callable $verbosePrinter=null)
handleTypeFile( $moduleName, $destDir, array $info)
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
if(!file_exists( $CREDITS)) $lines