Merge "Use new isTalkPage/isTalkNamespace methods"
[lhc/web/wiklou.git] / maintenance / resources / manageForeignResources.php
index 6065401..aa22c68 100644 (file)
@@ -45,8 +45,8 @@ libraries registered as ResourceLoader modules. See also foreign-resources.yaml.
 For sources that don't publish an integrity hash, omit "integrity" (or leave empty)
 and run the "make-sri" action to compute the missing hashes.
 
-This script runs in dry mode by default. Use --update to actually change, remove,
-or add files to /resources/lib/.
+This script runs in dry-run mode by default. Use --update to actually change,
+remove, or add files to resources/lib/.
 TEXT
                );
                $this->addArg( 'action', 'One of "update", "verify" or "make-sri"', true );
@@ -99,6 +99,12 @@ TEXT
                                case 'tar':
                                        $this->handleTypeTar( $moduleName, $destDir, $info );
                                        break;
+                               case 'file':
+                                       $this->handleTypeFile( $moduleName, $destDir, $info );
+                                       break;
+                               case 'multi-file':
+                                       $this->handleTypeMultiFile( $moduleName, $destDir, $info );
+                                       break;
                                default:
                                        $this->fatalError( "Unknown type '{$info['type']}' for '$moduleName'" );
                        }
@@ -134,6 +140,40 @@ TEXT
                return $data;
        }
 
+       private function handleTypeFile( $moduleName, $destDir, array $info ) {
+               if ( !isset( $info['src'] ) ) {
+                       $this->fatalError( "Module '$moduleName' must have a 'src' key." );
+               }
+               $data = $this->fetch( $info['src'], $info['integrity'] ?? null );
+               $dest = $info['dest'] ?? basename( $info['src'] );
+               $path = "$destDir/$dest";
+               if ( $this->action === 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
+                       $this->fatalError( "File for '$moduleName' is different." );
+               } elseif ( $this->action === 'update' ) {
+                       wfMkdirParents( $destDir );
+                       file_put_contents( "$destDir/$dest", $data );
+               }
+       }
+
+       private function handleTypeMultiFile( $moduleName, $destDir, array $info ) {
+               if ( !isset( $info['files'] ) ) {
+                       $this->fatalError( "Module '$moduleName' must have a 'files' key." );
+               }
+               foreach ( $info['files'] as $dest => $file ) {
+                       if ( !isset( $file['src'] ) ) {
+                               $this->fatalError( "Module '$moduleName' file '$dest' must have a 'src' key." );
+                       }
+                       $data = $this->fetch( $file['src'], $file['integrity'] ?? null );
+                       $path = "$destDir/$dest";
+                       if ( $this->action === 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
+                               $this->fatalError( "File '$dest' for '$moduleName' is different." );
+                       } elseif ( $this->action === 'update' ) {
+                               wfMkdirParents( $destDir );
+                               file_put_contents( "$destDir/$dest", $data );
+                       }
+               }
+       }
+
        private function handleTypeTar( $moduleName, $destDir, array $info ) {
                $info += [ 'src' => null, 'integrity' => null, 'dest' => null ];
                if ( $info['src'] === null ) {