Skip to content

Commit

Permalink
Downloader // use the "updated" date for translations as filemtime().…
Browse files Browse the repository at this point in the history
… Ensure that updated zip files are downloaded again.

GlotPress API allows to keep updating translations for an already released version which is represented by "updated"-field in REST API. It is possible, that an existing plugin, theme or WP core can have release as version 1.0, but the translation will be updated after that release was done. Currently, our file caching for translation zip files was only storing the zip files, which contained the release version in file name. Downloaded zip files were on installation marked as "CACHED", because the version did not change, but in reality the "updated"-date changed. With this commit we will ensure that the downloaded zip file will get the "updated"-date as file time to allow better comparison and detection on next installation for "cached" or "not cached".
  • Loading branch information
Chrico committed May 13, 2022
1 parent 37929ef commit ad31a73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Composer\Cache;
use Composer\Composer;
use Composer\Config;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvent;
use Composer\IO\IOInterface;
Expand Down
21 changes: 15 additions & 6 deletions src/Util/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function __construct(
Locker $locker,
string $cacheRoot
) {

$this->io = $io;
$this->unzipper = $unzipper;
$this->remoteFilesystem = $remoteFilesystem;
Expand Down Expand Up @@ -121,7 +120,7 @@ public function download(TranslatablePackageInterface $transPackage, array $allo
continue;
}

$this->downloadZipFile($zipFile, $packageUrl);
$this->downloadZipFile($zipFile, $packageUrl, $lastUpdated);

// phpcs:disable NeutronStandard.Extract.DisallowExtract.Extract
$this->unzipper->extract($zipFile, $directory);
Expand Down Expand Up @@ -159,14 +158,19 @@ public function download(TranslatablePackageInterface $transPackage, array $allo
}

/**
* Downloads the zipFile if not exists yet or the file was updated in the meantime.
*
* @param string $zipFile
* @param $packageUrl
* @param string $packageUrl
* @param string $lastUpdated date time in format yyyy-mm-dd hh:ii:ss
*
* @return bool
*/
private function downloadZipFile(string $zipFile, $packageUrl): bool
private function downloadZipFile(string $zipFile, string $packageUrl, string $lastUpdated): bool
{
if (file_exists($zipFile)) {
$lastUpdated = new \DateTimeImmutable($lastUpdated);

if (file_exists($zipFile) && filemtime($zipFile) >= $lastUpdated->getTimestamp()) {
$this->io->write(
sprintf(
' <info>[CACHED]</info> %s</info> ',
Expand All @@ -180,7 +184,12 @@ private function downloadZipFile(string $zipFile, $packageUrl): bool
$origin = $this->origin($packageUrl);
$result = $this->remoteFilesystem->copy($origin, $packageUrl, $zipFile, false);

return !!$result;
if ($result === false) {
return false;
}

// set the "updated" time as file time.
return touch($zipFile, $lastUpdated->getTimestamp());
}

/**
Expand Down

0 comments on commit ad31a73

Please sign in to comment.