| 1 |
<?php |
| 2 |
|
| 3 |
namespace Mgleis\DiskUsageInsights\Domain\Collect; |
| 4 |
|
| 5 |
use Mgleis\DiskUsageInsights\Domain\Jobs\BaseJob; |
| 6 |
|
| 7 |
class DetermineWpCoreFileJob extends BaseJob { |
| 8 |
|
| 9 |
private int $skip; |
| 10 |
private int $count; |
| 11 |
private int $totalCount; |
| 12 |
|
| 13 |
public function __construct(int $skip, int $count, int $totalCount) { |
| 14 |
$this->skip = $skip; |
| 15 |
$this->count = $count; |
| 16 |
$this->totalCount = $totalCount; |
| 17 |
} |
| 18 |
|
| 19 |
public function work() { |
| 20 |
|
| 21 |
$this->log(self::class); |
| 22 |
|
| 23 |
$snapshot = $this->snapshotRepository->load(); |
| 24 |
if (sizeof($snapshot->wpcorefiles) == 0) { |
| 25 |
|
| 26 |
// Fetch WP Core files and store them in kv store |
| 27 |
global $wp_version; |
| 28 |
$url = sprintf('https://api.wordpress.org/core/checksums/1.0/?version=%s&locale=en_US', $wp_version); |
| 29 |
$this->log("Fetching from url: " . $url); |
| 30 |
$str = wp_remote_get($url)['body']; // TODO: Cannot use object of type WP_Error as array |
| 31 |
$arr = json_decode($str, true, JSON_THROW_ON_ERROR); |
| 32 |
if ($arr['checksums'] === false) { |
| 33 |
throw new \Exception("Could not fetch the checksums of wordpress api."); |
| 34 |
} |
| 35 |
|
| 36 |
$snapshot->wpcorefiles = array_keys($arr['checksums']); |
| 37 |
$this->snapshotRepository->save($snapshot); |
| 38 |
} |
| 39 |
|
| 40 |
// TODO: batch it |
| 41 |
$fileEntries = $this->fileEntryRepository->get($this->skip, $this->count); |
| 42 |
foreach ($fileEntries as $fileEntry) { |
| 43 |
|
| 44 |
$relativeFilename = $this->fileEntryRepository->calcFullPath($fileEntry); |
| 45 |
$relativeFilename = trim($relativeFilename, '/'); // remove slash at the beginning |
| 46 |
if (in_array($relativeFilename, $snapshot->wpcorefiles)) { |
| 47 |
$fileEntry->is_wp_core_file = 1; |
| 48 |
$this->fileEntryRepository->createOrUpdate($fileEntry); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
public function toArray() { |
| 55 |
return ['type' => self::class, 'args' => [$this->skip, $this->count, $this->totalCount]]; |
| 56 |
} |
| 57 |
|
| 58 |
public function toDescription(): string { |
| 59 |
return sprintf('Determining WordPress core files... %s%%', round(100 * $this->skip / $this->totalCount)); |
| 60 |
} |
| 61 |
|
| 62 |
} |
| 63 |
|