Elgg  Version 6.1
ElggUpgrade.php
Go to the documentation of this file.
1 <?php
11 
26 class ElggUpgrade extends ElggObject {
27 
28  use TimeUsing;
29 
30  private $requiredProperties = [
31  'id',
32  'title',
33  'description',
34  'class',
35  ];
36 
42  public function initializeAttributes() {
43  parent::initializeAttributes();
44 
45  $this->attributes['subtype'] = 'elgg_upgrade';
46 
47  // unowned
48  $this->attributes['container_guid'] = 0;
49  $this->attributes['owner_guid'] = 0;
50  }
51 
57  public function setCompleted(): void {
58  $this->setStartTime(); // to make sure a start time is present
59  $this->setCompletedTime();
60  $this->is_completed = true;
61 
62  elgg_trigger_event('complete', 'upgrade', $this);
63  }
64 
70  public function isCompleted(): bool {
71  return (bool) $this->is_completed;
72  }
73 
80  public function setID(string $id): void {
81  $this->id = $id;
82  }
83 
90  public function setClass(string $class): void {
91  $this->class = $class;
92  }
93 
98  public function isAsynchronous(): bool {
99  return !is_subclass_of($this->class, \Elgg\Upgrade\SystemUpgrade::class);
100  }
101 
107  public function getBatch(): Batch|false {
108  try {
109  $batch = _elgg_services()->upgradeLocator->getBatch($this->class, $this);
110  } catch (ElggInvalidArgumentException $ex) {
111  // only report error if the upgrade still needs to run
112  $loglevel = $this->isCompleted() ? \Psr\Log\LogLevel::INFO : \Psr\Log\LogLevel::ERROR;
113  elgg_log($ex->getMessage(), $loglevel);
114 
115  return false;
116  }
117 
118  // check version before shouldBeSkipped() so authors can get immediate feedback on an invalid batch.
119  $version = $batch->getVersion();
120 
121  // Version must be in format yyyymmddnn
122  if (preg_match('/^[0-9]{10}$/', $version) === 0) {
123  elgg_log("Upgrade {$this->class} returned an invalid version: {$version}");
124  return false;
125  }
126 
127  return $batch;
128  }
129 
137  public function setCompletedTime(int $time = null): void {
138  $this->completed_time = $time ?? $this->getCurrentTime()->getTimestamp();
139  }
140 
146  public function getCompletedTime(): int {
147  return (int) $this->completed_time;
148  }
149 
155  public function reset(): void {
156  unset($this->is_completed);
157  unset($this->completed_time);
158  unset($this->processed);
159  unset($this->offset);
160  unset($this->start_time);
161  }
162 
171  public function setStartTime(int $time = null): void {
172  if (isset($this->start_time)) {
173  return;
174  }
175 
176  $this->start_time = $time ?? $this->getCurrentTime()->getTimestamp();
177  }
178 
184  public function getStartTime() {
185  return (int) $this->start_time;
186  }
187 
192  public function save(): bool {
193  if (!isset($this->is_completed)) {
194  $this->is_completed = false;
195  }
196 
197  foreach ($this->requiredProperties as $prop) {
198  if (!$this->$prop) {
199  throw new ElggUnexpectedValueException("ElggUpgrade objects must have a value for the {$prop} property.");
200  }
201  }
202 
203  return parent::save();
204  }
205 
209  public function getDisplayName(): string {
210  return elgg_echo($this->title);
211  }
212 }
isCompleted()
Has this upgrade completed?
Definition: ElggUpgrade.php:70
setClass(string $class)
Sets a class for the upgrade.
Definition: ElggUpgrade.php:90
$version
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(!$annotation instanceof ElggAnnotation) $time
Definition: time.php:20
getStartTime()
Gets the time when the upgrade completed.
isAsynchronous()
Check if the upgrade should be run asynchronously.
Definition: ElggUpgrade.php:98
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
initializeAttributes()
Set subtype to upgrade.
Definition: ElggUpgrade.php:42
setCompletedTime(int $time=null)
Sets the timestamp for when the upgrade completed.
$class
Definition: summary.php:44
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
getCompletedTime()
Gets the time when the upgrade completed.
getBatch()
Return instance of the class that processes the data.
Represents an upgrade that runs outside of the upgrade.php script.
Definition: ElggUpgrade.php:26
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:88
Long running upgrades should implement this interface.
Definition: Batch.php:10
setStartTime(int $time=null)
Sets the timestamp for when the upgrade started.
setCompleted()
Mark this upgrade as completed.
Definition: ElggUpgrade.php:57
setID(string $id)
Sets an unique id for the upgrade.
Definition: ElggUpgrade.php:80
getDisplayName()
{}
reset()
Resets the update in order to be able to run it again.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_trigger_event(string $event, string $type, $object=null)
Trigger an Elgg Event and attempt to run all handler callbacks registered to that event...
Definition: events.php:102
$id
Generic annotation delete action.
Definition: delete.php:6