Elgg  Version 4.3
ElggUpgrade.php
Go to the documentation of this file.
1 <?php
8 use Elgg\Exceptions\UnexpectedValueException as ElggUnexpectedValueException;
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() {
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() {
71  return (bool) $this->is_completed;
72  }
73 
80  public function setID($id) {
81  $this->id = $id;
82  }
83 
90  public function setClass($class) {
91  $this->class = $class;
92  }
93 
98  public function isAsynchronous() {
99  return !is_subclass_of($this->class, \Elgg\Upgrade\SystemUpgrade::class);
100  }
101 
107  public function getBatch() {
108  try {
109  $batch = _elgg_services()->upgradeLocator->getBatch($this->class);
110  } catch (ElggInvalidArgumentException $ex) {
111  // only report error if the upgrade still needs to run
112  $loglevel = $this->isCompleted() ? 'INFO' : '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($time = null) {
138  if (!is_int($time)) {
139  $time = $this->getCurrentTime()->getTimestamp();
140  }
141 
142  return $this->completed_time = $time;
143  }
144 
150  public function getCompletedTime() {
151  return $this->completed_time;
152  }
153 
159  public function reset() {
160  unset($this->is_completed);
161  unset($this->completed_time);
162  unset($this->processed);
163  unset($this->offset);
164  unset($this->start_time);
165  }
166 
175  public function setStartTime($time = null) {
176  if (!is_int($time)) {
177  $time = $this->getCurrentTime()->getTimestamp();
178  }
179 
180  if (isset($this->start_time)) {
181  return $this->start_time;
182  }
183 
184  return $this->start_time = $time;
185  }
186 
192  public function getStartTime() {
193  return (int) $this->start_time;
194  }
195 
200  public function save() : bool {
201  if (!isset($this->is_completed)) {
202  $this->is_completed = false;
203  }
204 
205  foreach ($this->requiredProperties as $prop) {
206  if (!$this->$prop) {
207  throw new ElggUnexpectedValueException("ElggUpgrade objects must have a value for the {$prop} property.");
208  }
209  }
210 
211  return parent::save();
212  }
213 
223  public function __set($name, $value) {
224  if (array_key_exists($name, $this->attributes)) {
225  parent::__set($name, $value);
226  } else {
227  $this->setPrivateSetting($name, $value);
228  }
229  }
230 
237  public function __get($name) {
238  // See if its in our base attribute
239  if (array_key_exists($name, $this->attributes)) {
240  return parent::__get($name);
241  }
242 
243  return $this->getPrivateSetting($name);
244  }
245 
250  public function __isset($name) {
251  if (array_key_exists($name, $this->attributes)) {
252  return parent::__isset($name);
253  }
254 
255  $private_setting = $this->getPrivateSetting($name);
256  return !is_null($private_setting);
257  }
258 
262  public function getDisplayName() {
263  return elgg_echo($this->title);
264  }
265 }
isCompleted()
Has this upgrade completed?
Definition: ElggUpgrade.php:70
__get($name)
Get an attribute or private setting value.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
setPrivateSetting($name, $value)
Adds a private setting to this entity.
Definition: ElggEntity.php:682
getPrivateSetting($name)
Returns a private setting value.
Definition: ElggEntity.php:720
$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: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
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:80
Represents an upgrade that runs outside of the upgrade.php script.
Definition: ElggUpgrade.php:26
Exception thrown if a value does not match with a set of values.
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:57
setCompletedTime($time=null)
Sets the timestamp for when the upgrade completed.
setClass($class)
Sets a class for the upgrade.
Definition: ElggUpgrade.php:90
getDisplayName()
{}
reset()
Resets the update in order to be able to run it again.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
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