Merge "Add phpdoc for some ApiQueryInfo properties"
[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->addDescription(
16 'Checks whether your composer.lock file is up to date with the current composer.json' );
17 }
18
19 public function execute() {
20 global $IP;
21 $lockLocation = "$IP/composer.lock";
22 $jsonLocation = "$IP/composer.json";
23 if ( !file_exists( $lockLocation ) ) {
24 // Maybe they're using mediawiki/vendor?
25 $lockLocation = "$IP/vendor/composer.lock";
26 if ( !file_exists( $lockLocation ) ) {
27 $this->error(
28 'Could not find composer.lock file. Have you run "composer install"?',
29 1
30 );
31 }
32 }
33
34 $lock = new ComposerLock( $lockLocation );
35 $json = new ComposerJson( $jsonLocation );
36
37 if ( $lock->getHash() === $json->getHash() ) {
38 $this->output( "Your composer.lock file is up to date with current dependencies!\n" );
39 return;
40 }
41 // Out of date, lets figure out which dependencies are old
42 $found = false;
43 $installed = $lock->getInstalledDependencies();
44 foreach ( $json->getRequiredDependencies() as $name => $version ) {
45 if ( isset( $installed[$name] ) ) {
46 if ( $installed[$name]['version'] !== $version ) {
47 $this->output(
48 "$name: {$installed[$name]['version']} installed, $version required.\n"
49 );
50 $found = true;
51 }
52 } else {
53 $this->output( "$name: not installed, $version required.\n" );
54 $found = true;
55 }
56 }
57 if ( $found ) {
58 $this->error(
59 'Error: your composer.lock file is not up to date. ' .
60 'Run "composer update" to install newer dependencies',
61 1
62 );
63 } else {
64 // The hash is the entire composer.json file,
65 // so it can be updated without any of the dependencies changing
66 // We couldn't find any out-of-date dependencies, so assume everything is ok!
67 $this->output( "Your composer.lock file is up to date with current dependencies!\n" );
68 }
69
70 }
71 }
72
73 $maintClass = 'CheckComposerLockUpToDate';
74 require_once RUN_MAINTENANCE_IF_MAIN;