maintenance: Implement 'file' type and use for jquery and qunitjs
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 25 Aug 2018 04:43:43 +0000 (05:43 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Sat, 25 Aug 2018 21:45:01 +0000 (22:45 +0100)
Change-Id: I05243e5a73664353d2b27ffca14ef0b6a3c86eaf

maintenance/resources/foreign-resources.yaml
maintenance/resources/manageForeignResources.php

index 8a21d40..6a02eea 100644 (file)
@@ -1,10 +1,12 @@
 ### Format of this file
 #
 # The top-level keys are module names (as registered in Resources.php).
-# Each top-level key holds a resource descriptor that must have
-# the following `type` value:
+# Each top-level key holds a resource descriptor that must have one of
+# the following `type` values:
 #
 # - `tar`: For tarball archive (may be gzip-compressed).
+# - `file: For a plain file.
+# - `multi-file`: For multiple plain files.
 #
 ### Type tar
 #
 # * `dest`: An object mapping paths to files or directory from the remote resource to a destination
 #    in the module directory. The value of key in dest may be omitted, which will extract the key
 #    directly to the module directory.
+#
+### Type file
+#
+# The `src` and `integrity` keys are quired.
+#
+# * `src`: Full URL to thes remote resource.
+# * `integrity`: Cryptographic hash (integrity metadata format per <https://www.w3.org/TR/SRI/>).
+# * `dest`: The name of the file in the module directory. Default: Basename of URL.
+#
+### Type mult-file
+#
+# The `files` key is required.
+#
+# * `files`: An object mapping destination paths to an object containing `src` and `integrity`
+#    keys.
 oojs:
   type: tar
   src: https://registry.npmjs.org/oojs/-/oojs-2.2.2.tgz
@@ -54,3 +71,18 @@ oojs-ui:
     package/dist/History.md:
     package/dist/LICENSE-MIT:
     package/dist/README.md:
+jquery:
+  type: file
+  src: https://code.jquery.com/jquery-3.2.1.js
+  # From https://code.jquery.com/jquery/
+  integrity: sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=
+  dest: jquery.js
+qunitjs:
+  type: multi-file
+  files:
+    qunit.js:
+      src: https://code.jquery.com/qunit/qunit-2.6.0.js
+      integrity: sha384-5O3bKbJBbAbxsqV+w/I1fcXgWJgbqM+hmYAPOE9aELSYpcTEsv48X8H+Hnq66V/9
+    qunit.css:
+      src: https://code.jquery.com/qunit/qunit-2.6.0.css
+      integrity: sha384-8vDvsmsuiD7tCQyC+pW2LOwDDgsluGsIPeCqr3rHsDSF2k4WpmfvKKxcgSV5zPai
index 6065401..41e579b 100644 (file)
@@ -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 ) {