Elgg  Version 4.x
ElggUpgrade.php
Go to the documentation of this file.
1 <?php
10 
25 class ElggUpgrade extends ElggObject {
26 
27  use TimeUsing;
28 
29  private $requiredProperties = [
30  'id',
31  'title',
32  'description',
33  'class',
34  ];
35 
41  public function initializeAttributes() {
42  parent::initializeAttributes();
43 
44  $this->attributes['subtype'] = 'elgg_upgrade';
45 
46  // unowned
47  $this->attributes['container_guid'] = 0;
48  $this->attributes['owner_guid'] = 0;
49  }
50 
56  public function setCompleted() {
57  $this->setStartTime(); // to make sure a start time is present
58  $this->setCompletedTime();
59  $this->is_completed = true;
60 
61  elgg_trigger_event('complete', 'upgrade', $this);
62  }
63 
69  public function isCompleted() {
70  return (bool) $this->is_completed;
71  }
72 
79  public function setID($id) {
80  $this->id = $id;
81  }
82 
89  public function setClass($class) {
90  $this->class = $class;
91  }
92 
97  public function isAsynchronous() {
98  return !is_subclass_of($this->class, \Elgg\Upgrade\SystemUpgrade::class);
99  }
100 
106  public function getBatch() {
107  try {
108  $batch = _elgg_services()->upgradeLocator->getBatch($this->class);
109  } catch (ElggInvalidArgumentException $ex) {
110  // only report error if the upgrade still needs to run
111  $loglevel = $this->isCompleted() ? 'INFO' : 'ERROR';
112  elgg_log($ex->getMessage(), $loglevel);
113 
114  return false;
115  }
116 
117  // check version before shouldBeSkipped() so authors can get immediate feedback on an invalid batch.
118  $version = $batch->getVersion();
119 
120  // Version must be in format yyyymmddnn
121  if (preg_match("/^[0-9]{10}$/", $version) == 0) {
122  elgg_log("Upgrade $this->class returned an invalid version: $version");
123  return false;
124  }
125 
126  return $batch;
127  }
128 
136  public function setCompletedTime($time = null) {
137  if (!is_int($time)) {
138  $time = $this->getCurrentTime()->getTimestamp();
139  }
140 
141  return $this->completed_time = $time;
142  }
143 
149  public function getCompletedTime() {
150  return $this->completed_time;
151  }
152 
158  public function reset() {
159  unset($this->is_completed);
160  unset($this->completed_time);
161  unset($this->processed);
162  unset($this->offset);
163  unset($this->start_time);
164  }
165 
174  public function setStartTime($time = null) {
175  if (!is_int($time)) {
176  $time = $this->getCurrentTime()->getTimestamp();
177  }
178 
179  if (isset($this->start_time)) {
180  return $this->start_time;
181  }
182 
183  return $this->start_time = $time;
184  }
185 
191  public function getStartTime() {
192  return (int) $this->start_time;
193  }
194 
199  public function save() : bool {
200  if (!isset($this->is_completed)) {
201  $this->is_completed = false;
202  }
203 
204  foreach ($this->requiredProperties as $prop) {
205  if (!$this->$prop) {
206  throw new UnexpectedValueException("ElggUpgrade objects must have a value for the {$prop} property.");
207  }
208  }
209 
210  return parent::save();
211  }
212 
222  public function __set($name, $value) {
223  if (array_key_exists($name, $this->attributes)) {
224  parent::__set($name, $value);
225  } else {
226  $this->setPrivateSetting($name, $value);
227  }
228  }
229 
236  public function __get($name) {
237  // See if its in our base attribute
238  if (array_key_exists($name, $this->attributes)) {
239  return parent::__get($name);
240  }
241 
242  return $this->getPrivateSetting($name);
243  }
244 
249  public function __isset($name) {
250  if (array_key_exists($name, $this->attributes)) {
251  return parent::__isset($name);
252  }
253 
254  $private_setting = $this->getPrivateSetting($name);
255  return !is_null($private_setting);
256  }
257 
261  public function getDisplayName() {
262  return elgg_echo($this->title);
263  }
264 }
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
isCompleted()
Has this upgrade completed?
Definition: ElggUpgrade.php:69
__get($name)
Get an attribute or private setting value.
setPrivateSetting($name, $value)
Adds a private setting to this entity.
Definition: ElggEntity.php:608
getPrivateSetting($name)
Returns a private setting value.
Definition: ElggEntity.php:646
$version
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:97
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
initializeAttributes()
Set subtype to upgrade.
Definition: ElggUpgrade.php:41
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
$value
Definition: generic.php:51
$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.
setID($id)
Sets an unique id for the upgrade.
Definition: ElggUpgrade.php:79
Represents an upgrade that runs outside of the upgrade.php script.
Definition: ElggUpgrade.php:25
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:399
__set($name, $value)
Set a value as private setting or attribute.
__isset($name)
setCompleted()
Mark this upgrade as completed.
Definition: ElggUpgrade.php:56
setCompletedTime($time=null)
Sets the timestamp for when the upgrade completed.
setClass($class)
Sets a class for the upgrade.
Definition: ElggUpgrade.php:89
getDisplayName()
{}
reset()
Resets the update in order to be able to run it again.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
setStartTime($time=null)
Sets the timestamp for when the upgrade started.
elgg_trigger_event($event, $object_type, $object=null)
Definition: elgglib.php:190
$id
Generic annotation delete action.
Definition: delete.php:6