Elgg  Version 6.2
ElggUpgrade.php
Go to the documentation of this file.
1 <?php
7 use Elgg\Exceptions\InvalidArgumentException as ElggInvalidArgumentException;
8 use Elgg\Exceptions\UnexpectedValueException as ElggUnexpectedValueException;
9 use Elgg\Traits\TimeUsing;
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 }
$id
Generic annotation delete action.
Definition: delete.php:6
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
if(! $annotation instanceof ElggAnnotation) $time
Definition: time.php:20
$class
Definition: summary.php:44
Represents an upgrade that runs outside of the upgrade.php script.
Definition: ElggUpgrade.php:26
getBatch()
Return instance of the class that processes the data.
getDisplayName()
{{Get the entity's display name.string The title or name of this entity.}}
setCompleted()
Mark this upgrade as completed.
Definition: ElggUpgrade.php:57
initializeAttributes()
Set subtype to upgrade.
Definition: ElggUpgrade.php:42
getCompletedTime()
Gets the time when the upgrade completed.
save()
Save this data to the appropriate database table.bool
isCompleted()
Has this upgrade completed?
Definition: ElggUpgrade.php:70
setStartTime(?int $time=null)
Sets the timestamp for when the upgrade started.
setCompletedTime(?int $time=null)
Sets the timestamp for when the upgrade completed.
getStartTime()
Gets the time when the upgrade completed.
reset()
Resets the update in order to be able to run it again.
isAsynchronous()
Check if the upgrade should be run asynchronously.
Definition: ElggUpgrade.php:98
setClass(string $class)
Sets a class for the upgrade.
Definition: ElggUpgrade.php:90
setID(string $id)
Sets an unique id for the upgrade.
Definition: ElggUpgrade.php:80
Exception thrown if an argument is not of the expected type.
Exception thrown if a value does not match with a set of values.
Long running upgrades should implement this interface.
Definition: Batch.php:10
$version
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:88
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
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
try
Definition: login_as.php:33
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10