Elgg  Version 3.0
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 
41  public $_callable_egefps = 'elgg_get_entities_from_private_settings';
42 
48  public function initializeAttributes() {
49  parent::initializeAttributes();
50 
51  $this->attributes['subtype'] = 'elgg_upgrade';
52 
53  // unowned
54  $this->attributes['container_guid'] = 0;
55  $this->attributes['owner_guid'] = 0;
56  }
57 
63  public function setCompleted() {
64  $this->setCompletedTime();
65  $this->is_completed = true;
66 
67  elgg_trigger_event('complete', 'upgrade', $this);
68  }
69 
75  public function isCompleted() {
76  return (bool) $this->is_completed;
77  }
78 
85  public function setID($id) {
86  $this->id = $id;
87  }
88 
95  public function setClass($class) {
96  $this->class = $class;
97  }
98 
103  public function isAsynchronous() {
104  return !is_subclass_of($this->class, \Elgg\Upgrade\SystemUpgrade::class);
105  }
106 
112  public function getBatch() {
113  try {
114  $batch = _elgg_services()->upgradeLocator->getBatch($this->class);
115  } catch (InvalidArgumentException $ex) {
116  // only report error if the upgrade still needs to run
117  $loglevel = $this->isCompleted() ? 'INFO' : 'ERROR';
118  elgg_log($ex->getMessage(), $loglevel);
119 
120  return false;
121  }
122 
123  // check version before shouldBeSkipped() so authors can get immediate feedback on an invalid batch.
124  $version = $batch->getVersion();
125 
126  // Version must be in format yyyymmddnn
127  if (preg_match("/^[0-9]{10}$/", $version) == 0) {
128  elgg_log("Upgrade $this->class returned an invalid version: $version");
129  return false;
130  }
131 
132  return $batch;
133  }
134 
142  public function setCompletedTime($time = null) {
143  if (!is_int($time)) {
144  $time = $this->getCurrentTime()->getTimestamp();
145  }
146 
147  return $this->completed_time = $time;
148  }
149 
155  public function getCompletedTime() {
156  return $this->completed_time;
157  }
158 
165  public function save() {
166  if (!isset($this->is_completed)) {
167  $this->is_completed = false;
168  }
169 
170  foreach ($this->requiredProperties as $prop) {
171  if (!$this->$prop) {
172  throw new UnexpectedValueException("ElggUpgrade objects must have a value for the $prop property.");
173  }
174  }
175 
176  return parent::save();
177  }
178 
188  public function __set($name, $value) {
189  if (array_key_exists($name, $this->attributes)) {
190  parent::__set($name, $value);
191  } else {
192  $this->setPrivateSetting($name, $value);
193  }
194  }
195 
202  public function __get($name) {
203  // See if its in our base attribute
204  if (array_key_exists($name, $this->attributes)) {
205  return parent::__get($name);
206  }
207 
208  return $this->getPrivateSetting($name);
209  }
210 
215  public function __isset($name) {
216  if (array_key_exists($name, $this->attributes)) {
217  return parent::__isset($name);
218  }
219 
220  $private_setting = $this->getPrivateSetting($name);
221  return !is_null($private_setting);
222  }
223 
227  public function getDisplayName() {
228  return elgg_echo($this->title);
229  }
230 }
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
isCompleted()
Has this upgrade completed?
Definition: ElggUpgrade.php:75
__get($name)
Get an attribute or private setting value.
setPrivateSetting($name, $value)
Adds a private setting to this entity.
Definition: ElggEntity.php:589
getPrivateSetting($name)
Returns a private setting value.
Definition: ElggEntity.php:625
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:27
isAsynchronous()
Check if the upgrade should be run asynchronously.
initializeAttributes()
Set subtype to upgrade.
Definition: ElggUpgrade.php:48
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
getCompletedTime()
Gets the time when the upgrade completed.
Configuration exception.
getBatch()
Return instance of the class that processes the data.
$id
River item delete action.
Definition: delete.php:6
$class
Definition: field.php:29
setID($id)
Sets an unique id for the upgrade.
Definition: ElggUpgrade.php:85
if(!$entity instanceof ElggEntity) $time
Definition: time.php:21
save()
Require an upgrade page.
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
__set($name, $value)
Set a value as private setting or attribute.
__isset($name)
setCompleted()
Mark this upgrade as completed.
Definition: ElggUpgrade.php:63
setCompletedTime($time=null)
Sets the timestamp for when the upgrade completed.
setClass($class)
Sets a class for the upgrade.
Definition: ElggUpgrade.php:95
$value
Definition: debugging.php:7
getDisplayName()
{}
class
Definition: placeholder.php:21
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:12
elgg_trigger_event($event, $object_type, $object=null)
Definition: elgglib.php:513
$version
Definition: version.php:14