Merge "Add tests for WikiMap and WikiReference"
[lhc/web/wiklou.git] / maintenance / checkComposerLockUpToDate.php
1 <?php
2
3 require_once __DIR__ . '/Maintenance.php';
4
5 /**
6 * Checks whether your composer-installed dependencies are up to date
7 *
8 * Composer creates a "composer.lock" file which specifies which versions are installed
9 * (via `composer install`). It has a hash, which can be compared to the value of
10 * the composer.json file to see if dependencies are up to date.
11 */
12 class CheckComposerLockUpToDate extends Maintenance {
13 public function __construct() {
14 parent::__construct();
15 $this->mDescription = 'Checks whether your composer.lock file is up to date with the current composer.json';
16 }
17
18 public function execute() {
19 global $IP;
20 $lockLocation = "$IP/composer.lock";
21 $jsonLocation = "$IP/composer.json";
22 if ( !file_exists( $lockLocation ) ) {
23 // Maybe they're using mediawiki/vendor?
24 $lockLocation = "$IP/vendor/composer.lock";
25 if ( !file_exists( $lockLocation ) ) {
26 $this->error( 'Could not find composer.lock file. Have you run "composer install"?', 1 );
27 }
28 }
29
30 $lock = new ComposerLock( $lockLocation );
31 $json = new ComposerJson( $jsonLocation );
32
33 if ( $lock->getHash() === $json->getHash() ) {
34 $this->output( "Your composer.lock file is up to date with current dependencies!\n" );
35 return;
36 }
37 // Out of date, lets figure out which dependencies are old
38 $found = false;
39 $installed = $lock->getInstalledDependencies();
40 foreach ( $json->getRequiredDependencies() as $name => $version ) {
41 if ( isset( $installed[$name] ) ) {
42 if ( $installed[$name]['version'] !== $version ) {
43 $this->output( "$name: {$installed[$name]['version']} installed, $version required.\n" );
44 $found = true;
45 }
46 } else {
47 $this->output( "$name: not installed, $version required.\n" );
48 $found = true;
49 }
50 }
51 if ( $found ) {
52 $this->error( 'Error: your composer.lock file is not up to date, run "composer update" to install newer dependencies', 1 );
53 } else {
54 // The hash is the entire composer.json file, so it can be updated without any of the dependencies changing
55 // We couldn't find any out-of-date dependencies, so assume everything is ok!
56 $this->output( "Your composer.lock file is up to date with current dependencies!\n" );
57 }
58
59 }
60 }
61
62 $maintClass = 'CheckComposerLockUpToDate';
63 require_once RUN_MAINTENANCE_IF_MAIN;