Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / checkComposerLockUpToDate.php
blob6f640b05f76b96f525de86699ecef6e8fdb3719d
1 <?php
3 // @codeCoverageIgnoreStart
4 require_once __DIR__ . '/Maintenance.php';
5 // @codeCoverageIgnoreEnd
7 use MediaWiki\Composer\LockFileChecker;
8 use MediaWiki\Maintenance\Maintenance;
9 use Wikimedia\Composer\ComposerJson;
10 use Wikimedia\Composer\ComposerLock;
12 /**
13 * Checks whether your composer-installed dependencies are up to date
15 * Composer creates a "composer.lock" file which specifies which versions are installed
16 * (via `composer install`). It has a hash, which can be compared to the value of
17 * the composer.json file to see if dependencies are up to date.
19 class CheckComposerLockUpToDate extends Maintenance {
20 public function __construct() {
21 parent::__construct();
22 $this->addDescription(
23 'Checks whether your composer.lock file is up to date with the current composer.json' );
26 public function canExecuteWithoutLocalSettings(): bool {
27 return true;
30 /**
31 * @return string The value of the constant MW_INSTALL_PATH. This method mocked in phpunit tests.
33 protected function getMwInstallPath(): string {
34 return MW_INSTALL_PATH;
37 public function execute() {
38 $installPath = $this->getMwInstallPath();
39 $lockLocation = "$installPath/composer.lock";
40 $jsonLocation = "$installPath/composer.json";
41 if ( !file_exists( $lockLocation ) ) {
42 // Maybe they're using mediawiki/vendor?
43 $lockLocation = "$installPath/vendor/composer.lock";
44 if ( !file_exists( $lockLocation ) ) {
45 $this->fatalError(
46 'Could not find composer.lock file. Have you run "composer install --no-dev"?'
51 $lock = new ComposerLock( $lockLocation );
52 $json = new ComposerJson( $jsonLocation );
54 // Check all the dependencies to see if any are old
55 $checker = new LockFileChecker( $json, $lock );
56 $errors = $checker->check();
58 // NOTE: This is used by TestSetup before MediaWikiServices is initialized and thus
59 // may not rely on global singletons.
60 // NOTE: This is used by maintenance/update.php and thus may not rely on
61 // database connections, including e.g. interface messages without useDatabase=false,
62 // which would call MessageCache.
63 if ( $errors ) {
64 foreach ( $errors as $error ) {
65 $this->error( $error . "\n" );
67 $suggestedCommand = 'composer update';
68 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
69 $suggestedCommand .= ' --no-dev';
71 $this->fatalError(
72 'Error: your composer.lock file is not up to date. ' .
73 'Run "' . $suggestedCommand . '" to install newer dependencies'
75 } else {
76 // We couldn't find any out-of-date dependencies, so assume everything is ok!
77 $this->output( "Your composer.lock file is up to date with current dependencies!\n" );
82 // @codeCoverageIgnoreStart
83 $maintClass = CheckComposerLockUpToDate::class;
84 require_once RUN_MAINTENANCE_IF_MAIN;
85 // @codeCoverageIgnoreEnd