23use Wikimedia\AtEase\AtEase;
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 throw new Exception(
"Integrity check failed for {$src}\n" .
207 "\tExpected: {$integrity}\n" .
208 "\tActual: {$actualIntegrity}"
215 if ( !isset( $info[
'src'] ) ) {
216 throw new Exception(
"Module '$moduleName' must have a 'src' key." );
218 $data = $this->
fetch( $info[
'src'], $info[
'integrity'] ??
null );
219 $dest = $info[
'dest'] ?? basename( $info[
'src'] );
220 $path =
"$destDir/$dest";
221 if ( $this->action ===
'verify' && sha1_file(
$path ) !== sha1( $data ) ) {
222 throw new Exception(
"File for '$moduleName' is different." );
224 if ( $this->action ===
'update' ) {
226 file_put_contents(
"$destDir/$dest", $data );
231 if ( !isset( $info[
'files'] ) ) {
232 throw new Exception(
"Module '$moduleName' must have a 'files' key." );
234 foreach ( $info[
'files'] as $dest =>
$file ) {
235 if ( !isset(
$file[
'src'] ) ) {
236 throw new Exception(
"Module '$moduleName' file '$dest' must have a 'src' key." );
239 $path =
"$destDir/$dest";
240 if ( $this->action ===
'verify' && sha1_file(
$path ) !== sha1( $data ) ) {
241 throw new Exception(
"File '$dest' for '$moduleName' is different." );
242 } elseif ( $this->action ===
'update' ) {
244 file_put_contents(
"$destDir/$dest", $data );
250 $info += [
'src' =>
null,
'integrity' =>
null,
'dest' => null ];
251 if ( $info[
'src'] ===
null ) {
252 throw new Exception(
"Module '$moduleName' must have a 'src' key." );
255 $data = $this->
fetch( $info[
'src'], $info[
'integrity' ] );
256 $tmpFile =
"{$this->tmpParentDir}/$moduleName.tar";
257 $this->
verbose(
"... writing '$moduleName' src to $tmpFile\n" );
258 file_put_contents( $tmpFile, $data );
259 $p =
new PharData( $tmpFile );
260 $tmpDir =
"{$this->tmpParentDir}/$moduleName";
261 $p->extractTo( $tmpDir );
264 if ( $info[
'dest'] ===
null ) {
266 $toCopy = [ $tmpDir => $destDir ];
270 foreach ( $info[
'dest'] as $fromSubPath => $toSubPath ) {
272 $fromPaths = glob(
"{$tmpDir}/{$fromSubPath}", GLOB_BRACE );
274 throw new Exception(
"Path '$fromSubPath' of '$moduleName' not found." );
276 foreach ( $fromPaths as $fromPath ) {
277 $toCopy[$fromPath] = $toSubPath ===
null
278 ?
"$destDir/" . basename( $fromPath )
279 :
"$destDir/$toSubPath/" . basename( $fromPath );
283 foreach ( $toCopy as $from => $to ) {
284 if ( $this->action ===
'verify' ) {
285 $this->
verbose(
"... verifying $to\n" );
286 if ( is_dir( $from ) ) {
287 $rii =
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
289 RecursiveDirectoryIterator::SKIP_DOTS
292 foreach ( $rii as
$file ) {
293 $remote =
$file->getPathname();
294 $local = strtr( $remote, [ $from => $to ] );
295 if ( sha1_file( $remote ) !== sha1_file( $local ) ) {
296 $this->
error(
"File '$local' is different." );
297 $this->hasErrors =
true;
300 } elseif ( sha1_file( $from ) !== sha1_file( $to ) ) {
301 $this->
error(
"File '$to' is different." );
302 $this->hasErrors =
true;
304 } elseif ( $this->action ===
'update' ) {
305 $this->
verbose(
"... moving $from to $to\n" );
307 if ( !rename( $from, $to ) ) {
308 throw new Exception(
"Could not move $from to $to." );
331 foreach ( $this->registry as $info ) {
332 if ( $info[
'type'] ===
'file' || $info[
'type'] ===
'tar' ) {
333 $knownKeys[] = $this->
cacheKey( $info[
'src'], $info[
'integrity'] );
334 } elseif ( $info[
'type'] ===
'multi-file' ) {
335 foreach ( $info[
'files'] as
$file ) {
336 $knownKeys[] = $this->
cacheKey( $file[
'src'],
$file[
'integrity'] );
340 foreach ( glob(
"{$this->cacheDir}/*" ) as $cacheFile ) {
341 if ( !in_array( basename( $cacheFile,
'.data' ), $knownKeys ) ) {
342 unlink( $cacheFile );
357 $lines = explode(
"\n", $input );
361 foreach (
$lines as $i => $text ) {
363 $trimmed = ltrim( $text,
' ' );
364 if ( $trimmed ===
'' || $trimmed[0] ===
'#' ) {
367 $indent = strlen( $text ) - strlen( $trimmed );
368 if ( $indent % 2 !== 0 ) {
369 throw new Exception( __METHOD__ .
": Odd indentation on line $line." );
371 $depth = $indent === 0 ? 0 : ( $indent / 2 );
372 if ( $depth < $prev ) {
374 array_splice( $stack, $depth + 1 );
376 if ( !array_key_exists( $depth, $stack ) ) {
377 throw new Exception( __METHOD__ .
": Too much indentation on line $line." );
379 if ( strpos( $trimmed,
':' ) ===
false ) {
380 throw new Exception( __METHOD__ .
": Missing colon on line $line." );
382 $dest =& $stack[ $depth ];
383 if ( $dest ===
null ) {
387 list( $key, $val ) = explode(
':', $trimmed, 2 );
388 $val = ltrim( $val,
' ' );
391 $dest[ $key ] = $val;
396 $dest[ $key ] = &$val;
399 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
-var callable(string):void
cacheKey( $src, $integrity)
callable Closure $infoPrinter
-var callable(string):void
callable Closure $verbosePrinter
-var callable(string):void
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