Elgg  Version 3.0
MigrateCronLog.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrades;
4 
7 
12 
16  public function getVersion() {
17  return 2018061401;
18  }
19 
23  public function needsIncrementOffset() {
24  return false;
25  }
26 
30  public function shouldBeSkipped() {
31  return (bool) !$this->countItems();
32  }
33 
37  public function countItems() {
38  return count($this->getLogs());
39  }
40 
46  protected function getLogs() {
47 
48  $intervals = elgg_get_config('elgg_cron_periods');
49  if (empty($intervals)) {
50  return [];
51  }
52 
53  $suffixes = [
54  'ts',
55  'msg',
56  ];
57 
59  $result = [];
60  foreach ($intervals as $interval) {
61  foreach ($suffixes as $suffix) {
62  $result[$interval][$suffix] = $site->getPrivateSetting("cron_latest:{$interval}:{$suffix}");
63  }
64  }
65 
66  return $result;
67  }
68 
72  public function run(Result $result, $offset) {
73 
75 
76  $fh = new \ElggFile();
77  $fh->owner_guid = $site->guid;
78 
79  $logs = $this->getLogs();
80  foreach ($logs as $interval => $values) {
81  if (empty($values)) {
82  $result->addSuccesses();
83  continue;
84  }
85 
86  foreach ($values as $suffix => $value) {
87 
88  $site->removePrivateSetting("cron_latest:{$interval}:{$suffix}");
89 
90  if (empty($value)) {
91  continue;
92  }
93 
94  if ($suffix === 'ts') {
95  $fh->setFilename("{$interval}-completion.log");
96  } else {
97  $fh->setFilename("{$interval}-output.log");
98  }
99 
100  $fh->open('write');
101  $fh->write($value);
102  $fh->close();
103  }
104 
105  $result->addSuccesses();
106  }
107  }
108 }
Interface to be implement for asynchronous upgrades, i.e.
Migrate the database cron log values to the new (file) location.
if(!$count) $offset
Definition: pagination.php:26
addSuccesses($num=1)
Set an item (or items) as successfully upgraded.
Definition: Result.php:68
shouldBeSkipped()
{Should this upgrade be skipped?If true, the upgrade will not be performed and cannot be accessed lat...
$suffix
Definition: excerpt.php:13
run(Result $result, $offset)
{Runs upgrade on a single batch of items.If countItems() returns Batch::UNKNOWN_COUNT, this method must call $result->markCompleted() when the upgrade is complete.Result of the batch (this must be returned) Number to skip when processingResult}
getLogs()
Get all the database cron logs.
Result of a single BatchUpgrade run.
Definition: Result.php:8
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:130
$value
Definition: debugging.php:7
countItems()
{The total number of items to process during the upgrade.If unknown, Batch::UNKNOWN_COUNT should be r...
getVersion()
{Version of the upgrade.This tells the date when the upgrade was added. It consists of eight digits a...
elgg_get_config($name, $default=null)
Get an Elgg configuration value.
needsIncrementOffset()
{Should the run() method receive an offset representing all processed items?If true, run() will receive as $offset the number of items already processed. This is useful if you are only modifying data, and need to use the $offset in a function like elgg_get_entities*() to know how many to skip over.If false, run() will receive as $offset the total number of failures. This should be used if your process deletes or moves data out of the way of the process. E.g. if you delete 50 objects on each run(), you may still use the $offset to skip objects that already failed once.bool}